[CORD-2138] add rule setup parts to ONOS with Ansible playbook

Change-Id: I803a186d202357264a296bad98d4634bcbfe1879
(cherry picked from commit e1a679e7ceb17b8ff9c8aa66b3fc6b75d2dc3f61)
diff --git a/xos/synchronizer/steps/roles/interface_config/templates/cp_config.cfg.j2 b/xos/synchronizer/steps/roles/interface_config/templates/cp_config.cfg.j2
index 33e5413..00101ae 100644
--- a/xos/synchronizer/steps/roles/interface_config/templates/cp_config.cfg.j2
+++ b/xos/synchronizer/steps/roles/interface_config/templates/cp_config.cfg.j2
@@ -3,7 +3,7 @@
 S11_SGW_IP={{ s11_sgw_ip }}
 S11_MME_IP={{ s11_mme_ip }}
 S1U_SGW_IP={{ s1u_sgw_ip }}
-IP_POOL_IP=16.0.0.0
-IP_POOL_MASK=255.0.0.0
+IP_POOL_IP={{ ip_addr_pool }}
+IP_POOL_MASK={{ ip_pool_mask }}
 APN=apn1
 MEMORY=1024
diff --git a/xos/synchronizer/steps/roles/onos_rule_setup/tasks/main.yml b/xos/synchronizer/steps/roles/onos_rule_setup/tasks/main.yml
new file mode 100644
index 0000000..46f43c4
--- /dev/null
+++ b/xos/synchronizer/steps/roles/onos_rule_setup/tasks/main.yml
@@ -0,0 +1,61 @@
+# 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: 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 }}"
+
+- 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 }}"
diff --git a/xos/synchronizer/steps/roles/onos_rule_setup/templates/flow_enb.json.j2 b/xos/synchronizer/steps/roles/onos_rule_setup/templates/flow_enb.json.j2
new file mode 100644
index 0000000..a08e3da
--- /dev/null
+++ b/xos/synchronizer/steps/roles/onos_rule_setup/templates/flow_enb.json.j2
@@ -0,0 +1,27 @@
+{
+   "deviceId":"{{ enb_sw_id }}",
+   "isPermanent":true,
+   "priority":6001,
+   "selector":{
+      "criteria":[
+         {
+            "ethType":"0x800",
+            "type":"ETH_TYPE"
+         },
+         {
+            "ip":"{{ ip_addr_pool }}/{{ ip_pool_mask_int }}",
+            "type":"IPV4_SRC"
+         }
+      ]
+   },
+   "tableId":0,
+   "timeout":0,
+   "treatment":{
+      "instructions":[
+         {
+            "port":"{{ as_sgi_port }}",
+            "type":"OUTPUT"
+         }
+      ]
+   }
+}
\ No newline at end of file
diff --git a/xos/synchronizer/steps/roles/onos_rule_setup/templates/flow_spgwu.json.j2 b/xos/synchronizer/steps/roles/onos_rule_setup/templates/flow_spgwu.json.j2
new file mode 100644
index 0000000..470b753
--- /dev/null
+++ b/xos/synchronizer/steps/roles/onos_rule_setup/templates/flow_spgwu.json.j2
@@ -0,0 +1,27 @@
+{
+   "deviceId":"{{ spgwu_sw_id }}",
+   "isPermanent":true,
+   "priority":6001,
+   "selector":{
+      "criteria":[
+         {
+            "ethType":"0x800",
+            "type":"ETH_TYPE"
+         },
+         {
+            "ip":"{{ ip_addr_pool }}/{{ ip_pool_mask_int }}",
+            "type":"IPV4_DST"
+         }
+      ]
+   },
+   "tableId":0,
+   "timeout":0,
+   "treatment":{
+      "instructions":[
+         {
+            "port":"{{ spgwu_sgi_port }}",
+            "type":"OUTPUT"
+         }
+      ]
+   }
+}
\ No newline at end of file
diff --git a/xos/synchronizer/steps/roles/onos_rule_setup/templates/onos_rule_setup_info.sh.j2 b/xos/synchronizer/steps/roles/onos_rule_setup/templates/onos_rule_setup_info.sh.j2
new file mode 100644
index 0000000..b52f281
--- /dev/null
+++ b/xos/synchronizer/steps/roles/onos_rule_setup/templates/onos_rule_setup_info.sh.j2
@@ -0,0 +1,150 @@
+#!/bin/bash
+
+#variable
+CMD=$1
+OPTION=$2
+
+SGI_AS={{ sgi_as_ip }}
+SGI_SPGWU={{ sgi_spgwu_ip }}
+UE_IP_POOL={{ ip_addr_pool }}
+UE_NETMASK={{ ip_pool_mask }}
+ONOS_ID="karaf"
+ONOS_PW="karaf"
+
+function get_head_node {
+	netstat -nr | awk '{if ($1 == "0.0.0.0") print $2}'
+}
+
+function get_hosts {
+	echo $(curl $(get_head_node)/vtn/onos/v1/hosts -u $ONOS_ID:$ONOS_PW)
+}
+
+function get_host {
+	INDEX=$1
+	get_hosts | awk '{split($0, RESULTS, "},{"); print RESULTS['$INDEX']}'
+}
+
+function num_hosts {
+	get_hosts | awk -F },{ '{print NF}'
+}
+
+function get_port_number {
+	IP_ADDR=$1
+	NUM_HOSTS=$(num_hosts)
+	for INDEX in `seq 1 $NUM_HOSTS`
+	do
+		TMP_IP_ADDR=$(get_host $INDEX | awk -F "," '{split($5, IP_RAW_RESULT, ":") ; print substr(IP_RAW_RESULT[2], 3, length(IP_RAW_RESULT[2])-4)}')
+		TMP_PORT_NUMBER=$(get_host $INDEX | awk -F "," '{split($7, PORT_RAW_RESULT, ":"); print substr(PORT_RAW_RESULT[2], 2, length(PORT_RAW_RESULT[2])-3)}')
+		if [ "$IP_ADDR" = "$TMP_IP_ADDR" ];
+		then
+			echo $TMP_PORT_NUMBER
+		fi
+	done
+}
+
+function get_mac_address {
+	IP_ADDR=$1
+	NUM_HOSTS=$(num_hosts)
+	for INDEX in `seq 1 $NUM_HOSTS`
+	do
+		TMP_IP_ADDR=$(get_host $INDEX | awk -F "," '{split($5, IP_RAW_RESULT, ":") ; print substr(IP_RAW_RESULT[2], 3, length(IP_RAW_RESULT[2])-4)}')
+		TMP_MAC_ADDR=$(get_host $INDEX | awk -F "," '{split($2, MAC_RAW_RESULT, "\":\"") ; print substr(MAC_RAW_RESULT[2], 1, length(MAC_RAW_RESULT[2])-1)}')
+		if [ "$IP_ADDR" = "$TMP_IP_ADDR" ];
+		then
+			echo $TMP_MAC_ADDR | tr '[A-Z]' '[a-z]'
+		fi
+	done
+}
+
+function get_switch_id {
+	IP_ADDR=$1
+	NUM_HOSTS=$(num_hosts)
+	for INDEX in `seq 1 $NUM_HOSTS`
+	do
+		TMP_IP_ADDR=$(get_host $INDEX | awk -F "," '{split($5, IP_RAW_RESULT, ":") ; print substr(IP_RAW_RESULT[2], 3, length(IP_RAW_RESULT[2])-4)}')
+		TMP_SWITCH_ID=$(get_host $INDEX | awk -F "," '{split($6, SW_RAW_RESULT, "\":\"") ; print substr(SW_RAW_RESULT[2], 1, length(SW_RAW_RESULT[2])-1)}')
+		if [ "$IP_ADDR" = "$TMP_IP_ADDR" ];
+		then
+			echo $TMP_SWITCH_ID
+		fi
+	done
+}
+
+
+function print_help {
+	echo "**Options**"
+	print_help_port
+	print_help_mac
+	print_help_ip
+	print_help_switch_id
+}
+
+function print_help_port {
+	echo "--port|-p <arg>: get OVS port number for <arg> device, one of {enb, spgwu}"
+}
+
+function print_help_mac {
+	echo "--mac|-m <arg>: get MAC address for <arg> device, one of {enb, spgwu}"
+}
+
+function print_help_ip {
+	echo "--ip|-i <arg>: get IP address for <arg> device, one of {enb, spgwu, onos}"
+}
+
+function print_help_switch_id {
+	echo "--sw|-s <arg>: get switch ID for <arg> device, one of {enb, spgwu}"
+}
+
+if [ "$CMD" == "-h" ] || [ "$CMD" == "--help" ]; then
+	print_help
+elif [ "$CMD" == "-p" ] || [ "$CMD" == "--port" ]; then
+	if [ "$OPTION" == "enb" ]; then
+		get_port_number $SGI_AS > enb_port
+		cat enb_port
+	elif [ "$OPTION" == "spgwu" ]; then
+		get_port_number $SGI_SPGWU > spgwu_port
+		cat spgwu_port
+	else
+		echo "Wrong argument"
+		print_help_port
+	fi
+elif [ "$CMD" == "-m" ] || [ "$CMD" == "--mac" ]; then
+	if [ "$OPTION" == "enb" ]; then
+		get_mac_address $SGI_AS > enb_mac
+		cat enb_mac
+	elif [ "$OPTION" == "spgwu" ]; then
+		get_mac_address $SGI_SPGWU > spgwu_mac
+		cat spgwu_mac
+	else
+		echo "Wrong argument"
+		print_help_mac
+	fi
+elif [ "$CMD" == "-i" ] || [ "$CMD" == "--ip" ]; then
+	if [ "$OPTION" == "enb" ]; then
+		echo $SGI_AS > enb_ip
+		cat enb_ip
+	elif [ "$OPTION" == "spgwu" ]; then
+		echo $SGI_SPGWU > spgwu_ip
+		cat spgwu_ip
+	elif [ "$OPTION" == "onos" ]; then
+		get_head_node > onos_ip
+		cat onos_ip
+	else
+		echo "Wrong argument"
+		print_help_ip
+	fi
+elif [ "$CMD" == "-s" ] || [ "$CMD" == "--sw" ]; then
+	if [ "$OPTION" == "enb" ]; then
+		get_switch_id $SGI_AS > enb_sw_id
+		cat enb_sw_id
+	elif [ "$OPTION" == "spgwu" ]; then
+		get_switch_id $SGI_SPGWU > spgwu_sw_id
+		cat spgwu_sw_id
+	else
+	    echo "Wrong argument"
+		print_help_switch_id
+	fi
+else
+	echo "Wrong command"
+	print_help
+fi
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_vspgwctenant.py b/xos/synchronizer/steps/sync_vspgwctenant.py
index 0c90978..0ca473c 100644
--- a/xos/synchronizer/steps/sync_vspgwctenant.py
+++ b/xos/synchronizer/steps/sync_vspgwctenant.py
@@ -14,6 +14,7 @@
 
 import os
 import sys
