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