[CORD-2809] Synchronizer OLTDevice in VOLTHA

Change-Id: Ie4f2e10efd035e3fcb6066e5f7bd859179fa2fea
diff --git a/xos/synchronizer/models/volt.xproto b/xos/synchronizer/models/volt.xproto
index 68c4352..651957e 100644
--- a/xos/synchronizer/models/volt.xproto
+++ b/xos/synchronizer/models/volt.xproto
@@ -4,6 +4,15 @@
 message VOLTService (Service){
     option verbose_name = "vOLT Service";
     option kind = "vOLT";
+
+    required string voltha_url = 2 [max_length = 254, null = False, db_index = False, blank = False];
+    optional string voltha_user = 3 [max_length = 254, null = True, db_index = False, blank = True];
+    optional string voltha_pass = 4 [max_length = 254, null = True, db_index = False, blank = True];
+    required string p_onos_url = 5 [max_length = 254, null = False, db_index = False, blank = False];
+    optional string p_onos_user = 6 [max_length = 254, null = True, db_index = False, blank = True];
+    optional string p_onos_pass = 7 [max_length = 254, null = True, db_index = False, blank = True];
+
+    required string onu_provisioning = 8 [default = "allow_all", choices = "(('allow_all', 'Allow All'), ('pre_provisioned', 'Pre-provisione'), ('oss', 'OSS'))", null = False, db_index = False, blank = False];
 }
 
 message VOLTServiceInstance (ServiceInstance){
@@ -11,25 +20,42 @@
     option owner_class_name = "VOLTService";
     option verbose_name = "vOLT Service Instance";
 
-    required string onu_type = 2 [help_text = "ONU Type", default = "v-ontani", null = False, db_index = False, blank = False];
-    optional string description = 3 [max_length = 254, null = True, db_index = False, blank = True];
+    optional string description = 1 [max_length = 254, null = True, db_index = False, blank = True];
 }
 
