---
- hosts: {{ instance_name }}
  gather_facts: False
  connection: ssh
  user: {{ username }}
  sudo: yes
  vars:
    appname: {{ appname }}
    dependencies: {{ dependencies }}
{% if component_configs %}
    component_configs:
{% for component_config in component_configs %}
       - component: {{ component_config.component }}
         config_params: {{  component_config.config_params }}
{% endfor %}
{% endif %}
{% if rest_configs %}
    rest_configs:
{% for rest_config in rest_configs %}
       - endpoint: {{ rest_config.endpoint }}
         body: "{{ '{{' }} lookup('file', '{{ files_dir }}/{{ rest_config.fn }}') {{ '}}' }}"
{% endfor %}
{% endif %}
{% if early_rest_configs %}
    early_rest_configs:
{% for early_rest_config in early_rest_configs %}
       - endpoint: {{ early_rest_config.endpoint }}
         body: "{{ '{{' }} lookup('file', '{{ files_dir }}/{{ early_rest_config.fn }}') {{ '}}' }}"
{% endfor %}
{% endif %}

  tasks:

  - name: Get Docker IP
    script: /opt/xos/synchronizers/onos/scripts/dockerip.sh {{ ONOS_container }}
    register: onosaddr

  - name: Wait for ONOS to come up
    wait_for:
      host={{ '{{' }} onosaddr.stdout {{ '}}' }}
      port={{ '{{' }} item {{ '}}' }}
      state=present
    with_items:
    - 8101
    - 8181
    - 9876

  - name: Config file directory
    file:
      path=/home/ubuntu/{{ appname }}/
      state=directory

{% if node_key_fn %}
  - name: Copy over key
    copy:
      src={{ files_dir }}/{{ node_key_fn }}
      dest=/home/ubuntu/node_key

  - name: Copy node key into container
    shell: docker cp /home/ubuntu/node_key {{ ONOS_container }}:/root/node_key
{% endif %}

{% if config_fns %}
  - name: Copy over configuration files
    copy:
      src={{ files_dir }}/{{ '{{' }} item {{ '}}' }}
      dest=/home/ubuntu/{{ appname }}/{{ '{{' }} item {{ '}}' }}
    with_items:
        {% for config_fn in config_fns %}
        - {{ config_fn }}
        {% endfor %}

  - name: Make sure config directory exists
    shell: docker exec {{ ONOS_container }} mkdir -p /root/onos/config/
    sudo: yes

  - name: Copy config files into container
    shell: docker cp {{ appname }}/{{ '{{' }} item {{ '}}' }} {{ ONOS_container }}:/root/onos/config/
    sudo: yes
    with_items:
        {% for config_fn in config_fns %}
        - {{ config_fn }}
        {% endfor %}
{% endif %}

  # Don't know how to check for this condition, just wait
  - name: Wait for ONOS to install the apps
    wait_for: timeout=15

{% if early_rest_configs %}
  - name: Add ONOS early configuration values
    uri:
      url: http://{{ '{{' }} onosaddr.stdout {{ '}}' }}:8181/{{ '{{' }} item.endpoint {{ '}}' }}
      body: "{{ '{{' }} item.body {{ '}}' }}"
      body_format: raw
      method: POST
      user: karaf
      password: karaf
    with_items: "early_rest_configs"

  # Don't know how to check for this condition, just wait
  - name: Wait for ONOS to restart
    wait_for: timeout=15
{% endif %}

{% if install_dependencies %}
  - name: Install app file directory
    file:
      path=/home/ubuntu/{{ appname }}/apps/
      state=directory

  - name: Copy over app install files to ONOS host
    copy:
      src=/opt/xos/synchronizers/onos/{{ '{{' }} item {{ '}}' }}
      dest=/home/ubuntu/{{ appname }}/apps/{{ '{{' }} item {{ '}}' }}
    with_items:
        {% for install_app in install_dependencies %}
        - {{ install_app }}
        {% endfor %}

  - name: POST onos-app install command
    command: >
        curl -XPOST -HContent-Type:application/octet-stream -u karaf:karaf --data-binary @/home/ubuntu/{{ appname }}/apps/{{ '{{' }} item {{ '}}' }} http://{{ '{{' }} onosaddr.stdout  {{ '}}' }}:8181/onos/v1/applications
    with_items:
        {% for dependency in install_dependencies %}
        - {{ dependency }}
        {% endfor %}
{% endif %}

{% if dependencies %}
  - name: Add dependencies to ONOS
    uri:
      url: http://{{ '{{' }} onosaddr.stdout {{ '}}' }}:8181/onos/v1/applications/{{ '{{' }} item {{ '}}' }}/active
      method: POST
      user: karaf
      password: karaf
    with_items:
        {% for dependency in dependencies %}
        - {{ dependency }}
        {% endfor %}
{% endif %}

{% if component_configs %}
  - name: Add ONOS component configuration values
    command: >
        curl -XPOST -HContent-Type:application/json -u karaf:karaf -d {{ '{{' }} item.config_params | to_json {{ '}}' }} http://{{ '{{' }} onosaddr.stdout  {{ '}}' }}:8181/onos/v1/configuration/{{
 '{{' }} item.component {{ '}}' }}
    with_items: "component_configs"

#    uri:
#      url: http://{{ '{{' }} onosaddr.stdout {{ '}}' }}:8181/onos/v1/configuration/{{ '{{' }} item.component {{ '}}' }} #http://localhost:8181/onos/v1/configuration/
#      body: "{{ '{{' }} item.config_params | to_json {{ '}}' }}"
#      body_format: json
#      method: POST
#      user: karaf
#      password: karaf
#    with_items: "component_configs"
{% endif %}

{% if rest_configs %}
# Do this after services have been activated, or it will cause an exception.
# vOLT will re-read its net config; vbng may not.
  - name: Add ONOS configuration values
    uri:
      url: http://{{ '{{' }} onosaddr.stdout {{ '}}' }}:8181/{{ '{{' }} item.endpoint {{ '}}' }} #http://localhost:8181/onos/v1/network/configuration/
      body: "{{ '{{' }} item.body {{ '}}' }}"
      body_format: raw
      method: POST
      user: karaf
      password: karaf
    with_items: "rest_configs"
{% endif %}
