VOL-1169 OpenOLT - ONU disable/reenable/delte clean up

Removing diable_child/reenable_child, adding clean serial number destringification and consolidate onu-activation in one place

Change-Id: Ic2e1b2b751c6ca00df1cfcaa45087ecc05d67fc5
diff --git a/voltha/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py b/voltha/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py
index a9b7614..a21980c 100644
--- a/voltha/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py
+++ b/voltha/adapters/brcm_openomci_onu/brcm_openomci_onu_handler.py
@@ -634,20 +634,6 @@
             task = BrcmUniLockTask(self.omci_agent, self.device_id, lock=True)
             self._deferred = self._onu_omci_device.task_runner.queue_task(task)
             self._deferred.addCallbacks(stop_anyway, stop_anyway)
-            '''
-            # Disable in parent device (OLT)
-            parent_device = self.adapter_agent.get_device(device.parent_id)
-
-            if parent_device.type == 'openolt':
-                parent_adapter = registry('adapter_loader').get_agent(parent_device.adapter).adapter
-                self.log.info('parent-adapter-disable-onu', onu_device=device,
-                              parent_device=parent_device,
-                              parent_adapter=parent_adapter)
-                try:
-                    parent_adapter.disable_child_device(parent_device.id, device)
-                except AttributeError:
-                    self.log.debug('parent-device-disable-child-not-implemented')
-            '''
         except Exception as e:
             log.exception('exception-in-onu-disable', exception=e)
 
diff --git a/voltha/adapters/broadcom_onu/broadcom_onu.py b/voltha/adapters/broadcom_onu/broadcom_onu.py
index 9618ee7..8b770ec 100644
--- a/voltha/adapters/broadcom_onu/broadcom_onu.py
+++ b/voltha/adapters/broadcom_onu/broadcom_onu.py
@@ -1746,20 +1746,6 @@
             device.oper_status = OperStatus.UNKNOWN
             device.connect_status = ConnectStatus.UNREACHABLE
             self.adapter_agent.update_device(device)
-            '''
-            # Disable in parent device (OLT)
-            parent_device = self.adapter_agent.get_device(device.parent_id)
-
-            if parent_device.type == 'openolt':
-                parent_adapter = registry('adapter_loader').get_agent(parent_device.adapter).adapter
-                self.log.info('parent-adapter-disable-onu', onu_device=device,
-                              parent_device=parent_device,
-                              parent_adapter=parent_adapter)
-                try:
-                    parent_adapter.disable_child_device(parent_device.id, device)
-                except AttributeError:
-                    self.log.debug('parent-device-disable-child-not-implemented')
-            '''
         except Exception as e:
             log.exception('exception-in-onu-disable', exception=e)
 
diff --git a/voltha/adapters/openolt/openolt.py b/voltha/adapters/openolt/openolt.py
index e2df59c..219d5d4 100644
--- a/voltha/adapters/openolt/openolt.py
+++ b/voltha/adapters/openolt/openolt.py
@@ -342,14 +342,13 @@
                  'not use this', data=data)
         raise NotImplementedError()
 
-    def disable_child_device(self, parent_device_id, child_device):
-        log.info('disable-child_device', parent_device_id=parent_device_id,
-                 child_device=child_device)
-        handler = self.devices[parent_device_id]
-        handler.disable_child_device(child_device)
-
     def delete_child_device(self, parent_device_id, child_device):
         log.info('delete-child_device', parent_device_id=parent_device_id,
                  child_device=child_device)
         handler = self.devices[parent_device_id]
-        handler.delete_child_device(child_device)
+        if handler is not None:
+            handler.delete_child_device(child_device)
+        else:
+            log.error('Could not find matching handler',
+                      looking_for_device_id =parent_device_id,
+                      available_handlers=self.devices.keys())
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 366b900..1127e83 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -363,10 +363,6 @@
                                errmsg=disc_alarm_error.message)
             # continue for now.
 
-        pir = self.bw_mgr.pir(serial_number_str)
-        self.log.debug("peak information rate", serial_number=serial_number,
-                       pir=pir)
-
         onu_device = self.adapter_agent.get_child_device(
             self.device_id,
             serial_number=serial_number_str)
@@ -378,11 +374,8 @@
                     intf_id,
                     platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
                     onu_id, serial_number)
-                self.log.info("activate-onu", intf_id=intf_id, onu_id=onu_id,
-                              serial_number=serial_number_str)
-                onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
-                                      serial_number=serial_number, pir=pir)
-                self.stub.ActivateOnu(onu)
+                self.activate_onu(intf_id, onu_id, serial_number,
+                                  serial_number_str)
             except Exception as e:
                 self.log.exception('onu-activation-failed', e=e)
 
