{%- set ors_defaults = {
  "bandwidth": "20 MHz",
  "n_antenna_dl": 2,
  "n_antenna_ul": 1,
  "rf_mode": "tdd",
  "plmn": "00101",
  "disable_sdr": false,
  "power_on": true
} %}

{%- for k,v in ors_defaults|dictsort %}
{%-   do slapparameter_dict.setdefault(k, v) %}
{%- endfor %}


{#- make real ru/cell/peer/... shared instances to be rejected in ORS mode #}
{%- set ishared_list = slap_configuration.setdefault('slave-instance-list', [])  %}
{%- for ishared in ishared_list %}
{%-   set _ = json_module.loads(ishared['_']) %}
{%-   if 'ru_type' in _  or  'cell_type' in _ %}
{%-     do ishared.update({'_': {'REJECT': 1}|tojson})  %}
{%-   endif %}
{%-   if 'imsi' in _ %}
{%-     do _.update({'ue_type': 'lte'}) %}
{%-   endif %}
{%- endfor  %}


{#- inject ru+cell synthesized from ORS-specific parameters #}
{%- macro iref(name)  %}
{{-   '%s.%s' % (slap_configuration['instance-title'], name)  -}}
{%- endmacro  %}
{%- do ishared_list.append({
      'slave_title':      iref('RU'),
      'slave_reference':  False,
      '_': {
        'ru_type':      'sdr',
        'ru_link_type': 'sdr',
        'sdr_dev_list': [0],
        'n_antenna_dl': slapparameter_dict.n_antenna_dl,
        'n_antenna_ul': slapparameter_dict.n_antenna_ul,
        'tx_gain':      ors_version['current-tx-gain'],
        'rx_gain':      ors_version['current-rx-gain'],
        'txrx_active':  'ACTIVE'  if (not slapparameter_dict.disable_sdr)  else  'INACTIVE',
      } |tojson
    })
%}

{%-   set cell = {
      'cell_type':  'lte',
      'dl_earfcn':  ors_version['current-earfcn'],
      'bandwidth':  float(slapparameter_dict.bandwidth.removesuffix(' MHz')),
    }
%}

{%- do cell.update({
      'cell_kind':  'ue',
      'rf_mode':    slapparameter_dict.rf_mode,
      'ru': { 'ru_type':  'ru_ref',
              'ru_ref':   iref('RU') }
    })
%}

{%- do ishared_list.append({
      'slave_title':      iref('CELL'),
      'slave_reference':  False,
      '_': cell | tojson
    })
%}

# code of generic ue
{%  include 'instance-ue-base.jinja2.cfg' %}

# let all templates know we are running in ORS mode
[config-base]
context -=
   json ors false
context +=
   key ors :ors
   raw ue_ifup {{ ue_ifup }}
ors = {{ dumps(ors_version) }}

# add ORS-specific bits to published information
[publish-connection-information]
ors-version = {{ ors_version['ors-version'] }}
frequency-range-rating = {{ ors_version['range'] }}
current-tx-power-estimate = {{ ors_version['power-estimate'] }}
current-tx-gain = {{ ors_version['current-tx-gain'] }}
current-rx-gain = {{ ors_version['current-rx-gain'] }}

current-earfcn  = {{ ors_version['current-earfcn'] }}
