/* REST message definitions for eIM REST API
 *
 * 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 REST_Types_JSON {

type record JSON_REST_download {
       charstring activationCode
};

type union JSON_REST_Psmo {
	record {
		octetstring iccid,
		boolean rollback
	} enable,
	record {
		octetstring iccid
	} disable,
	record {
		octetstring iccid
	} delete,
	record {
		union {
			octetstring isdpAid,
			octetstring iccid,
			integer profileClass
		} searchCriteria optional,
		octetstring tagList optional
	} listProfileInfo,
	record {
	} getRAT,
	record {
		boolean autoEnableFlag,
		objid smdpOid optional,
		charstring smdpAddress optional
	} configureAutoEnable
};

type union JSON_REST_Eco {
	record {
		/* ASN.1 encoded EimConfigurationData */
		octetstring eimConfigurationData
	} addEim,
	record {
		charstring eimId
	} deleteEim,
	record {
		/* ASN.1 encoded EimConfigurationData */
		octetstring eimConfigurationData
	} updateEim,
	record {
	} listEim
};

type record JSON_REST_Edr {
	octetstring tagList
};

type union JSON_REST_Euicc {
	integer counterValue,
	boolean consumerEuicc,
	integer associationToken,
	octetstring signAlgo,
	octetstring signPubKey
};

type record JSON_REST_Resource {
	charstring eidValue,
	union {
		JSON_REST_download download,
		record of JSON_REST_Psmo psmo,
		record of JSON_REST_Eco eco,
		JSON_REST_Edr edr,
		record of JSON_REST_Euicc euicc
	} order
};

type record JSON_REST_ProfileInfo {
	charstring iccid optional,
	charstring isdpAid optional,
	charstring profileState optional,
	charstring profileNickname optional,
	charstring serviceProviderName optional,
	charstring profileName optional,
	charstring iconType optional,
	charstring icon optional,
	charstring profileClass optional
	//TODO: notificationConfigurationInfo
	//TODO: profileOwner
	//TODO: dpProprietaryData
	//TODO: profilePolicyRules
	//TODO: serviceSpecificDataStoredInEuicc
};

type record JSON_REST_EimIdList {
	charstring eimId,
	charstring eimIdType optional
}

type record JSON_REST_OperatorId {
	octetstring mccMnc,
	octetstring gid1 optional,
	octetstring gid2 optional
}

type record JSON_REST_ProfilePolicyAuthorisationRule {
	boolean	pprUpdateControl,
	boolean ppr1,
	boolean ppr2,
	record of JSON_REST_OperatorId allowedOperators,
	boolean consentRequired
}

type union JSON_REST_Result {
	charstring enableResult,
	charstring disableResult,
	charstring deleteResult,
	record {
		charstring finalResult,
		record of JSON_REST_ProfileInfo profileInfoList optional
	} listProfileInfoResult,
	record of JSON_REST_ProfilePolicyAuthorisationRule getRATResult,
	charstring configureAutoEnableResult,
	record {
	       charstring addEimResultCode,
	       integer associationToken optional
	} addEimResult,
	charstring deleteEimResult,
	charstring updateEimResult,
	record {
		charstring finalResult,
		record of JSON_REST_EimIdList eimIdList optional
	} listEimResult,
	charstring rollbackResult,
	charstring processingTerminated,
	charstring notificationResult,
	charstring cancelSessionResult,
	record {
		charstring finalResult,
		charstring iccid optional
	} profileInstallationResult,
	charstring euiccUpdateResult,
	record {
		charstring edrResult,
		record {
			charstring defaultSmdpAddress optional,
			octetstring euiccInfo1 optional,
			octetstring euiccInfo2 optional,
			charstring rootSmdsAddress optional,
			integer associationToken optional,
			octetstring eumCertificate optional,
			octetstring euiccCertificate optional,
			octetstring ipaCapabilities optional,
			octetstring deviceInfo optional,
			octetstring notificationsList optional
		} euiccData optional
	} euiccDataResult
}

type record JSON_REST_Response_long {
	charstring status,
	charstring timestamp,
	JSON_REST_Resource resource,
	record of JSON_REST_Result outcome,
	charstring debuginfo
};

