api-v3: First draft
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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 #include <stdarg.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v2.h>
28
29 #include "afb-api.h"
30 #include "afb-api-so-v2.h"
31 #include "afb-apiset.h"
32 #include "afb-auth.h"
33 #include "afb-export.h"
34 #include "afb-evt.h"
35 #include "afb-context.h"
36 #include "afb-api-so.h"
37 #include "afb-xreq.h"
38 #include "verbose.h"
39
40 /*
41  * names of symbols
42  */
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44 static const char afb_api_so_v2_data[] = "afbBindingV2data";
45
46 static const struct afb_verb_v2 *search(const struct afb_binding_v2 *binding, const char *name)
47 {
48         const struct afb_verb_v2 *verb;
49
50         verb = binding->verbs;
51         while (verb->verb && strcasecmp(verb->verb, name))
52                 verb++;
53         return verb->verb ? verb : NULL;
54         return NULL;
55 }
56
57 void afb_api_so_v2_process_call(const struct afb_binding_v2 *binding, struct afb_xreq *xreq)
58 {
59         const struct afb_verb_v2 *verb;
60
61         verb = search(binding, xreq->request.called_verb);
62         afb_xreq_call_verb_v2(xreq, verb);
63 }
64
65 struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_binding_v2 *binding, const char *apiname)
66 {
67         char buffer[256];
68         const struct afb_verb_v2 *verb;
69         struct json_object *r, *f, *a, *i, *p, *g;
70
71
72         if (binding->specification) {
73                 r = json_tokener_parse(binding->specification);
74                 if (r)
75                         return r;
76         }
77
78         r = json_object_new_object();
79         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
80
81         i = json_object_new_object();
82         json_object_object_add(r, "info", i);
83         json_object_object_add(i, "title", json_object_new_string(apiname));
84         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
85         json_object_object_add(i, "description", json_object_new_string(binding->info ?: apiname));
86
87         p = json_object_new_object();
88         json_object_object_add(r, "paths", p);
89         verb = binding->verbs;
90         while (verb->verb) {
91                 buffer[0] = '/';
92                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
93                 buffer[sizeof buffer - 1] = 0;
94                 f = json_object_new_object();
95                 json_object_object_add(p, buffer, f);
96                 g = json_object_new_object();
97                 json_object_object_add(f, "get", g);
98
99                 a = afb_auth_json_v2(verb->auth, verb->session);
100                 if (a)
101                         json_object_object_add(g, "x-permissions", a);
102
103                 a = json_object_new_object();
104                 json_object_object_add(g, "responses", a);
105                 f = json_object_new_object();
106                 json_object_object_add(a, "200", f);
107                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
108                 verb++;
109         }
110         return r;
111 }
112
113 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set, struct afb_binding_data_v2 *data)
114 {
115         int rc;
116         struct afb_export *export;
117
118         /* basic checks */
119         assert(binding);
120         assert(binding->api);
121         assert(binding->verbs);
122         assert(data);
123
124         /* allocates the description */
125         export = afb_export_create_v2(declare_set, call_set, binding->api, binding, data, binding->init, binding->onevent);
126         if (!export) {
127                 ERROR("out of memory");
128                 goto error;
129         }
130
131         /* records the binding */
132         if (afb_export_declare(export, binding->noconcurrency) < 0) {
133                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(export), afb_apiset_name(declare_set));
134                 goto error;
135         }
136         /* init the binding */
137         if (binding->preinit) {
138                 INFO("binding %s calling preinit function", binding->api);
139                 rc = binding->preinit();
140                 if (rc < 0) {
141                         ERROR("binding %s preinit function failed...", afb_export_apiname(export));
142                         afb_export_undeclare(export);
143                         goto error;
144                 }
145         }
146
147         INFO("binding %s added to set %s", afb_export_apiname(export), afb_apiset_name(declare_set));
148         return 1;
149
150 error:
151         afb_export_unref(export);
152
153         return -1;
154 }
155
156 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
157 {
158         const struct afb_binding_v2 *binding;
159         struct afb_binding_data_v2 *data;
160
161         /* retrieves the register function */
162         binding = dlsym(handle, afb_api_so_v2_descriptor);
163         data = dlsym(handle, afb_api_so_v2_data);
164         if (!binding && !data)
165                 return 0;
166
167         INFO("binding [%s] looks like an AFB binding V2", path);
168
169         /* basic checks */
170         if (!binding || !data) {
171                 ERROR("binding [%s] incomplete symbol set: %s is missing",
172                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
173                 goto error;
174         }
175         if (binding->api == NULL || *binding->api == 0) {
176                 ERROR("binding [%s] bad api name...", path);
177                 goto error;
178         }
179         if (!afb_api_is_valid_name(binding->api)) {
180                 ERROR("binding [%s] invalid api name...", path);
181                 goto error;
182         }
183
184         if (binding->verbs == NULL) {
185                 ERROR("binding [%s] no verbs...", path);
186                 goto error;
187         }
188
189         return afb_api_so_v2_add_binding(binding, handle, declare_set, call_set, data);
190
191  error:
192         return -1;
193 }
194