/* REST interface API for OsmoS1GW
 *
 * (C) 2025 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Vadim Yanitskiy <vyanitskiy@sysmocom.de>
 *
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

module S1GW_REST_Functions {

import from HTTP_Adapter all;
import from HTTPmsg_Types all;
import from S1GW_REST_Types all;

/* Get a list of metrics */
function f_REST_MetricsList(charstring metric_type := "all",
			    charstring path := "")
runs on http_CT return MetricsList {
	var HTTPMessage rsp := f_http_transact("/metrics-list?type=" & metric_type & "&path=" & path,
					       exp := tr_HTTP_RespBody(decmatch MetricsList:?));
	return dec_MetricsList(char2oct(rsp.response.body));
}

/* Get the PFCP association state */
function f_REST_PfcpAssocState()
runs on http_CT return PfcpAssocInfo {
	var HTTPMessage rsp := f_http_transact("/pfcp/assoc", method := "GET",
					       exp := tr_HTTP_RespBody(decmatch PfcpAssocInfo:?));
	return dec_PfcpAssocInfo(char2oct(rsp.response.body));
}

/* Initiate the PFCP Association Setup procedure */
function f_REST_PfcpAssocSetup()
runs on http_CT return OperationResult {
	var HTTPMessage rsp := f_http_transact("/pfcp/assoc", method := "POST",
					       exp := tr_HTTP_RespBody(decmatch OperationResult:?));
	return dec_OperationResult(char2oct(rsp.response.body));
}

/* Initiate the PFCP Association Release procedure */
function f_REST_PfcpAssocRelease()
runs on http_CT return OperationResult {
	var HTTPMessage rsp := f_http_transact("/pfcp/assoc", method := "DELETE",
					       exp := tr_HTTP_RespBody(decmatch OperationResult:?));
	return dec_OperationResult(char2oct(rsp.response.body));
}

/* Send a PFCP Heartbeat Request to the peer */
function f_REST_PfcpHeartbeat()
runs on http_CT return OperationResult {
	var HTTPMessage rsp := f_http_transact("/pfcp/heartbeat", method := "POST",
					       exp := tr_HTTP_RespBody(decmatch OperationResult:?));
	return dec_OperationResult(char2oct(rsp.response.body));
}

/* Get a list of MMEs in the pool */
function f_REST_MmeList()
runs on http_CT return MmeList {
	var HTTPMessage rsp := f_http_transact("/mme-list", method := "GET",
					       exp := tr_HTTP_RespBody(decmatch MmeList:?));
	return dec_MmeList(char2oct(rsp.response.body));
}

/* Add an MME to the pool */
function f_REST_MmeAdd(MmeItem mme_item)
runs on http_CT return boolean {
	var HTTPMessage rsp := f_http_transact("/mme-list", method := "POST",
					       binary_body := enc_MmeItem(mme_item));
	return rsp.response.statuscode == 201;
}

/* Get information about a specific MME */
function f_REST_MmeInfo(ParamMmeId mme_id)
runs on http_CT return MmeItem {
	var charstring p_mme_id := enc_ParamMmeId(mme_id);
	var HTTPMessage rsp := f_http_transact("/mme/" & p_mme_id, method := "GET",
					       exp := tr_HTTP_RespBody(decmatch MmeItem:?));
	return dec_MmeItem(char2oct(rsp.response.body));
}

/* Delete an MME from the pool */
function f_REST_MmeDelete(ParamMmeId mme_id)
runs on http_CT return boolean {
	var charstring p_mme_id := enc_ParamMmeId(mme_id);
	var HTTPMessage rsp := f_http_transact("/mme/" & p_mme_id, method := "DELETE");
	return rsp.response.statuscode == 200;
}

/* Get a list of eNB connections */
function f_REST_EnbList()
runs on http_CT return EnbList {
	var HTTPMessage rsp := f_http_transact("/enb-list", method := "GET",
					       exp := tr_HTTP_RespBody(decmatch EnbList:?));
	return dec_EnbList(char2oct(rsp.response.body));
}

/* Get information about a specific eNB */
function f_REST_EnbInfo(ParamEnbId enb_id)
runs on http_CT return EnbItem {
	var charstring p_enb_id := enc_ParamEnbId(enb_id);
	var HTTPMessage rsp := f_http_transact("/enb/" & p_enb_id, method := "GET",
					       exp := tr_HTTP_RespBody(decmatch EnbItem:?));
	return dec_EnbItem(char2oct(rsp.response.body));
}

/* Get E-RAB list for a specific eNB */
function f_REST_EnbErabList(ParamEnbId enb_id)
runs on http_CT return ErabList {
	var charstring p_enb_id := enc_ParamEnbId(enb_id);
	var HTTPMessage rsp := f_http_transact("/enb/" & p_enb_id & "/erab-list", method := "GET",
					       exp := tr_HTTP_RespBody(decmatch ErabList:?));
	return dec_ErabList(char2oct(rsp.response.body));
}

/* Get E-RAB list for all eNBs */
function f_REST_ErabList()
runs on http_CT return ErabList {
	var HTTPMessage rsp := f_http_transact("/erab-list", method := "GET",
					       exp := tr_HTTP_RespBody(decmatch ErabList:?));
	return dec_ErabList(char2oct(rsp.response.body));
}

/* Get information about a specific E-RAB */
function f_REST_ErabInfo(ParamErabId erab_id)
runs on http_CT return ErabItem {
	var charstring p_erab_id := enc_ParamErabId(erab_id);
	var HTTPMessage rsp := f_http_transact("/erab/" & p_erab_id, method := "GET",
					       exp := tr_HTTP_RespBody(decmatch ErabItem:?));
	return dec_ErabItem(char2oct(rsp.response.body));
}

}