+import time
 from django.db.models import Q, F
 from synchronizers.new_base.modelaccessor import *
 from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
@@ -39,14 +40,14 @@
 
         scenario = self.get_scenario()
 
-        if scenario == 'ng4t_with_sdncontroller':
-            return self.get_values_for_ng4t_w_sdncontroller()
-        elif scenario == 'ng4t_without_sdncontroller':
-            return self.get_values_for_ng4t_wo_sdncontroller()
-        elif scenario == 'spirent_with_sdncontroller':
-            return self.get_values_for_spirent_w_sdncontroller()
-        elif scenario == 'spirent_without_sdncontroller':
-            return self.get_values_for_spirent_wo_sdncontroller()
+        if scenario == 'normal_scenario':
+            return self.get_values_for_normal_scenario()
+        elif scenario == 'normal_scenario_without_sdncontroller':
+            return self.get_values_for_normal_scenario_wo_sdncontroller()
+        elif scenario == 'emulator_scenario':
+            return self.get_values_for_emulator_scenario()
+        elif scenario == 'emulator_scenario_without_sdncontroller':
+            return self.get_values_for_emulator_scenario_wo_sdncontroller()
         else:
             return self.get_extra_attributes_for_manual()
 
@@ -67,11 +68,15 @@
         fields['s11_mme_ip'] = "manual"
         fields['s1u_sgw_ip'] = "manual"
 
