# instance-ue implements UEsim service.
{#- defaults for global UE parameters.
    TODO automatically load ue defaults from JSON schema #}
{%- set ue_defaults = {
  'com_ws_port':  9001,
  'com_addr':     '127.0.1.2',
} %}
{%- for k,v in ue_defaults|dictsort %}
{%-   do slapparameter_dict.setdefault(k, v) %}
{%- endfor %}

[buildout]
parts =
  directory
  lte-ue-config
  lte-ue-service
  nginx-launcher
  nginx-graceful
  monitor-base
  publish-connection-information

extends = {{ monitor_template }}

eggs-directory = {{ eggs_directory }}
develop-eggs-directory = {{ develop_eggs_directory }}
offline = true

{%- set icell_kind='ue'                                         %}
{%- import 'slaplte.jinja2'            as slaplte with context  %}
{%- import 'ru_libinstance.jinja2.cfg' as rulib   with context  %}
{%- set iue_dict = {}                                           %}
{%- do slaplte.load_iue(iue_dict)                               %}
{%- do slaplte.check_loaded_everything()                        %}

{{ rulib.buildout() }}

[myslap]
# see instance-enb.jinja2.cfg about myslap
parameter_dict = {{ dumps(slapparameter_dict) }}
configuration = {{ dumps(slap_configuration) }}


[monitor-httpd-conf-parameter]
httpd-include-file = {{ buildout_directory }}/etc/httpd-include-file.conf
port = ${monitor-instance-parameter:monitor-httpd-port}
url = https://[${monitor-instance-parameter:monitor-httpd-ipv6}]:${:port}

[monitor-instance-parameter]
monitor-httpd-port = ${monitor-address:port}

[monitor-address]
recipe = slapos.cookbook:free_port
minimum = 8035
maximum = 8055
ip = ${monitor-instance-parameter:monitor-httpd-ipv6}

[directory]
recipe = slapos.cookbook:mkdirectory
software = {{ buildout_directory }}
home = ${buildout:directory}
etc = ${:home}/etc
var = ${:home}/var
etc = ${:home}/etc
bin = ${:home}/bin
tmp = ${:home}/tmp
run = ${:var}/run
script = ${:etc}/run
service = ${:etc}/service
promise = ${:etc}/promise
log = ${:var}/log

[lte-ue-sh-wrapper]
recipe = slapos.recipe.template
output = ${directory:bin}/${:_buildout_section_name_}
ue-log = ${directory:log}/ue-output.log
ue-radio-log = ${directory:log}/ue.log
ue-start-date = ${directory:run}/enb-start.date
inline =
  #!/bin/sh
  sudo {{ amarisoft['dir'] }}/rm-tmp-lte | true;
  sudo -n {{ amarisoft['dir'] }}/init-sdr {{ amarisoft['dir'] }}/v{{ amarisoft['version'] }};
  sudo -n {{ amarisoft['dir'] }}/init-ue;
  stat ${:ue-start-date} && mv ${:ue-radio-log} ${directory:log}/ue-$(cat ${:ue-start-date}).log
  rm -f $(ls -1t ${directory:log}/ue-2* | tail -n+50)
  date +"%Y-%m-%d-%T" > ${:ue-start-date}
  (echo && echo && date "+[%Y/%m/%d %T.%N %Z] Starting UE software..." && echo) >> ${:ue-log};
  tail -c 1M ${:ue-log} > ${:ue-log}.tmp;
  mv ${:ue-log}.tmp ${:ue-log};
{%- if slapparameter_dict.get('power_on', False) %}
  echo "power_on" | sudo -n {{ amarisoft['ue_dir'] }}/lteue ${directory:etc}/ue.cfg >> ${:ue-log} 2>> ${:ue-log};
{%- else %}
  {{ amarisoft['ue_dir'] }}/lteue ${directory:etc}/ue.cfg >> ${:ue-log} 2>> ${:ue-log};
{%- endif %}

### User Equipment (UE)
[lte-ue-service]
recipe = slapos.cookbook:wrapper
command-line = ${lte-ue-sh-wrapper:output}
wrapper-path = ${directory:service}/lte-ue
mode = 0775
reserve-cpu = True
pidfile = ${directory:run}/ue.pid
hash-files =
  ${lte-ue-config:output}
  ${lte-ue-sh-wrapper:output}
environment =
  LD_LIBRARY_PATH={{ openssl_location }}/lib
  AMARISOFT_PATH={{ amarisoft['license_dir'] }}

[config-base]
recipe = slapos.recipe.template:jinja2
extensions = jinja2.ext.do
extra-context =
context =
  json ors false
  section directory directory
  section pub_info publish-connection-information
  key slap_configuration myslap:configuration
  key slapparameter_dict myslap:parameter_dict
  import xbuildout xbuildout
  ${:extra-context}

[lte-ue-config]
<= config-base
url = {{ ue_template }}
output = ${directory:etc}/ue.cfg
import-list =
    rawfile slaplte.jinja2 {{ slaplte_template }}
extra-context =
    import json_module json
    key iru_dict   :iru_dict
    key icell_dict :icell_dict
    key iue_dict   :iue_dict
    key websocket_password websocket-password:passwd
iru_dict   = {{ dumps(rulib.iru_dict) }}
icell_dict = {{ dumps(rulib.icell_dict) }}
iue_dict   = {{ dumps(iue_dict) }}


[publish-connection-information]
<= monitor-publish
recipe = slapos.cookbook:publish.serialised
rue_bind_addr = {{my_ipv6}}
ue-ipv4 = {{ lan_ipv4 }}
ue-mac = {{ mac }}
websocket-hostname = ${frontend-urlparse:hostname}
websocket-port = ${frontend-urlparse:port}
websocket-password = ${websocket-password:passwd}
amarisoft-version = {{ amarisoft['version'] }}
amarisoft-host-id = {{ amarisoft['lteue_host_id'] }}
amarisoft-available-versions = {{ amarisoft['version_installed'] }}
license-expiration = {{ amarisoft['lteenb_expiration'] }}
monitor-gadget-url = ${:monitor-base-url}/gadget/software.cfg.html

[monitor-instance-parameter]
{% if slapparameter_dict.get("name", None) %}
monitor-title = {{ slapparameter_dict['name'] | string }}
{% endif %}
{% if slapparameter_dict.get("monitor-password", None) %}
password = {{ slapparameter_dict['monitor-password'] | string }}
{% endif %}
