[CORD-2886] Sending device config to VOLTHA ONOS

Change-Id: Iec466bd5cf1528b2ca80064819de36a24b883e10
diff --git a/xos/synchronizer/steps/helpers.py b/xos/synchronizer/steps/helpers.py
new file mode 100644
index 0000000..914279e
--- /dev/null
+++ b/xos/synchronizer/steps/helpers.py
@@ -0,0 +1,43 @@
+# 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.
+
+class Helpers():
+    @staticmethod
+    def format_url(url):
+        if 'http' in url:
+            return url
+        else:
+            return 'http://%s' % url
+
+    @staticmethod
+    def get_voltha_info(olt_service):
+        return {
+            'url': Helpers.format_url(olt_service.voltha_url),
+            'user': olt_service.voltha_user,
+            'pass': olt_service.voltha_pass
+        }
+
+    @staticmethod
+    def get_p_onos_info(olt_service):
+        return {
+            'url': Helpers.format_url(olt_service.p_onos_url),
+            'user': olt_service.p_onos_user,
+            'pass': olt_service.p_onos_pass
+        }
+
+    @staticmethod
+    def datapath_id_to_hex(id):
+        if isinstance(id, basestring):
+            id = int(id)
+        return "{0:0{1}x}".format(id, 16)
\ No newline at end of file
diff --git a/xos/synchronizer/steps/sync_olt_device.py b/xos/synchronizer/steps/sync_olt_device.py
index 74da3b0..2845721 100644
--- a/xos/synchronizer/steps/sync_olt_device.py
+++ b/xos/synchronizer/steps/sync_olt_device.py
@@ -21,6 +21,7 @@
 from time import sleep
 import requests
 from requests.auth import HTTPBasicAuth
+from helpers import Helpers
 
 log = create_logger(Config().get('logging'))
 
@@ -30,31 +31,8 @@
     observes = OLTDevice
 
     @staticmethod
-    def format_url(url):
-        if 'http' in url:
-            return url
-        else:
-            return 'http://%s' % url
-
-    @staticmethod
-    def get_voltha_info(o):
-        return {
-            'url': SyncOLTDevice.format_url(o.volt_service.voltha_url),
-            'user': o.volt_service.voltha_user,
-            'pass': o.volt_service.voltha_pass
-        }
-
-    @staticmethod
-    def get_p_onos_info(o):
-        return {
-            'url': SyncOLTDevice.format_url(o.volt_service.p_onos_url),
-            'user': o.volt_service.p_onos_user,
-            'pass': o.volt_service.p_onos_pass
-        }
-
-    @staticmethod
-    def get_of_id_from_device(o):
-        voltha_url = SyncOLTDevice.get_voltha_info(o)['url']
+    def get_ids_from_logical_device(o):
+        voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
 
         r = requests.get(voltha_url + "/api/v1/logical_devices")
 
@@ -65,7 +43,10 @@
 
         for ld in res["items"]:
             if ld["root_device_id"] == o.device_id:
-                return ld["id"]
+                o.of_id = ld["id"]
+                o.dp_id = "of:" + Helpers.datapath_id_to_hex(ld["datapath_id"]) # convert to hex
+                return o
+
         raise Exception("Can't find a logical device for device id: %s" % o.device_id)
 
 
@@ -75,7 +56,7 @@
         # If the device has feedback_state is already present in voltha
         if not o.device_id and not o.admin_state and not o.oper_status and not o.of_id:
             log.info("Pushing device to VOLTHA", object=str(o), **o.tologdict())
