blob: a1c5718331b64ecab8ae302ef7e2086791c0c9b7 [file] [log] [blame]
Harald Weltef5a0fa32019-03-03 15:44:18 +01001#include <string.h>
2#include <stdlib.h>
3#include <errno.h>
4
5#include <jansson.h>
6#include <ulfius.h>
7
8#include <osmocom/core/utils.h>
9#include <osmocom/core/linuxlist.h>
10
11#define PREFIX "/api/backend/v1"
12
13#include "debug.h"
14#include "rest_api.h"
15#include "slotmap.h"
16#include "rspro_server.h"
17
18static json_t *comp_id2json(const struct app_comp_id *comp_id)
19{
20 json_t *ret = json_object();
21
22 static const char *type_names[] = {
23 [ComponentType_remsimClient] = "remsimClient",
24 [ComponentType_remsimServer] = "remsimServer",
25 [ComponentType_remsimBankd] = "remsimBankd"
26 };
27
28 json_object_set_new(ret, "type_", json_string(type_names[comp_id->type]));
29 json_object_set_new(ret, "name", json_string(comp_id->name));
30 json_object_set_new(ret, "software", json_string(comp_id->software));
31 json_object_set_new(ret, "swVersion", json_string(comp_id->sw_version));
32 if (strlen(comp_id->hw_manufacturer))
33 json_object_set_new(ret, "hwManufacturer", json_string(comp_id->hw_manufacturer));
34 if (strlen(comp_id->hw_model))
35 json_object_set_new(ret, "hwModel", json_string(comp_id->hw_model));
36 if (strlen(comp_id->hw_serial_nr))
37 json_object_set_new(ret, "hwSerialNr", json_string(comp_id->hw_serial_nr));
38 if (strlen(comp_id->hw_version))
39 json_object_set_new(ret, "hwVersion", json_string(comp_id->hw_version));
40 if (strlen(comp_id->fw_version))
41 json_object_set_new(ret, "fwVersion", json_string(comp_id->fw_version));
42
43 return ret;
44}
45
46static json_t *client2json(const struct rspro_client_conn *conn)
47{
48 json_t *ret = json_object();
49
50 json_object_set_new(ret, "peer", json_string(conn->fi->id));
51 json_object_set_new(ret, "state", json_string(osmo_fsm_inst_state_name(conn->fi)));
52 /* FIXME: only in the right state */
53 json_object_set_new(ret, "component_id", comp_id2json(&conn->comp_id));
54
55 return ret;
56}
57
58static json_t *bank2json(const struct rspro_client_conn *conn)
59{
60 json_t *ret = client2json(conn);
61 json_object_set_new(ret, "bankId", json_integer(conn->bank.bank_id));
62 json_object_set_new(ret, "numberOfSlots", json_integer(conn->bank.num_slots));
63 return ret;
64}
65
66static json_t *bank_slot2json(const struct bank_slot *bslot)
67{
68 json_t *ret = json_object();
69 json_object_set_new(ret, "bankId", json_integer(bslot->bank_id));
70 json_object_set_new(ret, "slotNr", json_integer(bslot->slot_nr));
71 return ret;
72}
73static int json2bank_slot(struct bank_slot *bslot, json_t *in)
74{
75 json_t *jbank_id, *jslot_nr;
76
77 if (!json_is_object(in))
78 return -EINVAL;
79 jbank_id = json_object_get(in, "bankId");
80 if (!jbank_id || !json_is_integer(jbank_id))
81 return -EINVAL;
82 jslot_nr = json_object_get(in, "slotNr");
83 if (!jslot_nr || !json_is_integer(jslot_nr))
84 return -EINVAL;
85 bslot->bank_id = json_integer_value(jbank_id);
86 bslot->slot_nr = json_integer_value(jslot_nr);
87 return 0;
88}
89
90static json_t *client_slot2json(const struct client_slot *bslot)
91{
92 json_t *ret = json_object();
93 json_object_set_new(ret, "clientId", json_integer(bslot->client_id));
94 json_object_set_new(ret, "slotNr", json_integer(bslot->slot_nr));
95 return ret;
96}
97static int json2client_slot(struct client_slot *cslot, json_t *in)
98{
99 json_t *jclient_id, *jslot_nr;
100
101 if (!json_is_object(in))
102 return -EINVAL;
103 jclient_id = json_object_get(in, "clientId");
104 if (!jclient_id || !json_is_integer(jclient_id))
105 return -EINVAL;
106 jslot_nr = json_object_get(in, "slotNr");
107 if (!jslot_nr || !json_is_integer(jslot_nr))
108 return -EINVAL;
109 cslot->client_id = json_integer_value(jclient_id);
110 cslot->slot_nr = json_integer_value(jslot_nr);
111 return 0;
112}
113
114static json_t *slotmap2json(const struct slot_mapping *slotmap)
115{
116 json_t *ret = json_object();
117 json_object_set_new(ret, "bank", bank_slot2json(&slotmap->bank));
118 json_object_set_new(ret, "client", client_slot2json(&slotmap->client));
119 json_object_set_new(ret, "state", json_string(slotmap_state_name(slotmap->state)));
120 return ret;
121}
122static int json2slotmap(struct slot_mapping *out, json_t *in)
123{
124 json_t *jbank, *jclient;
125 int rc;
126
127 if (!json_is_object(in))
128 return -EINVAL;
129 jbank = json_object_get(in, "bank");
130 if (!jbank || !json_is_object(jbank))
131 return -EINVAL;
132 jclient = json_object_get(in, "client");
133 if (!jclient || !json_is_object(jclient))
134 return -EINVAL;
135
136 rc = json2bank_slot(&out->bank, jbank);
137 if (rc < 0)
138 return rc;
139 return json2client_slot(&out->client, jclient);
140}
141
142
143extern struct rspro_server *g_rps;
144
145static int api_cb_rest_ctr_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
146{
147 return U_CALLBACK_CONTINUE;
148}
149
150static int api_cb_banks_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
151{
152 struct rspro_client_conn *conn;
153 json_t *json_body = json_object();
154 json_t *json_banks = json_array();
155
156 pthread_rwlock_rdlock(&g_rps->rwlock);
157 llist_for_each_entry(conn, &g_rps->banks, list) {
158 json_array_append_new(json_banks, bank2json(conn));
159 }
160 pthread_rwlock_unlock(&g_rps->rwlock);
161
162 json_object_set_new(json_body, "banks", json_banks);
163 ulfius_set_json_body_response(resp, 200, json_body);
164 json_decref(json_body);
165
166 return U_CALLBACK_COMPLETE;
167}
168
169static int api_cb_bank_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
170{
171 const char *bank_id_str = u_map_get(req->map_url, "bank_id");
172 struct rspro_client_conn *conn;
173 json_t *json_body = NULL;
174 unsigned long bank_id;
175 int status;
176
177 if (!bank_id_str) {
178 status = 400;
179 goto out_err;
180 }
181
182 bank_id = strtoul(bank_id_str, NULL, 10);
183 if (bank_id > 0xffff) {
184 status = 400;
185 goto out_err;
186 }
187
188 pthread_rwlock_rdlock(&g_rps->rwlock);
189 llist_for_each_entry(conn, &g_rps->banks, list) {
190 if (conn->bank.bank_id == bank_id) {
191 json_body = bank2json(conn);
192 break;
193 }
194 }
195 pthread_rwlock_unlock(&g_rps->rwlock);
196
197 if (json_body) {
198 ulfius_set_json_body_response(resp, 200, json_body);
199 json_decref(json_body);
200 } else {
201 ulfius_set_json_body_response(resp, 404, json_body);
202 }
203
204 return U_CALLBACK_COMPLETE;
205
206out_err:
207 ulfius_set_empty_body_response(resp, status);
208 return U_CALLBACK_COMPLETE;
209}
210
211
212static int api_cb_clients_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
213{
214 struct rspro_client_conn *conn;
215 json_t *json_body = json_object();
216 json_t *json_clients = json_array();
217
218 pthread_rwlock_rdlock(&g_rps->rwlock);
219 llist_for_each_entry(conn, &g_rps->clients, list) {
220 json_array_append_new(json_clients, client2json(conn));
221 }
222 pthread_rwlock_unlock(&g_rps->rwlock);
223
224 json_object_set_new(json_body, "clients", json_clients);
225 ulfius_set_json_body_response(resp, 200, json_body);
226 json_decref(json_body);
227
228 return U_CALLBACK_COMPLETE;
229}
230
231static int api_cb_client_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
232{
233 const char *client_id_str = u_map_get(req->map_url, "client_id");
234 struct rspro_client_conn *conn;
235 json_t *json_body = NULL;
236 unsigned long client_id;
237 int status;
238
239 if (!client_id_str) {
240 status = 400;
241 goto out_err;
242 }
243
244 client_id = strtoul(client_id_str, NULL, 10);
245 if (client_id > 0xffff) {
246 status = 400;
247 goto out_err;
248 }
249
250 pthread_rwlock_rdlock(&g_rps->rwlock);
251 llist_for_each_entry(conn, &g_rps->clients, list) {
252 if (conn->bank.bank_id == client_id) { /* FIXME */
253 json_body = client2json(conn);
254 break;
255 }
256 }
257 pthread_rwlock_unlock(&g_rps->rwlock);
258
259 if (json_body) {
260 ulfius_set_json_body_response(resp, 200, json_body);
261 json_decref(json_body);
262 } else {
263 ulfius_set_json_body_response(resp, 404, json_body);
264 }
265
266 return U_CALLBACK_COMPLETE;
267
268out_err:
269 ulfius_set_empty_body_response(resp, status);
270 return U_CALLBACK_COMPLETE;
271}
272
273static int api_cb_slotmaps_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
274{
275 struct slot_mapping *map;
276 json_t *json_body = json_object();
277 json_t *json_maps = json_array();
278
279 slotmaps_rdlock(g_rps->slotmaps);
280 llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) {
281 json_array_append_new(json_maps, slotmap2json(map));
282 }
283 slotmaps_unlock(g_rps->slotmaps);
284
285 json_object_set_new(json_body, "slotmaps", json_maps);
286 ulfius_set_json_body_response(resp, 200, json_body);
287 json_decref(json_body);
288
289 return U_CALLBACK_COMPLETE;
290}
291
292extern struct osmo_fd g_event_ofd;
293/* trigger our main thread select() loop */
294static void trigger_main_thread_via_eventfd(void)
295{
296 uint64_t one = 1;
297 int rc;
298
299 rc = write(g_event_ofd.fd, &one, sizeof(one));
300 if (rc < 8)
301 fprintf(stderr, "Error writing to eventfd(): %d\n", rc);
302}
303
304static int api_cb_slotmaps_post(const struct _u_request *req, struct _u_response *resp, void *user_data)
305{
306 struct rspro_server *srv = g_rps;
307 struct slot_mapping slotmap, *map;
308 struct rspro_client_conn *conn;
309 json_error_t json_err;
310 json_t *json_req;
311 int rc;
312
313 json_req = ulfius_get_json_body_request(req, &json_err);
314 if (!json_req) {
315 fprintf(stderr, "REST: No JSON Body\n");
316 goto err;
317 }
318
319 rc = json2slotmap(&slotmap, json_req);
320 if (rc < 0)
321 goto err;
322 map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client);
323 if (!map) {
324 fprintf(stderr, "REST: Cannot add slotmap\n");
325 goto err;
326 }
327 slotmap_state_change(map, SLMAP_S_NEW, NULL);
328
329 /* check if any already-connected bankd matches this new map. If yes, associate it */
330 pthread_rwlock_rdlock(&srv->rwlock);
331 llist_for_each_entry(conn, &srv->banks, list) {
332 if (conn->bank.bank_id == slotmap.bank.bank_id) {
333 slotmap_state_change(map, SLMAP_S_NEW, &conn->bank.maps_new);
334 /* FIXME: Notify the conn FSM about some new maps being available */
335 trigger_main_thread_via_eventfd();
336 break;
337 }
338 }
339 pthread_rwlock_unlock(&srv->rwlock);
340
341
342 ulfius_set_empty_body_response(resp, 201);
343
344 return U_CALLBACK_COMPLETE;
345err:
346 ulfius_set_empty_body_response(resp, 400);
347 return U_CALLBACK_COMPLETE;
348}
349
350/* caller is holding a write lock on slotmaps->rwlock */
351static void _slotmap_mark_deleted(struct slot_mapping *map)
352{
353 struct rspro_client_conn *conn = bankd_conn_by_id(g_rps, map->bank.bank_id);
354
355 /* delete map from global list to ensure it's not found by further lookups,
356 * particularly in case somebody wants to create a new map for the same bank/slot */
357 llist_del(&map->list);
358 /* safely initialize list head to avoid trouble when del_slotmap() does another llist_del() */
359 INIT_LLIST_HEAD(&map->list);
360
361 switch (map->state) {
362 case SLMAP_S_NEW:
363 /* new map, not yet sent to bank: we can remove it immediately */
364 /* delete from bank list (if any) */
365 llist_del(&map->bank_list);
366 /* safely initialize list head to avoid trouble when del_slotmap() does another llist_del() */
367 INIT_LLIST_HEAD(&map->bank_list);
368 _slotmap_del(map->maps, map);
369 break;
370 case SLMAP_S_UNACKNOWLEDGED:
371 /* map has been sent to bank already, but wasn't acknowledged yet */
372 /* FIXME: what to do now? If we keep it unchanged, it will not be deleted. If we
373 * move it to DELETE_REQ, */
374 break;
375 case SLMAP_S_ACTIVE:
376 /* map is fully active. Need to move it to DELETE_REQ state + trigger rspro thread,
377 * so the deletion can propagate to the bankd */
378 _slotmap_state_change(map, SLMAP_S_DELETE_REQ, &conn->bank.maps_delreq);
379 trigger_main_thread_via_eventfd();
380 break;
381 case SLMAP_S_DELETE_REQ:
382 /* REST had already requested deletion, but RSPRO thread hasn't issued the delete
383 * command to the bankd yet: Do nothing */
384 break;
385 case SLMAP_S_DELETING:
386 /* we had already requested deletion of this map previously: Do nothing */
387 break;
388 default:
389 OSMO_ASSERT(0);
390 }
391}
392
393static int api_cb_slotmaps_del(const struct _u_request *req, struct _u_response *resp, void *user_data)
394{
395 const char *slotmap_id_str = u_map_get(req->map_url, "slotmap_id");
396 struct slot_mapping *map;
397 int status = 404;
398 unsigned long map_id;
399
400 if (!slotmap_id_str) {
401 status = 400;
402 goto err;
403 }
404 map_id = strtoul(slotmap_id_str, NULL, 10);
405 if (map_id < 0) {
406 status = 400;
407 goto err;
408 }
409
410 slotmaps_wrlock(g_rps->slotmaps);
411 llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) {
412 if (slotmap_get_id(map) == map_id) {
413 _slotmap_mark_deleted(map);
414 status = 200;
415 break;
416 }
417 }
418 slotmaps_unlock(g_rps->slotmaps);
419 trigger_main_thread_via_eventfd();
420
421
422 ulfius_set_empty_body_response(resp, status);
423 return U_CALLBACK_COMPLETE;
424err:
425 ulfius_set_empty_body_response(resp, status);
426 return U_CALLBACK_COMPLETE;
427}
428
429static int api_cb_global_reset_post(const struct _u_request *req, struct _u_response *resp, void *user_data)
430{
431 struct slot_mapping *map, *map2;
432
433 LOGP(DMAIN, LOGL_NOTICE, "Global RESET from REST API\n");
434
435 /* mark all slot mappings as deleted */
436 slotmaps_wrlock(g_rps->slotmaps);
437 llist_for_each_entry_safe(map, map2, &g_rps->slotmaps->mappings, list) {
438 _slotmap_mark_deleted(map);
439 }
440 slotmaps_unlock(g_rps->slotmaps);
441 trigger_main_thread_via_eventfd();
442
443 ulfius_set_empty_body_response(resp, 200);
444 return U_CALLBACK_COMPLETE;
445}
446
447static const struct _u_endpoint api_endpoints[] = {
448 /* get the current restart counter */
449 { "GET", PREFIX, "/restart-counter", 0, &api_cb_rest_ctr_get, NULL },
450 /* get a list of SIM banks */
451 { "GET", PREFIX, "/banks", 0, &api_cb_banks_get, NULL },
452 { "GET", PREFIX, "/banks/:bank_id", 0, &api_cb_bank_get, NULL },
453 /* get a list of SIM clients */
454 { "GET", PREFIX, "/clients", 0, &api_cb_clients_get, NULL },
455 { "GET", PREFIX, "/clients/:client_id", 0, &api_cb_client_get, NULL },
456 /* get a list of mappings */
457 { "GET", PREFIX, "/slotmaps", 0, &api_cb_slotmaps_get, NULL },
458 { "POST", PREFIX, "/slotmaps", 0, &api_cb_slotmaps_post, NULL },
459 { "DELETE", PREFIX, "/slotmaps/:slotmap_id", 0, &api_cb_slotmaps_del, NULL },
460 { "POST", PREFIX, "/global-reset", 0, &api_cb_global_reset_post, NULL },
461};
462
463static struct _u_instance g_instance;
464
465int rest_api_init(uint16_t port)
466{
467 int i;
468
469 if (ulfius_init_instance(&g_instance, port, NULL, NULL) != U_OK)
470 return -1;
471
472 for (i = 0; i < ARRAY_SIZE(api_endpoints); i++)
473 ulfius_add_endpoint(&g_instance, &api_endpoints[i]);
474
475 if (ulfius_start_framework(&g_instance) != U_OK) {
476 fprintf(stderr, "Cannot start REST API on port %u\n", port);
477 return -1;
478 }
479 return 0;
480}
481
482void rest_api_fini(void)
483{
484 ulfius_stop_framework(&g_instance);
485 ulfius_clean_instance(&g_instance);
486}