afb-api: Define the notion of group for concurrency
[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-export.h"
32 #include "afb-evt.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
36 #include "afb-xreq.h"
37 #include "verbose.h"
38
39 /*
40  * names of symbols
41  */
42 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
43 static const char afb_api_so_v2_data[] = "afbBindingV2data";
44
45 /*
46  * Description of a binding
47  */
48 struct api_so_v2 {
49         const struct afb_binding_v2 *binding;   /* descriptor */
50         void *handle;                           /* context of dlopen */
51         struct afb_export *export;              /* exportations */
52 };
53
54 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
55 {
56         const struct afb_verb_v2 *verb;
57
58         verb = desc->binding->verbs;
59         while (verb->verb && strcasecmp(verb->verb, name))
60                 verb++;
61         return verb->verb ? verb : NULL;
62         return NULL;
63 }
64
65 static void call_cb(void *closure, struct afb_xreq *xreq)
66 {
67         struct api_so_v2 *desc = closure;
68         const struct afb_verb_v2 *verb;
69
70         verb = search(desc, xreq->verb);
71         afb_xreq_call_verb_v2(xreq, verb);
72 }
73
74 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
75 {
76         struct api_so_v2 *desc = closure;
77         return afb_export_start(desc->export, share_session, onneed, apiset);
78 }
79
80 static void update_hooks_cb(void *closure)
81 {
82         struct api_so_v2 *desc = closure;
83         afb_export_update_hook(desc->export);
84 }
85
86 static int get_verbosity_cb(void *closure)
87 {
88         struct api_so_v2 *desc = closure;
89         return afb_export_verbosity_get(desc->export);
90 }
91
92 static void set_verbosity_cb(void *closure, int level)
93 {
94         struct api_so_v2 *desc = closure;
95         afb_export_verbosity_set(desc->export, level);
96 }
97
98 static struct json_object *addperm(struct json_object *o, struct json_object *x)
99 {
100         struct json_object *a;
101
102         if (!o)
103                 return x;
104
105         if (!json_object_object_get_ex(o, "allOf", &a)) {
106                 a = json_object_new_array();
107                 json_object_array_add(a, o);
108                 o = json_object_new_object();
109                 json_object_object_add(o, "allOf", a);
110         }
111         json_object_array_add(a, x);
112         return o;
113 }
114
115 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
116 {
117         struct json_object *x = json_object_new_object();
118         json_object_object_add(x, key, val);
119         return addperm(o, x);
120 }
121
122 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
123 {
124         return addperm_key_val(o, key, json_object_new_string(val));
125 }
126
127 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
128 {
129         return addperm_key_val(o, key, json_object_new_int(val));
130 }
131
132 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth);
133
134 static struct json_object *addauth(struct json_object *o, const struct afb_auth *auth)
135 {
136         switch(auth->type) {
137         case afb_auth_No: return addperm(o, json_object_new_boolean(0));
138         case afb_auth_Token: return addperm_key_valstr(o, "session", "check");
139         case afb_auth_LOA: return addperm_key_valint(o, "LOA", auth->loa);
140         case afb_auth_Permission: return addperm_key_valstr(o, "permission", auth->text);
141         case afb_auth_Or: return addperm_key_val(o, "anyOf", addauth_or_array(json_object_new_array(), auth));
142         case afb_auth_And: return addauth(addauth(o, auth->first), auth->next);
143         case afb_auth_Not: return addperm_key_val(o, "not", addauth(NULL, auth->first));
144         case afb_auth_Yes: return addperm(o, json_object_new_boolean(1));
145         }
146         return o;
147 }
148
149 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth)
150 {
151         if (auth->type != afb_auth_Or)
152                 json_object_array_add(o, addauth(NULL, auth));
153         else {
154                 addauth_or_array(o, auth->first);
155                 addauth_or_array(o, auth->next);
156         }
157
158         return o;
159 }
160
161 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
162 {
163         char buffer[256];
164         const struct afb_verb_v2 *verb;
165         struct json_object *r, *f, *a, *i, *p, *g;
166
167         r = json_object_new_object();
168         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
169
170         i = json_object_new_object();
171         json_object_object_add(r, "info", i);
172         json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
173         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
174         json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: afb_export_apiname(desc->export)));
175
176         p = json_object_new_object();
177         json_object_object_add(r, "paths", p);
178         verb = desc->binding->verbs;
179         while (verb->verb) {
180                 buffer[0] = '/';
181                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
182                 buffer[sizeof buffer - 1] = 0;
183                 f = json_object_new_object();
184                 json_object_object_add(p, buffer, f);
185                 g = json_object_new_object();
186                 json_object_object_add(f, "get", g);
187
188                 a = NULL;
189                 if (verb->session & AFB_SESSION_CLOSE_V2)
190                         a = addperm_key_valstr(a, "session", "close");
191                 if (verb->session & AFB_SESSION_CHECK_V2)
192                         a = addperm_key_valstr(a, "session", "check");
193                 if (verb->session & AFB_SESSION_REFRESH_V2)
194                         a = addperm_key_valstr(a, "token", "refresh");
195                 if (verb->session & AFB_SESSION_LOA_MASK_V2)
196                         a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
197                 if (verb->auth)
198                         a = addauth(a, verb->auth);
199                 if (a)
200                         json_object_object_add(g, "x-permissions", a);
201
202                 a = json_object_new_object();
203                 json_object_object_add(g, "responses", a);
204                 f = json_object_new_object();
205                 json_object_object_add(a, "200", f);
206                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
207                 verb++;
208         }
209         return r;
210 }
211
212 static struct json_object *describe_cb(void *closure)
213 {
214         struct api_so_v2 *desc = closure;
215         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
216         if (!r)
217                 r = make_description_openAPIv3(desc);
218         return r;
219 }
220
221 static struct afb_api_itf so_v2_api_itf = {
222         .call = call_cb,
223         .service_start = service_start_cb,
224         .update_hooks = update_hooks_cb,
225         .get_verbosity = get_verbosity_cb,
226         .set_verbosity = set_verbosity_cb,
227         .describe = describe_cb
228 };
229
230 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)
231 {
232         int rc;
233         struct api_so_v2 *desc;
234         struct afb_api afb_api;
235         struct afb_export *export;
236
237         /* basic checks */
238         assert(binding);
239         assert(binding->api);
240         assert(binding->verbs);
241         assert(data);
242
243         /* allocates the description */
244         export = afb_export_create_v2(binding->api, data, binding->init, binding->onevent);
245         desc = calloc(1, sizeof *desc);
246         if (desc == NULL) {
247                 ERROR("out of memory");
248                 goto error;
249         }
250         desc->binding = binding;
251         desc->handle = handle;
252         desc->export = export;
253
254         /* init the binding */
255         if (binding->preinit) {
256                 INFO("binding %s calling preinit function", binding->api);
257                 rc = binding->preinit();
258                 if (rc < 0) {
259                         ERROR("binding %s preinit function failed...", afb_export_apiname(desc->export));
260                         goto error;
261                 }
262         }
263
264         /* records the binding */
265         afb_api.closure = desc;
266         afb_api.itf = &so_v2_api_itf;
267         afb_api.group = binding->noconcurrency ? export : NULL;
268         if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
269                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(desc->export), afb_apiset_name(apiset));
270                 goto error;
271         }
272         INFO("binding %s added to set %s", afb_export_apiname(desc->export), afb_apiset_name(apiset));
273         return 1;
274
275 error:
276         afb_export_destroy(export);
277         free(desc);
278
279         return -1;
280 }
281
282 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
283 {
284         const struct afb_binding_v2 *binding;
285         struct afb_binding_data_v2 *data;
286
287         /* retrieves the register function */
288         binding = dlsym(handle, afb_api_so_v2_descriptor);
289         data = dlsym(handle, afb_api_so_v2_data);
290         if (!binding && !data)
291                 return 0;
292
293         INFO("binding [%s] looks like an AFB binding V2", path);
294
295         /* basic checks */
296         if (!binding || !data) {
297                 ERROR("binding [%s] incomplete symbol set: %s is missing",
298                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
299                 goto error;
300         }
301         if (binding->api == NULL || *binding->api == 0) {
302                 ERROR("binding [%s] bad api name...", path);
303                 goto error;
304         }
305         if (!afb_api_is_valid_name(binding->api)) {
306                 ERROR("binding [%s] invalid api name...", path);
307                 goto error;
308         }
309 #if 0
310         if (binding->specification == NULL || *binding->specification == 0) {
311                 ERROR("binding [%s] bad specification...", path);
312                 goto error;
313         }
314 #endif
315         if (binding->verbs == NULL) {
316                 ERROR("binding [%s] no verbs...", path);
317                 goto error;
318         }
319
320         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
321
322  error:
323         return -1;
324 }
325