/*
 * Copyright (C) 2019-2022 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 "ogs-sbi.h"

static void handle_nf_profile_retrieval(
        char *nf_instance_id,
        OpenAPI_nf_profile_t *NFProfile)
{
    ogs_sbi_nf_instance_t *nf_instance;
    ogs_sbi_subscription_spec_t *subscription_spec = NULL;
    bool save = false;

    ogs_assert(nf_instance_id);
    ogs_assert(NFProfile);

    if (!NFProfile->nf_instance_id) {
        ogs_error("No NFProfile.NFInstanceId");
        return;
    }

    if (!NFProfile->nf_type) {
        ogs_error("No NFProfile.NFType");
        return;
    }

    if (!NFProfile->nf_status) {
        ogs_error("No NFProfile.NFStatus");
        return;
    }

    nf_instance = ogs_sbi_nf_instance_find(nf_instance_id);
    if (nf_instance) {
        /* already have this nf_instance; done */
        return;
    }

    if (NF_INSTANCE_ID_IS_SELF(nf_instance_id)) {
        /* don't save ourselves */
        return;
    }

    nf_instance = ogs_sbi_nf_instance_add();
    if (!nf_instance) {
        ogs_error("Can't add retrieved NF instance [%s] "
                "due to insufficient space", nf_instance_id);
        return;
    }

    ogs_sbi_nf_instance_set_id(nf_instance, nf_instance_id);

    if (ogs_nnrf_nfm_handle_nf_profile(nf_instance, NFProfile) == false) {
        ogs_error("[%s] (NRF-profile-get) Invalid NFProfile", nf_instance_id);
        ogs_sbi_nf_instance_remove(nf_instance);
        return;
    }

    /* verify against our subscription list that we want to save this
     * nf instance to our context */
    ogs_list_for_each(&ogs_sbi_self()->subscription_spec_list, subscription_spec) {
        ogs_sbi_nf_service_t *nf_service = NULL;

        if (subscription_spec->subscr_cond.nf_type == nf_instance->nf_type) {
            /* ok; save the nf_instance */
            save = true;
            break;
        }

        ogs_list_for_each(&nf_instance->nf_service_list, nf_service) {
            if (subscription_spec->subscr_cond.service_name &&
                nf_service->name &&
                subscription_spec->subscr_cond.service_name ==
                    nf_service->name) {
                /* ok; save the nf_instance */
                save = true;
                break;
            }
        }

        if (save)
            break;
    }

    if (!save) {
        ogs_sbi_nf_instance_remove(nf_instance);
    } else {
        ogs_sbi_nf_fsm_init(nf_instance);
        ogs_info("[%s] (NRF-profile-get) NF registered", nf_instance->id);
        ogs_sbi_client_associate(nf_instance);
    }
}

static void handle_nf_list_retrieval(ogs_sbi_links_t *links)
{
    ogs_sbi_header_t header;
    ogs_sbi_message_t msg;
    OpenAPI_lnode_t *node = NULL;

    OpenAPI_list_for_each(links->items, node) {

        memset(&header, 0, sizeof(header));
        header.uri = node->data;

        if (ogs_sbi_parse_header(&msg, &header) != OGS_OK) {
            ogs_error("Cannot parse href: %s", header.uri);
            continue;
        }

        if (msg.h.resource.component[1])
            ogs_nnrf_nfm_send_nf_profile_get(msg.h.resource.component[1]);

        ogs_sbi_header_free(&header);
    }
}

void ogs_sbi_nf_fsm_init(ogs_sbi_nf_instance_t *nf_instance)
{
    ogs_event_t e;

    ogs_assert(nf_instance);

    memset(&e, 0, sizeof(e));
    e.sbi.data = nf_instance;

    ogs_fsm_init(&nf_instance->sm,
            ogs_sbi_nf_state_initial, ogs_sbi_nf_state_final, &e);
}

void ogs_sbi_nf_fsm_fini(ogs_sbi_nf_instance_t *nf_instance)
{
    ogs_event_t e;

    ogs_assert(nf_instance);

    memset(&e, 0, sizeof(e));
    e.sbi.data = nf_instance;

    if (NF_INSTANCE_TYPE_IS_NRF(nf_instance) &&
        OGS_FSM_CHECK(&nf_instance->sm, ogs_sbi_nf_state_registered)) {
        ogs_assert(true ==
                ogs_nnrf_nfm_send_nf_de_register(nf_instance));
    }

    ogs_fsm_fini(&nf_instance->sm, &e);
}

