{#- Package slaplte provides helpers for configuring Amarisoft LTE services in SlapOS.

    - load_cell initializes cell registry.

    The following utilities are also provided:

    - J should be used around macro calls to retrieve returned objects.
    - error reports instantiation error.
-#}


{#- J is used around macro calls to retrieve returned objects.

    It is needed to workaround jinja2 limitation that macro can return only
    strings - not arbitrary objects: we return objects as JSON-encoded string
    and J decodes them.

    By convention macros that return JSON-encoded objects start with "j" prefix.

    Usage example:

      set obj = J(jmymacro(...))
#}
{%- set J = json_module.loads %}


{#- tap indicates tap interface, that slapos told us to use,
    or 'xxx-notap-xxx' if slapos provided us either nothing or empty string. #}
{%- set tap = slap_configuration.get('tap-name', '')   %}
{%- if tap == '' %}
{%-   set tap = 'xxx-notap-xxx'   %}
{%- endif %}


{#- error reports instantiation error. #}
{%- macro error(msg)  %}
{%-   set msg = 'Instantiation Error: %s\n' % msg %}
{%-   do assert(False, msg) %}
{%- endmacro  %}



{#- ---- loading ---- #}

{#- load_cell initializes cell registry.

    cell_list keeps configured cells: {} cell reference -> cell parameters
#}
{%- macro load_cell(cell_list)	%}
{%-   do cell_list.update( slapparameter_dict.get('cell_list', {'default': {}}) )  %}
{%-   for i, k in enumerate(cell_list) %}
{%-     set cell = cell_list[k] %}
{%-     do cell.setdefault('cpri_port_number', i) %}
{%-   endfor %}

{#-   assign TAP interfaces to RUs  #}
{%-   for i, (cell_ref, cell) in enumerate(cell_list|dictsort)  %}
{%-     if len(cell_list) > 1 %}
{%-       set ru_tap = "%s-%d" % (tap, i+1) %}
{%-     else  %}
{%-       set ru_tap = tap  %}
{%-     endif %}
{%-     do cell.update({'_tap': ru_tap})  %}
{%-   endfor  %}

{%- endmacro  %}