+        # for rules setup in ONOS
+        fields['sgi_as_ip'] = "manual"
+        fields['sgi_spgwu_ip'] = "manual"
+
         return fields
 
-    def get_values_for_ng4t_w_sdncontroller(self):
+    def get_values_for_normal_scenario(self):
         fields = {}
-        fields['scenario'] = "ng4t_with_sdncontroller"
+        fields['scenario'] = "normal_scenario"
         # for interface.cfg file
         fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
         fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
@@ -85,11 +90,15 @@
         fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
         fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
 
+        # for rules setup in ONOS
+        fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
+        fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
+
         return fields
 
-    def get_values_for_ng4t_wo_sdncontroller(self):
+    def get_values_for_normal_scenario_wo_sdncontroller(self):
         fields = {}
-        fields['scenario'] = "ng4t_without_sdncontroller"
+        fields['scenario'] = "normal_scenario_without_sdncontroller"
         # for interface.cfg file
         fields['zmq_sub_ip'] = "127.0.0.1"
         fields['zmq_pub_ip'] = "127.0.0.1"
@@ -103,11 +112,15 @@
         fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
         fields['s11_mme_ip'] = self.get_ip_address('s11_network', VMMETenant, 's11_mme_ip')
 
+        # for rules setup in ONOS
+        fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
+        fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
+
         return fields
 
