{%- import 'slaplte.jinja2' as slaplte  with context %}
{%- set J               = slaplte.J %}
{%- set ierror          = slaplte.ierror %}
{%- set bug             = slaplte.bug %}

{#- for standalone testing via slapos-render-config.py
    NOTE: keep in sync with ru/libinstance.jinja2.cfg   #}
{%- if _standalone is defined %}
{%-   set cell_list = {}  %}
{%-   do slaplte.load_cell(cell_list) %}
{%- endif %}


{#- do_lte/do_nr indicate whether we have LTE or NR cell #}
{%- do assert(do_lte or do_nr)  %}
{%- do assert(not (do_lte and do_nr))  %}


{#- ru and cell are namespace objects that hold Radio Unit and Cell related parameters #}
{%- set ru = namespace(ru_type=ru_type) %}
{%- set cell = namespace()  %}


{#- handover_config emits handover configuration #}
{%- macro handover_config()  %}
      ncell_list: [
        // Inter-ENB HO
{%-   for _, ncell in slapparameter_dict.get('ncell_list', {})|dictsort %}
        {
{%-   if do_lte %}
          rat:          "eutra",
          n_id_cell:    {{ ncell.get('pci', '') }},
          dl_earfcn:    {{ ncell.get('dl_earfcn', '') }},
          cell_id:      {{ ncell.get('cell_id', '') }},
          tac:          {{ ncell.get('tac', 1) }},
{%-   elif do_nr  %}
          rat:          "nr",
          dl_nr_arfcn:  {{ ncell.get('dl_nr_arfcn', '') }},
          ssb_nr_arfcn: {{ ncell.get('ssb_nr_arfcn', '') }},
          ul_nr_arfcn:  {{ ncell.get('dl_nr_arfcn', '') }},
          n_id_cell:    {{ ncell.get('pci', '') }},
          gnb_id_bits:  {{ ncell.get('gnb_id_bits', '') }},
          nr_cell_id:   {{ ncell.get('nr_cell_id', '') }},
          tac:          {{ ncell.get('tac', 1) }},
          band:         {{ ncell.get('nr_band', '') }},
          ssb_subcarrier_spacing: 30,
          ssb_period: 20,
          ssb_offset: 0,
          ssb_duration: 1,
{%-   else  %}
{%-     do bug('unreachable') %}
{%-   endif %}
        },
{%- endfor %}
      ],
{%- endmacro %}

{#- jlte_n_rb_dl returns n_rb_dl for an LTE bandwidth. #}
{%- macro jlte_n_rb_dl(bandwidth) %}
{%-   set _ = {1.4:  6,
               3:   15,
               5:   25,
               10:  50,
               15:  75,
               20: 100} %}
{{-   _[float(bandwidth.removesuffix(' MHz'))] | tojson }}
{%- endmacro %}


{%- if do_lte %}
{%- if slapparameter_dict.get('tdd_ul_dl_config', '[Configuration 2] 5ms 2UL 6DL (default)') == '[Configuration 2] 5ms 2UL 6DL (default)' %}
{%- set tdd_config = 2 %}
{%- elif slapparameter_dict.get('tdd_ul_dl_config', '[Configuration 2] 5ms 2UL 6DL (default)') == '[Configuration 6] 5ms 5UL 3DL (maximum uplink)' %}
{%- set tdd_config = 6 %}
{%- endif %}
{%- endif %}

{%- if do_nr %}
{%- if slapparameter_dict.get('tdd_ul_dl_config', '5ms 2UL 7DL 4/6 (default)') == '5ms 2UL 7DL 4/6 (default)' %}
{%- set tdd_config = 1 %}
{%- elif slapparameter_dict.get('tdd_ul_dl_config', '5ms 2UL 7DL 4/6 (default)') == '2.5ms 1UL 3DL 2/10' %}
{%- set tdd_config = 2 %}
{%- elif slapparameter_dict.get('tdd_ul_dl_config', '5ms 2UL 7DL 4/6 (default)') == '5ms 8UL 1DL 2/10 (maximum uplink)' %}
{%- set tdd_config = 3 %}
{%- endif %}
{%- endif %}

{%- if do_lte %}
{%-   set cell.bandwidth = slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) %}
{%- endif %}

{%- set ru.n_antenna_dl = slapparameter_dict.get('n_antenna_dl', int(slap_configuration['configuration.default_n_antenna_dl'])) %}
{% if ru.ru_type == "m2ru" %}
{%-  set ru.n_antenna_ul = slapparameter_dict.get('n_antenna_ul', 1) %}
{% else %}
{%-  set ru.n_antenna_ul = slapparameter_dict.get('n_antenna_ul', int(slap_configuration['configuration.default_n_antenna_ul'])) %}
{% endif %}

{
  log_options: "all.level=error,all.max_size=0,nas.level=debug,nas.max_size=1,s1ap.level=debug,s1ap.max_size=1,x2ap.level=debug,x2ap.max_size=1,rrc.level=debug,rrc.max_size=1,ngap.level=debug,ngap.max_size=1,xnap.level=debug,xnap.max_size=1,
{%- if slapparameter_dict.get('log_phy_debug', False) -%}
    phy.level=debug
{%- else -%}
    phy.level=info
{%- endif -%}
    ,file.rotate=1G,file.path=/dev/null",
  log_filename: "{{ directory['log'] }}/enb.log",

{% if ru.ru_type == "lopcomm" %}
  rf_driver: {
{%- if slapparameter_dict.get('disable_sdr', False) %}
      name: "dummy",
{%- else %}
      name: "sdr",
{%- endif %}
        args: "dev0=
        {%- for i, k in enumerate(cell_list) %}
        {%- set cpri_port = cell_list[k].cpri_port_number %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          /dev/sdr{{ slapparameter_dict.get('sdr_number', 0) }}@{{ cpri_port }}
        {%- endfor -%}
        ",
      cpri_mapping: "hw",
      {%- set cpri_mult = slapparameter_dict.get('cpri_mult', 16) %}
      cpri_mult: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cpri_mult }}
        {%- endfor -%}
        ",
      cpri_rx_delay: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cell_list[k].get('cpri_rx_delay', 0) }}
        {%- endfor -%}
        ",
      cpri_tx_delay: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cell_list[k].get('cpri_tx_delay', 0) }}
        {%- endfor -%}
        ",
      cpri_tx_dbm: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cell_list[k].get('cpri_tx_dbm', 0) }}
        {%- endfor -%}
        ",
      ifname: "{{ cell_list.values() | map(attribute='_tap') | join(',') }}",
  },
  tx_gain: 0,
  rx_gain: 0,
{% elif ru.ru_type == "m2ru" %}
  rf_driver: {
{%- if slapparameter_dict.get('disable_sdr', False) %}
      name: "dummy",
{%- else %}
      name: "sdr",
{%- endif %}
      args: "dev0=/dev/sdr0@0",
      cpri_mapping: "bf1",
      cpri_mult: 16,
      cpri_rx_delay: 11.0,
      cpri_tx_delay: 0,
      ifname: "cpri0",
      cpri_tx_dbm: 42.0,
  },
  tx_gain: 0,
  rx_gain: 0,
{% elif ors %}
  rf_driver: {
{%- if slapparameter_dict.get('disable_sdr', False) %}
      name: "dummy",
{%- else %}
      name: "sdr",
{%- endif %}
      args: "dev0=/dev/sdr0",
{% if slapparameter_dict.get('gps_sync', False) %}
      sync: "gps",
{% endif %}
      rx_antenna:"tx_rx",
      tdd_tx_mod: 1,
  },
  tx_gain: {{ tx_gain }},
  rx_gain: {{ rx_gain }},
{% endif %}
  {%- if slapparameter_dict.get('websocket_password', '') %}
  com_addr: "[{{ gtp_addr_v6 }}]:{{ slap_configuration['configuration.com_ws_port'] }}",
  com_auth: {
    password: "{{ slapparameter_dict['websocket_password'] }}",
  },
  {%- else %}
  com_addr: "{{ slap_configuration['configuration.com_addr'] }}:{{ slap_configuration['configuration.com_ws_port'] }}",
  {%- endif %}

