/* PFCP Templates in TTCN-3
 * (C) 2022 sysmocom - s.f.m.c. GmbH <info@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 PFCP_Templates {

import from General_Types all;
import from Native_Functions all;
import from Osmocom_Types all;
import from PFCP_Types all;

type enumerated e_PFCP_Cause {
	RESERVED (0),
	REQUEST_ACCEPTED (1),
	MORE_USAGE_REPORT_TO_SEND (2),
	REQUEST_REJECTED (64),
	SESSION_CTX_NOT_FOUND (65),
	MANDATORY_IE_MISSING (66),
	CONDITIONAL_IE_MISSING (67),
	INVALID_LENGTH (68),
	MANDATORY_IE_INCORRECT (69),
	INVALID_FORW_POLICY (70),
	INVALID_F_TEID_ALLOC_OPTION (71),
	NO_ESTABLISHED_PFCP_ASSOC (72),
	RULE_CREATION_MOD_FAILURE (73),
	PFCP_ENTITY_IN_CONGESTION (74),
	NO_RESOURCES_AVAILABLE (75),
	SERVICE_NOT_SUPPORTED (76),
	SYSTEM_FAILURE (77),
	REDIRECTION_REQUESTED (78),
	ALL_DYNAMIC_ADDRESSES_ARE_OCCUPIED (79)
};

template (value) Cause ts_PFCP_Cause(e_PFCP_Cause cause) := {
	elementIdentifier := 19,
	lengthIndicator := 0,
	causeValue := int2oct(enum2int(cause), 1)
};

template (present) Cause tr_PFCP_Cause(e_PFCP_Cause cause) := {
	elementIdentifier := 19,
	lengthIndicator := ?,
	causeValue := int2oct(enum2int(cause), 1)
};

const INT4b PFCP_Node_ID_IPv4 := 0;
const INT4b PFCP_Node_ID_IPv6 := 1;
const INT4b PFCP_Node_ID_FQDN := 2;

template (value) Node_ID ts_PFCP_Node_ID(INT4b id_type, octetstring id_value) := {
	elementIdentifier := 60,
	lengthIndicator := 0,
	node_id_type := id_type,
	spare := '0000'B,
	node_id_value := id_value
};

template (present) Node_ID tr_PFCP_Node_ID(template (present) INT4b id_type := ?,
					   template (present) octetstring id_value := ?) := {
	elementIdentifier := 60,
	lengthIndicator := ?,
	node_id_type := id_type,
	spare := ?,
	node_id_value := id_value
};

/* t_PFCP_Node_ID_IPv4(f_inet_addr("127.0.0.1")) */
template (value) Node_ID ts_PFCP_Node_ID_ipv4(OCT4 ip_value) := ts_PFCP_Node_ID(PFCP_Node_ID_IPv4, ip_value);
template (value) Node_ID ts_PFCP_Node_ID_fqdn(charstring fqdn) := ts_PFCP_Node_ID(PFCP_Node_ID_FQDN, char2oct(fqdn));

template (value) Recovery_Time_Stamp ts_PFCP_Recovery_Timestamp(LIN4_BO_LAST time_value) := {
	elementIdentifier := 96,
	lengthIndicator := 0,
	time_value := time_value
};

template (present) Recovery_Time_Stamp tr_PFCP_Recovery_Timestamp(template (present) LIN4_BO_LAST time_value := ?) := {
	elementIdentifier := 96,
	lengthIndicator := ?,
	time_value := time_value
};

template (value) PDU_PFCP ts_PDU_PFCP_ := {
	s_flag := '0'B,
	mp := '0'B,
	spare := '000'B,
	version := 1,
	message_type := 0,
	lengthIndicator := 0,
	seid := omit,
	sequence_number := 0,
	spare2 := '0000'B,
	mp_or_spare := '0000'B,
	message_body := -
};