void ogs_sbi_nf_state_initial(ogs_fsm_t *s, ogs_event_t *e)
{
    ogs_sbi_nf_instance_t *nf_instance = NULL;

    ogs_assert(s);
    ogs_assert(e);

    ogs_sbi_sm_debug(e);

    nf_instance = e->sbi.data;
    ogs_assert(nf_instance);

    nf_instance->t_registration_interval = ogs_timer_add(ogs_app()->timer_mgr,
            ogs_timer_nf_instance_registration_interval, nf_instance);
    ogs_assert(nf_instance->t_registration_interval);
    nf_instance->t_heartbeat_interval = ogs_timer_add(ogs_app()->timer_mgr,
            ogs_timer_nf_instance_heartbeat_interval, nf_instance);
    ogs_assert(nf_instance->t_heartbeat_interval);
    nf_instance->t_no_heartbeat = ogs_timer_add(ogs_app()->timer_mgr,
            ogs_timer_nf_instance_no_heartbeat, nf_instance);
    ogs_assert(nf_instance->t_no_heartbeat);
    nf_instance->t_validity = ogs_timer_add(ogs_app()->timer_mgr,
            ogs_timer_nf_instance_validity, nf_instance);
    ogs_assert(nf_instance->t_validity);

    if (NF_INSTANCE_TYPE_IS_NRF(nf_instance)) {
        OGS_FSM_TRAN(s, &ogs_sbi_nf_state_will_register);
    } else {
        ogs_assert(nf_instance->id);
        OGS_FSM_TRAN(s, &ogs_sbi_nf_state_registered);
    }
}

void ogs_sbi_nf_state_final(ogs_fsm_t *s, ogs_event_t *e)
{
    ogs_sbi_nf_instance_t *nf_instance = NULL;

    ogs_assert(s);
    ogs_assert(e);

    ogs_sbi_sm_debug(e);

    nf_instance = e->sbi.data;
    ogs_assert(nf_instance);

    ogs_timer_delete(nf_instance->t_registration_interval);
    ogs_timer_delete(nf_instance->t_heartbeat_interval);
    ogs_timer_delete(nf_instance->t_no_heartbeat);
    ogs_timer_delete(nf_instance->t_validity);
}

void ogs_sbi_nf_state_will_register(ogs_fsm_t *s, ogs_event_t *e)
{
    ogs_sbi_nf_instance_t *nf_instance = NULL;
    ogs_sbi_message_t *message = NULL;
    int service_name_id = OpenAPI_service_name_NULL;

    ogs_assert(s);
    ogs_assert(e);

    ogs_sbi_sm_debug(e);

    nf_instance = e->sbi.data;
    ogs_assert(nf_instance);
    ogs_assert(NF_INSTANCE_TYPE_IS_NRF(nf_instance));

    switch (e->id) {
    case OGS_FSM_ENTRY_SIG:
        ogs_timer_start(nf_instance->t_registration_interval,
            ogs_local_conf()->time.message.sbi.reconnect_interval);

        ogs_expect(true == ogs_nnrf_nfm_send_nf_register(nf_instance));
        break;

    case OGS_FSM_EXIT_SIG:
        ogs_timer_stop(nf_instance->t_registration_interval);
        break;

    case OGS_EVENT_SBI_CLIENT:
        message = e->sbi.message;
        ogs_assert(message);

        service_name_id = ogs_sbi_service_name_id_from_string(
                message->h.service.name);
        switch (service_name_id) {
        case OpenAPI_service_name_nnrf_nfm:

            SWITCH(message->h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_NF_INSTANCES)

                if (message->res_status == OGS_SBI_HTTP_STATUS_OK ||
                    message->res_status == OGS_SBI_HTTP_STATUS_CREATED) {
                    ogs_nnrf_nfm_handle_nf_register(nf_instance, message);
                    OGS_FSM_TRAN(s, &ogs_sbi_nf_state_registered);
                } else {
                    ogs_error("[%s] HTTP Response Status Code [%d]",
                            NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                            message->res_status);
                    OGS_FSM_TRAN(s, &ogs_sbi_nf_state_exception);
                }
                break;

            DEFAULT
                ogs_error("[%s] Invalid resource name [%s]",
                        NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                        message->h.resource.component[0]);
            END
            break;

        default:
            ogs_error("[%s] Invalid API name [%s]",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                    message->h.service.name);
        }
        break;

    case OGS_EVENT_SBI_TIMER:
        switch(e->timer_id) {
        case OGS_TIMER_NF_INSTANCE_REGISTRATION_INTERVAL:
            ogs_warn("[%s] Retry registration with NRF",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance));

            ogs_timer_start(nf_instance->t_registration_interval,
                ogs_local_conf()->time.message.sbi.reconnect_interval);

            ogs_expect(true == ogs_nnrf_nfm_send_nf_register(nf_instance));
            break;

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

    default:
        ogs_error("Unknown event %s", ogs_event_get_name(e));
        break;
    }
}

