/* JSON message definitions for ES2+
 *
 * Author: Philipp Maier <pmaier@sysmocom.de> / sysmocom - s.f.m.c. GmbH
 *
 * 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 es2p_Types_JSON {

import from esx_header_Types_JSON all;

/* GSMA SGP.22, section 6.5.2.1 (MNO to SMDP+) */
type record JSON_ES2p_DownloadOrderRequest {
	JSON_ESx_RequestHeader header,
	charstring eid optional,
	charstring iccid optional,
	charstring profileType optional
};


/* GSMA SGP.22, section 6.5.2.1 (SMDP+ to MNO) */
type record JSON_ES2p_DownloadOrderResponse {
	JSON_ESx_ResponseHeader header,
	charstring iccid optional
};
private external function dec_JSON_ES2p_DownloadOrderRequest(in octetstring stream)
	return JSON_ES2p_DownloadOrderRequest
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}
private external function dec_JSON_ES2p_DownloadOrderResponse(in octetstring stream)
	return JSON_ES2p_DownloadOrderResponse
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


/* GSMA SGP.22, section 6.5.2.2 (MNO to SMDP+) */
type record JSON_ES2p_ConfirmOrderRequest {
	JSON_ESx_RequestHeader header,
	charstring iccid,
	charstring eid optional,
	charstring matchingId optional,
	charstring confirmationCode optional,
	charstring smdsAddress optional,
	boolean releaseFlag
};


/* GSMA SGP.22, section 6.5.2.2 (SMDP+ to MNO) */
type record JSON_ES2p_ConfirmOrderResponse {
	JSON_ESx_ResponseHeader header,
	charstring eid optional,
	charstring matchingId optional,
	charstring smdpAddress optional
};
private external function dec_JSON_ES2p_ConfirmOrderRequest(in octetstring stream)
	return JSON_ES2p_ConfirmOrderRequest
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}
private external function dec_JSON_ES2p_ConfirmOrderResponse(in octetstring stream)
	return JSON_ES2p_ConfirmOrderResponse
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


/* GSMA SGP.22, section 6.5.2.3 (MNO to SMDP+) */
type record JSON_ES2p_CancelOrderRequest {
	JSON_ESx_RequestHeader header,
	charstring iccid,
	charstring eid optional,
	charstring matchingId optional,
	charstring finalProfileStatusIndicator optional
};


/* GSMA SGP.22, section 6.5.2.3 (SMDP+ to MNO) */
type record JSON_ES2p_CancelOrderResponse {
	JSON_ESx_ResponseHeader header,
	charstring iccid optional
};
private external function dec_JSON_ES2p_CancelOrderRequest(in octetstring stream)
	return JSON_ES2p_CancelOrderRequest
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}
private external function dec_JSON_ES2p_CancelOrderResponse(in octetstring stream)
	return JSON_ES2p_CancelOrderResponse
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


/* GSMA SGP.22, section 6.5.2.4 (MNO to SMDP+) */
type record JSON_ES2p_ReleaseProfileRequest {
	JSON_ESx_RequestHeader header,
	charstring iccid
};


/* GSMA SGP.22, section 6.5.2.4 (SMDP+ to MNO) */
type record JSON_ES2p_ReleaseProfileResponse {
	JSON_ESx_ResponseHeader header
};
private external function dec_JSON_ES2p_ReleaseProfileRequest(in octetstring stream)
	return JSON_ES2p_ReleaseProfileRequest
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}
private external function dec_JSON_ES2p_ReleaseProfileResponse(in octetstring stream)
	return JSON_ES2p_ReleaseProfileResponse
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


/* GSMA SGP.22, section 6.5.2.5 (SMDP+ to MNO) */
type record JSON_ES2p_HandleDownloadProgressInfoRequest {
	JSON_ESx_RequestHeader header,
	charstring eid optional,
	charstring iccid,
	charstring profileType,
	charstring timestamp,
	integer notificationPointId,
	record {
		charstring status,
		record {
			charstring subjectCode,
			charstring reasonCode,
			charstring subjectIdentifier optional,
			charstring message_ optional
		} statusCodeData optional
	} notificationPointStatus,
	charstring resultData optional
};
external function enc_JSON_ES2p_HandleDownloadProgressInfoRequest(in JSON_ES2p_HandleDownloadProgressInfoRequest msg)
	return octetstring
with {
	extension "prototype (convert) encode(JSON)";
	extension "printing(pretty)";
	extension "errorbehavior(ALL:ERROR)"
}
external function dec_JSON_ES2p_HandleDownloadProgressInfoRequest(in octetstring stream)
	return JSON_ES2p_HandleDownloadProgressInfoRequest
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


