[CORD-1969] VSGWC migration from 3.0 to 4.1

Change-Id: I7c33f351e494ed8a0ba18bf2a0fbf2447442cf95
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..859575e
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,39 @@
+# xosproject/vsgwc-synchronizer
+FROM xosproject/xos-synchronizer-base:candidate
+
+COPY . /opt/xos/synchronizers/vsgwc
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/vsgwc"
+
+# Label image
+ARG org_label_schema_schema_version=1.0
+ARG org_label_schema_name=vsgwc-synchronizer
+ARG org_label_schema_version=unknown
+ARG org_label_schema_vcs_url=unknown
+ARG org_label_schema_vcs_ref=unknown
+ARG org_label_schema_build_date=unknown
+ARG org_opencord_vcs_commit_date=unknown
+ARG org_opencord_component_chameleon_version=unknown
+ARG org_opencord_component_chameleon_vcs_url=unknown
+ARG org_opencord_component_chameleon_vcs_ref=unknown
+ARG org_opencord_component_xos_version=unknown
+ARG org_opencord_component_xos_vcs_url=unknown
+ARG org_opencord_component_xos_vcs_ref=unknown
+
+LABEL org.label-schema.schema-version=$org_label_schema_schema_version \
+      org.label-schema.name=$org_label_schema_name \
+      org.label-schema.version=$org_label_schema_version \
+      org.label-schema.vcs-url=$org_label_schema_vcs_url \
+      org.label-schema.vcs-ref=$org_label_schema_vcs_ref \
+      org.label-schema.build-date=$org_label_schema_build_date \
+      org.opencord.vcs-commit-date=$org_opencord_vcs_commit_date \
+      org.opencord.component.chameleon.version=$org_opencord_component_chameleon_version \
+      org.opencord.component.chameleon.vcs-url=$org_opencord_component_chameleon_vcs_url \
+      org.opencord.component.chameleon.vcs-ref=$org_opencord_component_chameleon_vcs_ref \
+      org.opencord.component.xos.version=$org_opencord_component_xos_version \
+      org.opencord.component.xos.vcs-url=$org_opencord_component_xos_vcs_url \
+      org.opencord.component.xos.vcs-ref=$org_opencord_component_xos_vcs_ref
+
+CMD bash -c "cd /opt/xos/synchronizers/vsgwc; ./run-from-api.sh"
\ No newline at end of file
diff --git a/xos/synchronizer/manifest b/xos/synchronizer/manifest
deleted file mode 100644
index 04700fd..0000000
--- a/xos/synchronizer/manifest
+++ /dev/null
@@ -1,8 +0,0 @@
-model-deps
-manifest
-run.sh
-stop.sh
-vsgw-synchronizer.py
-vsgw_config
-steps/sync_vsgw.py
-steps/sync_vsgw.yaml
diff --git a/xos/synchronizer/stop.sh b/xos/synchronizer/run-from-api.sh
old mode 100644
new mode 100755
similarity index 91%
rename from xos/synchronizer/stop.sh
rename to xos/synchronizer/run-from-api.sh
index 4834de9..94a37fb
--- a/xos/synchronizer/stop.sh
+++ b/xos/synchronizer/run-from-api.sh
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
-# Kill the observer
-pkill -9 -f vsgw-synchronizer.py
+python vsgwc-synchronizer.py
diff --git a/xos/synchronizer/run.sh b/xos/synchronizer/steps/roles/ngic_config/tasks/main.yml
similarity index 68%
copy from xos/synchronizer/run.sh
copy to xos/synchronizer/steps/roles/ngic_config/tasks/main.yml
index 48db7ca..f6e9b8c 100644
--- a/xos/synchronizer/run.sh
+++ b/xos/synchronizer/steps/roles/ngic_config/tasks/main.yml
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,7 +12,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+---
+- name: create config directory for cp_config file
+  file:
+    path: /var/tmp/config
+    state: directory
+    mode: 0755
 
