afb-api-so-v2: improve generation of description
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding-v2.h>
26 #include <json-c/json.h>
27
28 #include "afb-api.h"
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
31 #include "afb-svc.h"
32 #include "afb-ditf.h"
33 #include "afb-evt.h"
34 #include "afb-common.h"
35 #include "afb-context.h"
36 #include "afb-api-so.h"
37 #include "afb-xreq.h"
38 #include "jobs.h"
39 #include "verbose.h"
40
41 /*
42  * names of symbols
43  */
44 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
45 static const char afb_api_so_v2_data[] = "afbBindingV2data";
46
47 /*
48  * Description of a binding
49  */
50 struct api_so_v2 {
51         const struct afb_binding_v2 *binding;   /* descriptor */
52         struct afb_binding_data_v2 *data;       /* data */
53         void *handle;                   /* context of dlopen */
54         struct afb_svc *service;        /* handler for service started */
55         struct afb_ditf ditf;           /* daemon interface */
56 };
57
58 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
59 {
60         const struct afb_verb_v2 *verb;
61
62         verb = desc->binding->verbs;
63         while (verb->verb && strcasecmp(verb->verb, name))
64                 verb++;
65         return verb->verb ? verb : NULL;
66         return NULL;
67 }
68
69 static void call_cb(void *closure, struct afb_xreq *xreq)
70 {
71         struct api_so_v2 *desc = closure;
72         const struct afb_verb_v2 *verb;
73
74         verb = search(desc, xreq->verb);
75         afb_xreq_call_verb_v2(xreq, verb);
76 }
77
78 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
79 {
80         int rc;
81         int (*start)();
82         void (*onevent)(const char *event, struct json_object *object);
83
84         struct api_so_v2 *desc = closure;
85
86         /* check state */
87         if (desc->service != NULL) {
88                 /* not an error when onneed */
89                 if (onneed != 0)
90                         goto done;
91
92                 /* already started: it is an error */
93                 ERROR("Service %s already started", desc->ditf.api);
94                 return -1;
95         }
96
97         /* get the initialisation */
98         start = desc->binding->init;
99         onevent = desc->binding->onevent;
100         if (start == NULL && onevent == NULL) {
101                 /* not an error when onneed */
102                 if (onneed != 0)
103                         goto done;
104
105                 /* no initialisation method */
106                 ERROR("Binding %s is not a service", desc->ditf.api);
107                 return -1;
108         }
109
110         /* get the event handler if any */
111         desc->service = afb_svc_create(desc->ditf.api, apiset, share_session, onevent, &desc->data->service);
112         if (desc->service == NULL) {
113                 /* starting error */
114                 ERROR("Starting service %s failed", desc->ditf.api);
115                 return -1;
116         }
117
118         /* Starts the service */
119         desc->ditf.state = Daemon_Init;
120         rc = afb_svc_start_v2(desc->service, start);
121         if (rc < 0) {
122                 /* initialisation error */
123                 ERROR("Initialisation of service %s failed (%d): %m", desc->ditf.api, rc);
124                 afb_svc_destroy(desc->service, &desc->data->service);
125                 desc->service = NULL;
126                 return rc;
127         }
128
129 done:
130         desc->ditf.state = Daemon_Run;
131         return 0;
132 }
133
134 static void update_hooks_cb(void *closure)
135 {
136         struct api_so_v2 *desc = closure;
137         afb_ditf_update_hook(&desc->ditf);
138         if (desc->service)
139                 afb_svc_update_hook(desc->service);
140 }
141
142 static int get_verbosity_cb(void *closure)
143 {
144         struct api_so_v2 *desc = closure;
145         return desc->data->verbosity;
146 }
147
148 static void set_verbosity_cb(void *closure, int level)
149 {
150         struct api_so_v2 *desc = closure;
151         desc->data->verbosity = level;
152 }
153
154 static struct json_object *addperm(struct json_object *o, struct json_object *x)
155 {
156         struct json_object *a;
157
158         if (!o)
159                 return x;
160
161         if (!json_object_object_get_ex(o, "allOf", &a)) {
162                 a = json_object_new_array();
163                 json_object_array_add(a, o);
164                 o = json_object_new_object();
165                 json_object_object_add(o, "allOf", a);
166         }
167         json_object_array_add(a, x);
168         return o;
169 }
170
171 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
172 {
173         struct json_object *x = json_object_new_object();
174         json_object_object_add(x, key, val);
175         return addperm(o, x);
176 }
177
178 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
179 {
180         return addperm_key_val(o, key, json_object_new_string(val));
181 }
182
183 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
184 {
185         return addperm_key_val(o, key, json_object_new_int(val));
186 }
187
188 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth);
189
190 static struct json_object *addauth(struct json_object *o, const struct afb_auth *auth)
191 {
192         switch(auth->type) {
193         case afb_auth_No: return addperm(o, json_object_new_boolean(0));
194         case afb_auth_Token: return addperm_key_valstr(o, "session", "check");
195         case afb_auth_LOA: return addperm_key_valint(o, "LOA", auth->loa);
196         case afb_auth_Permission: return addperm_key_valstr(o, "permission", auth->text);
197         case afb_auth_Or: return addperm_key_val(o, "anyOf", addauth_or_array(json_object_new_array(), auth));
198         case afb_auth_And: return addauth(addauth(o, auth->first), auth->next);
199         case afb_auth_Not: return addperm_key_val(o, "not", addauth(NULL, auth->first));
200         case afb_auth_Yes: return addperm(o, json_object_new_boolean(1));
201         }
202         return o;
203 }
204
205 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth)
206 {
207         if (auth->type != afb_auth_Or)
208                 json_object_array_add(o, addauth(NULL, auth));
209         else {
210                 addauth_or_array(o, auth->first);
211                 addauth_or_array(o, auth->next);
212         }
213
214         return o;
215 }
216
217 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
218 {
219         char buffer[256];
220         const struct afb_verb_v2 *verb;
221         struct json_object *r, *f, *a, *i, *p, *g;
222
223         r = json_object_new_object();
224         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
225
226         i = json_object_new_object();
227         json_object_object_add(r, "info", i);
228         json_object_object_add(i, "title", json_object_new_string(desc->ditf.api));
229         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
230         json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: desc->ditf.api));
231
232         p = json_object_new_object();
233         json_object_object_add(r, "paths", p);
234         verb = desc->binding->verbs;
235         while (verb->verb) {
236                 buffer[0] = '/';
237                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
238                 buffer[sizeof buffer - 1] = 0;
239                 f = json_object_new_object();
240                 json_object_object_add(p, buffer, f);
241                 g = json_object_new_object();
242                 json_object_object_add(f, "get", g);
243
244                 a = NULL;
245                 if (verb->session & AFB_SESSION_CLOSE_V2)
246                         a = addperm_key_valstr(a, "session", "close");
247                 if (verb->session & AFB_SESSION_CHECK_V2)
248                         a = addperm_key_valstr(a, "session", "check");
249                 if (verb->session & AFB_SESSION_REFRESH_V2)
250                         a = addperm_key_valstr(a, "token", "refresh");
251                 if (verb->session & AFB_SESSION_LOA_MASK_V2)
252                         a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
253                 if (verb->auth)
254                         a = addauth(a, verb->auth);
255                 if (a)
256                         json_object_object_add(g, "x-permissions", a);
257
258                 a = json_object_new_object();
259                 json_object_object_add(g, "responses", a);
260                 f = json_object_new_object();
261                 json_object_object_add(a, "200", f);
262                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
263                 verb++;
264         }
265         return r;
266 }
267
268 static struct json_object *describe_cb(void *closure)
269 {
270         struct api_so_v2 *desc = closure;
271         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
272         if (!r)
273                 r = make_description_openAPIv3(desc);
274         return r;
275 }
276
277 static struct afb_api_itf so_v2_api_itf = {
278         .call = call_cb,
279         .service_start = service_start_cb,
280         .update_hooks = update_hooks_cb,
281         .get_verbosity = get_verbosity_cb,
282         .set_verbosity = set_verbosity_cb,
283         .describe = describe_cb
284 };
285
286 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, struct afb_binding_data_v2 *data)
287 {
288         int rc;
289         struct api_so_v2 *desc;
290         struct afb_api afb_api;
291
292         /* basic checks */
293         assert(binding);
294         assert(binding->api);
295         assert(binding->verbs);
296         assert(data);
297
298         /* allocates the description */
299         desc = calloc(1, sizeof *desc);
300         if (desc == NULL) {
301                 ERROR("out of memory");
302                 goto error;
303         }
304         desc->binding = binding;
305         desc->data = data;
306         desc->handle = handle;
307         desc->service = NULL;
308
309         /* init the interface */
310         desc->data->verbosity = verbosity;
311         afb_ditf_init_v2(&desc->ditf, binding->api, data);
312
313         /* init the binding */
314         if (binding->preinit) {
315                 INFO("binding %s calling preinit function", binding->api);
316                 rc = binding->preinit();
317                 if (rc < 0) {
318                         ERROR("binding %s preinit function failed...", desc->ditf.api);
319                         goto error2;
320                 }
321         }
322
323         /* records the binding */
324         afb_api.closure = desc;
325         afb_api.itf = &so_v2_api_itf;
326         afb_api.noconcurrency = binding->noconcurrency;
327         if (afb_apiset_add(apiset, desc->ditf.api, afb_api) < 0) {
328                 ERROR("binding %s can't be registered to set %s...", desc->ditf.api, afb_apiset_name(apiset));
329                 goto error2;
330         }
331         INFO("binding %s added to set %s", desc->ditf.api, afb_apiset_name(apiset));
332         return 1;
333
334 error2:
335         free(desc);
336 error:
337         return -1;
338 }
339
340 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
341 {
342         const struct afb_binding_v2 *binding;
343         struct afb_binding_data_v2 *data;
344
345         /* retrieves the register function */
346         binding = dlsym(handle, afb_api_so_v2_descriptor);
347         data = dlsym(handle, afb_api_so_v2_data);
348         if (!binding && !data)
349                 return 0;
350
351         INFO("binding [%s] looks like an AFB binding V2", path);
352
353         /* basic checks */
354         if (!binding || !data) {
355                 ERROR("binding [%s] incomplete symbol set: %s is missing",
356                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
357                 goto error;
358         }
359         if (binding->api == NULL || *binding->api == 0) {
360                 ERROR("binding [%s] bad api name...", path);
361                 goto error;
362         }
363         if (!afb_api_is_valid_name(binding->api)) {
364                 ERROR("binding [%s] invalid api name...", path);
365                 goto error;
366         }
367 #if 0
368         if (binding->specification == NULL || *binding->specification == 0) {
369                 ERROR("binding [%s] bad specification...", path);
370                 goto error;
371         }
372 #endif
373         if (binding->verbs == NULL) {
374                 ERROR("binding [%s] no verbs...", path);
375                 goto error;
376         }
377
378         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
379
380  error:
381         return -1;
382 }
383