blob: abd46b98c82bdf3df0e076a148aa66f820f91494 [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;
Harald Welte35913822019-07-21 20:00:33 +0200312 json_t *json_req = NULL;
Harald Weltef5a0fa32019-03-03 15:44:18 +0100313 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
Harald Welte35913822019-07-21 20:00:33 +0200344 json_decref(json_req);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100345 ulfius_set_empty_body_response(resp, 201);
346
347 return U_CALLBACK_COMPLETE;
348err:
Harald Welte35913822019-07-21 20:00:33 +0200349 json_decref(json_req);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100350 ulfius_set_empty_body_response(resp, 400);
351 return U_CALLBACK_COMPLETE;
352}
353
354/* caller is holding a write lock on slotmaps->rwlock */
355static void _slotmap_mark_deleted(struct slot_mapping *map)
356{
357 struct rspro_client_conn *conn = bankd_conn_by_id(g_rps, map->bank.bank_id);
358
359 /* delete map from global list to ensure it's not found by further lookups,
360 * particularly in case somebody wants to create a new map for the same bank/slot */
361 llist_del(&map->list);
362 /* safely initialize list head to avoid trouble when del_slotmap() does another llist_del() */
363 INIT_LLIST_HEAD(&map->list);
364
365 switch (map->state) {
366 case SLMAP_S_NEW:
367 /* new map, not yet sent to bank: we can remove it immediately */
368 /* delete from bank list (if any) */
369 llist_del(&map->bank_list);
370 /* safely initialize list head to avoid trouble when del_slotmap() does another llist_del() */
371 INIT_LLIST_HEAD(&map->bank_list);
372 _slotmap_del(map->maps, map);
373 break;
374 case SLMAP_S_UNACKNOWLEDGED:
375 /* map has been sent to bank already, but wasn't acknowledged yet */
376 /* FIXME: what to do now? If we keep it unchanged, it will not be deleted. If we
377 * move it to DELETE_REQ, */
378 break;
379 case SLMAP_S_ACTIVE:
380 /* map is fully active. Need to move it to DELETE_REQ state + trigger rspro thread,
381 * so the deletion can propagate to the bankd */
382 _slotmap_state_change(map, SLMAP_S_DELETE_REQ, &conn->bank.maps_delreq);
383 trigger_main_thread_via_eventfd();
384 break;
385 case SLMAP_S_DELETE_REQ:
386 /* REST had already requested deletion, but RSPRO thread hasn't issued the delete
387 * command to the bankd yet: Do nothing */
388 break;
389 case SLMAP_S_DELETING:
390 /* we had already requested deletion of this map previously: Do nothing */
391 break;
392 default:
393 OSMO_ASSERT(0);
394 }
395}
396
397static int api_cb_slotmaps_del(const struct _u_request *req, struct _u_response *resp, void *user_data)
398{
399 const char *slotmap_id_str = u_map_get(req->map_url, "slotmap_id");
400 struct slot_mapping *map;
401 int status = 404;
402 unsigned long map_id;
403
404 if (!slotmap_id_str) {
405 status = 400;
406 goto err;
407 }
408 map_id = strtoul(slotmap_id_str, NULL, 10);
409 if (map_id < 0) {
410 status = 400;
411 goto err;
412 }
413
414 slotmaps_wrlock(g_rps->slotmaps);
415 llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) {
416 if (slotmap_get_id(map) == map_id) {
417 _slotmap_mark_deleted(map);
418 status = 200;
419 break;
420 }
421 }
422 slotmaps_unlock(g_rps->slotmaps);
423 trigger_main_thread_via_eventfd();
424
425
426 ulfius_set_empty_body_response(resp, status);
427 return U_CALLBACK_COMPLETE;
428err:
429 ulfius_set_empty_body_response(resp, status);
430 return U_CALLBACK_COMPLETE;
431}
432
433static int api_cb_global_reset_post(const struct _u_request *req, struct _u_response *resp, void *user_data)
434{
435 struct slot_mapping *map, *map2;
436
437 LOGP(DMAIN, LOGL_NOTICE, "Global RESET from REST API\n");
438
439 /* mark all slot mappings as deleted */
440 slotmaps_wrlock(g_rps->slotmaps);
441 llist_for_each_entry_safe(map, map2, &g_rps->slotmaps->mappings, list) {
442 _slotmap_mark_deleted(map);
443 }
444 slotmaps_unlock(g_rps->slotmaps);
445 trigger_main_thread_via_eventfd();
446
447 ulfius_set_empty_body_response(resp, 200);
448 return U_CALLBACK_COMPLETE;
449}
450
451static const struct _u_endpoint api_endpoints[] = {
452 /* get the current restart counter */
453 { "GET", PREFIX, "/restart-counter", 0, &api_cb_rest_ctr_get, NULL },
454 /* get a list of SIM banks */
455 { "GET", PREFIX, "/banks", 0, &api_cb_banks_get, NULL },
456 { "GET", PREFIX, "/banks/:bank_id", 0, &api_cb_bank_get, NULL },
457 /* get a list of SIM clients */
458 { "GET", PREFIX, "/clients", 0, &api_cb_clients_get, NULL },
459 { "GET", PREFIX, "/clients/:client_id", 0, &api_cb_client_get, NULL },
460 /* get a list of mappings */
461 { "GET", PREFIX, "/slotmaps", 0, &api_cb_slotmaps_get, NULL },
462 { "POST", PREFIX, "/slotmaps", 0, &api_cb_slotmaps_post, NULL },
463 { "DELETE", PREFIX, "/slotmaps/:slotmap_id", 0, &api_cb_slotmaps_del, NULL },
464 { "POST", PREFIX, "/global-reset", 0, &api_cb_global_reset_post, NULL },
465};
466
467static struct _u_instance g_instance;
Harald Welte0c50c342019-07-21 20:16:29 +0200468static pthread_mutex_t g_tall_lock = PTHREAD_MUTEX_INITIALIZER;
469static void *g_tall_rest;
Harald Weltef5a0fa32019-03-03 15:44:18 +0100470
Harald Welte0c50c342019-07-21 20:16:29 +0200471static void *my_o_malloc(size_t sz)
472{
473 void *obj;
474 pthread_mutex_lock(&g_tall_lock);
475 obj = talloc_size(g_tall_rest, sz);
476 pthread_mutex_unlock(&g_tall_lock);
477 return obj;
478}
479
480static void *my_o_realloc(void *obj, size_t sz)
481{
482 pthread_mutex_lock(&g_tall_lock);
483 obj = talloc_realloc_size(g_tall_rest, obj, sz);
484 pthread_mutex_unlock(&g_tall_lock);
485 return obj;
486}
487
488static void my_o_free(void *obj)
489{
490 pthread_mutex_lock(&g_tall_lock);
491 talloc_free(obj);
492 pthread_mutex_unlock(&g_tall_lock);
493}
494
495int rest_api_init(void *ctx, uint16_t port)
Harald Weltef5a0fa32019-03-03 15:44:18 +0100496{
497 int i;
498
Harald Welte0c50c342019-07-21 20:16:29 +0200499 g_tall_rest = ctx;
500 o_set_alloc_funcs(my_o_malloc, my_o_realloc, my_o_free);
501
Harald Weltef5a0fa32019-03-03 15:44:18 +0100502 if (ulfius_init_instance(&g_instance, port, NULL, NULL) != U_OK)
503 return -1;
Harald Welte0c50c342019-07-21 20:16:29 +0200504 g_instance.mhd_response_copy_data = 1;
Harald Weltef5a0fa32019-03-03 15:44:18 +0100505
506 for (i = 0; i < ARRAY_SIZE(api_endpoints); i++)
507 ulfius_add_endpoint(&g_instance, &api_endpoints[i]);
508
509 if (ulfius_start_framework(&g_instance) != U_OK) {
510 fprintf(stderr, "Cannot start REST API on port %u\n", port);
511 return -1;
512 }
513 return 0;
514}
515
516void rest_api_fini(void)
517{
518 ulfius_stop_framework(&g_instance);
519 ulfius_clean_instance(&g_instance);
520}