File size: 32,638 Bytes
258fb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
 * Copyright (C) 2019-2024 by Sukchan Lee <acetcom@gmail.com>
 *
 * This file is part of Open5GS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "sbi-path.h"
#include "nnrf-handler.h"

void pcf_state_initial(ogs_fsm_t *s, pcf_event_t *e)
{
    pcf_sm_debug(e);

    ogs_assert(s);

    OGS_FSM_TRAN(s, &pcf_state_operational);
}

void pcf_state_final(ogs_fsm_t *s, pcf_event_t *e)
{
    pcf_sm_debug(e);
}

void pcf_state_operational(ogs_fsm_t *s, pcf_event_t *e)
{
    int rv;

    ogs_sbi_stream_t *stream = NULL;
    ogs_pool_id_t stream_id = OGS_INVALID_POOL_ID;
    ogs_sbi_request_t *request = NULL;

    ogs_sbi_nf_instance_t *nf_instance = NULL;
    ogs_sbi_subscription_data_t *subscription_data = NULL;
    ogs_sbi_response_t *response = NULL;
    ogs_sbi_message_t message;

    ogs_sbi_object_t *sbi_object = NULL;
    ogs_sbi_xact_t *sbi_xact = NULL;
    ogs_pool_id_t sbi_xact_id = OGS_INVALID_POOL_ID;

    ogs_sbi_service_type_e service_type = OGS_SBI_SERVICE_TYPE_NULL;

    pcf_ue_t *pcf_ue = NULL;
    ogs_pool_id_t pcf_ue_id = OGS_INVALID_POOL_ID;
    pcf_sess_t *sess = NULL;
    ogs_pool_id_t sess_id = OGS_INVALID_POOL_ID;
    pcf_app_t *app_session = NULL;

    pcf_sm_debug(e);

    ogs_assert(s);

    switch (e->h.id) {
    case OGS_FSM_ENTRY_SIG:
        break;

    case OGS_FSM_EXIT_SIG:
        break;

    case OGS_EVENT_SBI_SERVER:
        request = e->h.sbi.request;
        ogs_assert(request);

        stream_id = OGS_POINTER_TO_UINT(e->h.sbi.data);
        ogs_assert(stream_id >= OGS_MIN_POOL_ID &&
                stream_id <= OGS_MAX_POOL_ID);

        stream = ogs_sbi_stream_find_by_id(stream_id);
        if (!stream) {
            ogs_error("STREAM has already been removed [%d]", stream_id);
            break;
        }

        rv = ogs_sbi_parse_request(&message, request);
        if (rv != OGS_OK) {
            /* 'message' buffer is released in ogs_sbi_parse_request() */
            ogs_error("cannot parse HTTP message");
            ogs_assert(true ==
                ogs_sbi_server_send_error(
                    stream, OGS_SBI_HTTP_STATUS_BAD_REQUEST,
                    NULL, "cannot parse HTTP message", NULL, NULL));
            break;
        }

        if (strcmp(message.h.api.version, OGS_SBI_API_V1) != 0) {
            ogs_error("Not supported version [%s]", message.h.api.version);
            ogs_assert(true ==
                ogs_sbi_server_send_error(
                    stream, OGS_SBI_HTTP_STATUS_BAD_REQUEST,
                    &message, "Not supported version", NULL, NULL));
            ogs_sbi_message_free(&message);
            break;
        }

        SWITCH(message.h.service.name)
        CASE(OGS_SBI_SERVICE_NAME_NNRF_NFM)

            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_NF_STATUS_NOTIFY)
                SWITCH(message.h.method)
                CASE(OGS_SBI_HTTP_METHOD_POST)
                    ogs_nnrf_nfm_handle_nf_status_notify(stream, &message);
                    break;

                DEFAULT
                    ogs_error("Invalid HTTP method [%s]", message.h.method);
                    ogs_assert(true ==
                        ogs_sbi_server_send_error(stream,
                            OGS_SBI_HTTP_STATUS_FORBIDDEN, &message,
                            "Invalid HTTP method", message.h.method, NULL));
                END
                break;

            DEFAULT
                ogs_error("Invalid resource name [%s]",
                        message.h.resource.component[0]);
                ogs_assert(true ==
                    ogs_sbi_server_send_error(stream,
                        OGS_SBI_HTTP_STATUS_BAD_REQUEST, &message,
                        "Unknown resource name",
                        message.h.resource.component[0], NULL));
            END
            break;

        CASE(OGS_SBI_SERVICE_NAME_NPCF_AM_POLICY_CONTROL)
            SWITCH(message.h.method)
            CASE(OGS_SBI_HTTP_METHOD_POST)
                if (message.PolicyAssociationRequest &&
                    message.PolicyAssociationRequest->supi) {
                    pcf_ue = pcf_ue_find_by_supi(
                                message.PolicyAssociationRequest->supi);
                    if (!pcf_ue) {
                        pcf_ue = pcf_ue_add(
                            message.PolicyAssociationRequest->supi);
                        ogs_assert(pcf_ue);
                    }
                }
                break;
            CASE(OGS_SBI_HTTP_METHOD_DELETE)
                if (message.h.resource.component[1]) {
                    pcf_ue = pcf_ue_find_by_association_id(
                                message.h.resource.component[1]);
                } else {
                    ogs_error("No Policy Association Id");
                }
                break;
            DEFAULT
            END

            if (!pcf_ue) {
                ogs_error("Not found [%s]", message.h.method);
                ogs_assert(true ==
                    ogs_sbi_server_send_error(stream,
                    OGS_SBI_HTTP_STATUS_NOT_FOUND,
                    &message, "Not found", message.h.method, NULL));
                break;
            }

            ogs_assert(OGS_FSM_STATE(&pcf_ue->sm));

            e->pcf_ue_id = pcf_ue->id;
            e->h.sbi.message = &message;
            ogs_fsm_dispatch(&pcf_ue->sm, e);
            if (OGS_FSM_CHECK(&pcf_ue->sm, pcf_am_state_exception)) {
                ogs_error("[%s] State machine exception", pcf_ue->supi);
                pcf_ue_remove(pcf_ue);
            } else if (OGS_FSM_CHECK(&pcf_ue->sm, pcf_am_state_deleted)) {
                ogs_debug("[%s] PCF-AM removed", pcf_ue->supi);
                pcf_ue_remove(pcf_ue);
            }
            break;

        CASE(OGS_SBI_SERVICE_NAME_NPCF_SMPOLICYCONTROL)
            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_SM_POLICIES)
                if (!message.h.resource.component[1]) {
                    if (message.SmPolicyContextData &&
                        message.SmPolicyContextData->supi &&
                        message.SmPolicyContextData->pdu_session_id) {

                        pcf_ue = pcf_ue_find_by_supi(
                                    message.SmPolicyContextData->supi);
                        if (!pcf_ue) {
                            if (!strcmp(message.h.method,
                                        OGS_SBI_HTTP_METHOD_POST)) {
                                pcf_ue = pcf_ue_add(
                                            message.SmPolicyContextData->supi);
                                if (!pcf_ue) {
                                    ogs_error("[%s:%d] Invalid Request [%s]",
                                            message.SmPolicyContextData->supi,
                                            message.SmPolicyContextData->
                                                pdu_session_id,
                                            message.h.method);
                                } else
                                    ogs_debug("[%s:%d] PCF UE added",
                                        message.SmPolicyContextData->supi,
                                        message.SmPolicyContextData->
                                            pdu_session_id);
                            } else {
                                ogs_error("[%s:%d] Invalid HTTP method [%s]",
                                        message.SmPolicyContextData->supi,
                                        message.SmPolicyContextData->
                                            pdu_session_id,
                                        message.h.method);
                            }
                        }

                        if (pcf_ue) {
                            sess = pcf_sess_find_by_psi(pcf_ue, message.
                                    SmPolicyContextData->pdu_session_id);
                            if (!sess) {
                                if (!strcmp(message.h.method,
                                            OGS_SBI_HTTP_METHOD_POST)) {
                                    sess = pcf_sess_add(pcf_ue, message.
                                        SmPolicyContextData->pdu_session_id);
                                    if (!sess) {
                                        ogs_error("[%s:%d] "
                                                "Invalid Request [%s]",
                                                message.SmPolicyContextData->
                                                    supi,
                                                message.SmPolicyContextData->
                                                    pdu_session_id,
                                                message.h.method);
                                    } else
                                        ogs_debug("[%s:%d] PCF session added",
                                                    pcf_ue->supi, sess->psi);
                                } else {
                                    ogs_error("[%s:%d] "
                                            "Invalid HTTP method [%s]",
                                            message.SmPolicyContextData->supi,
                                            message.SmPolicyContextData->
                                                pdu_session_id,
                                            message.h.method);
                                }
                            }
                        }
                    }
                } else {
                    sess = pcf_sess_find_by_sm_policy_id(
                            message.h.resource.component[1]);
                }
                break;

            DEFAULT
            END
            if (!sess) {
                ogs_error("Not found [%s]", message.h.uri);
                /*
                 * TS29.512
                 * 4.2.2.2 SM Policy Association establishment
                 *
                 * If the user information received within the "supi" attribute is
                 * unknown, the PCF shall reject the request with an HTTP "400 Bad
                 * Request" response message including the "cause" attribute
                 * of the ProblemDetails data structure set to "USER_UNKNOWN".
                 */
                ogs_assert(true ==
                    ogs_sbi_server_send_error(stream,
                        OGS_SBI_HTTP_STATUS_BAD_REQUEST,
                        &message, "Not found", message.h.uri, "USER_UNKNOWN"));
                break;
            }

            ogs_assert(OGS_FSM_STATE(&sess->sm));

            e->sess_id = sess->id;
            e->h.sbi.message = &message;
            ogs_fsm_dispatch(&sess->sm, e);
            if (OGS_FSM_CHECK(&sess->sm, pcf_sm_state_exception)) {
                ogs_error("[%s:%d] State machine exception",
                        pcf_ue->supi, sess->psi);
                pcf_sess_remove(sess);
            }
            break;

        CASE(OGS_SBI_SERVICE_NAME_NPCF_POLICYAUTHORIZATION)
            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_APP_SESSIONS)
                if (!message.h.resource.component[1]) {
                    if (message.AppSessionContext &&
                        message.AppSessionContext->asc_req_data &&
                        (message.AppSessionContext->asc_req_data->ue_ipv4 ||
                         message.AppSessionContext->asc_req_data->ue_ipv6)) {

                        if (!sess &&
                            message.AppSessionContext->asc_req_data->ue_ipv4)
                            sess = pcf_sess_find_by_ipv4addr(message.
                                    AppSessionContext->asc_req_data->ue_ipv4);
                        if (!sess &&
                            message.AppSessionContext->asc_req_data->ue_ipv6)
                            sess = pcf_sess_find_by_ipv6addr(message.
                                    AppSessionContext->asc_req_data->ue_ipv6);
                    }
                } else {
                    app_session = pcf_app_find_by_app_session_id(
                            message.h.resource.component[1]);
                    if (app_session)
                        sess = app_session->sess;
                }
                break;

            DEFAULT
            END

            if (!sess) {
                ogs_error("Not found [%s]", message.h.uri);
                ogs_assert(true ==
                    ogs_sbi_server_send_error(stream,
                        OGS_SBI_HTTP_STATUS_NOT_FOUND,
                        &message, "Not found", message.h.uri, NULL));
                break;
            }

            ogs_assert(OGS_FSM_STATE(&sess->sm));

            e->sess_id = sess->id;
            e->app = app_session;
            e->h.sbi.message = &message;
            ogs_fsm_dispatch(&sess->sm, e);
            if (OGS_FSM_CHECK(&sess->sm, pcf_sm_state_exception)) {
                ogs_error("[%s:%d] State machine exception",
                        pcf_ue->supi, sess->psi);
                pcf_sess_remove(sess);
            }
            break;

        DEFAULT
            ogs_error("Invalid API name [%s]", message.h.service.name);
            ogs_assert(true ==
                ogs_sbi_server_send_error(stream,
                    OGS_SBI_HTTP_STATUS_BAD_REQUEST, &message,
                    "Invalid API name", message.h.service.name, NULL));
        END

        /* In lib/sbi/server.c, notify_completed() releases 'request' buffer. */
        ogs_sbi_message_free(&message);
        break;

    case OGS_EVENT_SBI_CLIENT:
        ogs_assert(e);

        response = e->h.sbi.response;
        ogs_assert(response);
        rv = ogs_sbi_parse_response(&message, response);
        if (rv != OGS_OK) {
            ogs_error("cannot parse HTTP response");
            ogs_sbi_message_free(&message);
            ogs_sbi_response_free(response);
            break;
        }

        if (strcmp(message.h.api.version, OGS_SBI_API_V1) != 0) {
            ogs_error("Not supported version [%s]", message.h.api.version);
            ogs_sbi_message_free(&message);
            ogs_sbi_response_free(response);
            break;
        }

        SWITCH(message.h.service.name)
        CASE(OGS_SBI_SERVICE_NAME_NNRF_NFM)

            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_NF_INSTANCES)
                nf_instance = e->h.sbi.data;
                ogs_assert(nf_instance);
                ogs_assert(OGS_FSM_STATE(&nf_instance->sm));

                e->h.sbi.message = &message;
                ogs_fsm_dispatch(&nf_instance->sm, e);
                break;

            CASE(OGS_SBI_RESOURCE_NAME_SUBSCRIPTIONS)
                subscription_data = e->h.sbi.data;
                ogs_assert(subscription_data);

                SWITCH(message.h.method)
                CASE(OGS_SBI_HTTP_METHOD_POST)
                    if (message.res_status == OGS_SBI_HTTP_STATUS_CREATED ||
                        message.res_status == OGS_SBI_HTTP_STATUS_OK) {
                        ogs_nnrf_nfm_handle_nf_status_subscribe(
                                subscription_data, &message);
                    } else {
                        ogs_error("HTTP response error : %d",
                                message.res_status);
                    }
                    break;

                CASE(OGS_SBI_HTTP_METHOD_PATCH)
                    if (message.res_status == OGS_SBI_HTTP_STATUS_OK ||
                        message.res_status == OGS_SBI_HTTP_STATUS_NO_CONTENT) {
                        ogs_nnrf_nfm_handle_nf_status_update(
                                subscription_data, &message);
                    } else {
                        ogs_error("[%s] HTTP response error [%d]",
                                subscription_data->id ?
                                    subscription_data->id : "Unknown",
                                message.res_status);
                    }
                    break;

                CASE(OGS_SBI_HTTP_METHOD_DELETE)
                    if (message.res_status ==
                            OGS_SBI_HTTP_STATUS_NO_CONTENT) {
                        ogs_sbi_subscription_data_remove(subscription_data);
                    } else {
                        ogs_error("[%s] HTTP response error [%d]",
                                subscription_data->id ?
                                    subscription_data->id : "Unknown",
                                message.res_status);
                    }
                    break;

                DEFAULT
                    ogs_error("[%s] Invalid HTTP method [%s]",
                            subscription_data->id, message.h.method);
                    ogs_assert_if_reached();
                END
                break;
            
            DEFAULT
                ogs_error("Invalid resource name [%s]",
                        message.h.resource.component[0]);
                ogs_assert_if_reached();
            END
            break;

        CASE(OGS_SBI_SERVICE_NAME_NNRF_DISC)
            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_NF_INSTANCES)
                sbi_xact_id = OGS_POINTER_TO_UINT(e->h.sbi.data);
                ogs_assert(sbi_xact_id >= OGS_MIN_POOL_ID &&
                        sbi_xact_id <= OGS_MAX_POOL_ID);

                sbi_xact = ogs_sbi_xact_find_by_id(sbi_xact_id);
                if (!sbi_xact) {
                    /* CLIENT_WAIT timer could remove SBI transaction
                     * before receiving SBI message */
                    ogs_error("SBI transaction has already been removed [%d]",
                            sbi_xact_id);
                    break;
                }

                SWITCH(message.h.method)
                CASE(OGS_SBI_HTTP_METHOD_GET)
                    if (message.res_status == OGS_SBI_HTTP_STATUS_OK)
                        pcf_nnrf_handle_nf_discover(sbi_xact, &message);
                    else
                        ogs_error("HTTP response error [%d]",
                                message.res_status);
                    break;

                DEFAULT
                    ogs_error("Invalid HTTP method [%s]", message.h.method);
                    ogs_assert_if_reached();
                END
                break;

            DEFAULT
                ogs_error("Invalid resource name [%s]",
                        message.h.resource.component[0]);
                ogs_assert_if_reached();
            END
            break;

        CASE(OGS_SBI_SERVICE_NAME_NUDR_DR)
            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_POLICY_DATA)
                SWITCH(message.h.resource.component[3])
                CASE(OGS_SBI_RESOURCE_NAME_AM_DATA)
                    sbi_xact_id = OGS_POINTER_TO_UINT(e->h.sbi.data);
                    ogs_assert(sbi_xact_id >= OGS_MIN_POOL_ID &&
                            sbi_xact_id <= OGS_MAX_POOL_ID);

                    sbi_xact = ogs_sbi_xact_find_by_id(sbi_xact_id);
                    if (!sbi_xact) {
                        /* CLIENT_WAIT timer could remove SBI transaction
                         * before receiving SBI message */
                        ogs_error(
                                "SBI transaction has already been removed [%d]",
                                sbi_xact_id);
                        break;
                    }

                    pcf_ue_id = sbi_xact->sbi_object_id;
                    ogs_assert(pcf_ue_id >= OGS_MIN_POOL_ID &&
                            pcf_ue_id <= OGS_MAX_POOL_ID);

                    if (sbi_xact->assoc_stream_id >= OGS_MIN_POOL_ID &&
                        sbi_xact->assoc_stream_id <= OGS_MAX_POOL_ID)
                        e->h.sbi.data =
                            OGS_UINT_TO_POINTER(sbi_xact->assoc_stream_id);

                    ogs_sbi_xact_remove(sbi_xact);

                    pcf_ue = pcf_ue_find_by_id(pcf_ue_id);
                    if (!pcf_ue) {
                        ogs_error("UE(pcf_ue) Context "
                                    "has already been removed");
                        break;
                    }

                    e->pcf_ue_id = pcf_ue->id;
                    e->h.sbi.message = &message;

                    ogs_fsm_dispatch(&pcf_ue->sm, e);
                    if (OGS_FSM_CHECK(&pcf_ue->sm, pcf_am_state_exception)) {
                        ogs_error("[%s] State machine exception", pcf_ue->supi);
                        pcf_ue_remove(pcf_ue);
                    }
                    break;

                CASE(OGS_SBI_RESOURCE_NAME_SM_DATA)
                    sbi_xact_id = OGS_POINTER_TO_UINT(e->h.sbi.data);
                    ogs_assert(sbi_xact_id >= OGS_MIN_POOL_ID &&
                            sbi_xact_id <= OGS_MAX_POOL_ID);

                    sbi_xact = ogs_sbi_xact_find_by_id(sbi_xact_id);
                    if (!sbi_xact) {
                        /* CLIENT_WAIT timer could remove SBI transaction
                         * before receiving SBI message */
                        ogs_error(
                                "SBI transaction has already been removed [%d]",
                                sbi_xact_id);
                        break;
                    }

                    sess_id = sbi_xact->sbi_object_id;
                    ogs_assert(sess_id >= OGS_MIN_POOL_ID &&
                            sess_id <= OGS_MAX_POOL_ID);

                    if (sbi_xact->assoc_stream_id >= OGS_MIN_POOL_ID &&
                        sbi_xact->assoc_stream_id <= OGS_MAX_POOL_ID)
                        e->h.sbi.data =
                            OGS_UINT_TO_POINTER(sbi_xact->assoc_stream_id);

                    ogs_sbi_xact_remove(sbi_xact);

                    sess = pcf_sess_find_by_id(sess_id);
                    if (!sess) {
                        ogs_error("Session has already been removed");
                        break;
                    }

                    pcf_ue = pcf_ue_find_by_id(sess->pcf_ue_id);
                    ogs_assert(pcf_ue);

                    e->sess_id = sess->id;
                    e->h.sbi.message = &message;

                    ogs_fsm_dispatch(&sess->sm, e);
                    if (OGS_FSM_CHECK(&sess->sm, pcf_sm_state_exception)) {
                        ogs_error("[%s:%d] State machine exception",
                                    pcf_ue->supi, sess->psi);
                        pcf_sess_remove(sess);
                    }
                    break;

                DEFAULT
                    ogs_error("Invalid resource name [%s]",
                            message.h.resource.component[3]);
                    ogs_assert_if_reached();
                END
                break;

            DEFAULT
                ogs_error("Invalid resource name [%s]",
                        message.h.resource.component[0]);
                ogs_assert_if_reached();
            END
            break;

        CASE(OGS_SBI_SERVICE_NAME_NBSF_MANAGEMENT)

            SWITCH(message.h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_PCF_BINDINGS)
                sbi_xact_id = OGS_POINTER_TO_UINT(e->h.sbi.data);
                ogs_assert(sbi_xact_id >= OGS_MIN_POOL_ID &&
                        sbi_xact_id <= OGS_MAX_POOL_ID);

                sbi_xact = ogs_sbi_xact_find_by_id(sbi_xact_id);
                if (!sbi_xact) {
                    /* CLIENT_WAIT timer could remove SBI transaction
                     * before receiving SBI message */
                    ogs_error("SBI transaction has already been removed [%d]",
                            sbi_xact_id);
                    break;
                }

                sess_id = sbi_xact->sbi_object_id;
                ogs_assert(sess_id >= OGS_MIN_POOL_ID &&
                        sess_id <= OGS_MAX_POOL_ID);

                if (sbi_xact->assoc_stream_id >= OGS_MIN_POOL_ID &&
                    sbi_xact->assoc_stream_id <= OGS_MAX_POOL_ID)
                    e->h.sbi.data = OGS_UINT_TO_POINTER(sbi_xact->assoc_stream_id);

                ogs_sbi_xact_remove(sbi_xact);

                sess = pcf_sess_find_by_id(sess_id);
                if (!sess) {
                    ogs_error("Session has already been removed");
                    break;
                }

                pcf_ue = pcf_ue_find_by_id(sess->pcf_ue_id);
                ogs_assert(pcf_ue);

                e->sess_id = sess->id;
                e->h.sbi.message = &message;

                ogs_fsm_dispatch(&sess->sm, e);
                if (OGS_FSM_CHECK(&sess->sm, pcf_sm_state_exception)) {
                    ogs_error("[%s:%d] State machine exception",
                                pcf_ue->supi, sess->psi);
                    pcf_sess_remove(sess);
                } else if (OGS_FSM_CHECK(&sess->sm, pcf_sm_state_deleted)) {
                    ogs_debug("[%s:%d] PCF session removed",
                                pcf_ue->supi, sess->psi);
                    pcf_sess_remove(sess);
                }
                break;

            DEFAULT
                ogs_error("Invalid resource name [%s]",
                        message.h.resource.component[0]);
                ogs_assert_if_reached();
            END
            break;

        DEFAULT
            ogs_error("Invalid API name [%s]", message.h.service.name);
            ogs_assert_if_reached();
        END

        ogs_sbi_message_free(&message);
        ogs_sbi_response_free(response);
        break;

    case OGS_EVENT_SBI_TIMER:
        ogs_assert(e);

        switch(e->h.timer_id) {
        case OGS_TIMER_NF_INSTANCE_REGISTRATION_INTERVAL:
        case OGS_TIMER_NF_INSTANCE_HEARTBEAT_INTERVAL:
        case OGS_TIMER_NF_INSTANCE_NO_HEARTBEAT:
        case OGS_TIMER_NF_INSTANCE_VALIDITY:
            nf_instance = e->h.sbi.data;
            ogs_assert(nf_instance);
            ogs_assert(OGS_FSM_STATE(&nf_instance->sm));

            ogs_sbi_self()->nf_instance->load = pcf_instance_get_load();

            ogs_fsm_dispatch(&nf_instance->sm, e);
            if (OGS_FSM_CHECK(&nf_instance->sm, ogs_sbi_nf_state_exception))
                ogs_error("[%s:%s] State machine exception [%d]",
                        OpenAPI_nf_type_ToString(nf_instance->nf_type),
                        nf_instance->id, e->h.timer_id);
            break;

        case OGS_TIMER_SUBSCRIPTION_VALIDITY:
            subscription_data = e->h.sbi.data;
            ogs_assert(subscription_data);

            ogs_assert(true ==
                ogs_nnrf_nfm_send_nf_status_subscribe(
                    ogs_sbi_self()->nf_instance->nf_type,
                    subscription_data->req_nf_instance_id,
                    subscription_data->subscr_cond.nf_type,
                    subscription_data->subscr_cond.service_name));

            ogs_error("[%s] Subscription validity expired",
                subscription_data->id);
            ogs_sbi_subscription_data_remove(subscription_data);
            break;

        case OGS_TIMER_SUBSCRIPTION_PATCH:
            subscription_data = e->h.sbi.data;
            ogs_assert(subscription_data);

            ogs_assert(true ==
                ogs_nnrf_nfm_send_nf_status_update(subscription_data));

            ogs_info("[%s] Need to update Subscription",
                    subscription_data->id);
            break;

        case OGS_TIMER_SBI_CLIENT_WAIT:
            /*
             * ogs_pollset_poll() receives the time of the expiration
             * of next timer as an argument. If this timeout is
             * in very near future (1 millisecond), and if there are
             * multiple events that need to be processed by ogs_pollset_poll(),
             * these could take more than 1 millisecond for processing,
             * resulting in the timer already passed the expiration.
             *
             * In case that another NF is under heavy load and responds
             * to an SBI request with some delay of a few seconds,
             * it can happen that ogs_pollset_poll() adds SBI responses
             * to the event list for further processing,
             * then ogs_timer_mgr_expire() is called which will add
             * an additional event for timer expiration. When all events are
             * processed one-by-one, the SBI xact would get deleted twice
             * in a row, resulting in a crash.
             *
             * 1. ogs_pollset_poll()
             *    message was received and put into an event list,
             * 2. ogs_timer_mgr_expire()
             *    add an additional event for timer expiration
             * 3. message event is processed. (free SBI xact)
             * 4. timer expiration event is processed. (double-free SBI xact)
             *
             * To avoid double-free SBI xact,
             * we need to check ogs_sbi_xact_find_by_id()
             */
            sbi_xact_id = OGS_POINTER_TO_UINT(e->h.sbi.data);
            ogs_assert(sbi_xact_id >= OGS_MIN_POOL_ID &&
                    sbi_xact_id <= OGS_MAX_POOL_ID);

            sbi_xact = ogs_sbi_xact_find_by_id(sbi_xact_id);
            if (!sbi_xact) {
                ogs_error("SBI transaction has already been removed [%d]",
                        sbi_xact_id);
                break;
            }

            sbi_object = sbi_xact->sbi_object;
            ogs_assert(sbi_object);

            ogs_assert(sbi_xact->assoc_stream_id >= OGS_MIN_POOL_ID &&
                    sbi_xact->assoc_stream_id <= OGS_MAX_POOL_ID);
            stream = ogs_sbi_stream_find_by_id(sbi_xact->assoc_stream_id);

            service_type = sbi_xact->service_type;

            ogs_sbi_xact_remove(sbi_xact);

            ogs_assert(sbi_object->type > OGS_SBI_OBJ_BASE &&
                        sbi_object->type < OGS_SBI_OBJ_TOP);

            switch(sbi_object->type) {
            case OGS_SBI_OBJ_UE_TYPE:
                pcf_ue_id = sbi_xact->sbi_object_id;
                ogs_assert(pcf_ue_id >= OGS_MIN_POOL_ID &&
                        pcf_ue_id <= OGS_MAX_POOL_ID);

                pcf_ue = pcf_ue_find_by_id(pcf_ue_id);
                if (!pcf_ue) {
                    ogs_error("UE(pcf_ue) has already been removed");
                    break;
                }
                ogs_error("[%s] Cannot receive SBI message", pcf_ue->supi);
                break;

            case OGS_SBI_OBJ_SESS_TYPE:
                sess_id = sbi_xact->sbi_object_id;
                ogs_assert(sess_id >= OGS_MIN_POOL_ID &&
                        sess_id <= OGS_MAX_POOL_ID);

                sess = pcf_sess_find_by_id(sess_id);
                if (!sess) {
                    ogs_error("Session has already been removed");
                    break;
                }
                ogs_error("[%d] Cannot receive SBI message", sess->psi);
                break;

            default:
                ogs_fatal("Not implemented [%s:%d]",
                    ogs_sbi_service_type_to_name(service_type),
                    sbi_object->type);
                ogs_assert_if_reached();
            }

            ogs_error("Cannot receive SBI message");

            if (!stream) {
                ogs_error("STREAM has alreadt been removed [%d]",
                        sbi_xact->assoc_stream_id);
                break;
            }
            ogs_assert(true ==
                ogs_sbi_server_send_error(stream,
                    OGS_SBI_HTTP_STATUS_GATEWAY_TIMEOUT, NULL,
                    "Cannot receive SBI message", NULL, NULL));
            break;

        default:
            ogs_error("Unknown timer[%s:%d]",
                    ogs_timer_get_name(e->h.timer_id), e->h.timer_id);
        }
        break;

    default:
        ogs_error("No handler for event %s", pcf_event_get_name(e));
        break;
    }
}