-            voltha_url = self.get_voltha_info(o)['url']
+            voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
 
             data = {
                 "type": o.device_type,
@@ -123,7 +104,7 @@
             o.oper_status = r['oper_status']
 
             # find of_id of device
-            o.of_id = self.get_of_id_from_device(o)
+            o = self.get_ids_from_logical_device(o)
             o.save()
         else:
             log.info("Device already exists in VOLTHA", object=str(o), **o.tologdict())
@@ -136,7 +117,7 @@
         # add device info to P-ONOS
         data = {
           "devices": {
-            o.of_id: {
+            o.dp_id: {
               "basic": {
                 "driver": o.driver
               },
@@ -148,7 +129,7 @@
           }
         }
 
-        onos= self.get_p_onos_info(o)
+        onos= Helpers.get_p_onos_info(o.volt_service)
 
         r = requests.post(onos['url'] + '/onos/v1/network/configuration/', data=json.dumps(data), auth=HTTPBasicAuth(onos['user'], onos['pass']))
 
@@ -163,8 +144,8 @@
 
     def delete_record(self, o):
 
-        voltha_url = self.get_voltha_info(o)['url']
-        onos = self.get_p_onos_info(o)
+        voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
+        onos = Helpers.get_p_onos_info(o.volt_service)
         if not o.device_id:
             log.error("Device %s has no device_id" % o.name)
 
diff --git a/xos/synchronizer/steps/sync_volt_service_instance.py b/xos/synchronizer/steps/sync_volt_service_instance.py
new file mode 100644
index 0000000..dccac6a
--- /dev/null
+++ b/xos/synchronizer/steps/sync_volt_service_instance.py
@@ -0,0 +1,68 @@
+# 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 synchronizers.new_base.syncstep import SyncStep, DeferredException
+from synchronizers.new_base.modelaccessor import model_accessor
+from synchronizers.new_base.modelaccessor import VOLTService, VOLTServiceInstance, ServiceInstance, OLTDevice
+
+from xosconfig import Config
+from multistructlog import create_logger
+import requests
+from requests.auth import HTTPBasicAuth
+from helpers import Helpers
+
+log = create_logger(Config().get("logging"))
+
+class SyncVOLTServiceInstance(SyncStep):
+    provides = [VOLTServiceInstance]
+
+    observes = VOLTServiceInstance
+
+    def sync_record(self, o):
+
+        volt_service = VOLTService.objects.get(id=o.owner_id)
+
+        si = ServiceInstance.objects.get(id=o.id)
+
+        log.info("sync'ing OLTServiceInstance", object=str(o), **o.tologdict())
+
+        c_tag = si.get_westbound_service_instance_properties("c_tag")
+        uni_port_id = si.get_westbound_service_instance_properties("uni_port_id")
+
+        olt_device_name = si.get_westbound_service_instance_properties("olt_device")
+
+        olt_device = OLTDevice.objects.get(name=olt_device_name)
+
+        if not olt_device.dp_id:
+            raise DeferredException("Waiting for OLTDevice %s to be synchronized" % olt_device.name)
+
+        log.debug("Adding subscriber with info",
+                 c_tag=c_tag,
+                 uni_port_id=uni_port_id,
+                 dp_id=olt_device.dp_id)
+
+        # sending request to ONOS
+
+        onos = Helpers.get_p_onos_info(volt_service)
+
+        url = onos['url'] + "/onos/olt/oltapp/%s/%s/%s" % (olt_device.dp_id, uni_port_id, c_tag)
+
+        log.info("sending request to P_ONOS", url=url)
+
+        r = requests.post(url,auth=HTTPBasicAuth(onos['user'], onos['pass']))
+
+        if r.status_code != 200:
+            raise Exception("Failed to add subscriber in P_ONOS: %s" % r.text)
+
+        log.info("P_ONOS response", res=r.text)
\ No newline at end of file
diff --git a/xos/synchronizer/steps/test_helpers.py b/xos/synchronizer/steps/test_helpers.py
new file mode 100644
index 0000000..2820d9e
--- /dev/null
+++ b/xos/synchronizer/steps/test_helpers.py
@@ -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.
+
+import unittest
+from mock import patch, call, Mock, PropertyMock
+from helpers import Helpers
+
+class TestHelpers(unittest.TestCase):
+
+    def setUp(self):
+        # create a mock service instance
+        o = Mock()
+        o.voltha_url = "voltha_url"
+        o.voltha_user = "voltha_user"
+        o.voltha_pass = "voltha_pass"
+        o.p_onos_url = "p_onos_url"
+        o.p_onos_user = "p_onos_user"
+        o.p_onos_pass = "p_onos_pass"
+
+        self.o = o
+
+    def test_format_url(self):
+        url = Helpers.format_url("onf.com")
+        self.assertEqual(url, "http://onf.com")
+        url = Helpers.format_url("http://onf.com")
+        self.assertEqual(url, "http://onf.com")
+
+    def test_get_voltha_info(self):
+        voltha_dict = Helpers.get_voltha_info(self.o)
+
+        self.assertEqual(voltha_dict["url"], "http://voltha_url")
+        self.assertEqual(voltha_dict["user"], "voltha_user")
+        self.assertEqual(voltha_dict["pass"], "voltha_pass")
+
+    def test_get_onos_info(self):
+        p_onos_dict = Helpers.get_p_onos_info(self.o)
+
+        self.assertEqual(p_onos_dict["url"], "http://p_onos_url")
+        self.assertEqual(p_onos_dict["user"], "p_onos_user")
+        self.assertEqual(p_onos_dict["pass"], "p_onos_pass")
+
+    def test_datapath_id_to_hex(self):
+        hex = Helpers.datapath_id_to_hex(55334486016)
+        self.assertEqual(hex, "0000000ce2314000")
+
+        hex = Helpers.datapath_id_to_hex("55334486016")
+        self.assertEqual(hex, "0000000ce2314000")
+
+if __name__ == "__main__":
+    unittest.main()
\ No newline at end of file
diff --git a/xos/synchronizer/steps/test_sync_olt_device.py b/xos/synchronizer/steps/test_sync_olt_device.py
index cd4906f..8143ef8 100644
--- a/xos/synchronizer/steps/test_sync_olt_device.py
+++ b/xos/synchronizer/steps/test_sync_olt_device.py
@@ -42,12 +42,12 @@
 
 def match_onos_req(req):
     r = req.json()['devices']
-    if not r['abc']:
+    if not r['of:0000000ce2314000']:
         return False
     else:
-        if not r['abc']['basic']['driver'] == 'pmc-olt':
+        if not r['of:0000000ce2314000']['basic']['driver'] == 'pmc-olt':
             return False
-        if not r['abc']['accessDevice']['vlan'] == "s_tag" or not r['abc']['accessDevice']['uplink'] == "129":
+        if not r['of:0000000ce2314000']['accessDevice']['vlan'] == "s_tag" or not r['of:0000000ce2314000']['accessDevice']['uplink'] == "129":
             return False
     return True
 
@@ -109,42 +109,25 @@
         self.o = None
         sys.path = self.sys_path_save
 
-    def test_format_url(self):
-        url = self.sync_step.format_url("onf.com")
-        self.assertEqual(url, "http://onf.com")
-        url = self.sync_step.format_url("http://onf.com")
-        self.assertEqual(url, "http://onf.com")
 
-    def test_get_voltha_info(self):
-        voltha_dict = self.sync_step.get_voltha_info(self.o)
-
-        self.assertEqual(voltha_dict["url"], "http://voltha_url")
-        self.assertEqual(voltha_dict["user"], "voltha_user")
-        self.assertEqual(voltha_dict["pass"], "voltha_pass")
-
-    def test_get_onos_info(self):
-        p_onos_dict = self.sync_step.get_p_onos_info(self.o)
-
-        self.assertEqual(p_onos_dict["url"], "http://p_onos_url")
-        self.assertEqual(p_onos_dict["user"], "p_onos_user")
-        self.assertEqual(p_onos_dict["pass"], "p_onos_pass")
 
     @requests_mock.Mocker()
     def test_get_of_id_from_device(self, m):
         logical_devices = {
             "items": [
-                {"root_device_id": "123", "id": "abc"},
+                {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
                 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
             ]
         }
         m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices)
         self.o.device_id = "123"
-        of_id = self.sync_step.get_of_id_from_device(self.o)
-        self.assertEqual(of_id, "abc")
+        self.o = self.sync_step.get_ids_from_logical_device(self.o)
+        self.assertEqual(self.o.of_id, "0001000ce2314000")
+        self.assertEqual(self.o.dp_id, "of:0000000ce2314000")
 
         with self.assertRaises(Exception) as e:
             self.o.device_id = "idonotexist"
-            self.sync_step.get_of_id_from_device(self.o)
+            self.sync_step.get_ids_from_logical_device(self.o)
         self.assertEqual(e.exception.message, "Can't find a logical device for device id: idonotexist")
 
     @requests_mock.Mocker()
@@ -191,7 +174,7 @@
         m.get("http://voltha_url/api/v1/devices/123", json={"oper_status": "ENABLED", "admin_state": "ACTIVE"})
         logical_devices = {
             "items": [
-                {"root_device_id": "123", "id": "abc"},
+                {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
                 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
             ]
         }
@@ -202,7 +185,8 @@
         self.sync_step().sync_record(self.o)
         self.assertEqual(self.o.admin_state, "ACTIVE")
         self.assertEqual(self.o.oper_status, "ENABLED")
-        self.assertEqual(self.o.of_id, "abc")
+        self.assertEqual(self.o.of_id, "0001000ce2314000")
+        # self.assertEqual(self.o.dp_id, "of:0000000ce2314000")
         self.o.save.assert_called_once()
 
     @requests_mock.Mocker()
@@ -212,7 +196,7 @@
         self.o.device_id = "123"
         self.o.admin_state = "ACTIVE"
         self.o.oper_status = "ENABLED"
-        self.o.of_id = "abc"
+        self.o.dp_id = "of:0000000ce2314000"
 
         m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={})
 
@@ -222,10 +206,10 @@
 
     @requests_mock.Mocker()
     def test_delete_record(self, m):
-        self.o.of_id = "abc"
+        self.o.of_id = "0001000ce2314000"
         self.o.device_id = "123"
 
-        m.delete("http://p_onos_url/onos/v1/network/configuration/devices/abc", status_code=200)
+        m.delete("http://p_onos_url/onos/v1/network/configuration/devices/0001000ce2314000", status_code=200)
         m.post("http://voltha_url/api/v1/devices/123/disable", status_code=200)
         m.delete("http://voltha_url/api/v1/devices/123/delete", status_code=200)
 
diff --git a/xos/synchronizer/steps/test_sync_volt_service_instance.py b/xos/synchronizer/steps/test_sync_volt_service_instance.py
new file mode 100644
index 0000000..dfab503
--- /dev/null
+++ b/xos/synchronizer/steps/test_sync_volt_service_instance.py
@@ -0,0 +1,158 @@
+# 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 unittest
+from mock import patch, call, Mock, PropertyMock
+import requests_mock
+
+import os, sys
+
+# Hack to load synchronizer framework
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir = os.path.join(xos_dir, "../../xos_services")
+sys.path.append(xos_dir)
+sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+# END Hack to load synchronizer framework
+
+# generate model from xproto
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+# END generate model from xproto
+
+def mock_get_westbound_service_instance_properties(prop):
+    return prop
+
+class TestSyncOLTDevice(unittest.TestCase):
+
+    def setUp(self):
+        global DeferredException
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+        # Setting up the config module
+        from xosconfig import Config
+        config = os.path.join(test_path, "../model_policies/test_config.yaml")
+        Config.clear()
+        Config.init(config, "synchronizer-config-schema.yaml")
+        # END Setting up the config module
+
+        from synchronizers.new_base.syncstep import DeferredException
+        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
+        import synchronizers.new_base.modelaccessor
+        from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor
+
+        # import all class names to globals
+        for (k, v) in model_accessor.all_model_classes.items():
+            globals()[k] = v
+
+        self.sync_step = SyncVOLTServiceInstance
+
+        # create a mock service instance
+        o = Mock()
+        o.id = 1
+        o.owner_id = "volt_service"
+        o.tologdict.return_value = {}
+
+        volt_service = Mock()
+        volt_service.p_onos_url = "p_onos_url"
+        volt_service.p_onos_user = "p_onos_user"
+        volt_service.p_onos_pass = "p_onos_pass"
+
+        si = Mock()
+        si.get_westbound_service_instance_properties = mock_get_westbound_service_instance_properties
+
+        olt_device = Mock()
+        olt_device.name = "Test OLT Device"
+
+        self.o = o
+        self.si = si
+        self.olt_device = olt_device
+        self.volt_service = volt_service
+
+    def tearDown(self):
+        self.o = None
+        sys.path = self.sys_path_save
+
+
+
+    @requests_mock.Mocker()
+    def test_do_not_sync(self, m):
+
+        self.olt_device.dp_id = None
+
+        with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \
+                patch.object(OLTDevice.objects, "get") as olt_device_mock, \
+                patch.object(VOLTService.objects, "get") as olt_service_mock:
+            service_instance_mock.return_value = self.si
+            olt_device_mock.return_value = self.olt_device
+            olt_service_mock.return_value = self.volt_service
+
+            with self.assertRaises(DeferredException) as e:
+                self.sync_step().sync_record(self.o)
+
+            self.assertFalse(m.called)
+            self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized")
+
+    @requests_mock.Mocker()
+    def test_do_sync(self, m):
+
+        m.post("http://p_onos_url/onos/olt/oltapp/of:dp_id/uni_port_id/c_tag", status_code=200, json={})
+
+        self.olt_device.dp_id = "of:dp_id"
+
+        with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \
+                patch.object(OLTDevice.objects, "get") as olt_device_mock, \
+                patch.object(VOLTService.objects, "get") as olt_service_mock:
+            service_instance_mock.return_value = self.si
+            olt_device_mock.return_value = self.olt_device
+            olt_service_mock.return_value = self.volt_service
+
+            self.sync_step().sync_record(self.o)
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_do_sync_fail(self, m):
+        m.post("http://p_onos_url/onos/olt/oltapp/of:dp_id/uni_port_id/c_tag", status_code=500, text="Mock Error")
+
+        self.olt_device.dp_id = "of:dp_id"
+
+        with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \
+                patch.object(OLTDevice.objects, "get") as olt_device_mock, \
+                patch.object(VOLTService.objects, "get") as olt_service_mock:
+            service_instance_mock.return_value = self.si
+            olt_device_mock.return_value = self.olt_device
+            olt_service_mock.return_value = self.volt_service
+
+            with self.assertRaises(Exception) as e:
+                self.sync_step().sync_record(self.o)
+
+            self.assertTrue(m.called)
+            self.assertEqual(e.exception.message, "Failed to add subscriber in P_ONOS: Mock Error")
+
+
+if __name__ == "__main__":
+    unittest.main()
\ No newline at end of file