[CORD-1977] add ansible-playbook file to make interface.cfg and dp_config.cfg files and to build it

Change-Id: Ifa58ef6e414ff63e6c8006dc6c9a312cbb626069
(cherry picked from commit 0e147402f29a3719d5a34635897563aaf71747c6)
diff --git a/xos/synchronizer/steps/roles/interface_config/tasks/main.yml b/xos/synchronizer/steps/roles/interface_config/tasks/main.yml
new file mode 100644
index 0000000..f7b3321
--- /dev/null
+++ b/xos/synchronizer/steps/roles/interface_config/tasks/main.yml
@@ -0,0 +1,73 @@
+# 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: resolve sudo timeout
+  become: yes
+  shell: "echo 127.0.1.1       $(cat /etc/hostname) >> /etc/hosts"
+
+- name: make interface config file
+  become: yes
+  template:
+    src=interface.cfg.j2
+    dest="{{source_path}}/config/interface.cfg" owner=root group=root mode=755
+
+- name: get date
+  shell: date +"%Y-%m-%d_%H-%M"
+  register: tmp_date
+
+- name: get port_0 value
+  shell: lspci -v -k | grep -i 'ethernet' | head -n 1 | tail -n 1 | awk '{print "0000:"$1}'
+  register: tmp_port_0
+
+- name: get port_1 value
+  shell: lspci -v -k | grep -i 'ethernet' | head -n 2 | tail -n 1 | awk '{print "0000:"$1}'
+  register: tmp_port_1
+
+- name: get s1u_mac value
+  shell: "ifconfig | grep -B 2 {{ s1u_ip }} | grep HWaddr | awk \'{print $5}\'"
+  register: tmp_s1u_mac
+
+- name: get sgi_mac value
+  shell: "ifconfig | grep -B 2 {{ sgi_ip }} | grep HWaddr | awk \'{print $5}\'"
+  register: tmp_sgi_mac
+
+- name: assign results into each variable
+  set_fact:
+    port_0: "{{ tmp_port_0.stdout }}"
+    port_1: "{{ tmp_port_1.stdout }}"
+    s1u_mac: "{{ tmp_s1u_mac.stdout }}"
+    sgi_mac: "{{ tmp_sgi_mac.stdout }}"
+    date: "{{ tmp_date.stdout }}"
+
+- name: make dp_config config file
+  become: yes
+  template:
+    src=dp_config.cfg.j2
+    dest="{{source_path}}/config/dp_config.cfg" owner=root group=root mode=755
+
+#- name: Build ngic
+#  become: yes
+#  shell: "{{ item }}"
+#  args:
+#    chdir: "{{ source_path }}"
+#  environment:
+#    NG_CORE: "{{ source_path }}"
+#    RTE_SDK: "{{ source_path }}/dpdk"
+#    RTE_TARGET: "x86_64-native-linuxapp-gcc"
+#    HYPERSCANDIR: "/root/ngic/hyperscan-4.1.0"
+#  with_items:
+#    - make
+#    - /root/ngic/dp/run.sh -c 1
diff --git a/xos/synchronizer/steps/roles/interface_config/templates/dp_config.cfg.j2 b/xos/synchronizer/steps/roles/interface_config/templates/dp_config.cfg.j2
new file mode 100644
index 0000000..915c875
--- /dev/null
+++ b/xos/synchronizer/steps/roles/interface_config/templates/dp_config.cfg.j2
@@ -0,0 +1,21 @@
+NOW={{ date }}
+FILE="logs/dp_{{ date }}.log"
+PORT0={{ port_0 }}
+PORT1={{ port_1 }}
+
+S1U_IP={{ s1u_ip }}
+S1U_MAC={{ s1u_mac }}
+
+SGI_IP={{ sgi_ip }}
+SGI_MAC={{ sgi_mac }}
+
+NUM_WORKER=1
+MEMORY=4096
+
+
+#Optional:
+#SGI_GW_IP=13.1.1.110
+#SGI_MASK=255.255.0.0
+
+#S1U_GW_IP=11.1.1.101
+#S1U_MASK=255.255.0.0
diff --git a/xos/synchronizer/steps/roles/interface_config/templates/interface.cfg.j2 b/xos/synchronizer/steps/roles/interface_config/templates/interface.cfg.j2
new file mode 100644
index 0000000..6fb91d1
--- /dev/null
+++ b/xos/synchronizer/steps/roles/interface_config/templates/interface.cfg.j2
@@ -0,0 +1,51 @@
+; 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.
+
+; scenario: {{ scenario }}
+
+[0]
+; please refer to INSTALL.MD for complete description of these parameters
+zmq_protocol = tcp
+
+; zmq publisher and subscriber ip:port. To be configured to an available ip
+; on the FPC Host. The port values are defined by the FPC Project.
+; These values are unused when DSDN_ODL_BUILD CFLAG not defined in
+; ng-core_cfg.mk
+zmq_sub_ip = {{ zmq_sub_ip }}
+zmq_sub_port = 5560
+zmq_pub_ip = {{ zmq_pub_ip }}
+zmq_pub_port = 5559
+
+; DP and CP communication ip:port: To be configured to available ip:ports on
+; the DP and CP Hosts respectively. Used for messages to communicate over UDP
+; including table creation, table entries; and when DSDN_ODL_BUILD CFLAG is NOT
+; defined, session establishment, modification, deletion, etc.
+dp_comm_ip = {{ dp_comm_ip }}
+dp_comm_port = 20
+cp_comm_ip = {{ cp_comm_ip }}
+cp_comm_port = 21
+
+; FPC ip:port: To be configured to an available IP of FPC host. The fpc_port
+; is defined by the FPC project.
+; These values are unused when DSDN_ODL_BUILD CFLAG not defined in
+; ng-core_cfg.mk
+fpc_ip = {{ fpc_ip }}
+fpc_port = 8181
+
+; The CP Northbound server ip:port: May be configured to any available ip:port
+; on the CP Host.
+; These values are unused when DSDN_ODL_BUILD CFLAG not defined in
+; ng-core_cfg.mk
+cp_nb_server_ip = {{ cp_nb_server_ip }}
+cp_nb_server_port = 9997
diff --git a/xos/synchronizer/steps/sync_vspgwutenant.py b/xos/synchronizer/steps/sync_vspgwutenant.py
index 4f5d9f2..c6083a8 100644
--- a/xos/synchronizer/steps/sync_vspgwutenant.py
+++ b/xos/synchronizer/steps/sync_vspgwutenant.py
@@ -15,7 +15,6 @@
 import os
 import sys
 from django.db.models import Q, F
