# Copyright 2017-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---

- name: make shell script file which is preliminary to setup onos rule
  template:
    src=onos_rule_setup_info.sh.j2
    dest=/opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh owner=root group=root mode=755

- name: get onos ip address
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -i onos
  register: tmp_onos_ip

- name: get application server (Internet) port for SGi
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -p enb
  register: tmp_as_sgi_port

- name: get spgw-u port for SGi
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -p spgwu
  register: tmp_spgwu_sgi_port

- name: get switch ID for enb
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -s enb
  register: tmp_enb_sw_id

- name: get switch ID for spgw-u
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -s spgwu
  register: tmp_spgwu_sw_id

- name: get switch IP connected with enb
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -d enb
  register: tmp_enb_sw_ip

- name: get switch IP connected with spgw-u
  shell: /opt/xos/synchronizers/vspgwc/onos_rule_setup_info.sh -d spgwu
  register: tmp_spgwu_sw_ip

- name: assign results into each variable
  set_fact:
    onos_ip: "{{ tmp_onos_ip.stdout }}"
    as_sgi_port: "{{ tmp_as_sgi_port.stdout }}"
    spgwu_sgi_port: "{{ tmp_spgwu_sgi_port.stdout }}"
    enb_sw_id: "{{ tmp_enb_sw_id.stdout }}"
    spgwu_sw_id: "{{ tmp_spgwu_sw_id.stdout }}"
    enb_sw_ip: "{{ tmp_enb_sw_ip.stdout }}"
    spgwu_sw_ip: "{{ tmp_spgwu_sw_ip.stdout }}"

- name: make json file for flow of enb
  template:
    src=flow_enb.json.j2
    dest=/opt/xos/synchronizers/vspgwc/flow_enb.json owner=root group=root mode=755

- name: make json file for flow of spgwu
  template:
    src=flow_spgwu.json.j2
    dest=/opt/xos/synchronizers/vspgwc/flow_spgwu.json owner=root group=root mode=755

- name: set flow for enb and spgwu to ONOS
  shell: "curl -X POST -H \"Content-Type: application/json\" -u onos:rocks -d @/opt/xos/synchronizers/vspgwc/flow_enb.json http://{{ onos_ip }}:8182/onos/v1/flows/{{ enb_sw_id }} ; curl -X POST -H \"Content-Type: application/json\" -u onos:rocks -d @/opt/xos/synchronizers/vspgwc/flow_spgwu.json http://{{ onos_ip }}:8182/onos/v1/flows/{{ spgwu_sw_id }}"

- name: make json file for flow of enb to tunnel
  when: enb_sw_ip != spgwu_sw_ip
  template:
    src=flow_enb_tnl.json.j2
    dest=/opt/xos/synchronizers/vspgwc/flow_enb_tnl.json owner=root group=root mode=755

- name: make json file for flow of spgwu to tunnel
  when: enb_sw_ip != spgwu_sw_ip
  template:
    src=flow_spgwu_tnl.json.j2
    dest=/opt/xos/synchronizers/vspgwc/flow_spgwu_tnl.json owner=root group=root mode=755

- name: set flow for enb and spgwu through tunnel to ONOS
  when: enb_sw_ip != spgwu_sw_ip
  shell: "curl -X POST -H \"Content-Type: application/json\" -u onos:rocks -d @/opt/xos/synchronizers/vspgwc/flow_enb_tnl.json http://{{ onos_ip }}:8182/onos/v1/flows/{{ spgwu_sw_id }} ; curl -X POST -H \"Content-Type: application/json\" -u onos:rocks -d @/opt/xos/synchronizers/vspgwc/flow_spgwu_tnl.json http://{{ onos_ip }}:8182/onos/v1/flows/{{ enb_sw_id }}"