template (present) PDU_PFCP tr_PDU_PFCP_ := {
	s_flag := ?,
	mp := ?,
	spare := ?,
	version := 1,
	message_type := ?,
	lengthIndicator := ?,
	seid := *,
	sequence_number := ?,
	spare2 := ?,
	mp_or_spare := ?,
	message_body := ?
};

template (value) PDU_PFCP ts_PDU_PFCP(LIN3_BO_LAST sequence_number := 0, template (omit) OCT8 seid := omit)
modifies ts_PDU_PFCP_ := {
	seid := seid,
	sequence_number := sequence_number
};

template (present) PDU_PFCP tr_PDU_PFCP(template OCT8 seid := *)
modifies tr_PDU_PFCP_ := {
	seid := seid
};

template (value) PDU_PFCP ts_PFCP_Assoc_Setup_Req(template (value) Node_ID node_id, LIN4_BO_LAST recovery_timestamp)
modifies ts_PDU_PFCP_ := {
	message_body := {
		pfcp_association_setup_request := {
			node_id := node_id,
			time_stamp := ts_PFCP_Recovery_Timestamp(recovery_timestamp),
			up_function_features := omit,
			cp_function_features := omit,
			UP_IP_resource_list := omit
		}
	}
};

function tr_PFCP_Assoc_Setup_Req(template (present) Node_ID node_id := ?) return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP();
	t.message_body := {
		pfcp_association_setup_request := {
			node_id := node_id,
			time_stamp := ?,
			up_function_features := *,
			cp_function_features := *,
			UP_IP_resource_list := *
		}
	};
	return t;
};

template (value) PDU_PFCP ts_PFCP_Assoc_Setup_Resp(LIN3_BO_LAST sequence_number,
						   template (value) Node_ID node_id,
						   template (value) Cause cause,
						   LIN4_BO_LAST recovery_timestamp)
modifies ts_PDU_PFCP_ := {
	sequence_number := sequence_number,
	message_body := {
		pfcp_association_setup_response := {
			node_id := node_id,
			cause := cause,
			time_stamp := ts_PFCP_Recovery_Timestamp(recovery_timestamp),
			up_function_features := omit,
			cp_function_features := omit,
			UP_IP_resource_list := omit
		}
	}
};

function tr_PFCP_Assoc_Setup_Resp(template (present) Node_ID node_id := ?,
				  template (present) Cause cause := ?) return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP();
	t.message_body := {
		pfcp_association_setup_response := {
			node_id := node_id,
			cause := cause,
			time_stamp := ?,
			up_function_features := *,
			cp_function_features := *,
			UP_IP_resource_list := *
		}
	};
	return t;
};

function ts_PFCP_Assoc_Release_Req(template (value) Node_ID node_id) return template (value) PDU_PFCP {
	var template (value) PDU_PFCP t := ts_PDU_PFCP();

	t.message_body := {
		pfcp_association_release_request := {
			node_id := node_id
		}
	};
	return t;
};

function tr_PFCP_Assoc_Release_Resp(template (present) Node_ID node_id := ?, template (present) Cause cause := ?)
 return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP();
	t.message_body := {
		pfcp_association_release_response := {
			node_id := node_id,
			cause := cause
		}
	};
	return t;
};

template (value) F_SEID ts_PFCP_F_SEID_ipv4(charstring addr_v4, OCT8 seid) := {
        elementIdentifier := 57,
        lengthIndicator := 0,
        v6 := '0'B,
        v4 := '1'B,
        spare := '000000'B,
        seid := seid,
        ipv4_address := f_inet_addr(addr_v4),
        ipv6_address := omit
}

type enumerated e_PFCP_Src_Iface {
	ACCESS (0),
	CORE (1),
	SGI_LAN_N6_LAN (2),
	CP_FUNCTION (3),
	x_5G_VN_INTERNAL (4)
};

template (value) Source_Interface ts_PFCP_Src_Iface(e_PFCP_Src_Iface iface) := {
        elementIdentifier := 20,
        lengthIndicator := 0,
        interfacevalue := enum2int(iface),
        spare := '0000'B
}