type record JSON_REST_Response_short {
	charstring status
};

/* Definition for JSON REST responses */
type union JSON_REST_Response {
	JSON_REST_Response_long long,
	JSON_REST_Response_short short
} with {
	variant "JSON : as value"
}

external function enc_JSON_REST_Response(in JSON_REST_Response msg)
return octetstring with {
	extension "prototype (convert) encode(JSON)";
	extension "errorbehavior(ALL:ERROR)"
}
external function dec_JSON_REST_Response(in octetstring stream) return JSON_REST_Response with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}

external function enc_JSON_REST_Resource(in JSON_REST_Resource msg)
return octetstring with {
	extension "prototype (convert) encode(JSON)";
	extension "errorbehavior(ALL:ERROR)"
}
external function dec_JSON_REST_Resource(in octetstring stream) return JSON_REST_Resource with {
	extension "prototype (convert) decode(JSON)"
	extension "errorbehavior(ALL:ERROR)"
}


template (present) JSON_REST_Response tr_JSON_REST_success := {
	long := {
		status := "done",
		timestamp := ?,
		resource := ?,
		outcome := ?,
		debuginfo := ?
	}
}

template (present) JSON_REST_Response tr_JSON_REST_deleted := {
	short := { status := "deleted" }
}

template (present) JSON_REST_Response tr_JSON_REST_absent := {
	short := { status := "absent" }
}

template (value) JSON_REST_Resource ts_JSON_REST_download_order(octetstring eidValue, charstring activationCode) := {
	eidValue := oct2str(eidValue),
	order := { download := { activationCode := activationCode } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_enable(octetstring eidValue, octetstring iccid, boolean rollback) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { enable := { iccid := iccid, rollback := rollback } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_disable(octetstring eidValue, octetstring iccid) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { disable := { iccid := iccid } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_delete(octetstring eidValue, octetstring iccid) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { delete := { iccid := iccid } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_disable_and_delete(octetstring eidValue, octetstring iccid) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { disable := { iccid := iccid } }, { delete := { iccid := iccid } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_listProfileInfo(octetstring eidValue, octetstring iccid) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { listProfileInfo := {searchCriteria := {iccid := iccid}, tagList := '92'O /* profileName */} } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_getRAT(octetstring eidValue) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { getRAT := {} } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_psmo_order_configureAutoEnable(octetstring eidValue) := {
	eidValue := oct2str(eidValue),
	order := { psmo := { { configureAutoEnable := { autoEnableFlag := true, smdpOid := objid {1 2 3}, smdpAddress := "smdp.example.com" } } } }
}

const octetstring eimConfigurationData :=
'3079800465494D32810E3132372E302E302E313A383030308301018401FF
A55BA059301306072A8648CE3D020106082A8648CE3D03010703420004FE
584A6F450459574AECA195D0299737F74C89BA2D36DF9286EC25D973037A
0FBA70D14DF3E1F7D0A305E57B95B731C4DE218D2D7F9F22113ED5D18C2E
3DDF1C'O

template (value) JSON_REST_Resource ts_JSON_REST_eco_order_addEim(octetstring eidValue) := {
	eidValue := oct2str(eidValue),
	order := { eco := { { addEim := { eimConfigurationData := eimConfigurationData } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_eco_order_deleteEim(octetstring eidValue) := {
	eidValue := oct2str(eidValue),
	order := { eco := { { deleteEim := { eimId := "eIM" } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_eco_order_updateEim(octetstring eidValue) := {
	eidValue := oct2str(eidValue),
	order := { eco := { { updateEim := { eimConfigurationData := eimConfigurationData } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_eco_order_listEim(octetstring eidValue) := {
	eidValue := oct2str(eidValue),
	order := { eco := { { listEim := { } } } }
}

template (value) JSON_REST_Resource ts_JSON_REST_edr_order(octetstring eidValue, octetstring tagList) := {
	eidValue := oct2str(eidValue),
	order := { edr := { tagList := tagList } }
}

} with {
	encode "JSON";
}
