[buildout]
parts =
  switch-softwaretype

eggs-directory = ${buildout:eggs-directory}
develop-eggs-directory = ${buildout:develop-eggs-directory}
offline = true

[directory]
recipe = slapos.cookbook:mkdirectory
software = ${buildout:directory}
home = $${buildout:directory}
etc = $${:home}/etc

[slap-configuration]
recipe = slapos.cookbook:slapconfiguration.serialised
computer = $${slap-connection:computer-id}
partition = $${slap-connection:partition-id}
url = $${slap-connection:server-url}
key = $${slap-connection:key-file}
cert = $${slap-connection:cert-file}

[jinja2-template-base]
recipe = slapos.recipe.template:jinja2
output = $${buildout:directory}/$${:filename}
extra-context =
context =
    import json_module json
    import netaddr netaddr
    key eggs_directory buildout:eggs-directory
    key develop_eggs_directory buildout:develop-eggs-directory
    raw buildout_directory ${buildout:directory}
    raw pythonwitheggs ${buildout:bin-directory}/pythonwitheggs
    section slap_connection slap-connection
    key slapparameter_dict slap-configuration:configuration
    key lan_ipv4 lan-ip:ipv4
    raw rf_mode ${rf-mode:rf-mode}
    raw software_name ${rf-mode:software-name}
    raw trx ${rf-mode:trx}
    raw bbu ${rf-mode:bbu}
    raw ru ${rf-mode:ru}
    $${:extra-context}
import-list =
    rawfile slaplte.jinja2 ${slaplte.jinja2:target}
    rawfile ru_libinstance.jinja2.cfg ${ru_libinstance.jinja2.cfg:target}
    rawfile ru_sdr_libinstance.jinja2.cfg ${ru_sdr_libinstance.jinja2.cfg:target}
    rawfile ru_lopcomm_libinstance.jinja2.cfg ${ru_lopcomm_libinstance.jinja2.cfg:target}
    rawfile ru_sunwave_libinstance.jinja2.cfg ${ru_sunwave_libinstance.jinja2.cfg:target}


[amarisoft]
recipe = slapos.recipe.build
init =
  import os, re
  try:
    lte_version = sorted(filter(lambda x: re.match(r"v[0-9]{4}-[0-9]{2}-[0-9]{2}", x), os.listdir('/opt/amarisoft')))[-1][1:]
  except FileNotFoundError:
    lte_version = 'LTEVERSION'
  path = "/opt/amarisoft/v" + lte_version
  options['lte-version'] = lte_version
  options['path'] = path
  options['sdr'] = path + "/trx_sdr"
  options['enb'] = path + "/enb"
  options['mme'] = path + "/mme"
  options['ims'] = path + "/ims"
  options['ue'] = path + "/ue"
  import os
  lte_expiration = "Unknown"
  amarisoft_dir = '/opt/amarisoft/.amarisoft'
  try:
    for filename in os.listdir(amarisoft_dir):
      if filename.endswith('.key'):
        with open(os.path.join(amarisoft_dir, filename), 'r') as f:
          f.seek(260)
          for l in f:
            if l.startswith('version='):
              lte_expiration = l.split('=')[1].strip()
  except FileNotFoundError:
    pass
  options['lte-expiration'] = lte_expiration

[lan-ip]
recipe = slapos.recipe.build
init =
  import netifaces
  for i in netifaces.interfaces():
      if not (i.startswith("slaptun") or i.startswith("slaptap") or i.startswith("re6stnet") or i == "lo"):
          a = netifaces.ifaddresses(i)
          if netifaces.AF_INET in a:
              try:
                  options['ipv4'] = a[netifaces.AF_INET][0]['addr']
              except:
                  options['ipv4'] = "0.0.0.0"

[comp-id]
recipe = slapos.recipe.build
computer = $${slap-connection:computer-id}
title = $${slap-configuration:root-instance-title}
init =
  import socket
  options['hostname'] = socket.gethostname()
  comp_id = '__'.join(options[x] for x in ('hostname', 'computer', 'title'))
  options['comp-id'] = comp_id

[switch-softwaretype]
recipe = slapos.cookbook:switch-softwaretype
enb = dynamic-template-enb:output
gnb = dynamic-template-enb:output
core-network = dynamic-template-core-network:output
ue-lte = dynamic-template-ue:output
ue-nr = dynamic-template-ue:output
RootSoftwareInstance = $${:core-network}