template (value) UE_IP_Address ts_PFCP_UE_IP_Address_v4(charstring addr_v4, boolean is_destination := true) := {
        elementIdentifier := 93,
        lengthIndicator := 0,
        v6 := '0'B,
        v4 := '1'B,
        sd := bool2bit(is_destination),
        spare := '00000'B,
        ipv4_address := f_inet_addr(addr_v4),
        ipv6_address := omit
}

template (value) F_TEID ts_PFCP_F_TEID_ipv4(OCT4 teid, charstring addr_v4) := {
        elementIdentifier := 21,
        lengthIndicator := 0,
        v4 := '1'B,
        v6 := '0'B,
        ch := '0'B,
        chid := '0'B,
        spare := '0000'B,
        teid := teid,
        ipv4_address := f_inet_addr(addr_v4),
        ipv6_address := omit,
        choose_id := omit
}

template (value) F_TEID ts_PFCP_F_TEID_choose_v4(template (omit) OCT1 choose_id := omit) := {
        elementIdentifier := 21,
        lengthIndicator := 0,
        v4 := '1'B,
        v6 := '0'B,
        ch := '1'B,
        chid := '0'B,
        spare := '0000'B,
        teid := omit,
        ipv4_address := omit,
        ipv6_address := omit,
        choose_id := choose_id
}

template (value) PDI_IE ts_PFCP_PDI(e_PFCP_Src_Iface src_iface,
				    template (omit) F_TEID local_F_TEID := omit,
				    template (omit) UE_IP_Address ue_addr_v4 := omit) := {
        elementIdentifier := 2,
        lengthIndicator := 0,
	grouped_ie := {
		source_interface := ts_PFCP_Src_Iface(src_iface),
		local_F_TEID := local_F_TEID,
		pdn_instance := omit,
		ue_ip_address := ue_addr_v4,
		traffic_endpoint_id := omit,
		sdf_filter_list := omit,
		application_id := omit,
		ethernet_packet_filter_list := omit,
		qfi_list := omit
	}
}

template (value) Apply_Action ts_PFCP_Apply_Action(BIT1 forw := '0'B, BIT1 drop := '0'B, BIT1 buff := '0'B) := {
        elementIdentifier := 44,
        lengthIndicator := 0,
        drop := drop,
        forw := forw,
        buff := buff,
        nocp := '0'B,
        dupl := '0'B,
        spare := '000'B
}

function ts_PFCP_Apply_Action_FORW() return template (value) Apply_Action {
	return ts_PFCP_Apply_Action(forw := '1'B);
}
template (value) Apply_Action ts_PFCP_Apply_Action_DROP := ts_PFCP_Apply_Action(drop := '1'B);
template (value) Apply_Action ts_PFCP_Apply_Action_BUFF := ts_PFCP_Apply_Action(buff := '1'B);

type enumerated e_PFCP_Dest_Iface {
	ACCESS (0),
	CORE (1),
	SGI_LAN_N6_LAN (2),
	CP_FUNCTION (3),
	LI_FUNCTION (4),
	x_5G_VN_INTERNAL (5)
};

template (value) Destination_Interface ts_PFCP_Destination_Interface(e_PFCP_Dest_Iface di) := {
        elementIdentifier := 42,
        lengthIndicator := 0,
        interface_value := enum2int(di),
        spare := '0000'B
}

template (value) Outer_Header_Creation ts_PFCP_Outer_Header_Creation_GTP_ipv4(OCT4 remote_teid, charstring remote_addr_v4) := {
        elementIdentifier := 84,
        lengthIndicator := 0,
        ohc_description_oct5 := '00000001'B,
        ohc_description_oct6 := '00000000'B,
        teid := remote_teid,
        ipv4 := f_inet_addr(remote_addr_v4),
        ipv6 := omit,
        udp_port := omit
}