-# Runs the XOS observer using vsgw_config
-export XOS_DIR=/opt/xos
-python vsgw-synchronizer.py  -C $XOS_DIR/synchronizers/vsgw/vsgw_config
+- name: save control plane configuration information in vSGWC VM
+  template:
+    src=cp_config.cfg.j2
+    dest=/var/tmp/config/cp_config.cfg
diff --git a/xos/synchronizer/steps/roles/ngic_config/templates/cp_config.cfg.j2 b/xos/synchronizer/steps/roles/ngic_config/templates/cp_config.cfg.j2
new file mode 100644
index 0000000..f6bb725
--- /dev/null
+++ b/xos/synchronizer/steps/roles/ngic_config/templates/cp_config.cfg.j2
@@ -0,0 +1,10 @@
+{% set date = ansible_date_time['date'] ~ '_' ~ ansible_date_time['hour'] ~ '_' ~ ansible_date_time['minute'] %}
+NOW={{ date }}
+FILE="logs/cp_{{ date }}.log"
+S11_SGW_IP={{ sgwc_shared_ip }}
+S11_MME_IP={{ mme_shared_ip }}
+S1U_SGW_IP={{ sgwu_shared_ip }}
+IP_POOL_IP=16.0.0.0
+IP_POOL_MASK=255.0.0.0
+APN=apn1
+MEMORY=1024
diff --git a/xos/synchronizer/steps/sync_vsgw.py b/xos/synchronizer/steps/sync_vsgw.py
deleted file mode 100644
index 2f16a88..0000000
--- a/xos/synchronizer/steps/sync_vsgw.py
+++ /dev/null
@@ -1,57 +0,0 @@
-
-# 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.
-
-
-import os
-import sys
-from django.db.models import Q, F
-from services.vsgw.models import VSGWService, VSGWTenant
-from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
-
-parentdir = os.path.join(os.path.dirname(__file__), "..")
-sys.path.insert(0, parentdir)
-
-class SyncVSGWTenant(SyncInstanceUsingAnsible):
-
-    provides = [VSGWTenant]
-
-    observes = VSGWTenant
-
-    requested_interval = 0
-
-    template_name = "sync_vsgw.yaml"
-
-    service_key_name = "/opt/xos/synchronizers/vsgw/vsgw_private_key"
-
-    def __init__(self, *args, **kwargs):
-        super(SyncVSGWTenant, self).__init__(*args, **kwargs)
-
-    def fetch_pending(self, deleted):
-
-        if (not deleted):
-            objs = VSGWTenant.get_tenant_objects().filter(
-                Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False))
-        else:
-            # If this is a deletion we get all of the deleted tenants..
-            objs = VSGWTenant.get_deleted_tenant_objects()
-
-        return objs
-
-    def get_extra_attributes(self, o):
-        fields = {}
-        fields['tenant_message'] = o.tenant_message
-        fields['image_name'] = o.image_name
-        return fields
-
diff --git a/xos/synchronizer/steps/sync_vsgw.yaml b/xos/synchronizer/steps/sync_vsgw.yaml
deleted file mode 100644
index 0ad9397..0000000
--- a/xos/synchronizer/steps/sync_vsgw.yaml
+++ /dev/null
@@ -1,43 +0,0 @@
-
-# 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.
-
-
----
-- hosts: {{ instance_name }}
-  connection: ssh
-  user: ubuntu
-  become: yes
-  gather_facts: no
-  tasks:
-
-  vars:
-    - tenant_message: "{{ tenant_message }}"
-  
-#  - name: Write message
-#    shell: echo "{{ tenant_message }}" > /var/tmp/index.html
-#
-#  - name: setup s1u interface config
-#    shell: ./start_3gpp_int.sh eth1 {{ s1u_sgw_tag }} {{ s1u_sgw_ip }}/24
-#
-#  - name: setup s11 interface config
-#    shell: ./start_3gpp_int.sh eth2 {{ s11_sgw_tag }} {{ s11_sgw_ip }}/24
-#
-#  - name: setup s5s8 interface config
-#    shell: ./start_3gpp_int.sh eth3 {{ s5s8_sgw_tag }} {{ s5s8_sgw_ip }}/24
-  
-#  roles:
-#    - install_apache
-#    - create_index
-
diff --git a/xos/synchronizer/steps/sync_vsgwctenant.py b/xos/synchronizer/steps/sync_vsgwctenant.py
new file mode 100644
index 0000000..cc1078c
--- /dev/null
+++ b/xos/synchronizer/steps/sync_vsgwctenant.py
@@ -0,0 +1,71 @@
+# 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.
+
+import os
+import sys
+from django.db.models import Q, F
+from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+class SyncVSGWCTenant(SyncInstanceUsingAnsible):
+    provides = [VSGWCTenant]
+
+    observes = VSGWCTenant
+
+    requested_interval = 0
+
+    template_name = "vsgwctenant_playbook.yaml"
+
+    service_key_name = "/opt/xos/configurations/mcord/mcord_private_key"
+
+    def __init__(self, *args, **kwargs):
+        super(SyncVSGWCTenant, self).__init__(*args, **kwargs)
+
+#    def fetch_pending(self, deleted):
+#        if (not deleted):
+#            objs = VSGWCTenant.get_tenant_objects().filter(
+#                Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False))
+#        else:
+#            # If this is a deletion we get all of the deleted tenants..
+#            objs = VSGWCTenant.get_deleted_tenant_objects()
+#
+#        return objs
+
+    # Gets the attributes that are used by the Ansible template but are not
+    # part of the set of default attribtues.
+    def get_extra_attributes(self, o):
+        fields = {}
+        shared_net_id = Network.objects.get(name='shared_network').id
+
+	try:
+            fields['sgwc_shared_ip'] = Port.objects.get(network_id=shared_net_id, instance_id=o.instance_id).ip
+        except Exception:
+            print '{} does not have an instance'.format(o.name)
+
+        try:
+            mme = TenantWithContainer.objects.get(provider_service_id=Service.objects.get(name='vmme').id, subscriber_tenant_id=o.subscriber_tenant_id)
+            fields['mme_shared_ip'] = Port.objects.get(network_id=shared_net_id, instance_id=mme.instance_id).ip
+	except Exception:
+            print '{} does not have a VMME instance'.format(o.subscriber_tenant.name)
+
+        try:
+            sgwu = TenantWithContainer.objects.get(provider_service_id=Service.objects.get(name='vsgwu').id, subscriber_tenant_id=o.subscriber_tenant_id)
+            fields['sgwu_shared_ip'] = Port.objects.get(network_id=shared_net_id, instance_id=sgwu.instance_id).ip
+        except Exception:
+            print '{} does not have a VSGWU instance'.format(o.subscriber_tenant.name)
+
+        return fields
diff --git a/xos/synchronizer/run.sh b/xos/synchronizer/steps/vsgwctenant_playbook.yaml
similarity index 69%
rename from xos/synchronizer/run.sh
rename to xos/synchronizer/steps/vsgwctenant_playbook.yaml
index 48db7ca..ef1b986 100644
--- a/xos/synchronizer/run.sh
+++ b/xos/synchronizer/steps/vsgwctenant_playbook.yaml
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,7 +12,16 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+---
+- hosts: {{ instance_name }}
+  gather_facts: True
+  connection: ssh
+  user: ubuntu
+  sudo: yes
+  vars:
+    - sgwc_shared_ip: {{ sgwc_shared_ip }}
+    - mme_shared_ip: {{ mme_shared_ip }}
+    - sgwu_shared_ip: {{ sgwu_shared_ip }}
+  roles:
+    - ngic_config
 