void ogs_sbi_nf_state_registered(ogs_fsm_t *s, ogs_event_t *e)
{
    ogs_sbi_nf_instance_t *nf_instance = NULL;
    ogs_sbi_message_t *message = NULL;
    int service_name_id = OpenAPI_service_name_NULL;
    ogs_assert(s);
    ogs_assert(e);

    ogs_sbi_sm_debug(e);

    nf_instance = e->sbi.data;
    ogs_assert(nf_instance);

    switch (e->id) {
    case OGS_FSM_ENTRY_SIG:
        if (NF_INSTANCE_TYPE_IS_NRF(nf_instance)) {
            ogs_sbi_subscription_spec_t *subscription_spec = NULL;

            ogs_info("[%s] NF registered [Heartbeat:%ds]",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                    nf_instance->time.heartbeat_interval);

            if (nf_instance->time.heartbeat_interval) {
                ogs_timer_start(nf_instance->t_heartbeat_interval,
                    ogs_time_from_sec(nf_instance->time.heartbeat_interval));
                ogs_timer_start(nf_instance->t_no_heartbeat,
                    ogs_time_from_sec(
                        nf_instance->time.heartbeat_interval +
                        ogs_local_conf()->time.nf_instance.
                        no_heartbeat_margin));
            }

            ogs_list_for_each(
                &ogs_sbi_self()->subscription_spec_list, subscription_spec) {
                if (ogs_sbi_nf_status_subscription_exists(
                            ogs_sbi_self()->nf_instance->id,
                            subscription_spec->subscr_cond.nf_type,
                            subscription_spec->subscr_cond.service_name,
                            false)) {
                    ogs_warn("[%s] NF status subscription already exists, skip",
                            ogs_sbi_self()->nf_instance->id);
                    continue;
                }

                ogs_nnrf_nfm_send_nf_status_subscribe(
                        ogs_sbi_self()->nf_instance->nf_type,
                        ogs_sbi_self()->nf_instance->id,
                        subscription_spec->subscr_cond.nf_type,
                        subscription_spec->subscr_cond.service_name);
            }

            ogs_nnrf_nfm_send_nf_list_retrieve();
        }
        break;

    case OGS_FSM_EXIT_SIG:
        if (NF_INSTANCE_TYPE_IS_NRF(nf_instance)) {
            ogs_info("[%s] NF de-registered",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance));

            if (nf_instance->time.heartbeat_interval) {
                ogs_timer_stop(nf_instance->t_heartbeat_interval);
                ogs_timer_stop(nf_instance->t_no_heartbeat);
            }
        }
        break;

    case OGS_EVENT_SBI_CLIENT:
        message = e->sbi.message;
        ogs_assert(message);

        service_name_id = ogs_sbi_service_name_id_from_string(
                message->h.service.name);
        switch (service_name_id) {
        case OpenAPI_service_name_nnrf_nfm:

            SWITCH(message->h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_NF_INSTANCES)

                if (message->h.resource.component[1]) {
                    SWITCH(message->h.method)
                    CASE(OGS_SBI_HTTP_METHOD_PATCH)
                        if (message->res_status == OGS_SBI_HTTP_STATUS_NO_CONTENT ||
                            message->res_status == OGS_SBI_HTTP_STATUS_OK) {

                            if (nf_instance->time.heartbeat_interval)
                                ogs_timer_start(nf_instance->t_no_heartbeat,
                                    ogs_time_from_sec(
                                        nf_instance->time.heartbeat_interval +
                                        ogs_local_conf()->time.nf_instance.
                                            no_heartbeat_margin));

                        } else {
                            ogs_warn("[%s] HTTP response error [%d]",
                                NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                                message->res_status);
                            OGS_FSM_TRAN(s, &ogs_sbi_nf_state_exception);
                        }
                        break;

                    CASE(OGS_SBI_HTTP_METHOD_GET)
                        if (message->res_status == OGS_SBI_HTTP_STATUS_OK) {
                            if (!message->h.resource.component[1]) {
                                ogs_error("No NFInstanceId");
                                break;
                            }
                            if (!message->NFProfile) {
                                ogs_error("No NFProfile");
                                break;
                            }
                            handle_nf_profile_retrieval(
                                message->h.resource.component[1],
                                message->NFProfile);
                        } else {
                            ogs_warn("[%s] HTTP response error [%d]",
                                NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                                message->res_status);
                            OGS_FSM_TRAN(s, &ogs_sbi_nf_state_exception);
                        }
                        break;
                    DEFAULT
                        ogs_error("Unknown method [%s]", message->h.method);
                        break;
                    END
                } else {
                    if (!message->links) {
                        ogs_warn("No links");
                        break;
                    }
                    if (message->res_status != OGS_SBI_HTTP_STATUS_OK) {
                        ogs_warn("[%s] HTTP response error [%d]",
                                NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                                message->res_status);
                        break;
                    }

                    handle_nf_list_retrieval(message->links);
                }

                break;

            DEFAULT
                ogs_error("[%s] Invalid resource name [%s]",
                        NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                        message->h.resource.component[0]);
            END
            break;

        default:
            ogs_error("[%s] Invalid API name [%s]",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                    message->h.service.name);
        }
        break;

    case OGS_EVENT_SBI_TIMER:
        switch(e->timer_id) {
        case OGS_TIMER_NF_INSTANCE_HEARTBEAT_INTERVAL:
            if (nf_instance->time.heartbeat_interval)
                ogs_timer_start(nf_instance->t_heartbeat_interval,
                    ogs_time_from_sec(nf_instance->time.heartbeat_interval));

            ogs_assert(true == ogs_nnrf_nfm_send_nf_update(nf_instance));
            break;

        case OGS_TIMER_NF_INSTANCE_NO_HEARTBEAT:
            ogs_error("[%s] No heartbeat [type:%s]",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance),
                    OpenAPI_nf_type_ToString(
                        NF_INSTANCE_TYPE(ogs_sbi_self()->nf_instance)));


        /*
         * In case of NF re-registration due to heartbeat loss, clear any
         * local subscription bookkeeping tied to the current NF instance id.
         *
         * This prevents unbounded growth of subscription_data entries when
         * re-registration and re-subscription loops happen (e.g., docker-compose
         * timing/race), which could otherwise exhaust subscription_data_pool.
         */
            if (ogs_sbi_self()->nf_instance && ogs_sbi_self()->nf_instance->id)
                ogs_sbi_subscription_data_delete_and_remove_all_by_nf_instance_id(
                        ogs_sbi_self()->nf_instance->id);

            OGS_FSM_TRAN(s, &ogs_sbi_nf_state_will_register);
            break;

        case OGS_TIMER_NF_INSTANCE_VALIDITY:
            ogs_assert(!NF_INSTANCE_TYPE_IS_NRF(nf_instance));
            ogs_assert(nf_instance->id);

            ogs_info("[%s] NF expired [type:%s]",
                    nf_instance->id,
                    OpenAPI_nf_type_ToString(nf_instance->nf_type));
            OGS_FSM_TRAN(s, &ogs_sbi_nf_state_de_registered);
            break;

        default:
            ogs_error("[%s] Unknown timer [type:%s timer:%s:%d]",
                    nf_instance->id ? nf_instance->id : "Undefined",
                    OpenAPI_nf_type_ToString(nf_instance->nf_type),
                    ogs_timer_get_name(e->timer_id), e->timer_id);
        }
        break;

    default:
        ogs_error("[%s] Unknown event [type:%s event:%s]",
                nf_instance->id ? nf_instance->id : "Undefined",
                OpenAPI_nf_type_ToString(nf_instance->nf_type),
                ogs_event_get_name(e));
        break;
    }
}