-    def get_values_for_spirent_w_sdncontroller(self):
+    def get_values_for_emulator_scenario(self):
         fields = {}
-        fields['scenario'] = "spirent_with_sdncontroller"
+        fields['scenario'] = "emulator_scenario"
         # for interface.cfg file
         fields['zmq_sub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_sub_ip')
         fields['zmq_pub_ip'] = self.get_ip_address('sbi_network', SDNControllerServiceInstance, 'zmq_pub_ip')
@@ -121,11 +134,15 @@
         fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
         fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
 
+        # for rules setup in ONOS
+        fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
+        fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
+
         return fields
 
-    def get_values_for_spirent_wo_sdncontroller(self):
+    def get_values_for_emulator_scenario_wo_sdncontroller(self):
         fields = {}
-        fields['scenario'] = "spirent_without_sdncontroller"
+        fields['scenario'] = "emulator_scenario_without_sdncontroller"
         # for interface.cfg file
         fields['zmq_sub_ip'] = "127.0.0.1"
         fields['zmq_pub_ip'] = "127.0.0.1"
@@ -139,6 +156,10 @@
         fields['s1u_sgw_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_sgw_ip')
         fields['s11_mme_ip'] = self.get_ip_address('s11_network', VENBServiceInstance, 's11_mme_ip')
 
+        # for rules setup in ONOS
+        fields['sgi_as_ip'] = self.get_ip_address('sgi_network', VENBServiceInstance, 'sgi_as_ip')
+        fields['sgi_spgwu_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_spgwu_ip')
+
         return fields
 
 
@@ -193,7 +214,6 @@
         return True
 
 
-    # Which scenario does it use among Spirent or NG4T?
     def get_scenario(self):
         # try get vENB instance: one of both Spirent and NG4T
         venb_flag = self.has_venb()