/* GSMA SGP.22, section 6.5.2.5 (MNO+ to SMDP+) */
type record JSON_ES2p_HandleDownloadProgressInfoResponse {
	JSON_ESx_ResponseHeader header
};

external function enc_JSON_ES2p_HandleDownloadProgressInfoResponse(in JSON_ES2p_HandleDownloadProgressInfoResponse msg)
	return octetstring
with {
	extension "prototype (convert) encode(JSON)";
	extension "printing(pretty)";
	extension "errorbehavior(ALL:ERROR)"
}
external function dec_JSON_ES2p_HandleDownloadProgressInfoResponse(in octetstring stream)
	return JSON_ES2p_HandleDownloadProgressInfoResponse
with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


/* Definition for JSON ES2+ requests (MNO to SMDP+) */
type union JSON_ES2p_Request {
	JSON_ES2p_DownloadOrderRequest downloadOrderRequest,
	JSON_ES2p_ConfirmOrderRequest confirmOrderRequest,
	JSON_ES2p_CancelOrderRequest cancelOrderRequest,
	JSON_ES2p_ReleaseProfileRequest releaseProfileRequest
} with {
	variant "JSON : as value"
}
external function enc_JSON_ES2p_Request(in JSON_ES2p_Request msg) return octetstring
with {
	extension "prototype (convert) encode(JSON)";
	extension "printing(pretty)";
	extension "errorbehavior(ALL:ERROR)"
}
function dec_JSON_ES2p_Request(in octetstring stream, charstring functionName) return JSON_ES2p_Request
{
	/* Unfortunately the ES2p message definitions are ambiguous, so that it is not possible to reliably detect
	 * the message type without knowing further context (funtionName). */

	var JSON_ES2p_Request request;

	if (functionName == "downloadOrder") {
		request.downloadOrderRequest := dec_JSON_ES2p_DownloadOrderRequest(stream);
	} else if (functionName == "confirmOrder") {
		request.confirmOrderRequest := dec_JSON_ES2p_ConfirmOrderRequest(stream);
	} else if (functionName == "cancelOrder") {
		request.cancelOrderRequest := dec_JSON_ES2p_CancelOrderRequest(stream);
	} else if (functionName == "releaseProfile") {
		request.releaseProfileRequest := dec_JSON_ES2p_ReleaseProfileRequest(stream);
	} else {
		setverdict(fail, "unknown JSON ES2+ function selected for decoding!");
	}

	return request;
}


/* Definition for JSON ES2+ responses (SMDP+ to MNO) */
type union JSON_ES2p_Response
{
	JSON_ES2p_DownloadOrderResponse downloadOrderResponse,
	JSON_ES2p_ConfirmOrderResponse confirmOrderResponse,
	JSON_ES2p_CancelOrderResponse cancelOrderResponse,
	JSON_ES2p_ReleaseProfileResponse releaseProfileResponse
} with {
	variant "JSON : as value"
}
external function enc_JSON_ES2p_Response(in JSON_ES2p_Response msg) return octetstring
with {
	extension "prototype (convert) encode(JSON)";
	extension "printing(pretty)";
	extension "errorbehavior(ALL:ERROR)"
}
function dec_JSON_ES2p_Response(in octetstring stream, charstring functionName) return JSON_ES2p_Response
{
	/* Unfortunately the ES2p message definitions are ambiguous, so that it is not possible to reliably detect
	 * the message type without knowing further context (funtionName). */

	var JSON_ES2p_Response response;

	if (functionName == "downloadOrder") {
		response.downloadOrderResponse := dec_JSON_ES2p_DownloadOrderResponse(stream);
	} else if (functionName == "confirmOrder") {
		response.confirmOrderResponse := dec_JSON_ES2p_ConfirmOrderResponse(stream);
	} else if (functionName == "cancelOrder") {
		response.cancelOrderResponse := dec_JSON_ES2p_CancelOrderResponse(stream);
	} else if (functionName == "releaseProfile") {
		response.releaseProfileResponse := dec_JSON_ES2p_ReleaseProfileResponse(stream);
	} else {
		setverdict(fail, "unknown JSON ES2+ function selected for decoding!");
	}

	return response;
}


/* GSMA SGP.22, section 6.5.2.1 */
template (value) JSON_ES2p_Request
ts_downloadOrderRequest := {
	downloadOrderRequest := {
		header := ts_requestHeader,
		eid := omit,
		iccid := "123456789ABCDEFFAAAA",
		profileType := omit
	}
}
template (present) JSON_ES2p_Request
tr_downloadOrderRequest := {
	downloadOrderRequest := {
		header := tr_requestHeader,
		eid := *,
		iccid := *,
		profileType := *
	}
}