-#from services.vspgwu.models import VSPGWUService, VSPGWUTenant
 from synchronizers.new_base.modelaccessor import *
 from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
 
@@ -38,3 +37,200 @@
         super(SyncVSPGWUTenant, self).__init__(*args, **kwargs)
 
 
+    def get_extra_attributes(self, o):
+
+        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()
+        else:
+            return self.get_extra_attributes_for_manual()
+
+    # fields for manual case
+    def get_extra_attributes_for_manual(self):
+        fields = {}
+        fields['scenario'] = self.get_scenario()
+        # for interface.cfg file
+        fields['zmq_sub_ip'] = "manual"
+        fields['zmq_pub_ip'] = "manual"
+        fields['dp_comm_ip'] = "manual"
+        fields['cp_comm_ip'] = "manual"
+        fields['fpc_ip'] = "manual"
+        fields['cp_nb_server_ip'] = "manual"
+
+        # for dp_config.cfg file
+        fields['s1u_ip'] = "manual"
+        fields['sgi_ip'] = "manual"
+
+        return fields
+
+    def get_values_for_ng4t_w_sdncontroller(self):
+        fields = {}
+        fields['scenario'] = "ng4t_with_sdncontroller"
+        # 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')
+        fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
+        fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
+        fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
+        fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
+
+        # for dp_config.cfg file
+        fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip')
+        fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip')
+
+        return fields
+
+    def get_values_for_ng4t_wo_sdncontroller(self):
+        fields = {}
+        fields['scenario'] = "ng4t_without_sdncontroller"
+        # for interface.cfg file
+        fields['zmq_sub_ip'] = "127.0.0.1"
+        fields['zmq_pub_ip'] = "127.0.0.1"
+        fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
+        fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
+        fields['fpc_ip'] = "127.0.0.1"
+        fields['cp_nb_server_ip'] = "127.0.0.1"
+
+        # for cp_config.cfg file
+        fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip')
+        fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip')
+
+        return fields
+
+    def get_values_for_spirent_w_sdncontroller(self):
+        fields = {}
+        fields['scenario'] = "ng4t_with_sdncontroller"
+        # 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')
+        fields['dp_comm_ip'] = self.get_ip_address('sbi_network', VSPGWUTenant, 'dp_comm_ip')
+        fields['cp_comm_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_comm_ip')
+        fields['fpc_ip'] = self.get_ip_address('nbi_network', SDNControllerServiceInstance, 'fpc_ip')
+        fields['cp_nb_server_ip'] = self.get_ip_address('nbi_network', VSPGWCTenant, 'cp_nb_server_ip')
+
+        # for dp_config.cfg file
+        fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip')
+        fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip')
+
+        return fields
+
+    def get_values_for_spirent_wo_sdncontroller(self):
+        fields = {}
+        fields['scenario'] = "ng4t_without_sdncontroller"
+        # for interface.cfg file
+        fields['zmq_sub_ip'] = "127.0.0.1"
+        fields['zmq_pub_ip'] = "127.0.0.1"
+        fields['dp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWUTenant, 'dp_comm_ip')
+        fields['cp_comm_ip'] = self.get_ip_address('spgw_network', VSPGWCTenant, 'cp_comm_ip')
+        fields['fpc_ip'] = "127.0.0.1"
+        fields['cp_nb_server_ip'] = "127.0.0.1"
+
+        # for cp_config.cfg file
+        fields['s1u_ip'] = self.get_ip_address('s1u_network', VSPGWUTenant, 's1u_ip')
+        fields['sgi_ip'] = self.get_ip_address('sgi_network', VSPGWUTenant, 'sgi_ip')
+
+        return fields
+
+    def has_venb(self):
+        # try get vMME instance
+        try:
+            instance_id = self.get_instance_id(VENBServiceInstance)
+        except Exception:
+            print 'cannot get VENBServiceInstance'
+            return False
+
+        return True
+
+    def has_vmme(self):
+        # try get vMME instance
+        try:
+            instance_id = self.get_instance_id(VMMETenant)
+        except Exception:
+            print 'cannot get VMMETenant'
+            return False
+
+        return True
+
+    def has_sdncontroller(self):
+        # try get vMME instance
+        try:
+            instance_id = self.get_instance_id(SDNControllerServiceInstance)
+        except Exception:
+            print 'cannot get SDNControllerServiceInstance'
+            return False
+
+        return True
+
+    def has_vspgwu(self):
+        # try get vMME instance
+        try:
+            instance_id = self.get_instance_id(VSPGWUTenant)
+        except Exception:
+            print 'cannot get VSPGWUTenant'
+            return False
+
+        return True
+
+    def has_internetemulator(self):
+        # try get vMME instance
+        try:
+            instance_id = self.get_instance_id(InternetEmulatorServiceInstance)
+        except Exception:
+            print 'cannot get InternetEmulatorServiceInstance'
+            return False
+
+        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()
+        vmme_flag = self.has_vmme()
+        sdncontroller_flag = self.has_sdncontroller()
+        vspgwu_flag = self.has_vspgwu()
+        internetemulator_flag = self.has_internetemulator()
+
+        if vmme_flag and venb_flag and sdncontroller_flag and vspgwu_flag and internetemulator_flag:
+            return 'ng4t_with_sdncontroller'
+
+        if vmme_flag and venb_flag and (not sdncontroller_flag) and vspgwu_flag and internetemulator_flag:
+            return 'ng4t_without_sdncontroller'
+
+        if (not vmme_flag) and venb_flag and sdncontroller_flag and vspgwu_flag and (not internetemulator_flag):
+            return 'spirent_with_sdncontroller'
+
+        if (not vmme_flag) and venb_flag and (not sdncontroller_flag) and vspgwu_flag and (
+        not internetemulator_flag):
+            return 'spirent_without_sdncontroller'
+
+        return 'manual'
+
+    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
+
+        except Exception:
+            ip_address = "error"
+            print "get failed -- %s" % (parameter)
+
+        return ip_address
+
+    # To get each network id
+    def get_network_id(self, network_name):
+        return Network.objects.get(name=network_name).id
+
+    # To get service_instance (assumption: there is a single instance for each service)
+    def get_instance_id(self, serviceinstance):
+        instances = serviceinstance.objects.all()
+        instance_id = instances[0].instance_id
+        return instance_id
\ No newline at end of file
diff --git a/xos/synchronizer/steps/vspgwutenant_playbook.yaml b/xos/synchronizer/steps/vspgwutenant_playbook.yaml
index 05c13c1..b15a985 100644
--- a/xos/synchronizer/steps/vspgwutenant_playbook.yaml
+++ b/xos/synchronizer/steps/vspgwutenant_playbook.yaml
@@ -14,10 +14,24 @@
 
 ---
 - hosts: {{ instance_name }} 
-  gather_facts: False 
+  gather_facts: False
   connection: ssh 
-  user: ubuntu 
-  sudo: yes 
-  tasks: 
+  remote_user: ubuntu
+  become: yes
+  become_user: root
+  become_method: sudo
   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 }}
+    - s1u_ip: {{ s1u_ip }}
+    - sgi_ip: {{ sgi_ip }}
+    - scenario: {{ scenario }}
+
+  roles:
+    - interface_config
+