{%- if do_lte %}
  // LTE core network
  mme_list: [
{% if slapparameter_dict.get('mme_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['mme_list']) %}
{%- if i == 0 %}
    {
{%- else -%}
    , {
{%- endif %}
         mme_addr: "{{ slapparameter_dict['mme_list'][k]['mme_addr'] }}",
    }
{%- endfor -%}
{% else %}
    {
      mme_addr: "{{ slap_configuration['configuration.mme_addr'] }}",
    },
{% endif %}
  ],
{%- endif %}

{%- if do_nr  %}
  // NR core network
  amf_list: [
{% if slapparameter_dict.get('amf_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['amf_list']) %}
{%- if i == 0 %}
    {
{%- else -%}
    , {
{%- endif %}
         amf_addr: "{{ slapparameter_dict['amf_list'][k]['amf_addr'] }}",
    }
{%- endfor -%}
{% else %}
    {
      amf_addr: "{{ slap_configuration['configuration.amf_addr'] }}",
    },
{% endif %}
  ],
{%- endif %}

{% if slapparameter_dict.get('mme_list', '') or slapparameter_dict.get('amf_list', '') %}
  {% if slapparameter_dict.get('gtp_addr') %}
    gtp_addr: "{{ slapparameter_dict.get('gtp_addr') }}",
  {% else %}
    {% if slapparameter_dict.get('use_ipv4', False) %}
      gtp_addr: "{{ gtp_addr_v6 }}",
    {% else %}
      gtp_addr: "{{ gtp_addr_v4 }}",
    {% endif %}
  {% endif %}
{% else %}
  gtp_addr: "{{ slap_configuration['configuration.gtp_addr'] }}",
{% endif %}

{%- if do_nr  %}
{% if slapparameter_dict.get('xn_peers', '') %}
  xn_peers: [
{%- for k in slapparameter_dict['xn_peers'] -%}
    "{{ slapparameter_dict['xn_peers'][k]['xn_addr'] }}",
{%- endfor -%}
  ],
{% endif %}
{%- endif %}

{%- if do_lte %}
  enb_id: {{ slapparameter_dict.get('enb_id', '0x1A2D0') }},
{%- endif %}
{%- if do_nr %}
  gnb_id_bits: {{ slapparameter_dict.get('gnb_id_bits', 28) }},
  gnb_id: {{ slapparameter_dict.get('gnb_id', '0x12345') }},
  en_dc_support: true,
{%- endif %}

  // LTE cells
  cell_list: [
{%- if do_lte %}
    {%- for i, k in enumerate(cell_list) %}
    {
      rf_port: {{ i }},
      n_antenna_dl: {{ ru.n_antenna_dl }},
      n_antenna_ul: {{ ru.n_antenna_ul }},

      cell_id: {{ cell_list[k].get('cell_id', slapparameter_dict.get('cell_id', '0x0' + i|string)) }},
      tac: {{ cell_list[k].get('tac', slapparameter_dict.get('tac', '0x0001')) }},
      n_id_cell: {{ cell_list[k].get('pci', slapparameter_dict.get('pci', i)) }},
      root_sequence_index: {{ cell_list[k].get('root_sequence_index', slapparameter_dict.get('root_sequence_index', 204 + i)) }},
      dl_earfcn: {{ cell_list[k].get('dl_earfcn', slapparameter_dict.get('dl_earfcn', earfcn)) }},
      inactivity_timer: {{ slapparameter_dict.get('inactivity_timer', slap_configuration['configuration.default_lte_inactivity_timer']) }},

      // Handover
{{- handover_config() }}

      // Carrier Aggregation
      {%- set scell_list = [] %}
      {%- for j, l in enumerate(cell_list) %}
      {%-   if j != i %}
      {%-     do scell_list.append([j, l]) %}
      {%-   endif %}
      {%- endfor %}
      scell_list: [
        {%- for j, l in enumerate(scell_list) %}
        {%- if j == 0 -%}
        {
        {%- else -%}
        , {
        {%- endif %}
          cell_id: {{ cell_list[l[1]].get('cell_id', '0x0' + l[0]|string) }},
          cross_carrier_scheduling: false,
        }
        {%- endfor %}
      ],

    // tune LTE parameters for the cell
{% if ors %}
    manual_ref_signal_power: true,
{% endif %}

{%- set tdd = (rf_mode == 'tdd') %}
{%- if tdd %}
    uldl_config: {{ tdd_config }},
    sp_config: 7,
{%- endif %}

{%- set n_rb_dl = J(jlte_n_rb_dl(cell.bandwidth)) %}
    n_rb_dl: {{ n_rb_dl }},
    si_coderate: {{ 0.30  if n_rb_dl == 6  else 0.20 }},

    pdsch_dedicated: {
      p_a: {{ {4: -6, 2: -3}.get(ru.n_antenna_dl, 0) }},
      p_b: -1,
    },

    pdcch_format:       {{  1  if n_rb_dl == 6  else 2 }},
    prach_config_index: {{ 15  if n_rb_dl == 6  else 4 }},
    initial_cqi:        {{  5  if n_rb_dl == 6  else 3 }},

    pucch_dedicated: {
      n1_pucch_sr_count: 11,
      cqi_pucch_n_rb: 1,
{#- for CA with 2 cells it is possible to use PUCCH 1b CS ack/nack #}
{%- if len(cell_list) == 2  %}
      ack_nack_feedback_mode_ca: "cs",
      n1_pucch_an_cs_count: 8,
{#- starting from 3 cells it is always PUCCH 3 for ack/nack in CA #}
{%- elif len(cell_list) >= 3  %}
      ack_nack_feedback_mode_ca: "pucch3",
      n3_pucch_an_n_rb: 3,
{%- endif %}
{%- if tdd  %}
      tdd_ack_nack_feedback_mode: "multiplexing", /* TDD only */
{%- endif %}
    },

{%-       if ru.n_antenna_dl >= 2  %}
    m_ri: 8,
    transmission_mode: 3,
{%-       endif %}

    srs_dedicated: {
{%-       if n_rb_dl == 6  %}
      srs_bandwidth_config: 7,
      srs_bandwidth: 1,
{%-       elif n_rb_dl == 15  %}
      srs_bandwidth_config: 6,
      srs_bandwidth: 1,
{%-       elif n_rb_dl == 25  %}
      srs_bandwidth_config: 3,
      srs_bandwidth: 1,
{%-       elif n_rb_dl == 50  %}
      srs_bandwidth_config: 2,
      srs_bandwidth: 2,
{%-       elif n_rb_dl == 75  %}
      srs_bandwidth_config: 2,
      srs_bandwidth: 2,
{%-       else  %}
      srs_bandwidth_config: 2,
      srs_bandwidth: 3,
{%-       endif %}
      srs_subframe_config: 3,
      srs_period: 40,
      srs_hopping_bandwidth: 0,
    },

    drb_config: "{{ drb_file }}",

    sib_sched_list: [
      {
        filename: "{{ sib23_file }}",
        si_periodicity: 16,
      },
    ],
    },
    {%- endfor %}
{%- endif %}
  ],

{%- if do_lte %}
  cell_default: {
    plmn_list: [
{%- if slapparameter_dict.get('plmn_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['plmn_list']) %}
{%- if i == 0 -%}
      {
{%- else -%}
      , {
{%- endif %}
      plmn: "{{ slapparameter_dict['plmn_list'][k]['plmn'] }}",
      reserved: {{ str(slapparameter_dict['plmn_list'][k].get('reserved', false)).lower() }},
      attach_without_pdn: {{ str(slapparameter_dict['plmn_list'][k].get('attach_without_pdn', false)).lower() }},
      }
{%- endfor -%}
{% else %}
      "00101",
{% endif %}
    ],
    cyclic_prefix: "normal",

    phich_duration: "normal",
    phich_resource: "1",

    si_value_tag: 0,
    cell_barred: false,
    intra_freq_reselection: true,
    q_rx_lev_min: -70,
    si_window_length: 40,

    si_pdcch_format: 2,

    n_symb_cch: 0,

    prach_freq_offset: -1,

    pusch_dedicated: {
      beta_offset_ack_index: 9,
      beta_offset_ri_index: 6,
      beta_offset_cqi_index: 6,
    },

    pusch_hopping_offset: -1,

    pusch_msg3_mcs: 0,

    dl_256qam: true,
    ul_64qam: true,

    sr_period: 20,

    cqi_period: 40,

    mac_config: {
       ul_max_harq_tx: 5,
       dl_max_harq_tx: 5,
    },

    pusch_max_its: 6,

    dpc: true,
    dpc_pusch_snr_target: 25,
    dpc_pucch_snr_target: 25,

    cipher_algo_pref: [],
    integ_algo_pref: [2, 1],

    srb_config: [
      {
        id: 1,
        maxRetxThreshold: 32,
        t_Reordering: 45,
        t_PollRetransmit: 60,
      },
      {
        id: 2 ,
        maxRetxThreshold: 32,
        t_Reordering: 45,
        t_PollRetransmit: 60,
      }
    ],

{#  TODO fully expose lte meas_config_desc in generic SR #}
  meas_config_desc: {
    a1_report_type: "rsrp",
    a1_rsrp: -70,
    a1_hysteresis: 0,
    a1_time_to_trigger: 640,
    a2_report_type: "rsrp",
    a2_rsrp: -80,
    a2_hysteresis: 0,
    a2_time_to_trigger: 640,
    a3_report_type: "rsrp",
    a3_offset: {{ slapparameter_dict.get('lte_handover_a3_offset', 6) }},
    a3_hysteresis: 0,
    a3_time_to_trigger: {{ slapparameter_dict.get('lte_handover_a3_time_to_trigger', 480) }},
  },
  meas_gap_config: "gp0",
  ho_from_meas: true,
  },
{%- endif %}

{%  if do_nr %}
  // NR cells
  nr_cell_list: [
  {
    rf_port: 0,
    n_antenna_dl: {{ ru.n_antenna_dl }},
    n_antenna_ul: {{ ru.n_antenna_ul }},

    cell_id: {{ slapparameter_dict.get('cell_id', '0x01') }},
    n_id_cell: {{ slapparameter_dict.get('pci', 500) }},
    band: {{ nr_band }},
    dl_nr_arfcn: {{ nr_arfcn }},
    bandwidth: {{ slapparameter_dict.get('nr_bandwidth', slap_configuration['configuration.default_nr_bandwidth']) }},

    subcarrier_spacing: 30,
    ssb_pos_bitmap: "{{ slapparameter_dict.get('ssb_pos_bitmap', slap_configuration['configuration.default_nr_ssb_pos_bitmap']) }}",

    inactivity_timer: {{ slapparameter_dict.get('inactivity_timer', slap_configuration['configuration.default_nr_inactivity_timer']) }},

      // Handover
{{- handover_config() }}

    // tune NR parameters for the cell
{%- if ors %}
    manual_ref_signal_power: true,
{%- if ors['one-watt'] %}
    ss_pbch_block_power: {{ (tx_gain | int)  - 54 }},
{%- else %}
    ss_pbch_block_power: {{ (tx_gain | int) - 35 }},
{%- endif -%}
{%- endif %}

{% if tdd_config == 1 %}
    tdd_ul_dl_config: {
      pattern1: {
        period: 5,
        dl_slots: 7,
        dl_symbols: 6,
        ul_slots: 2,
        ul_symbols: 4,
      },
    },
{% elif tdd_config == 2 %}
    tdd_ul_dl_config: {
      pattern1: {
        period: 2.5,
        dl_slots: 3,
        dl_symbols: 10,
        ul_slots: 1,
        ul_symbols: 2,
      },
    },
{% elif tdd_config == 3 %}
    tdd_ul_dl_config: {
      pattern1: {
         period: 5, /* in ms */
         dl_slots: 1,
         dl_symbols: 10,
         ul_slots: 8,
         ul_symbols: 2,
      },
    },
{% endif %}

    prach: {
{% if ru.ru_type == "m2ru" %}
      msg1_frequency_start: 0,
{% endif %}
    },

    pdcch: {
{% if ru.ru_type == "m2ru" %}
      n_rb_coreset0: 48,
      n_symb_coreset0: 1,
      dedicated_coreset: {
{% if ru.ru_type == "m2ru" %}
        duration: 1,
{% endif %}
      },
{% endif %}
{% if tdd_config == 3 %}
      uss: {
       n_candidates: [ 0, 8, 1, 0, 0 ],
       dci_0_1_and_1_1: true,
      },
{% else %}
      uss: {
        n_candidates: [ 0, 2, 1, 0, 0 ],
        dci_0_1_and_1_1: true,
      },
{% endif %}
    },

    pdsch: {
{% if ru.ru_type == "m2ru" %}
      k0: 0,
      k1: [ 8, 7, 7, 6, 5, 4, 12, 11 ],
{% elif tdd_config == 3 %}
      k1: [4, 11], 
{% endif %}
    },

    pusch: {
{% if ru.ru_type == "m2ru" %}
      k2: 4,
      msg3_k2: 7,
{% elif tdd_config == 3 %}
      k2: [11, 12, 4, 5, 6, 7, 7, 8], 
      msg3_k2: 7,
{% endif %}
    },

    csi_rs: {
      nzp_csi_rs_resource: [
        {
{%-       if ru.n_antenna_dl == 1     %}
          n_ports: 1,
          frequency_domain_allocation: "row2",
          bitmap: "100000000000",
          cdm_type: "no_cdm",
{%-       elif ru.n_antenna_dl == 2   %}
          n_ports: 2,
          frequency_domain_allocation: "other",
          bitmap: "100000",
          cdm_type: "fd_cdm2",
{%-       elif ru.n_antenna_dl == 4   %}
          n_ports: 4,
          frequency_domain_allocation: "row4",
          bitmap: "100",
          cdm_type: "fd_cdm2",
{%-       elif ru.n_antenna_dl == 8   %}
          n_ports: 8,
          frequency_domain_allocation: "other",
          bitmap: "110011",
          cdm_type: "fd_cdm2",
{%-       else  %}
{%-         do error('n_antenna_dl=%d is not supported' % ru.n_antenna_dl) %}
{%-       endif %}
        },
{% if tdd_config != 3 %}
        {
          csi_rs_id: 1,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 4,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 11,
          qcl_info_periodic_csi_rs: 0,
        },
        {
          csi_rs_id: 2,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 8,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 11,
          qcl_info_periodic_csi_rs: 0,
        },
        {
          csi_rs_id: 3,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 4,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 12,
          qcl_info_periodic_csi_rs: 0,
        },
        {
          csi_rs_id: 4,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 8,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 12,
          qcl_info_periodic_csi_rs: 0,
        },
{% endif %}
      ],

      nzp_csi_rs_resource_set: [
        {},
{% if tdd_config != 3 %}
        {
          csi_rs_set_id: 1,
          nzp_csi_rs_resources: [ 1, 2, 3, 4 ],
          repetition: false,
          trs_info: true,
        },
{% endif %}
      ],

      csi_resource_config: [
        {},
        {},
{% if tdd_config != 3 %}
        {
          csi_rsc_config_id: 2,
          nzp_csi_rs_resource_set_list: [ 1 ],
          resource_type: "periodic",
        },
{% endif %}
      ],

      csi_report_config: [
        {
{%-       if ru.n_antenna_dl > 1  %}
          codebook_config: {
            codebook_type: "type1",
            sub_type: "typeI_SinglePanel",
{%-         if ru.n_antenna_dl == 2 %}
{%-         elif ru.n_antenna_dl == 4 %}
            n1: 2,
            n2: 1,
            codebook_mode: 1,
{%-         elif ru.n_antenna_dl == 8 %}
            n1: 4,
            n2: 1,
            codebook_mode: 1,
{%-         endif %}
          },
{%-       endif %}
        },
      ],
    },

    drb_config: "{{ drb_file }}",
  },
  ],
  nr_cell_default: {
    ssb_period: 20,
  plmn_list: [
{%- if slapparameter_dict.get('plmn_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['plmn_list']) %}
{%- if i == 0 -%}
      {
{%- else -%}
      , {
{%- endif %}
      plmn: "{{ slapparameter_dict['plmn_list'][k]['plmn'] }}",
      tac: {{ slapparameter_dict['plmn_list'][k].get('tac', 100) }},
{%- if slapparameter_dict['plmn_list'][k].get('ranac', '') %}
      ranac: {{ slapparameter_dict['plmn_list'][k]['ranac'] }},
{%- endif %}
      reserved: {{ str(slapparameter_dict['plmn_list'][k].get('reserved', false)).lower() }},
      nssai: [
{%- if slapparameter_dict.get('nssai', '') %}
{%- for j, k in enumerate(slapparameter_dict['nssai']) %}
{%- if j == 0 %}
        {
{%- else -%}
        , {
{%- endif %}
         sst: {{ slapparameter_dict['nssai'][k]['sst'] }},
{%- if slapparameter_dict['nssai'][k].get('sd', '') %}
         sd: {{ slapparameter_dict['nssai'][k]['sd'] }},
{%- endif %}
        }
{%- endfor -%}
{% else %}
        {
         sst: 1,
        },
{% endif %}
      ],
      }
{%- endfor -%}
{% else %}
      {
      plmn: "00101",
      tac: 100,
      reserved: false,
      nssai: [
{%- if slapparameter_dict.get('nssai', '') %}
{%- for j, k in enumerate(slapparameter_dict['nssai']) %}
{%- if j == 0 %}
        {
{%- else -%}
        , {
{%- endif %}
         sst: {{ slapparameter_dict['nssai'][k]['sst'] }},
{%- if slapparameter_dict['nssai'][k].get('sd', '') %}
         sd: {{ slapparameter_dict['nssai'][k]['sd'] }},
{%- endif %}
        }
{%- endfor -%}
{% else %}
        {
         sst: 1,
        },
{% endif %}
       ],
      },
{%- endif %}
    ],
    si_window_length: 40,
    cell_barred: false,
    intra_freq_reselection: true,
    q_rx_lev_min: -70,
    q_qual_min: -20,
    root_sequence_index: 1,
    sr_period: 40,
    dmrs_type_a_pos: 2,
    prach: {
      prach_config_index: 160,
      msg1_subcarrier_spacing: 30,
      msg1_fdm: 1,
      msg1_frequency_start: -1,
      zero_correlation_zone_config: 15,
      preamble_received_target_power: -110,
      preamble_trans_max: 7,
      power_ramping_step: 4,
      ra_response_window: 20,
      restricted_set_config: "unrestricted_set",
      ra_contention_resolution_timer: 64,
      ssb_per_prach_occasion: 1,
      cb_preambles_per_ssb: 8,
    },
    pdcch: {
      search_space0_index: 0,
      dedicated_coreset: {
        rb_start: -1,
        l_crb: -1,
        duration: 0,
        precoder_granularity: "sameAsREG_bundle",
      },
      css: {
        n_candidates: [ 0, 0, 4, 0, 0 ],
      },
      rar_al_index: 2,
      si_al_index: 2,
      al_index: 1,
    },
    pdsch: {
      mapping_type: "typeA",
      dmrs_add_pos: 1,
      dmrs_type: 1,
      dmrs_max_len: 1,
      mcs_table: "qam256",
      rar_mcs: 2,
      si_mcs: 6,
    },
    csi_rs: {
      nzp_csi_rs_resource: [
        {
          csi_rs_id: 0,
          density: 1,
          first_symb: 4,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 80,
          offset: 1,
          qcl_info_periodic_csi_rs: 0,
        },
      ],
      nzp_csi_rs_resource_set: [
        {
          csi_rs_set_id: 0,
          nzp_csi_rs_resources: [ 0 ],
          repetition: false,
        },
      ],
     csi_im_resource: [
        {
          csi_im_id: 0,
          pattern: 1,
          subcarrier_location: 8,
          symbol_location: 8,
          rb_start: 0,
          l_crb: -1,
          period: 80,
          offset: 1,
        },
      ],
      csi_im_resource_set: [
        {
          csi_im_set_id: 0,
          csi_im_resources: [ 0 ],
        }
      ],
      zp_csi_rs_resource: [
        {
          csi_rs_id: 0,
          frequency_domain_allocation: "row4",
          bitmap: "100",
          n_ports: 4,
          cdm_type: "fd_cdm2",
          first_symb: 8,
          density: 1,
          rb_start: 0,
          l_crb: -1,
          period: 80,
          offset: 1,
        },
      ],
      p_zp_csi_rs_resource_set: [
        {
          zp_csi_rs_resources: [ 0 ],
        },
      ],
      csi_resource_config: [
        {
          csi_rsc_config_id: 0,
          nzp_csi_rs_resource_set_list: [ 0 ],
          resource_type: "periodic",
        },
        {
          csi_rsc_config_id: 1,
          csi_im_resource_set_list: [ 0 ],
          resource_type: "periodic",
        },
      ],
      csi_report_config: [
        {
          resources_for_channel_measurement: 0,
          csi_im_resources_for_interference: 1,
          report_config_type: "periodic",
          period: 80,
          report_quantity: "CRI_RI_PMI_CQI",
          cqi_table: 2,
          subband_size: "value1",
        },
      ],
    },
    pucch: {
      dpc_snr_target: 25,
      pucch_group_hopping: "neither",
      hopping_id: -1,
      p0_nominal: -90,
        pucch1: {
          n_cs: 3,
          n_occ: 3,
          freq_hopping: true,
        },
        pucch2: {
          n_symb: 2,
          n_prb: 1,
          freq_hopping: true,
          simultaneous_harq_ack_csi: false,
          max_code_rate: 0.25,
        },
    },
    pusch: {
      dpc_snr_target: 25,
      mapping_type: "typeA",
      n_symb: 14,
      dmrs_add_pos: 1,
      dmrs_type: 1,
      dmrs_max_len: 1,
      tf_precoding: false,
      mcs_table: "qam256",
      mcs_table_tp: "qam256",
      ldpc_max_its: 5,
      p0_nominal_with_grant: -84,
      msg3_mcs: 4,
      msg3_delta_power: 0,
      beta_offset_ack_index: 9,
    },
    mac_config: {
      msg3_max_harq_tx: 5,
      ul_max_harq_tx: 5,
      dl_max_harq_tx: 5,
      ul_max_consecutive_retx: 30,
      dl_max_consecutive_retx: 30,
      periodic_bsr_timer: 20,
      retx_bsr_timer: 320,
      periodic_phr_timer: 500,
      prohibit_phr_timer: 200,
      phr_tx_power_factor_change: "dB3",
      sr_prohibit_timer: 0,
      sr_trans_max: 64,
    },
    cipher_algo_pref: [],
    integ_algo_pref: [2, 1],

{#  TODO fully expose nr meas_config_desc in generic SR #}
  meas_config_desc: {
    a1_report_type: "rsrp",
    a1_rsrp: -60,
    a1_hysteresis: 10,
    a1_time_to_trigger: 100,
    a2_report_type: "rsrp",
    a2_rsrp: -70,
    a2_hysteresis: 0,
    a2_time_to_trigger: 100,
    a3_report_type: "rsrp",
    a3_offset: {{ slapparameter_dict.get('nr_handover_a3_offset', 6) }},
    a3_hysteresis: 0,
    a3_time_to_trigger: {{ slapparameter_dict.get('nr_handover_time_to_trigger', 100) }},
    ssb_rsrq_filter_coeff: 3,
    ssb_sinr_filter_coeff: 5
  },
  meas_gap_config: {
    pattern_id: 0
  },


  },
{%- endif %}
}
