{%- set tun_name         = slap_configuration.get('tun-name', 'slaptun0')              %}
{%- set tun_ipv4_addr    = slap_configuration.get('tun-ipv4-addr', '172.17.0.1')       %}
{%- set tun_ipv6_addr    = slap_configuration.get('tun-ipv6-addr', '2001:db8::1')      %}
{%- set tun_ipv4_network = slap_configuration.get('tun-ipv4-network', '172.17.0.0/17') %}
{%- set tun_ipv6_network = slap_configuration.get('tun-ipv6-network', '2001:db8::/55') %}
{%- set tun_ipv4_start   = int(netaddr.IPAddress(tun_ipv4_addr))                       %}
{%- set tun_ipv6_start   = int(netaddr.IPAddress(tun_ipv6_addr))                       %}
{%- set tun_ipv4_end     = netaddr.IPNetwork(tun_ipv4_network).last                    %}
{%- set tun_ipv6_end     = netaddr.IPNetwork(tun_ipv6_network).last                    %}

{%- set internet_ipv4_start = netaddr.IPAddress( tun_ipv4_start + 1                      ) %}
{%- set internet_ipv4_end   = netaddr.IPAddress((tun_ipv4_start + tun_ipv4_end) // 2 - 2 ) %}
{%- set internet_ipv6_start = netaddr.IPAddress( tun_ipv6_start + 1                      ) %}
{%- set internet_ipv6_end   = netaddr.IPAddress((tun_ipv6_start + tun_ipv6_end) // 2 - 1 ) %}
{%- set ims_ipv4_start      = netaddr.IPAddress((tun_ipv4_start + tun_ipv4_end) // 2 + 2 ) %}
{%- set ims_ipv4_end        = netaddr.IPAddress( tun_ipv4_end   - 1                      ) %}
{%- set ims_ipv4            = netaddr.IPAddress((tun_ipv4_start + tun_ipv4_end) // 2 + 1 ) %}
{%- set ims_ipv6            = netaddr.IPAddress((tun_ipv6_start + tun_ipv6_end) // 2     ) %}
{%- set ims_ipv6_start      = netaddr.IPAddress((tun_ipv6_start + tun_ipv6_end) // 2     ) %}
{%- set ims_ipv6_end        = netaddr.IPAddress( tun_ipv6_end   - 1                      ) %}

{
  log_options: "all.level=error,all.max_size=0,nas.level=debug,nas.max_size=1,s1ap.level=debug,s1ap.max_size=1,ngap.level=debug,ngap.max_size=1,file.rotate=1G,file.path=/dev/null",
  log_filename: "{{ directory['log'] }}/mme.log",

{%- if slapparameter_dict.get('external_enb_gnb', '') %}
{%-   if slapparameter_dict.get('use_ipv4', False) %}
  gtp_addr: "{{ gtp_addr_v4 }}",
{%-   else %}
  gtp_addr: "{{ gtp_addr_v6 }}",
{%-   endif %}
{%- else %}
  gtp_addr: "{{ slapparameter_dict.gtp_addr }}",
{%- endif %}

  plmn: "{{ slapparameter_dict.get('core_network_plmn', "00101") }}",
  mme_group_id: 32769,
  mme_code: 1,

  ims_vops_eps: true,
  ims_vops_5gs_3gpp: true,
  ims_vops_5gs_n3gpp: true,
  emergency_number_list: [

    { category: 0x1f, digits: "911" },
    { category: 0x1f, digits: "112" },
  ],

  rx: {
    bind_addr: "127.0.1.100",
    qci: {audio: 1, video: 2},
  },


  network_name: "{{ slapparameter_dict.get('network_name', 'RAPIDSPACE') }}",
  network_short_name: "{{ slapparameter_dict.get('network_short_name', 'RAPIDSPACE') }}",

  cp_ciot_opt: true,

  nr_support: true,

{%- set eps_5gs_interworking =
            {'With N26':    "with_n26",
             'Without N26': "without_n26",
             'None':        "none"}
            [slapparameter_dict.get('eps_5gs_interworking', 'With N26')] %}
  eps_5gs_interworking: "{{ eps_5gs_interworking }}",

  fifteen_bearers: false,

  ims_list: [
    {
      ims_addr: "{{ slapparameter_dict.ims_addr }}",
      bind_addr: "{{  slapparameter_dict.ims_bind }}",
    }
  ],

  pdn_list: [
    {
      access_point_name: [
{%- for _, pdn in slapparameter_dict.pdn_list |dictsort -%}
"{{ pdn.name }}",
{%- endfor -%}
],
      pdn_type: "ipv4v6",
      tun_ifname:        "{{ tun_name }}",
      first_ip_addr:     "{{ internet_ipv4_start }}",
      last_ip_addr:      "{{ internet_ipv4_end   }}",
      first_ipv6_prefix: "{{ internet_ipv6_start }}",
      last_ipv6_prefix:  "{{ internet_ipv6_end   }}",
{%- if slapparameter_dict.get('local_domain', '') %}
      dns_addr: ["{{ tun_ipv4_addr }}"],
{%- else %}
      dns_addr: ["8.8.8.8", "2001:4860:4860::8888"],
{%- endif %}
      erabs: [
        {
          qci: {{ slapparameter_dict.qci }},
          priority_level: 15,
          pre_emption_capability: "shall_not_trigger_pre_emption",
          pre_emption_vulnerability: "not_pre_emptable",
        },
      ],
    },
    {
      access_point_name: "ims",
      pdn_type: "ipv4v6",
      tun_ifname:        "{{ tun_name }}-1",
      first_ip_addr:     "{{ ims_ipv4_start }}",
      last_ip_addr:      "{{ ims_ipv4_end   }}",
      first_ipv6_prefix: "{{ ims_ipv6_start }}",
      last_ipv6_prefix:  "{{ ims_ipv6_end   }}",
      p_cscf_addr: ["{{ ims_ipv4 }}", "{{ ims_ipv6 }}"],
{%- if slapparameter_dict.get('local_domain', '') %}
      dns_addr: ["{{ tun_ipv4_addr }}"],
{%- else %}
      dns_addr: ["8.8.8.8", "2001:4860:4860::8888"],
{%- endif %}

      erabs: [
        {
          qci: 9,
          priority_level: 15,
          pre_emption_capability: "shall_not_trigger_pre_emption",
          pre_emption_vulnerability: "not_pre_emptable",
        },
      ],
    },
  ],

  tun_setup_script: "{{ ifup_empty }}",
  ue_to_ue_forwarding: false,
  nas_cipher_algo_pref: [ ],
  nas_integ_algo_pref: [ 2, 1 ],
  ue_db_filename: "{{ directory['var'] }}/lte_ue.db",
{%- if slapparameter_dict.get('hss_addr', '') %}
  ue_db: [],
  s6: {
    server_addr: "{{ slapparameter_dict.get('hss_addr', '') }}",
{%-   if slapparameter_dict.get('hss_bind_addr', '') %}
    bind_addr: "{{ slapparameter_dict.get('hss_bind_addr', '') }}",
{%-   endif %}
{%-   if slapparameter_dict.get('s6_origin_realm', '') %}
    origin_realm: "{{ slapparameter_dict.get('s6_origin_realm', '') }}",
{%-   endif %}
{%-   if slapparameter_dict.get('s6_origin_host', '') %}
    origin_host: "{{ slapparameter_dict.get('s6_origin_host', '') }}",
{%-   endif %}

  }
{%- else %}
  include "{{ slap_configuration['ue_db_path'] }}",
{%- endif %}
}
