Forward ONU events received by the maple_olt adapter to
the broadcom_onu adapter and handle them as required.

Change-Id: I7128cef40ab2a093ebbd72e8fb50a17e85e44a36
diff --git a/voltha/adapters/broadcom_onu/broadcom_onu.py b/voltha/adapters/broadcom_onu/broadcom_onu.py
index e02c9ed..a424831 100644
--- a/voltha/adapters/broadcom_onu/broadcom_onu.py
+++ b/voltha/adapters/broadcom_onu/broadcom_onu.py
@@ -20,7 +20,7 @@
 
 from uuid import uuid4
 import structlog
-from twisted.internet import reactor
+from twisted.internet import reactor, task
 from twisted.internet.defer import DeferredQueue, inlineCallbacks
 from zope.interface import implementer
 
@@ -143,6 +143,11 @@
 
     def receive_inter_adapter_message(self, msg):
         log.info('receive_inter_adapter_message', msg=msg)
+        proxy_address = msg['proxy_address']
+        assert proxy_address is not None
+
+        handler = self.devices_handlers[proxy_address.channel_id]
+        handler.event_messages.put(msg)
 
     def suppress_alarm(self, filter):
         raise NotImplementedError()
@@ -158,12 +163,65 @@
         self.device_id = device_id
         self.log = structlog.get_logger(device_id=device_id)
         self.incoming_messages = DeferredQueue()
+        self.event_messages = DeferredQueue()
         self.proxy_address = None
         self.tx_id = 0
 
+        # Need to query ONU for number of supported uni ports
+        # For now, temporarily set number of ports to 1 - port #2
+        self.uni_ports = (2,)
+
+        # Handle received ONU event messages
+        reactor.callLater(0, self.handle_onu_events)
+
     def receive_message(self, msg):
         self.incoming_messages.put(msg)
 
+    @inlineCallbacks
+    def handle_onu_events(self):
+        event_msg = yield self.event_messages.get()
+
+        if event_msg['event'] == 'activation-completed':
+
+            if event_msg['event_data']['activation_successful'] == True:
+                for uni in self.uni_ports:
+                    port_no = self.proxy_address.channel_id + uni
+                    reactor.callLater(1,
+                      self.message_exchange,
+                      self.proxy_address.onu_id,
+                      self.proxy_address.onu_session_id,
+                      port_no)
+
+                device = self.adapter_agent.get_device(self.device_id)
+                device.oper_status = OperStatus.ACTIVE
+                self.adapter_agent.update_device(device)
+
+            else:
+                device = self.adapter_agent.get_device(self.device_id)
+                device.oper_status = OperStatus.FAILED
+                self.adapter_agent.update_device(device)
+
+        elif event_msg['event'] == 'deactivation-completed':
+            device = self.adapter_agent.get_device(self.device_id)
+            device.oper_status = OperStatus.DISCOVERED
+            self.adapter_agent.update_device(device)
+
+        elif event_msg['event'] == 'ranging-completed':
+
+            if event_msg['event_data']['ranging_successful'] == True:
+                device = self.adapter_agent.get_device(self.device_id)
+                device.oper_status = OperStatus.ACTIVATING
+                self.adapter_agent.update_device(device)
+
+            else:
+                device = self.adapter_agent.get_device(self.device_id)
+                device.oper_status = OperStatus.FAILED
+                self.adapter_agent.update_device(device)
+
+        # Handle next event
+        reactor.callLater(0, self.handle_onu_events)
+
+
     def activate(self, device):
         self.log.info('activating')
 
@@ -205,11 +263,7 @@
         logical_device_id = parent_device.parent_id
         assert logical_device_id
 
-        # query ONU for number of supported uni ports
-        # temporarily set number of ports to 1 - port #2
-        uni_ports = (2,)
-
-        for uni in uni_ports:
+        for uni in self.uni_ports:
             # register physical ports
             uni_port = Port(
                 port_no=uni,
@@ -244,14 +298,8 @@
                 device_port_no=uni_port.port_no
             ))
 
-            reactor.callLater(10,
-                              self.message_exchange,
-                              self.proxy_address.onu_id,
-                              self.proxy_address.onu_session_id,
-                              port_no)
-
         device = self.adapter_agent.get_device(device.id)
-        device.oper_status = OperStatus.ACTIVE
+        device.oper_status = OperStatus.DISCOVERED
         self.adapter_agent.update_device(device)
 
     @inlineCallbacks
diff --git a/voltha/adapters/maple_olt/maple_olt.py b/voltha/adapters/maple_olt/maple_olt.py
index 89204b7..f77db19 100644
--- a/voltha/adapters/maple_olt/maple_olt.py
+++ b/voltha/adapters/maple_olt/maple_olt.py
@@ -293,14 +293,17 @@
             # event: 'disable-completed'
             #     event_data: {'serial_num-vendor_id': <str>
             #                  'serial_num-vendor_specific: <str>}
-            event_dict = {'event':event, 'event_data':event_data}
 
             # Get child_device from onu_id
             child_device = self.adapter_agent.get_child_device(self.device_id, onu_id=key['onu_id'])
             assert child_device is not None
 
+            # Build the message, the ONU adapter uses the proxy_address
+            # to uniquely identify a specific ONU
+            msg = {'proxy_address':child_device.proxy_address, 'event':event, 'event_data':event_data}
+
             # Send the event message to the ONU adapter
-            self.adapter_agent.publish_inter_adapter_message(child_device.id, event_dict)
+            self.adapter_agent.publish_inter_adapter_message(child_device.id, msg)
 
         elif _object == 'alloc_id':
             # key: {'device_id': <int>, 'pon_ni': <int>, 'onu_id': <int>, 'alloc_id': ,<int>}