-# Runs the XOS observer using vsgw_config
-export XOS_DIR=/opt/xos
-python vsgw-synchronizer.py  -C $XOS_DIR/synchronizers/vsgw/vsgw_config
diff --git a/xos/synchronizer/vsgw_config b/xos/synchronizer/vsgw_config
deleted file mode 100644
index 02d3fb0..0000000
--- a/xos/synchronizer/vsgw_config
+++ /dev/null
@@ -1,28 +0,0 @@
-# Required by XOS
-[db]
-name=xos
-user=postgres
-password=password
-host=xos_db
-port=5432
-
-# Required by XOS
-[api]
-nova_enabled=True
-
-# Sets options for the synchronizer
-[observer]
-name=vsgw
-dependency_graph=/opt/xos/synchronizers/vsgw/model-deps
-steps_dir=/opt/xos/synchronizers/vsgw/steps
-sys_dir=/opt/xos/synchronizers/vsgw/sys
-logfile=/var/log/xos_backend.log
-pretend=False
-backoff_disabled=True
-save_ansible_output=True
-proxy_ssh=True
-proxy_ssh_key=/root/setup/id_rsa
-proxy_ssh_user=root
-enable_watchers=True
-[networking]
-use_vtn=True
diff --git a/xos/synchronizer/vsgw-synchronizer.py b/xos/synchronizer/vsgwc-synchronizer.py
old mode 100644
new mode 100755
similarity index 73%
rename from xos/synchronizer/vsgw-synchronizer.py
rename to xos/synchronizer/vsgwc-synchronizer.py
index 616029c..4a527ef
--- a/xos/synchronizer/vsgw-synchronizer.py
+++ b/xos/synchronizer/vsgwc-synchronizer.py
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,7 +12,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
 #!/usr/bin/env python
 
 # Runs the standard XOS synchronizer