@@ -405,15 +398,17 @@
                               state=onu_device.oper_status)
             elif onu_device.oper_status == OperStatus.UNKNOWN:
                 self.log.info("onu in unknown state, recovering from olt \
-                              reboot, activate onu", intf_id=intf_id,
+                              reboot probably, activate onu", intf_id=intf_id,
                               onu_id=onu_id, serial_number=serial_number_str)
 
                 onu_device.oper_status = OperStatus.DISCOVERED
                 self.adapter_agent.update_device(onu_device)
-
-                onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
-                                      serial_number=serial_number, pir=pir)
-                self.stub.ActivateOnu(onu)
+                try:
+                    self.activate_onu(intf_id, onu_id, serial_number,
+                                  serial_number_str)
+                except Exception as e:
+                    self.log.error('onu-activation-error',
+                                   serial_number=serial_number_str, error=e)
             else:
                 self.log.warn('unexpected state', onu_id=onu_id,
                               onu_device_oper_state=onu_device.oper_status)
@@ -872,16 +867,6 @@
                        intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
         return None
 
-    def stringify_vendor_specific(self, vendor_specific):
-        return ''.join(str(i) for i in [
-                hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
-                hex(ord(vendor_specific[0]) & 0x0f)[2:],
-                hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
-                hex(ord(vendor_specific[1]) & 0x0f)[2:],
-                hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
-                hex(ord(vendor_specific[2]) & 0x0f)[2:],
-                hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
-                hex(ord(vendor_specific[3]) & 0x0f)[2:]])
 
     def update_flow_table(self, flows):
         self.log.debug('No updates here now, all is done in logical flows '
@@ -934,11 +919,29 @@
             hex_ip.append(octet_hex)
         return ":".join(hex_ip)
 
+    def stringify_vendor_specific(self, vendor_specific):
+        return ''.join(str(i) for i in [
+            hex(ord(vendor_specific[0]) >> 4 & 0x0f)[2:],
+            hex(ord(vendor_specific[0]) & 0x0f)[2:],
+            hex(ord(vendor_specific[1]) >> 4 & 0x0f)[2:],
+            hex(ord(vendor_specific[1]) & 0x0f)[2:],
+            hex(ord(vendor_specific[2]) >> 4 & 0x0f)[2:],
+            hex(ord(vendor_specific[2]) & 0x0f)[2:],
+            hex(ord(vendor_specific[3]) >> 4 & 0x0f)[2:],
+            hex(ord(vendor_specific[3]) & 0x0f)[2:]])
+
+
     def stringify_serial_number(self, serial_number):
         return ''.join([serial_number.vendor_id,
                         self.stringify_vendor_specific(
                             serial_number.vendor_specific)])
 
+    def destringify_serial_number(self, serial_number_str):
+        serial_number = openolt_pb2.SerialNumber(
+            vendor_id=serial_number_str[:4].encode('utf-8'),
+            vendor_specific=binascii.unhexlify(serial_number_str[4:]))
+        return serial_number
+
     def disable(self):
         self.log.debug('sending-deactivate-olt-message',
                       device_id=self.device_id)
@@ -983,23 +986,18 @@
         else:
             self.log.info('openolt device reenabled')
 
+    def activate_onu(self, intf_id, onu_id, serial_number,
+                     serial_number_str):
+        pir = self.bw_mgr.pir(serial_number_str)
+        self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
+                      serial_number_str=serial_number_str,
+                      serial_number=serial_number, pir=pir)
+        onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
+                              serial_number=serial_number, pir=pir)
+        self.stub.ActivateOnu(onu)
+        self.log.info('onu-activated', serial_number=serial_number_str)
 
 
-    def disable_child_device(self, child_device):
-        self.log.debug('sending-disable-onu',
-                       olt_device_id=self.device_id,
-                       onu_device=child_device,
-                       onu_serial_number=child_device.serial_number)
-        vendor_id = child_device.vendor_id.encode('hex')
-        vendor_specific = child_device.serial_number.replace(
-            child_device.vendor_id, '').encode('hex')
-        serial_number = openolt_pb2.SerialNumber(
-            vendor_id=vendor_id, vendor_specific=vendor_specific)
-        onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
-                              onu_id=child_device.proxy_address.onu_id,
-                              serial_number=serial_number)
-        self.stub.DeactivateOnu(onu)
-
     def delete_child_device(self, child_device):
         self.log.debug('sending-deactivate-onu',
                        olt_device_id=self.device_id,
@@ -1018,11 +1016,7 @@
             self.delete_port(child_device.serial_number)
         except Exception as e:
             self.log.error('port delete error', error=e)
-        vendor_id = child_device.vendor_id.encode('hex')
-        vendor_specific = child_device.serial_number.replace(
-            child_device.vendor_id, '').encode('hex')
-        serial_number = openolt_pb2.SerialNumber(
-            vendor_id=vendor_id, vendor_specific=vendor_specific)
+        serial_number = self.destringify_serial_number(child_device.serial_number)
         onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
                               onu_id=child_device.proxy_address.onu_id,
                               serial_number=serial_number)