-message VOLTDevice (XOSBase){
-    option verbose_name = "vOLT Device";
+message OLTDevice (XOSBase){
+    option verbose_name = "OLT Device";
+    option description="Represents a physical OLT device";
 
     required manytoone volt_service->VOLTService:volt_devices = 1 [db_index = True, null = False, blank = False];
     required string name = 2 [help_text = "name of device", max_length = 254, null = False, db_index = False, blank = False];
-    required string device_id = 3 [help_text = "Device ID", max_length = 254, null = False, db_index = False, blank = False];
-    required string device_type = 4 [help_text = "Device Type", default = "asfvolt16_olt", max_length = 254, null = False, db_index = False, blank = False];
-    required string host = 5 [help_text = "Host", max_length = 254, null = False, db_index = False, blank = False];
-    required int32 port = 6 [help_text = "Fabric port", null = False, db_index = False, blank = False];
+    required string device_type = 3 [help_text = "Device Type", default = "asfvolt16_olt", max_length = 254, null = False, db_index = False, blank = False];
+    required string host = 4 [help_text = "Host", max_length = 254, null = False, db_index = False, blank = False];
+    required int32 port = 5 [help_text = "Fabric port", null = False, db_index = False, blank = False];
+
+    optional string device_id = 10 [help_text = "Device ID", null = True, db_index = False, blank = False, feedback_state = True];
+    optional string admin_state = 11 [help_text = "admin_state", null = True, db_index = False, blank = False, feedback_state = True];
+    optional string oper_status = 12 [help_text = "oper_status", null = True, db_index = False, blank = False, feedback_state = True];
+    optional string of_id = 13 [help_text = "openflow id", null = True, db_index = False, blank = False, feedback_state = True];
+
+    required string uplink = 14 [default="129", help_text = "uplink port", null = False, db_index = False, blank = False];
+    required string vlan = 15 [default="3", help_text = "same as S-Tag", null = True, db_index = False, blank = False];
+    required string driver = 16 [default="pmc-olt", help_text = "Olt driver", null = True, db_index = False, blank = False];
 }
 
-message VOLTDevicePort (XOSBase){
-    option verbose_name = "vOLT Device Port";
+message ONUDevice (XOSBase){
+    option verbose_name = "ONU Device";
+    option description = "Represents a physical ONU device";
 
-    required manytoone volt_device->VOLTDevice:ports = 1 [db_index = True, null = False, blank = False];
-    required string port_id = 2 [help_text = "Port ID", max_length = 254, null = False, db_index = False, blank = False, tosca_key = True];
-    required int32 s_tag = 3 [help_text = "S Tag", null = False, db_index = False, blank = False];
+    required manytoone volt_device->OLTDevice:onu_devices = 1 [db_index = True, null = False, blank = False];
+    required string serial_number = 1 [max_length = 254, null = False, db_index = False, blank = False];
+}
+
+message PONPort (XOSBase){
+    option verbose_name = "PON Port";
+
+    required string name = 1 [db_index = True, null = False, blank = False];
+    required manytoone volt_device->OLTDevice:ports = 2 [db_index = True, null = False, blank = False];
+    required string port_id = 3 [help_text = "Port ID (Feedback State)", max_length = 254, null = False, db_index = False, blank = False, tosca_key = True];
+    required int32 s_tag = 4 [help_text = "S Tag", null = False, db_index = False, blank = False];
 }
diff --git a/xos/synchronizer/pull_steps/pull_olts.py b/xos/synchronizer/pull_steps/pull_olts.py
new file mode 100644
index 0000000..3ab76e0
--- /dev/null
+++ b/xos/synchronizer/pull_steps/pull_olts.py
@@ -0,0 +1,28 @@
+# 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.pullstep import PullStep
+from synchronizers.new_base.modelaccessor import OLTDevice
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class OLTDevicePullStep(PullStep):
+    def __init__(self):
+        super(OLTDevicePullStep, self).__init__(observed_model=OLTDevice)
+
+    def pull_records(self):
+        log.info("pulling OLT devices from VOLTHA")
diff --git a/xos/synchronizer/steps/sync_olt_device.py b/xos/synchronizer/steps/sync_olt_device.py
new file mode 100644
index 0000000..591f7f9
--- /dev/null
+++ b/xos/synchronizer/steps/sync_olt_device.py
@@ -0,0 +1,182 @@
+# 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 json
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
+from synchronizers.new_base.modelaccessor import OLTDevice
+
+from xosconfig import Config
+from multistructlog import create_logger
+from time import sleep
+import requests
+from requests.auth import HTTPBasicAuth
+
+log = create_logger(Config().get('logging'))
+
+class SyncOLTDevice(SyncStep):
+    provides = [OLTDevice]
+
+    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']
+
+        r = requests.get(voltha_url + "/api/v1/logical_devices")
+
+        if r.status_code != 200:
+            raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text)
+
+        res = r.json()
+
+        for ld in res["items"]:
+            if ld["root_device_id"] == o.device_id:
+                return ld["id"]
+        raise Exception("Can't find a logical device for device id: %s" % o.device_id)
+
+
+    def sync_record(self, o):
+        log.info("sync'ing device", object=str(o), **o.tologdict())
+
+        voltha_url = self.get_voltha_info(o)['url']
+
+        data = {
+            "type": o.device_type,
+            "host_and_port": "%s:%s" % (o.host, o.port)
+        }
+
+        if o.device_type == 'simulated_olt':
+            # simulated devices won't accept host and port, for testing only
+            data.pop('host_and_port')
+            data['mac_address'] = "00:0c:e2:31:40:00"
+
+        log.info("pushing olt to voltha", data=data)
+
+        r = requests.post(voltha_url + "/api/v1/devices", json=data)
+
+        if r.status_code != 200:
+            raise Exception("Failed to add device: %s" % r.text)
+
+        log.info("add device response", text=r.text)
+
+        res = r.json()
+
+        print log.info("add device json res", res=res)
+
+        if not res['id']:
+            raise Exception('VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA')
+        else:
+            o.device_id = res['id'];
+
+        # enable device
+
+        r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/enable")
+
+        if r.status_code != 200:
+            raise Exception("Failed to enable device: %s" % r.text)
+
+        # read state
+        r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json()
+        while r['oper_status'] == "ACTIVATING":
+            log.info("Waiting for device %s (%s) to activate" % (o.name, o.device_id))
+            sleep(5)
+            r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json()
+
+        o.admin_state = r['admin_state']
+        o.oper_status = r['oper_status']
+
+        # find of_id of device
+        o.of_id = self.get_of_id_from_device(o)
+        o.save()
+
+        # add device info to P-ONOS
+        data = {
+          "devices": {
+            o.of_id: {
+              "basic": {
+                "driver": o.driver
+              },
+              "accessDevice": {
+                "uplink": o.uplink,
+                "vlan": o.vlan
+              }
+            }
+          }
+        }
+
+        onos= self.get_p_onos_info(o)
+
+        r = requests.post(onos['url'] + '/onos/v1/network/configuration/', data=json.dumps(data), auth=HTTPBasicAuth(onos['user'], onos['pass']))
+
+        if r.status_code != 200:
+            log.error(r.text)
+            raise Exception("Failed to add device %s into ONOS" % o.name)
+        else:
+            try:
+                print r.json()
+            except Exception:
+                print r.text
+
+    def delete_record(self, o):
+
+        voltha_url = self.get_voltha_info(o)['url']
+        onos = self.get_p_onos_info(o)
+        if not o.device_id:
+            log.error("Device %s has no device_id" % o.name)
+
+        else:
+
+            # remove the device from ONOS
+            r = requests.delete(onos['url'] + '/onos/v1/network/configuration/devices/' + o.of_id, auth=HTTPBasicAuth(onos['user'], onos['pass']))
+
+            if r.status_code != 200:
+                log.error("Failed to remove device from ONOS: %s - %s" % (o.name, o.of_id), rest_responese=r.text, rest_status_code=r.status_code)
+                raise Exception("Failed to remove device in ONOS")
+
+            # disable the device
+            r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/disable")
+
+            if r.status_code != 200:
+                log.error("Failed to disable device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=r.text, rest_status_code=r.status_code)
+                raise Exception("Failed to disable device in VOLTHA")
+
+            # delete the device
+            r = requests.delete(voltha_url + "/api/v1/devices/" + o.device_id + "/delete")
+
+            if r.status_code != 200:
+                log.error("Failed to delete device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=r.text, rest_status_code=r.status_code)
+                raise Exception("Failed to delete device in VOLTHA")
diff --git a/xos/synchronizer/steps/sync_volt_service.py b/xos/synchronizer/steps/sync_volt_service.py
new file mode 100644
index 0000000..cae1ea3
--- /dev/null
+++ b/xos/synchronizer/steps/sync_volt_service.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 json
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
+from synchronizers.new_base.modelaccessor import VOLTService
+
+from xosconfig import Config
+from multistructlog import create_logger
+from time import sleep
+import requests
+from requests.auth import HTTPBasicAuth
+
+log = create_logger(Config().get('logging'))
+
+class SyncOLTService(SyncStep):
+    provides = [VOLTService]
+
+    observes = VOLTService
+
+    @staticmethod
+    def format_url(url):
+        if 'http' in url:
+            return url
+        else:
+            return 'http://%s' % url
+
+    @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
+        }
+
+    def sync_record(self, o):
+        log.info("sync'ing olt service", object=str(o), **o.tologdict())
+
+        if o.onu_provisioning == "allow_all":
+            # tell ONOS to create the ONU device (POST xosapi/v1/volt/onudevices)
+            pass
+        if o.onu_provisioning == "pre_provisioned" or o.onu_provisioning == "oss":
+            # tell ONOS to update the ONU device (POST xosapi/v1/volt/onudevices/<id>)
+            # NOTE ONOS will need to find the <id>
+            # NOTE if onu_provisioning == oss then XOS will need to make a call to the oss server to validate the onu
+            pass
+
+
+    def delete_record(self, o):
+        pass
diff --git a/xos/synchronizer/steps/test_sync_olt_device.py b/xos/synchronizer/steps/test_sync_olt_device.py
new file mode 100644
index 0000000..4163e32
--- /dev/null
+++ b/xos/synchronizer/steps/test_sync_olt_device.py
@@ -0,0 +1,188 @@
+# 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")
+sys.path.append(xos_dir)
+sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+# END Hack to load synchronizer framework
+
+# 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 sync_olt_device import SyncOLTDevice
+
+class TestSyncOLTDevice(unittest.TestCase):
+
+    def setUp(self):
+        # create a mock service instance
+        o = Mock()
+        o.volt_service.voltha_url = "voltha_url"
+        o.volt_service.voltha_user = "voltha_user"
+        o.volt_service.voltha_pass = "voltha_pass"
+        o.volt_service.p_onos_url = "p_onos_url"
+        o.volt_service.p_onos_user = "p_onos_user"
+        o.volt_service.p_onos_pass = "p_onos_pass"
+
+        o.device_type = "ponsim_olt"
+        o.host = "172.17.0.1"
+        o.port = "50060"
+        o.uplink = "129"
+        o.vlan = "3"
+        o.driver = "pmc-olt"
+
+        o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
+
+        o.save.return_value = "Saved"
+
+        self.o = o
+
+    def tearDown(self):
+        self.o = None
+
+    def test_format_url(self):
+        url = SyncOLTDevice.format_url("onf.com")
+        self.assertEqual(url, "http://onf.com")
+        url = SyncOLTDevice.format_url("http://onf.com")
+        self.assertEqual(url, "http://onf.com")
+
+    def test_get_voltha_info(self):
+        voltha_dict = SyncOLTDevice.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 = SyncOLTDevice.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": "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 = SyncOLTDevice.get_of_id_from_device(self.o)
+        self.assertEqual(of_id, "abc")
+
+        with self.assertRaises(Exception) as e:
+            self.o.device_id = "idonotexist"
+            SyncOLTDevice.get_of_id_from_device(self.o)
+        self.assertEqual(e.exception.message, "Can't find a logical device for device id: idonotexist")
+
+    @requests_mock.Mocker()
+    def test_sync_record_fail_add(self, m):
+        """
+        Should print an error if we can't add the device in VOLTHA
+        """
+        m.post("http://voltha_url/api/v1/devices", status_code=500, text="MockError")
+
+        with self.assertRaises(Exception) as e:
+            SyncOLTDevice().sync_record(self.o)
+        self.assertEqual(e.exception.message, "Failed to add device: MockError")
+
+    @requests_mock.Mocker()
+    def test_sync_record_fail_no_id(self, m):
+        """
+        Should print an error if VOLTHA does not return the device id
+        """
+        m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": ""})
+
+        with self.assertRaises(Exception) as e:
+            SyncOLTDevice().sync_record(self.o)
+        self.assertEqual(e.exception.message, "VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA")
+
+    @requests_mock.Mocker()
+    def test_sync_record_fail_enable(self, m):
+        """
+        Should print an error if device.enable fails
+        """
+        m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"})
+        m.post("http://voltha_url/api/v1/devices/123/enable", status_code=500, text="EnableError")
+
+        with self.assertRaises(Exception) as e:
+            SyncOLTDevice().sync_record(self.o)
+        self.assertEqual(e.exception.message, "Failed to enable device: EnableError")
+
+    @requests_mock.Mocker()
+    def test_sync_record_success(self, m):
+        """
+        If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS
+        """
+        m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"})
+        m.post("http://voltha_url/api/v1/devices/123/enable", status_code=200)
+        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": "0001cc4974a62b87", "id": "0001000000000001"}
+            ]
+        }
+        m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices)
+
+        def match_onos_req(req):
+            r = req.json()['devices']
+            if not r['abc']:
+                return False
+            else:
+                if not r['abc']['basic']['driver'] == 'pmc-olt':
+                    return False
+                if not r['abc']['accessDevice']['vlan'] == "3" or not r['abc']['accessDevice']['uplink'] == "129":
+                    return False
+            return True
+
+        m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={})
+
+        SyncOLTDevice().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.o.save.assert_called_once()
+
+    @requests_mock.Mocker()
+    def test_delete_record(self, m):
+        self.o.of_id = "abc"
+        self.o.device_id = "123"
+
+        m.delete("http://p_onos_url/onos/v1/network/configuration/devices/abc", 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)
+
+        SyncOLTDevice().delete_record(self.o)
+
+        # we don't need to assert here, if there are no exceptions it succeded
+
+if __name__ == "__main__":
+    unittest.main()
\ No newline at end of file
diff --git a/xos/synchronizer/volt_config.yaml b/xos/synchronizer/volt_config.yaml
index 12003cb..b3209aa 100644
--- a/xos/synchronizer/volt_config.yaml
+++ b/xos/synchronizer/volt_config.yaml
@@ -22,6 +22,26 @@
   - VOLTService
   - VOLTServiceInstance
   - ServiceInstanceLink
+  - OLTDevice
 dependency_graph: "/opt/xos/synchronizers/volt/model-deps"
 model_policies_dir: "/opt/xos/synchronizers/volt/model_policies"
 models_dir: "/opt/xos/synchronizers/volt/models"
+steps_dir: "/opt/xos/synchronizers/volt/steps"
+pull_steps_dir: "/opt/xos/synchronizers/volt/pull_steps"
+
+logging:
+  version: 1
+  handlers:
+    console:
+      class: logging.StreamHandler
+    file:
+      class: logging.handlers.RotatingFileHandler
+      filename: /var/log/xos.log
+      maxBytes: 10485760
+      backupCount: 5
+  loggers:
+    'multistructlog':
+      handlers:
+          - console
+          - file
+      level: DEBUG