@@ -21,9 +19,13 @@
 import importlib
 import os
 import sys
+from xosconfig import Config
 
-synchronizer_path = os.path.join(os.path.dirname(
-    os.path.realpath(__file__)), "../../synchronizers/base")
+config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/vsgwc_config.yaml')
+
+Config.init(config_file, 'synchronizer-config-schema.yaml')
+observer_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../synchronizers/new_base")
+
 sys.path.append(synchronizer_path)
 mod = importlib.import_module("xos-synchronizer")
 mod.main()
diff --git a/xos/synchronizer/run.sh b/xos/synchronizer/vsgwc_config.yaml
similarity index 66%
copy from xos/synchronizer/run.sh
copy to xos/synchronizer/vsgwc_config.yaml
index 48db7ca..5d3195e 100644
--- a/xos/synchronizer/run.sh
+++ b/xos/synchronizer/vsgwc_config.yaml
@@ -1,4 +1,3 @@
-
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,7 +12,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
-# Runs the XOS observer using vsgw_config
-export XOS_DIR=/opt/xos
-python vsgw-synchronizer.py  -C $XOS_DIR/synchronizers/vsgw/vsgw_config
+name: vsgwc-synchronizer
+accessor:
+  username: xosadmin@opencord.org
+  password: "@/opt/xos/services/vsgwc/credentials/xosadmin@opencord.org"
+dependency_graph: "/opt/xos/synchronizers/vsgwc/model-deps"
+steps_dir: "/opt/xos/synchronizers/vsgwc/steps"
+sys_dir: "/opt/xos/synchronizers/vsgwc/sys"
diff --git a/xos/synchronizer/vsgwc_from_api_config b/xos/synchronizer/vsgwc_from_api_config
new file mode 100644
index 0000000..fcd8bd3
--- /dev/null
+++ b/xos/synchronizer/vsgwc_from_api_config
@@ -0,0 +1,20 @@
+# Sets options for the synchronizer
+[observer]
+name=vsgwc
+dependency_graph=/opt/xos/synchronizers/vsgwc/model-deps
+steps_dir=/opt/xos/synchronizers/vsgwc/steps
+sys_dir=/opt/xos/synchronizers/vsgwc/sys
+#logfile=/var/log/xos_backend.log
+log_file=console
+log_level=debug
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+proxy_ssh=True
+proxy_ssh_key=/opt/cord_profile/node_key
+proxy_ssh_user=root
+accessor_kind=api
+accessor_password=@/opt/xos/services/vsgwc/credentials/xosadmin@opencord.org
+
+[networking]
+use_vtn=True
\ No newline at end of file