VOL-762. ONU disable/delete in openolt.

In the broadcom_onu adapter, I added an if statement
that checks if the parent adapter is openolt before
attempting a direct method call for disable/delete
of an onu.

Change-Id: I9678e4d6be5cc2149304caf57271690c72033e47
diff --git a/voltha/adapters/broadcom_onu/broadcom_onu.py b/voltha/adapters/broadcom_onu/broadcom_onu.py
index fcb07c0..5d1103e 100644
--- a/voltha/adapters/broadcom_onu/broadcom_onu.py
+++ b/voltha/adapters/broadcom_onu/broadcom_onu.py
@@ -47,6 +47,8 @@
 from common.frameio.frameio import hexify
 from voltha.extensions.omci.omci import *
 
+from voltha.registry import registry
+
 _ = third_party
 log = structlog.get_logger()
 
@@ -164,12 +166,15 @@
         raise NotImplementedError()
 
     def delete_device(self, device):
-        log.info('delete-device', device_id=device.id)
+        log.info('delete-device', device_id=device.id, device_handlers=self.devices_handlers)
         if device.id in self.devices_handlers:
             handler = self.devices_handlers[device.id]
             if handler is not None:
+                log.debug('calling-handler-delete', handler=handler)
                 handler.delete(device)
-            del self.devices_handlers[device.id]
+                del self.devices_handlers[device.id]
+        else:
+            log.warn('device-not-found-in-handlers', device=device, device_handlers=self.devices_handlers)
         return
 
     def get_device_details(self, device):
@@ -321,6 +326,10 @@
         self.proxy_address = None
         self.tx_id = 0
 
+        # Proxy for api calls
+        self.core = registry('core')
+        self.proxy = self.core.get_proxy('/')
+
         # Need to query ONU for number of supported uni ports
         # For now, temporarily set number of ports to 1 - port #2
         self.uni_ports = (1, 2, 3, 4, 5)
@@ -487,10 +496,20 @@
         except Exception as e:
             self.log.exception("exception-updating-port",e=e)
 
+    @inlineCallbacks
     def delete(self, device):
-        self.log.info('delete-onu')
-        # The device is already deleted in delete_v_ont_ani(). No more
-        # handling needed here
+        self.log.info('delete-onu', device=device)
+
+        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-delete-onu', onu_device=device,
+                          parent_device=parent_device,
+                          parent_adapter=parent_adapter)
+            try:
+                parent_adapter.delete_child_device(parent_device.id, device)
+            except AttributeError:
+                self.log.debug('parent-device-delete-child-not-implemented')
 
     @inlineCallbacks
     def update_flow_table(self, device, flows):
@@ -1724,6 +1743,18 @@
             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 45c8b8e..3659baf 100644
--- a/voltha/adapters/openolt/openolt.py
+++ b/voltha/adapters/openolt/openolt.py
@@ -299,3 +299,13 @@
     def remove_multicast_distribution_set(self, device, data):
         log.info('remove-mcast-distribution-set', 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)
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 5971b68..4257abe 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -886,3 +886,29 @@
         # Set all ports to enabled
         self.log.info('enabling-all-ports', device_id=self.device_id)
         self.adapter_agent.enable_all_ports(self.device_id)
+
+    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,
+                       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.DeleteOnu(onu)
\ No newline at end of file
diff --git a/voltha/adapters/openolt/protos/openolt.proto b/voltha/adapters/openolt/protos/openolt.proto
index e13cc83..1140748 100644
--- a/voltha/adapters/openolt/protos/openolt.proto
+++ b/voltha/adapters/openolt/protos/openolt.proto
@@ -25,6 +25,20 @@
         };
     }
 
+    rpc DeactivateOnu(Onu) returns (Empty) {
+        option (google.api.http) = {
+          post: "/v1/DisableOnu"
+          body: "*"
+        };
+    }
+
+    rpc DeleteOnu(Onu) returns (Empty) {
+        option (google.api.http) = {
+          post: "/v1/DeleteOnu"
+          body: "*"
+        };
+    }
+
     rpc OmciMsgOut(OmciMsg) returns (Empty) {
         option (google.api.http) = {
           post: "/v1/OmciMsgOut"