type enumerated e_PFCP_Outer_Header_Removal {
	GTP_U_UDP_IPV4 (0),
	GTP_U_UDP_IPV6 (1),
	UDP_IPV4 (2),
	UDP_IPV6 (3),
	IPV4 (4),
	IPV6 (5),
	GTP_U_UDP_IP (6),
	VLAN_S_TAG (7),
	S_TAG_AND_C_TAG (8)
};

template (value) Outer_Header_Removal ts_PFCP_Outer_Header_Removal(e_PFCP_Outer_Header_Removal ohr) := {
        elementIdentifier := 95,
        lengthIndicator := 0,
        ohc_description := enum2int(ohr)
}

template (value) Forwarding_Parameters ts_PFCP_Forwarding_Parameters(
		e_PFCP_Dest_Iface dest_iface,
		template (omit) Outer_Header_Creation outer_header_creation := omit
		) := {
        elementIdentifier := 4,
        lengthIndicator := 0,
        grouped_ie := {
		destination_interface := ts_PFCP_Destination_Interface(dest_iface),
		pdn_Instance := omit,
		redirect_information := omit,
		outer_header_creation := outer_header_creation,
		transport_level_marking := omit,
		forwarding_policy := omit,
		header_enrichment := omit,
		traffic_endpoint_ID := omit
	}
}

template (value) FAR_ID ts_PFCP_FAR_ID(LIN4_BO_LAST far_id) := {
        elementIdentifier := 108,
        lengthIndicator := 0,
        id_value := far_id
}

template (value) Create_FAR ts_PFCP_Create_FAR(LIN4_BO_LAST far_id, template (value) Apply_Action aa, Forwarding_Parameters fp) := {
        elementIdentifier := 3,
        lengthIndicator := 0,
	grouped_ie := {
		far_id := ts_PFCP_FAR_ID(far_id),
		apply_action := aa,
		forwarding_parameters := fp,
		duplicating_parameters := omit,
		bar_id := omit
        }
}

template (value) PDR_ID ts_PFCP_PDR_ID(OCT2 pdr_id) := {
        elementIdentifier := 56,
        lengthIndicator := 0,
        rule_id := pdr_id
}

template (value) Precedence ts_PFCP_Precedence(LIN4_BO_LAST val) := {
        elementIdentifier := 29,
        lengthIndicator := 0,
        precedence_value := val
}

template (value) Create_PDR ts_PFCP_Create_PDR(integer pdr_id, template (value) PDI_IE pdi,
					       template (omit) Outer_Header_Removal outer_header_removal := omit,
					       LIN4_BO_LAST far_id) := {
        elementIdentifier := 1,
        lengthIndicator := 0,
	grouped_ie := {
		pdr_id := ts_PFCP_PDR_ID(int2oct(pdr_id, 2)),
		precedence := ts_PFCP_Precedence(0),
		pdi := pdi,
		outer_header_removal := outer_header_removal,
		FAR_ID_list := { ts_PFCP_FAR_ID(far_id) },
		uRR_ID_list := omit,
		qER_ID_list := omit,
		activate_predefined_rules := omit
	}
}

function ts_PFCP_Session_Est_Req(charstring node_id, OCT8 cp_seid, Create_PDR_list create_pdr, Create_FAR_list create_far)
 return template (value) PDU_PFCP {
	var template (value) PDU_PFCP t := ts_PDU_PFCP();
	t.message_body := {
		pfcp_session_establishment_request := {
			node_id := ts_PFCP_Node_ID_ipv4(f_inet_addr(node_id)),
			CP_F_SEID := ts_PFCP_F_SEID_ipv4(node_id, cp_seid),
			create_PDR_list := create_pdr,
			create_FAR_list := create_far,
			create_URR_list := omit,
			create_QER_list := omit,
			create_BAR := omit,
			create_traffic_endpoint_list := omit,
			pdn_type := omit,
			node_list := omit,
			up_inactivity_timer := omit
		}
	};
	return t;
}

