[buildout]
parts =
  directory
  ltelogs
  lte-mme-config
  lte-mme-service
  monitor-base
  publish-connection-information
  check-interface-up.py
{% if slapparameter_dict.get("iperf3", None) %}
  iperf-service
  port-listening-promise
{% endif %}

extends = {{ monitor_template }}

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

[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'] }}

configuration.network_name = RAPIDSPACE
configuration.domain = rapid.space
configuration.log_size = 50M
configuration.mme_ws_port = 9000
configuration.enb_ws_port = 9002
configuration.ims_ws_port = 9003
configuration.mme_addr = 127.0.1.100
configuration.ims_addr = 127.0.0.1
configuration.ims_bind = 127.0.0.2
configuration.enb_addr = 127.0.1.1

ue_db_path = {{ ue_db_path }}

[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

[ltelogs]
recipe = slapos.recipe.template:jinja2
url = {{ ltelogs_template }}
output = ${directory:home}/ltelogs.sh
extensions = jinja2.ext.do
context =
  section directory directory

{% if slapparameter_dict.get("mme_config_link", None) %}
[mme-config-dl]
recipe = slapos.recipe.build:download
url = {{ slapparameter_dict.get("mme_config_link") }}
version = {{ slapparameter_dict.get("mme_config_version") }}
offline = false
{% endif %}

### IMS
[lte-ims-service]
recipe = slapos.cookbook:wrapper
init = ${ltelogs:output} ${directory:log}/ims.log; sleep 1
command-line = {{ mme }}/lteims ${directory:etc}/ims.cfg
wrapper-path = ${directory:service}/lte-ims
mode = 0775
pidfile = ${directory:run}/ims.pid
hash-files =
  ${lte-ims-config:output}
  {{ ue_db_path }}
environment = AMARISOFT_PATH=/opt/amarisoft/.amarisoft

[lte-mme-sh-wrapper]
recipe = slapos.recipe.template
output = ${directory:bin}/${:_buildout_section_name_}
mme-log = ${directory:log}/mme-output.log
inline =
  #!/bin/sh
{% if not slapparameter_dict.get("testing", False) %}
  rm -f ${directory:var}/lte_ue.db;
  (echo && echo && date "+[%Y/%m/%d %T.%N %Z] Starting MME software..." && echo) >> ${:mme-log};
  tail -c 1M ${:mme-log} > ${:mme-log}.tmp;
  mv ${:mme-log}.tmp ${:mme-log};
  {{ mme }}/ltemme ${directory:etc}/mme.cfg >> ${:mme-log} 2>> ${:mme-log};
{% endif %}

### MME
[lte-mme-service]
recipe = slapos.cookbook:wrapper
# When the machine shutdowns abruptly, lte_ue is not cleaned up which causes
# amarisoft ltemme to fail. TODO: find a cleaner way to handle this
init = ${ltelogs:output} ${directory:log}/mme.log
command-line = ${lte-mme-sh-wrapper:output}
wrapper-path = ${directory:service}/lte-mme
mode = 0775
pidfile = ${directory:run}/mme.pid
hash-files =
  ${lte-mme-config:output}
  {{ ue_db_path }}
  ${lte-mme-sh-wrapper:output}
environment =
  LD_LIBRARY_PATH={{ openssl_location }}/lib:{{ nghttp2_location }}/lib
  AMARISOFT_PATH=/opt/amarisoft/.amarisoft

### EMPTY mme-ifup script
[lte-mme-ifup-empty]
recipe = slapos.cookbook:wrapper
wrapper-path = ${directory:bin}/mme-ifup-empty
command-line = echo Using interface
mode = 775

{% if slapparameter_dict.get("iperf3", None) %}
### iperf3
[iperf-service]
recipe = slapos.cookbook:wrapper
port = 5001
ip = ${slap-configuration:tun-ipv4-addr}
command-line = {{ iperf3_location }}/bin/iperf3 --server --interval 1 --port ${:port} --bind ${:ip}
wrapper-path = ${directory:service}/iperf3
mode = 0775
pidfile = ${directory:run}/iperf3.pid

[port-listening-promise]
<= monitor-promise-base
promise = check_socket_listening
name = iperf3-port-listening.py
config-host = ${iperf-service:ip}
config-port = ${iperf-service:port}
{% endif %}

[config-base]
recipe = slapos.recipe.template:jinja2
extensions = jinja2.ext.do
context =
  section directory directory
  section slap_configuration slap-configuration
  key slapparameter_dict slap-configuration:configuration
  key gtp_addr_v6 slap-configuration:ipv6-random
  raw gtp_addr_v4 {{ epc_ipv4 }}
  import  netaddr netaddr
  key ifup_empty lte-mme-ifup-empty:wrapper-path

[lte-ims-config]
<= config-base
url = {{ ims_template }}
output = ${directory:etc}/ims.cfg

[lte-mme-config]
<= config-base
{% if slapparameter_dict.get("mme_config_link", None) %}
url = ${mme-config-dl:target}
{% else %}
url = {{ mme_template }}
{% endif %}
output = ${directory:etc}/mme.cfg

[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 %}

[publish-connection-information]
<= monitor-publish
recipe = slapos.cookbook:publish.serialised
epc-ipv6 = ${slap-configuration:ipv6-random}
epc-ipv4 = {{ epc_ipv4 }}

[macro.promise]
<= monitor-promise-base
name = ${:_buildout_section_name_}

[check-interface-up.py]
<= macro.promise
promise = check_interface_up
config-testing = {{ slapparameter_dict.get("testing", False) }}
{% if not slapparameter_dict.get("testing", False) %}
config-ifname = ${slap-configuration:tun-name}
{% else %}
config-ifname =
{% endif %}
