Moving metro-net to vnaas and porting to 4.0

Change-Id: Id42bb247033087e5d28bcbab9db07d58e6364625
(cherry picked from commit 95c02bd391ad5eb8edeba00e81b851b97a6abbd1)
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100755
index 0000000..fcbe0ef
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,25 @@
+
+# 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.
+
+
+FROM xosproject/xos-synchronizer-base:candidate
+
+ADD . /opt/xos/synchronizers/vnaas
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/vnaas"
+
+CMD bash -c "cd /opt/xos/synchronizers/vnaas; ./run-from-api.sh"
diff --git a/xos/synchronizer/Makefile b/xos/synchronizer/Makefile
new file mode 100755
index 0000000..1c0258d
--- /dev/null
+++ b/xos/synchronizer/Makefile
@@ -0,0 +1,5 @@
+synchronizer:
+	sudo docker build --rm -f Dockerfile.synchronizer -t xosproject/vnaas-synchronizer .
+
+up:
+	docker-compose -p vnaas up -d
\ No newline at end of file
diff --git a/xos/synchronizer/_DS_Store b/xos/synchronizer/_DS_Store
new file mode 100755
index 0000000..719edcd
--- /dev/null
+++ b/xos/synchronizer/_DS_Store
Binary files differ
diff --git a/xos/synchronizer/__init__.py b/xos/synchronizer/__init__.py
new file mode 100755
index 0000000..65fdf5b
--- /dev/null
+++ b/xos/synchronizer/__init__.py
@@ -0,0 +1,17 @@
+
+# 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.
+
+
+
diff --git a/xos/synchronizer/docker-compose.yml b/xos/synchronizer/docker-compose.yml
new file mode 100755
index 0000000..d24b7b6
--- /dev/null
+++ b/xos/synchronizer/docker-compose.yml
@@ -0,0 +1,38 @@
+
+# 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.
+
+
+version: '2'
+
+networks:
+  xos:
+    external: true
+
+services:
+  synchronizer:
+    image: xosproject/vnaas-synchronizer
+    networks:
+      - xos
+    #command: bash -c "sleep 86400"
+    volumes:
+        - /opt/cord_profile/node_key:/opt/cord_profile/node_key:ro
+        - /opt/cord/build/platform-install/credentials/xosadmin@opencord.org:/opt/xos/services/vnaas/credentials/xosadmin@opencord.org
+    external_links:
+        - rcordbs_xos_redis_1:redis
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "1000k"
+        max-file: "5"
\ No newline at end of file
diff --git a/xos/synchronizer/manifest b/xos/synchronizer/manifest
new file mode 100755
index 0000000..59df872
--- /dev/null
+++ b/xos/synchronizer/manifest
@@ -0,0 +1,7 @@
+vnaas-synchronizer.py
+__init__.py
+model-deps
+manifest
+steps/sync_vnaaseline.py
+run-from-api.sh
+vnaas_synchronizer_config
diff --git a/xos/synchronizer/model-deps b/xos/synchronizer/model-deps
new file mode 100755
index 0000000..0967ef4
--- /dev/null
+++ b/xos/synchronizer/model-deps
@@ -0,0 +1 @@
+{}
diff --git a/xos/synchronizer/run-from-api.sh b/xos/synchronizer/run-from-api.sh
new file mode 100755
index 0000000..1d69d4f
--- /dev/null
+++ b/xos/synchronizer/run-from-api.sh
@@ -0,0 +1,15 @@
+# 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.
+
+python vnaas-synchronizer.py
diff --git a/xos/synchronizer/run_devel.sh b/xos/synchronizer/run_devel.sh
new file mode 100755
index 0000000..bbdf1b6
--- /dev/null
+++ b/xos/synchronizer/run_devel.sh
@@ -0,0 +1,18 @@
+
+# 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.
+
+
+export XOS_DIR=/opt/xos
+python vnaas-synchronizer-devel.py  -C $XOS_DIR/synchronizers/vnaas/vnaas_synchronizer_config
diff --git a/xos/synchronizer/steps/__init__.py b/xos/synchronizer/steps/__init__.py
new file mode 100755
index 0000000..d4e8062
--- /dev/null
+++ b/xos/synchronizer/steps/__init__.py
@@ -0,0 +1,16 @@
+
+# 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.
+
+
diff --git a/xos/synchronizer/steps/sync_vnaaseline.py b/xos/synchronizer/steps/sync_vnaaseline.py
new file mode 100755
index 0000000..4be994b
--- /dev/null
+++ b/xos/synchronizer/steps/sync_vnaaseline.py
@@ -0,0 +1,105 @@
+
+# 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
+import requests
+import json
+from synchronizers.new_base.syncstep import SyncStep
+from synchronizers.new_base.modelaccessor import *
+#from xos.logger import Logger, logging
+
+from requests.auth import HTTPBasicAuth
+#logger = Logger(level=logging.INFO)
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+
+class SyncvNaaSEline(SyncStep):
+    provides = [ELine]
+
+    observes = ELine
+
+    requested_interval = 0
+
+    def __init__(self, *args, **kwargs):
+        super(SyncvNaaSEline, self).__init__(*args, **kwargs)
+
+    def get_onos_global_addr(self, onos):
+        #Fetching ip and port from the Global ONOS, append the CE specif API
+
+        onos_url = "http://%s:%s/onos/" % (onos.onos_ip, onos.onos_port)
+        evc_endpoint = "carrierethernet/evc"
+        return onos_url + evc_endpoint
+
+    def get_onos_global_auth(self, onos):
+        #Fetching username and password from the Global ONOS
+
+        return HTTPBasicAuth(onos.onos_username, onos.onos_password)
+
+    def sync_record(self, evc):
+        print "POST %s " % (evc)
+        #logger.info("Syncing Edited EVC: %s" % evc.name)
+        # Fetch the bwp from the DB
+        bwp = BandwidthProfile.objects.get(name=evc.bwp)
+
+        # json to configure ONOS to start the EVC.
+        # {
+        #     "evcId": "evc1",
+        #     "evcCfgId": "evpl1",
+        #     "uniList": [
+        #         "netconf:192.168.56.10:830/0",
+        #         "netconf:192.168.56.20:830/0"
+        #     ],
+        #     "evcType": "POINT_TO_POINT",
+        #     "vlanId": 100,
+        #     "cir": "400",
+        #     "eir": "200",
+        #     "cbs": "3000",
+        #     "ebs": "2000"
+        # }
+
+        data = {}
+        data["evcId"] = evc.name
+        data["evcCfgId"] = evc.name
+        data["uniList"] = [evc.connect_point_1_id, evc.connect_point_2_id]
+        data["evcType"] = "POINT_TO_POINT"
+        data["vlanId"] = evc.vlanids.split(",")
+        data["cbs"] = bwp.cbs
+        data["ebs"] = bwp.ebs
+        data["cir"] = bwp.cir
+        data["eir"] = bwp.eir
+        print "data %s" % data
+        # assuming that the CPEs are controller by the fabric ONOS
+        onos = OnosModel.objects.get(onos_type="global")
+        onos_addr = self.get_onos_global_addr(onos)
+
+        # FIXME - hardcoded auth
+        auth = self.get_onos_global_auth(onos)
+
+        print "POST %s for app %s, data = %s" % (onos_addr, evc.name, data)
+
+        r = requests.post(onos_addr, data=json.dumps(data), auth=auth)
+        #TODO XOS might fail to connect to ONOS.
+        if (r.status_code != 200):
+            print r
+            raise Exception("Received error from EVC Installation update (%d)" % r.status_code)
+
+    def delete_record(self, evc):
+        # TODO evaluate what to in this case.
+        print "Syncing delete EVC: %s" % evc.name
+        #logger.info("Syncing delete EVC: %s" % evc.name)
diff --git a/xos/synchronizer/vnaas-synchronizer.py b/xos/synchronizer/vnaas-synchronizer.py
new file mode 100755
index 0000000..4ced1aa
--- /dev/null
+++ b/xos/synchronizer/vnaas-synchronizer.py
@@ -0,0 +1,32 @@
+
+# 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.
+
+
+#!/usr/bin/env python
+
+# This imports and runs ../../xos-observer.py
+
+import importlib
+import os
+import sys
+from xosconfig import Config
+
+config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/vnaas_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(observer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
diff --git a/xos/synchronizer/vnaas_config.yaml b/xos/synchronizer/vnaas_config.yaml
new file mode 100755
index 0000000..0c8fb1e
--- /dev/null
+++ b/xos/synchronizer/vnaas_config.yaml
@@ -0,0 +1,21 @@
+# 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: vnaas-synchronizer
+accessor:
+  username: xosadmin@opencord.org
+  password: "@/opt/xos/services/vnaas/credentials/xosadmin@opencord.org"
+dependency_graph: "/opt/xos/synchronizers/vnaas/model-deps"
+steps_dir: "/opt/xos/synchronizers/vnaas/steps"
+sys_dir: "/opt/xos/synchronizers/vnaas/sys"