function tr_PFCP_Session_Est_Req() return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP(?);
	t.message_body := {
		pfcp_session_establishment_request := {
			node_id := ?
		}
	};
	return t;
}

template (value) Created_PDR ts_PFCP_Created_PDR(PDR_ID pdr_id, template (value) F_TEID local_F_TEID) := {
	elementIdentifier := 8,
	lengthIndicator := 0,
	grouped_ie := {
		pdr_id := pdr_id,
		local_F_TEID := local_F_TEID
	}
}

function ts_PFCP_Session_Est_Resp(LIN3_BO_LAST seq_nr, template (value) Node_ID node_id, OCT8 seid)
 return template (value) PDU_PFCP {
	var template (value) PDU_PFCP t := ts_PDU_PFCP(seq_nr, seid);
	t.sequence_number := seq_nr;
	t.message_body := {
		pfcp_session_establishment_response := {
			node_id := node_id,
			cause := ts_PFCP_Cause(REQUEST_ACCEPTED)
		}
	};
	return t;
}

function tr_PFCP_Session_Est_Resp(template (present) OCT8 hdr_seid := ?) return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP(hdr_seid);
	t.message_body := {
		pfcp_session_establishment_response := {
			node_id := ?,
			cause := tr_PFCP_Cause(REQUEST_ACCEPTED),
			offending_ie := *,
			UP_F_SEID := ?,
			created_PDR_list := ?,
			load_control_information := *,
			overload_control_information := *,
			node_list := *,
			failed_rule_id := *,
			created_traffic_endpoint_list := *
		}
	};
	return t;
}

function tr_PFCP_Session_Mod_Req(template (present) OCT8 seid := ?) return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP(seid);
	t.message_body := {
		pfcp_session_modification_request := ?
	};
	return t;
}

function ts_PFCP_Session_Mod_Resp(LIN3_BO_LAST seq_nr, OCT8 seid) return template (value) PDU_PFCP {
	var template (value) PDU_PFCP t := ts_PDU_PFCP(seq_nr, seid);
	t.message_body := {
		pfcp_session_modification_response := {
			cause := ts_PFCP_Cause(REQUEST_ACCEPTED),
			offending_IE := omit,
			created_PDR := omit,
			load_control_information := omit,
			overload_control_information := omit,
			usage_report := omit,
			failed_rule_id := omit,
			additional_usage_reports_information := omit,
			created_updated_traffic_endpoint := omit
		}
	};
	return t;
}

function ts_PFCP_Session_Del_Req(OCT8 seid) return template (value) PDU_PFCP {
	var template (value) PDU_PFCP t := ts_PDU_PFCP(seid := seid);
	t.message_body := {
		pfcp_session_deletion_request := { }
	};
	return t;
}

function tr_PFCP_Session_Del_Req(template (present) OCT8 seid := ?) return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP(seid);
	t.message_body := {
		pfcp_session_deletion_request := ?
	};
	return t;
}

function ts_PFCP_Session_Del_Resp(LIN3_BO_LAST seq_nr, OCT8 seid, e_PFCP_Cause cause := REQUEST_ACCEPTED)
 return template (value) PDU_PFCP {
	var template (value) PDU_PFCP t := ts_PDU_PFCP(seq_nr, seid);
	t.message_body := {
		pfcp_session_deletion_response := {
			cause := ts_PFCP_Cause(cause),
			offending_IE := omit,
			load_control_information := omit,
			overload_control_information := omit,
			usage_report := omit
		}
	};
	return t;
}

function tr_PFCP_Session_Del_Resp(template (present) OCT8 seid := ?,
				  template (present) Cause cause := tr_PFCP_Cause(REQUEST_ACCEPTED))
 return template (present) PDU_PFCP {
	var template PDU_PFCP t := tr_PDU_PFCP(seid);
	t.message_body := {
		pfcp_session_deletion_response := {
			cause := cause,
			offending_IE := *,
			load_control_information := *,
			overload_control_information := *,
			usage_report := *
		}
	};
	return t;
}

}