[dynamic-template-enb]
< = jinja2-template-base
url = ${template-enb:target}
filename = instance-enb.cfg
extensions = jinja2.ext.do
extra-context =
    raw monitor_template ${monitor2-template:output}
    json RF {}
    section comp_id comp-id
    section slap_configuration slap-configuration
    key lte_version amarisoft:lte-version
    key lte_expiration amarisoft:lte-expiration
    key enb amarisoft:enb
    key sdr amarisoft:sdr
    raw enb_template   ${enb.jinja2.cfg:target}
    raw slaplte_template ${slaplte.jinja2:target}
    raw drb_lte_template ${drb_lte.jinja2.cfg:target}
    raw drb_nr_template  ${drb_nr.jinja2.cfg:target}
    raw sib23_template   ${sib23.jinja2.asn:target}
    raw ru_amarisoft_stats_template   ${ru_amarisoft-stats.jinja2.py:target}
    raw ru_amarisoft_rf_info_template   ${ru_amarisoft-rf-info.jinja2.py:target}
    raw ru_lopcomm_stats_template   ${ru_lopcomm_stats.jinja2.py:target}
    raw ru_lopcomm_config_template   ${ru_lopcomm_config.jinja2.py:target}
    raw ru_lopcomm_software_template   ${ru_lopcomm_software.jinja2.py:target}
    raw ru_lopcomm_reset_info_template   ${ru_lopcomm_reset-info.jinja2.py:target}
    raw ru_lopcomm_reset_template   ${ru_lopcomm_reset.jinja2.py:target}
    raw ru_lopcomm_CreateProcessingEle_template   ${ru_lopcomm_CreateProcessingEle.jinja2.xml:target}
    raw ru_lopcomm_cu_config_template   ${ru_lopcomm_cu_config.jinja2.xml:target}
    raw ru_lopcomm_firmware_path ${ru_lopcomm_firmware-dl:target}
    raw ru_lopcomm_firmware_filename ${ru_lopcomm_firmware-dl:filename}
    raw ru_tapsplit ${ru_tapsplit:target}
    raw netcapdo ${netcapdo:exe}
    raw openssl_location ${openssl:location}
    raw default_lte_bandwidth ${default-params:default-lte-bandwidth}
    raw default_lte_inactivity_timer ${default-params:default-lte-inactivity-timer}
    raw default_nr_bandwidth ${default-params:default-nr-bandwidth}
    raw default_nr_ssb_pos_bitmap ${default-params:default-nr-ssb-pos-bitmap}
    raw default_nr_inactivity_timer ${default-params:default-nr-inactivity-timer}
    raw default_n_antenna_dl ${default-params:default-n-antenna-dl}
    raw default_n_antenna_ul ${default-params:default-n-antenna-ul}
    raw ru_dnsmasq_template  ${ru_dnsmasq.jinja2.cfg:target}
    raw dnsmasq_location ${dnsmasq:location}
    raw fluent_bit_location ${fluent-bit:location}
    raw openssh_location ${openssh:location}
    raw openssh_output_keygen ${openssh-output:keygen}

[dynamic-template-core-network]
< = jinja2-template-base
url = ${template-core-network:target}
filename = instance-core-network.cfg
extensions = jinja2.ext.do
extra-context =
    raw monitor_template ${monitor2-template:output}
    key lte_version amarisoft:lte-version
    key lte_expiration amarisoft:lte-expiration
    key mme amarisoft:mme
    raw mme_template   ${mme.jinja2.cfg:target}
    raw dnsmasq_template   ${dnsmasq-core-network.jinja2.cfg:target}
    raw ims_template   ${ims.jinja2.cfg:target}
    raw openssl_location ${openssl:location}
    raw nghttp2_location ${nghttp2:location}
    raw iperf3_location ${iperf3:location}
    raw dnsmasq_location ${dnsmasq:location}
    key ue_db_path ue-db-config:output
    key slave_instance_list slap-configuration:slave-instance-list

[dynamic-template-ue]
< = jinja2-template-base
url = ${template-ue:target}
filename = instance-ue-lte.cfg
extensions = jinja2.ext.do
extra-context =
    section slap_configuration slap-configuration
    raw monitor_template ${monitor2-template:output}
    key ue amarisoft:ue
    key sdr amarisoft:sdr
    raw ue_template   ${ue.jinja2.cfg:target}
    raw openssl_location ${openssl:location}
    raw default_lte_bandwidth ${default-params:default-lte-bandwidth}
    raw default_nr_bandwidth ${default-params:default-nr-bandwidth}
    raw default_n_antenna_dl ${default-params:default-n-antenna-dl}
    raw default_n_antenna_ul ${default-params:default-n-antenna-ul}
    raw default_lte_imsi ${default-params:default-lte-imsi}
    raw default_nr_imsi ${default-params:default-nr-imsi}
    raw default_lte_k ${default-params:default-lte-k}
    raw default_nr_k ${default-params:default-nr-k}

[ue-db-config]
recipe = slapos.recipe.template:jinja2
url = ${ue_db.jinja2.cfg:target}
filename = ue_db.cfg
extensions = jinja2.ext.do
output = $${directory:etc}/$${:filename}
context =
    import json_module json
    key slave_instance_list slap-configuration:slave-instance-list