@@ -202,31 +222,46 @@
         vspgwu_flag = self.has_vspgwu()
         internetemulator_flag = self.has_internetemulator()
 
+        # wait until vspgwu and env are comming up
+        while (not vspgwu_flag):
+            print "wait -- vSPGWU has not been comming up"
+            time.sleep(1)
+            vspgwu_flag = self.has_vspgwu()
+
+        while (not venb_flag):
+            print "wait -- vENB has not been comming up"
+            time.sleep(1)
+            venb_flag = self.has_venb()
+
         if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
-            return 'ng4t_with_sdncontroller'
+            return 'normal_scenario'
 
         if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag:
-            return 'ng4t_without_sdncontroller'
+            return 'normal_scenario_without_sdncontroller'
 
         if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
-            return 'spirent_with_sdncontroller'
+            return 'emulator_scenario'
 
         if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (not internetemulator_flag):
-            return 'spirent_without_sdncontroller'
+            return 'emulator_scenario_without_sdncontroller'
 
         return 'manual'
 
     # To get IP address
     def get_ip_address(self, network_name, service_instance, parameter):
 
-        try:
-            net_id = self.get_network_id(network_name)
-            ins_id = self.get_instance_id(service_instance)
-            ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
+        condition = False
 
-        except Exception:
-            ip_address = "error"
-            print "get failed -- %s" % (parameter)
+        while (not condition):
+            try:
+                net_id = self.get_network_id(network_name)
+                ins_id = self.get_instance_id(service_instance)
+                ip_address = Port.objects.get(network_id=net_id, instance_id=ins_id).ip
+                condition = True
+            except Exception:
+                ip_address = "error"
+                print "get failed -- %s" % (parameter)
+                time.sleep(1)
 
         return ip_address
 
diff --git a/xos/synchronizer/steps/vspgwctenant_playbook.yaml b/xos/synchronizer/steps/vspgwctenant_playbook.yaml
index 7dd9fd1..fc42ef6 100644
--- a/xos/synchronizer/steps/vspgwctenant_playbook.yaml
+++ b/xos/synchronizer/steps/vspgwctenant_playbook.yaml
@@ -33,7 +33,33 @@
     - s1u_sgw_ip: {{ s1u_sgw_ip }}
     - scenario: {{ scenario }}
     - cpu_hexacode: "0x0e"
-
+    - sgi_as_ip: {{ sgi_as_ip }}
+    - sgi_spgwu_ip: {{ sgi_spgwu_ip }}
+    - ip_addr_pool: "16.0.0.0"
+    - ip_pool_mask: "255.0.0.0"
+    - ip_pool_mask_int: "8"
   roles:
     - interface_config
     - build_and_run
+
+- hosts: localhost
+  vars:
+    - source_path: /root/ngic
+    - zmq_sub_ip: {{ zmq_sub_ip }}
+    - zmq_pub_ip: {{ zmq_pub_ip }}
+    - dp_comm_ip: {{ dp_comm_ip }}
+    - cp_comm_ip: {{ cp_comm_ip }}
+    - fpc_ip: {{ fpc_ip }}
+    - cp_nb_server_ip: {{ cp_nb_server_ip }}
+    - s11_sgw_ip: {{  s11_sgw_ip }}
+    - s11_mme_ip: {{ s11_mme_ip }}
+    - s1u_sgw_ip: {{ s1u_sgw_ip }}
+    - scenario: {{ scenario }}
+    - cpu_hexacode: "0x0e"
+    - sgi_as_ip: {{ sgi_as_ip }}
+    - sgi_spgwu_ip: {{ sgi_spgwu_ip }}
+    - ip_addr_pool: "16.0.0.0"
+    - ip_pool_mask: "255.0.0.0"
+    - ip_pool_mask_int: "8"
+  roles:
+      - onos_rule_setup
\ No newline at end of file