{%- set dns_slave_instance_list = [] %}
{%- set sim_slave_instance_list = [] %}
{%- for slave in slave_instance_list %}
{%-   set slave_parameters = json_module.loads(slave['_']) %}
{%-   if slave_parameters.get('subdomain', '') != '' %}
{%-     do dns_slave_instance_list.append(slave) %}
{%-   elif slave_parameters.get('imsi', '') != '' %}
{%-     do sim_slave_instance_list.append(slave) %}
{%-   endif %}
{%- endfor %}

{% set part_list = [] -%}
{%- for slave in sim_slave_instance_list %}
{%-   set slave_parameters = json_module.loads(slave['_']) %}
{%    set slave_reference = slave.get('slave_reference', '') %}
{%    set publish_section_title = 'publish-%s' % slave_reference %}
{%    do part_list.append(publish_section_title) %}
[{{ publish_section_title }}]
recipe = slapos.cookbook:publish.serialised
-slave-reference = {{ slave_reference }}
info = Your SIM card with IMSI {{ slave_parameters.get('imsi', '') }} has been attached to service ${slap-configuration:instance-title}.
{%- endfor %}

{%- for slave in dns_slave_instance_list %}
{%-   set slave_parameters = json_module.loads(slave['_']) %}
{%    set slave_reference = slave.get('slave_reference', '') %}
{%    set publish_section_title = 'publish-%s' % slave_reference %}
{%    do part_list.append(publish_section_title) %}
[{{ publish_section_title }}]
recipe = slapos.cookbook:publish.serialised
-slave-reference = {{ slave_reference }}
domain = {{ slave_parameters['subdomain'] }}.{{ slapparameter_dict.get('local_domain', '') }}
ip = {{ slave_parameters.get('ip', '') }}
info = DNS entry with has been attached to service ${slap-configuration:instance-title}.
{%- endfor %}

[buildout]
parts =
  directory
  ltelogs
  mme-config
  mme-service
  monitor-base
  check-cpu-temperature.py
  check-interface-up.py
  publish-connection-information
{% if slapparameter_dict.get("iperf3", None) %}
  iperf-service
  iperf-listen-promise
{% endif %}
{% if slapparameter_dict.get("local_domain", '') %}
  dnsmasq-service
{% endif %}
{% for part in part_list -%}
{{ '  %s' % part }}
{% endfor %}

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

[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
bin = ${:home}/bin
tmp = ${:home}/tmp
run = ${:var}/run
script = ${:etc}/run
service = ${:etc}/service
promise = ${:etc}/promise
log = ${:var}/log

[check-cpu-temperature.py]
<= macro.promise
promise = check_cpu_temperature
config-testing = {{ slapparameter_dict.get("testing", False) }}
config-frequency = {{ slapparameter_dict.get("promise_cpu_temperature_frequency", 5) }}
config-max-spot-temp = {{ slapparameter_dict.get("promise_cpu_max_spot_temp", 90) }}
config-max-avg-temp = {{ slapparameter_dict.get("promise_cpu_max_avg_temp", 80) }}
config-avg-temp-duration = {{ slapparameter_dict.get("promise_cpu_avg_temp_duration", 600) }}
config-avg-flag-file = ${directory:var}/promise_cpu_avg_flag_file

[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
[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}/ims
mode = 0775
pidfile = ${directory:run}/ims.pid
hash-files =
  ${ims-config:output}
  {{ ue_db_path }}
environment = AMARISOFT_PATH=/opt/amarisoft/.amarisoft

[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
[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 = ${mme-sh-wrapper:output}
wrapper-path = ${directory:service}/mme
mode = 0775
pidfile = ${directory:run}/mme.pid
hash-files =
  ${mme-config:output}
  {{ ue_db_path }}
  ${mme-sh-wrapper:output}
environment =
  LD_LIBRARY_PATH={{ openssl_location }}/lib:{{ nghttp2_location }}/lib
  AMARISOFT_PATH=/opt/amarisoft/.amarisoft

### EMPTY mme-ifup script
[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

[iperf-listen-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 {{ lan_ipv4 }}
  import  netaddr netaddr
  key ifup_empty mme-ifup-empty:wrapper-path

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

[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

{% if slapparameter_dict.get("local_domain", '') %}
[dnsmasq-service]
recipe = slapos.cookbook:wrapper
port = 5353
ip = ${slap-configuration:tun-ipv4-addr}
command-line = {{ dnsmasq_location }}/sbin/dnsmasq --conf-file={{ dnsmasq_config_path }} -x ${directory:run}/dnsmasq.pid --local-service --keep-in-foreground
wrapper-path = ${directory:service}/dnsmasq
mode = 0775
hash-files =
  {{ dnsmasq_config_path }}

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

[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
core-network-ipv6 = ${slap-configuration:ipv6-random}
core-network-ipv4 = {{ lan_ipv4 }}
monitor-gadget-url = ${:monitor-base-url}/gadget/software.cfg.html

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