template (value) JSON_ES2p_Response
ts_downloadOrderResponse := {
	downloadOrderResponse := {
		header := ts_responseHeader,
		iccid := "123456789012345"
	}
}
template (present) JSON_ES2p_Response
tr_downloadOrderResponse := {
	downloadOrderResponse := {
		header := tr_responseHeader,
		iccid := ?
	}
}


/* GSMA SGP.22, section 6.5.2.2 */
template (value) JSON_ES2p_Request
ts_confirmOrderRequest := {
	confirmOrderRequest := {
		header := ts_requestHeader,
		iccid := "123456789ABCDEFFAAAA",
		eid := omit,
		matchingId := omit,
		confirmationCode := omit,
		smdsAddress := omit,
		releaseFlag := false
	}
}
template (present) JSON_ES2p_Request
tr_confirmOrderRequest := {
	confirmOrderRequest := {
		header := tr_requestHeader,
		iccid := ?,
		eid := *,
		matchingId := *,
		confirmationCode := *,
		smdsAddress := *,
		releaseFlag := ?
	}
}

template (value) JSON_ES2p_Response
ts_confirmOrderResponse := {
	confirmOrderResponse := {
		header := ts_responseHeader,
		eid := omit,
		matchingId := omit,
		smdpAddress := omit
	}
}
template (present) JSON_ES2p_Response
tr_confirmOrderResponse := {
	confirmOrderResponse := {
		header := tr_responseHeader,
		eid := *,
		matchingId := *,
		smdpAddress := *
	}
}


/* GSMA SGP.22, section 6.5.2.3 */
template (value) JSON_ES2p_Request
ts_cancelOrderRequest := {
	cancelOrderRequest := {
		header := ts_requestHeader,
		iccid := "123456789ABCDEFFAAAA",
		eid := omit,
		matchingId := omit,
		finalProfileStatusIndicator := omit
	}
}
template (present) JSON_ES2p_Request
tr_cancelOrderRequest := {
	cancelOrderRequest := {
		header := tr_requestHeader,
		iccid := ?,
		eid := *,
		matchingId := *,
		finalProfileStatusIndicator := *
	}
}

template (value) JSON_ES2p_Response
ts_cancelOrderResponse := {
	cancelOrderResponse := {
		header := ts_responseHeader,
		iccid := "123456789012345"
	}
}
template (present) JSON_ES2p_Response
tr_cancelOrderResponse := {
	cancelOrderResponse := {
		header := tr_responseHeader,
		iccid := ?
	}
}


/* GSMA SGP.22, section 6.5.2.4 */
template (value) JSON_ES2p_Request
ts_releaseProfileRequest := {
	releaseProfileRequest := {
		header := ts_requestHeader,
		iccid := "123456789012345"
	}
}
template (present) JSON_ES2p_Request
tr_releaseProfileRequest := {
	releaseProfileRequest := {
		header := tr_requestHeader,
		iccid := ?
	}
}

template (value) JSON_ES2p_Response
ts_releaseProfileResponse := {
	releaseProfileResponse := {
		header := ts_responseHeader
	}
}
template (present) JSON_ES2p_Response
tr_releaseProfileResponse := {
	releaseProfileResponse := {
		header := tr_responseHeader
	}
}


/* GSMA SGP.22, section 6.5.2.5 */
template (value) JSON_ES2p_HandleDownloadProgressInfoRequest
ts_handleDownloadProgressInfoRequest := {
	header := ts_requestHeader,
	eid := omit,
	iccid := "123456789012345",
	profileType := "THIS_IS_A_PROFILE_TYPE",
	timestamp := "1970-01-01T00:00:00.511Z",
	notificationPointId := 1,
	notificationPointStatus := {
		status := "Executed-Success",
		statusCodeData := omit
	},
	resultData := omit
}
template (present) JSON_ES2p_HandleDownloadProgressInfoRequest
tr_handleDownloadProgressInfoRequest := {
	header := tr_requestHeader,
	eid := *,
	iccid := ?,
	profileType := ?,
	timestamp := ?,
	notificationPointId := ?,
	notificationPointStatus := {
		status := ?,
		statusCodeData := *
	},
	resultData := *
}

template (value) JSON_ES2p_HandleDownloadProgressInfoResponse
ts_handleDownloadProgressInfoResponse := {
	header := ts_responseHeader
}
template (present) JSON_ES2p_HandleDownloadProgressInfoResponse
tr_handleDownloadProgressInfoResponse := {
	header := tr_responseHeader
}


} with {
	encode "JSON";
}
