[SEBA-81] Getting ONU Serial Number from logical device openflow info
Reading the correct port_id for UNI Ports

Change-Id: Idfcc9932762d4659ae413d73e99857d81c46c1c1
diff --git a/.gitignore b/.gitignore
index 178635a..3d95f9c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
 .idea/
 *.pyc
-.DS_Store
\ No newline at end of file
+.DS_Store
+
+xos/.coverage
+xos/htmlcov
\ No newline at end of file
diff --git a/samples/pon_port.yaml b/samples/pon_port.yaml
index 28218e2..684238e 100644
--- a/samples/pon_port.yaml
+++ b/samples/pon_port.yaml
@@ -20,6 +20,7 @@
   - custom_types/onudevice.yaml
   - custom_types/ponport.yaml
   - custom_types/voltservice.yaml
+  - custom_types/uniport.yaml
 description: Create a simulated OLT Device in VOLTHA
 topology_template:
   node_templates:
@@ -41,7 +42,7 @@
             node: device#olt
             relationship: tosca.relationships.BelongsToOne
 
-    onu:
+    device#onu:
       type: tosca.nodes.ONUDevice
       properties:
         serial_number: BRCM1234
@@ -50,3 +51,13 @@
         - pon_port:
             node: pon_port
             relationship: tosca.relationships.BelongsToOne
+    
+    uni_port:
+      type: tosca.nodes.UNIPort
+      properties:
+        name: test_uni_port_1
+        port_no: 2
+      requirements:
+        - onu_device:
+            node: device#onu
+            relationship: tosca.relationships.BelongsToOne
diff --git a/samples/subscriber.yaml b/samples/subscriber.yaml
index 4b38bb4..1c38756 100644
--- a/samples/subscriber.yaml
+++ b/samples/subscriber.yaml
@@ -26,7 +26,6 @@
       properties:
         name: My House
         c_tag: 111
-        onu_device: BRCM1234
-        uni_port_id: 101
+        onu_device: 691d51a676f348319ca91972bc2bddcf
         mac_address: 00:AA:00:00:00:01
         ip_address: 10.8.2.1
diff --git a/xos/synchronizer/models/convenience/voltservice.py b/xos/synchronizer/models/convenience/voltservice.py
new file mode 100644
index 0000000..032aabd
--- /dev/null
+++ b/xos/synchronizer/models/convenience/voltservice.py
@@ -0,0 +1,52 @@
+
+# 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 xosapi.orm import ORMWrapper, register_convenience_wrapper
+from xosapi.convenience.service import ORMWrapperService
+
+import logging as log
+
+class ORMWrapperVOLTService(ORMWrapperService):
+
+    def get_onu_sn_from_openflow(self, dp_id, port_no):
+        """Return the ONU serial number from logical_device informations
+        
+        example usage:
+            volt = VOLTService.objects.first()
+            sn = volt.get_onu_from_openflow("of:0000000ce2314000", 2)
+            # BRCM1234
+
+        Arguments:
+            dp_id {string} -- The openflow id of the OLT device
+            port_no {int} -- The openflow port id (UNI Port)
+        
+        Returns:
+            string -- ONU Serial Number
+        """
+
+        log.debug("Searching ONUDevice for %s:%s", (dp_id, port_no))
+        print "Searching ONUDevice for %s:%s", (dp_id, port_no)
+        try:
+            olt = self.stub.OLTDevice.objects.get(dp_id=dp_id)
+            uni_ports = self.stub.UNIPort.objects.filter(port_no=port_no)
+            onu = [o.onu_device for o in uni_ports if o.onu_device.pon_port.olt_device.id == olt.id][0]
+            return onu.serial_number
+        except IndexError:
+            log.error("Can't find ONU for %s:%s", (dp_id, port_no))
+        except Exception:
+            log.exception("Error while finding ONUDevice for %s:%s", (dp_id, port_no))
+
+register_convenience_wrapper("VOLTService", ORMWrapperVOLTService)
\ No newline at end of file
diff --git a/xos/synchronizer/pull_steps/pull_onus.py b/xos/synchronizer/pull_steps/pull_onus.py
index 13a051b..4db1428 100644
--- a/xos/synchronizer/pull_steps/pull_onus.py
+++ b/xos/synchronizer/pull_steps/pull_onus.py
@@ -148,16 +148,44 @@
         self.create_or_update_uni_port(uni_ports, onu)
         self.create_or_update_pon_onu_port(pon_onu_ports, onu)
 
+    def get_onu_port_id(self, port, onu):
+        # find the correct port id as represented in the logical_device
+        logical_device_id = onu.pon_port.olt_device.of_id
+
+        voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
+        voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
+
+        try:
+            r = requests.get("%s:%s/api/v1/logical_devices/%s/ports" % (voltha_url, voltha_port, logical_device_id))
+
+            if r.status_code != 200:
+                log.info("It was not possible to fetch ports from VOLTHA for logical_device %s" % logical_device_id)
+
+            logical_ports = r.json()['items']
+            log.info("logical device ports for ONUDevice %s" % onu.device_id, logical_ports=logical_ports)
+
+            ports = [p['id'] for p in logical_ports if p['device_id'] == onu.device_id]
+            # log.info("Port_id for port %s on ONUDevice %s: %s" % (port['label'], onu.device_id, ports), logical_ports=logical_ports)
+            return int(ports[0])
+
+        except ConnectionError, e:
+            log.warn("It was not possible to connect to VOLTHA", reason=e)
+            return
+        except InvalidURL, e:
+            log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
+            return
+
     def create_or_update_uni_port(self, uni_ports, onu):
         update_ports = []
 
         for port in uni_ports:
+            port_no = self.get_onu_port_id(port, onu)
             try:
-                model = UNIPort.objects.filter(port_no=port["port_no"], onu_device_id=onu.id)[0]
-                log.debug("UNIPort already exists, updating it", port_no=port["port_no"], onu_device_id=onu.id)
+                model = UNIPort.objects.filter(port_no=port_no, onu_device_id=onu.id)[0]
+                log.debug("UNIPort already exists, updating it", port_no=port_no, onu_device_id=onu.id)
             except IndexError:
                 model = UNIPort()
-                model.port_no = port["port_no"]
+                model.port_no = port_no
                 model.onu_device_id = onu.id
                 model.name = port["label"]
                 log.debug("UNIPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id)
diff --git a/xos/unittest.cfg b/xos/unittest.cfg
index 426392e..ac55cdc 100644
--- a/xos/unittest.cfg
+++ b/xos/unittest.cfg
@@ -6,3 +6,9 @@
                  pull_steps
                  event_steps
                  models
+
+[coverage]
+always-on = True
+coverage = synchronizer
+coverage-report = term
+coverage-report = html
\ No newline at end of file