{%- import 'slaplte.jinja2' as slaplte  with context %}
{%- set B               = slaplte.B %}
{%- set J               = slaplte.J %}
{%- set jcell_ru_ref    = slaplte.jcell_ru_ref %}

{#- for standalone testing via slapos-render-config.py
    NOTE: keep in sync with instance-ue.jinja2.cfg and ru/libinstance.jinja2.cfg   #}
{%- if _standalone is defined                                                 %}
{%-   set iru_dict   = {}                                                     %}
{%-   set icell_dict = {}                                                     %}
{%-   set iue_dict   = {}                                                     %}
{%-   do slaplte.load_iru_and_icell(iru_dict, icell_dict, icell_kind='ue')    %}
{%-   do slaplte.load_iue(iue_dict)                                           %}
{%-   do slaplte.check_loaded_everything()                                    %}
{%- endif                                                                     %}


{#- start of the config -#}
{
  log_options: "all.level=error,all.max_size=0,nas.level=debug,nas.max_size=1,rrc.level=debug,rrc.max_size=1,phy.level=info,file.rotate=1G,file.path=/dev/null",
  log_filename: "{{ directory['log'] }}/ue.log",

{%- if not ors %}
  rue_bind_addr: "{{ pub_info['rue_bind_addr'] }}",
  com_addr: "{{ slapparameter_dict.com_addr }}:{{ slapparameter_dict.com_ws_port }}",
  com_auth: {
    password: "{{ websocket_password }}",
    unsecure: false,
  },
{%- endif %}

{# instantiate radio units #}
{%- if ors %}
{{ slaplte.ru_config(iru_dict, slapparameter_dict, False) }}
{%- else %}
{{ slaplte.ru_config(iru_dict, slapparameter_dict, True) }}
{%- endif %}

  cell_groups: [{
    // LTE cells
    group_type: "lte",
    multi_ue: true,

    cells: [
{%- for cell_ref, icell in icell_dict|dictsort  %}
{%-   set cell = icell['_']                     %}
{%-   if cell.cell_type == 'lte'                %}
{%-     set ru_ref = J(jcell_ru_ref(icell))     %}
{%-     set iru = iru_dict[ru_ref]              %}
{%-     set ru = iru['_']                       %}

        // {{ B(cell_ref) }}
        {
          rf_port:      {{ ru._rf_port }},
          n_antenna_dl: {{ ru.n_antenna_dl }},
          n_antenna_ul: {{ ru.n_antenna_ul }},

          dl_earfcn: {{ cell.dl_earfcn }},
          ul_earfcn: {{ cell.ul_earfcn }},
          bandwidth: {{ cell.bandwidth }},

          global_timing_advance: -1,
        },
{%-   endif %}
{%- endfor %}
    ],
    pdcch_decode_opt: false,
    pdcch_decode_opt_threshold: 0.1,

  }, {
    // NR cells
    group_type: "nr",
    multi_ue: true,

    cells: [
{%- for cell_ref, icell in icell_dict|dictsort  %}
{%-   set cell = icell['_']                     %}
{%-   if cell.cell_type == 'nr'                 %}
{%-     set ru_ref = J(jcell_ru_ref(icell))     %}
{%-     set iru = iru_dict[ru_ref]              %}
{%-     set ru = iru['_']                       %}
        // {{ B(cell_ref) }}
        {
          rf_port:      {{ ru._rf_port }},
          n_antenna_dl: {{ ru.n_antenna_dl }},
          n_antenna_ul: {{ ru.n_antenna_ul }},

          band:         {{ cell.nr_band }},
          dl_nr_arfcn:  {{ cell.dl_nr_arfcn }},
          ul_nr_arfcn:  {{ cell.ul_nr_arfcn }},
          ssb_nr_arfcn: {{ cell.ssb_nr_arfcn }},
          bandwidth:    {{ cell.bandwidth }},

          subcarrier_spacing: {{ cell.subcarrier_spacing }},
        },
{%-   endif %}
{%- endfor %}
    ]
  }],

  ue_list: [
{%- for ue_ref, iue in iue_dict|dictsort  %}
{%-   set ue = iue['_']                   %}
    // {{ B(ue_ref) }}
    {
      sim_algo: "{{ ue.sim_algo }}",
      opc: "{{ ue.opc }}",
      amf: {{ ue.amf }},
      sqn: "{{ ue.sqn }}",
      impu: "{{ ue.impu }}",
      impi: "{{ ue.impi }}",
      imsi: "{{ ue.imsi }}",
      K: "{{ ue.k }}",
{%-   if ue.get('rue_addr', False) %}
      rue_addr: "{{ ue.rue_addr }}",
{%-   endif %}
{%-   if ue.ue_type == 'lte'            %}
      as_release: 13,
      ue_category: 13,
{%-   elif ue.ue_type == 'nr'           %}
      as_release: 15,
      ue_category: "nr",
{%-   else                              %}
{%-     do bug('unreachable')   %}
{%-   endif                             %}
{%-   if ors %}
      tun_setup_script: "{{ ue_ifup }}",
{%-   else %}
      tun_setup_script: "ue_ifup",
{%-   endif %}
      apn: "internet",
    },
{%- endfor %}
  ],
}