void ogs_sbi_nf_state_de_registered(ogs_fsm_t *s, ogs_event_t *e)
{
    ogs_sbi_nf_instance_t *nf_instance = NULL;
    ogs_assert(s);
    ogs_assert(e);

    ogs_sbi_sm_debug(e);

    nf_instance = e->sbi.data;
    ogs_assert(nf_instance);
    ogs_assert(ogs_sbi_self()->nf_instance);

    switch (e->id) {
    case OGS_FSM_ENTRY_SIG:
        ogs_info("[%s] NF de-registered [type:%s]",
                nf_instance->id,
                OpenAPI_nf_type_ToString(nf_instance->nf_type));
        ogs_sbi_nf_fsm_fini(nf_instance);
        ogs_sbi_nf_instance_remove(nf_instance);
        break;

    case OGS_FSM_EXIT_SIG:
        break;

    default:
        ogs_error("[%s] Unknown event [type:%s event:%s]",
                nf_instance->id ? nf_instance->id : "Undefined",
                OpenAPI_nf_type_ToString(nf_instance->nf_type),
                ogs_event_get_name(e));
        break;
    }
}

void ogs_sbi_nf_state_exception(ogs_fsm_t *s, ogs_event_t *e)
{
    ogs_sbi_nf_instance_t *nf_instance = NULL;
    ogs_sbi_message_t *message = NULL;
    int service_name_id = OpenAPI_service_name_NULL;

    ogs_assert(s);
    ogs_assert(e);

    ogs_sbi_sm_debug(e);

    nf_instance = e->sbi.data;
    ogs_assert(nf_instance);
    ogs_assert(ogs_sbi_self()->nf_instance);

    switch (e->id) {
    case OGS_FSM_ENTRY_SIG:
        if (NF_INSTANCE_TYPE_IS_NRF(nf_instance)) {
            ogs_timer_start(nf_instance->t_registration_interval,
                ogs_local_conf()->time.message.sbi.
                    reconnect_interval_in_exception);
        }
        break;

    case OGS_FSM_EXIT_SIG:
        if (NF_INSTANCE_TYPE_IS_NRF(nf_instance)) {
            ogs_timer_stop(nf_instance->t_registration_interval);
        }
        break;

    case OGS_EVENT_SBI_TIMER:
        switch(e->timer_id) {
        case OGS_TIMER_NF_INSTANCE_REGISTRATION_INTERVAL:
            ogs_warn("[%s] Retry registration with NRF",
                    NF_INSTANCE_ID(ogs_sbi_self()->nf_instance));

            OGS_FSM_TRAN(s, &ogs_sbi_nf_state_will_register);
            break;

        default:
            ogs_error("[%s] Unknown timer[type:%s timer:%s:%d]",
                    nf_instance->id ? nf_instance->id : "Undefined",
                    OpenAPI_nf_type_ToString(nf_instance->nf_type),
                    ogs_timer_get_name(e->timer_id), e->timer_id);
        }
        break;

    case OGS_EVENT_SBI_CLIENT:
        message = e->sbi.message;
        ogs_assert(message);

        service_name_id = ogs_sbi_service_name_id_from_string(
                message->h.service.name);
        switch (service_name_id) {
        case OpenAPI_service_name_nnrf_nfm:

            SWITCH(message->h.resource.component[0])
            CASE(OGS_SBI_RESOURCE_NAME_NF_INSTANCES)
                break;
            DEFAULT
                ogs_error("Invalid resource name [%s]",
                        message->h.resource.component[0]);
            END
            break;
        default:
            ogs_error("Invalid API name [%s]", message->h.service.name);
        }
        break;

    default:
        ogs_error("[%s] Unknown event [type:%s event:%s]",
                nf_instance->id ? nf_instance->id : "Undefined",
                OpenAPI_nf_type_ToString(nf_instance->nf_type),
                ogs_event_get_name(e));
        break;
    }
}
