VOL-1207 Early init of alarm_mgr, stats_mgr and bw_mgr

Currently much of the initialization in the OpenOLT adapter
happens after the gRPC connection to the device is established.
We can move some parts of the initialization to happen earlier
(those that do not require the device connectivity).

Change-Id: I8cd0fc863b15badf667dd162e7c39f033df1e004
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 8bcf59a..c7410fc 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -37,7 +37,6 @@
 from voltha.protos.bbf_fiber_tcont_body_pb2 import TcontsConfigData
 from voltha.protos.bbf_fiber_gemport_body_pb2 import GemportsConfigData
 from voltha.protos.bbf_fiber_base_pb2 import VEnetConfig
-import voltha.core.flow_decomposer as fd
 
 from voltha.adapters.openolt.openolt_statistics import OpenOltStatisticsMgr
 import voltha.adapters.openolt.openolt_platform as platform
@@ -158,20 +157,20 @@
             # Start indications thread
             self.indications_thread_handle = threading.Thread(
                 target=self.indications_thread)
-            # Old getter/setter API for daemon; use it directly as a 
+            # Old getter/setter API for daemon; use it directly as a
             # property instead. The Jinkins error will happon on the reason of
-            # Exception in thread Thread-1 (most likely raised # during 
+            # Exception in thread Thread-1 (most likely raised # during
             # interpreter shutdown)
             self.indications_thread_handle.setDaemon(True)
             self.indications_thread_handle.start()
-        except Exception as e: 
+        except Exception as e:
             self.log.exception('do_state_init failed', e=e)
-        '''
-        # FIXME - Move to oper_up state without connecting to OLT?
-        if is_reconciliation:
-            # Put state machine in state up
-            reactor.callFromThread(self.go_state_up, reconciliation=True)
-        '''
+
+        self.alarm_mgr = OpenOltAlarmMgr(self.log, self.adapter_agent,
+                                         self.device_id,
+                                         self.logical_device_id)
+        self.stats_mgr = OpenOltStatisticsMgr(self, self.log)
+        self.bw_mgr = OpenOltBW(self.log, self.proxy)
 
         self.log.info('openolt-device-created', device_id=self.device_id)
 
@@ -192,17 +191,11 @@
 
         self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id,
                                        self.logical_device_id)
-        self.alarm_mgr = OpenOltAlarmMgr(self.log, self.adapter_agent,
-                                         self.device_id,
-                                         self.logical_device_id)
-        self.stats_mgr = OpenOltStatisticsMgr(self, self.log)
-        self.bw_mgr = OpenOltBW(self.log, self.proxy)
 
         # TODO: use content of device_info for Resource manager (VOL-948)
 
         # TODO: check for uptime and reboot if too long (VOL-1192)
 
-
         device.connect_status = ConnectStatus.REACHABLE
         self.adapter_agent.update_device(device)
 
@@ -232,14 +225,13 @@
             uni_name = self.port_name(uni_no, Port.ETHERNET_UNI,
                                       serial_number=onu_device.serial_number)
 
-
             if onu_device.adapter == 'brcm_openomci_onu':
                 self.log.debug('using-brcm_openomci_onu, update_interface '
                                'down')
                 onu_adapter_agent = \
                     registry('adapter_loader').get_agent(onu_device.adapter)
                 onu_adapter_agent.update_interface(onu_device,
-                                                   {'oper_state' :'down'})
+                                                   {'oper_state': 'down'})
 
             self.onu_ports_down(onu_device, uni_no, uni_name, oper_state)
         # Children devices
@@ -439,7 +431,7 @@
                 self.adapter_agent.update_device(onu_device)
                 try:
                     self.activate_onu(intf_id, onu_id, serial_number,
-                                  serial_number_str)
+                                      serial_number_str)
                 except Exception as e:
                     self.log.error('onu-activation-error',
                                    serial_number=serial_number_str, error=e)
@@ -475,10 +467,8 @@
                            onu_id=onu_indication.onu_id)
             return
 
-
-
         if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
-           != onu_indication.intf_id:
+                != onu_indication.intf_id:
             self.log.warn('ONU-is-on-a-different-intf-id-now',
                           previous_intf_id=platform.intf_id_from_pon_port_no(
                               onu_device.parent_port_no),
@@ -545,7 +535,7 @@
             if onu_device.adapter == 'brcm_openomci_onu':
                 self.log.debug('using-brcm_openomci_onu')
                 onu_adapter_agent.update_interface(onu_device,
-                                               {'oper_state':'down'})
+                                                   {'oper_state': 'down'})
 
         elif onu_indication.oper_state == 'up':
 
@@ -760,7 +750,7 @@
 
         elif egress_port_type == Port.ETHERNET_NNI:
             self.log.debug('sending-packet-to-uplink', egress_port=egress_port,
-                          packet=str(pkt).encode("HEX"))
+                           packet=str(pkt).encode("HEX"))
 
             send_pkt = binascii.unhexlify(str(pkt).encode("HEX"))
 
@@ -776,10 +766,11 @@
                           port_type=egress_port_type)
 
     def send_proxied_message(self, proxy_address, msg):
-        onu_device = self.adapter_agent.get_child_device(self.device_id,
-                onu_id=proxy_address.onu_id,
-                parent_port_no=platform.intf_id_to_port_no(
-                    proxy_address.channel_id, Port.PON_OLT))
+        onu_device = self.adapter_agent.get_child_device(
+            self.device_id,
+            onu_id=proxy_address.onu_id,
+            parent_port_no=platform.intf_id_to_port_no(
+                proxy_address.channel_id, Port.PON_OLT))
         if onu_device.connect_status != ConnectStatus.REACHABLE:
             self.log.debug('ONU is not reachable, cannot send OMCI',
                            serial_number=onu_device.serial_number,
@@ -864,7 +855,7 @@
         label = self.port_name(port_no, port_type, intf_id)
 
         self.log.debug('adding-port', port_no=port_no, label=label,
-                      port_type=port_type)
+                       port_type=port_type)
 
         port = Port(port_no=port_no, label=label, type=port_type,
                     admin_state=AdminState.ENABLED, oper_status=oper_status)
@@ -884,6 +875,7 @@
                 self.adapter_agent.delete_logical_port(
                     self.logical_device_id, logical_port)
                 return
+
     def delete_port(self, child_serial_number):
         ports = self.proxy.get('/devices/{}/ports'.format(
             self.device_id))
@@ -895,12 +887,11 @@
                 self.adapter_agent.delete_port(self.device_id, port)
                 return
 
-
     def new_onu_id(self, intf_id):
         onu_devices = self.adapter_agent.get_child_devices(self.device_id)
-        pon_onu_ids = [onu_device.proxy_address.onu_id for onu_device in
-                              onu_devices if
-                           onu_device.proxy_address.channel_id == intf_id]
+        pon_onu_ids = [onu_device.proxy_address.onu_id
+                       for onu_device in onu_devices
+                       if onu_device.proxy_address.channel_id == intf_id]
         for i in range(1, platform.MAX_ONUS_PER_PON):
             if i not in pon_onu_ids:
                 return i
@@ -909,12 +900,10 @@
                        intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
         return None
 
-
     def update_flow_table(self, flows):
         self.log.debug('No updates here now, all is done in logical flows '
                        'update')
 
-
     def update_logical_flows(self, flows_to_add, flows_to_remove,
                              device_rules_map):
         if not self.is_state_up():
@@ -929,7 +918,7 @@
             self.log.error('Error updating children flows', error=e)
 
         self.log.debug('logical flows update', flows_to_add=flows_to_add,
-            flows_to_remove=flows_to_remove)
+                       flows_to_remove=flows_to_remove)
 
         for flow in flows_to_add:
 
@@ -937,15 +926,13 @@
                 self.flow_mgr.add_flow(flow)
             except grpc.RpcError as grpc_e:
                 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
-                    self.log.warn('flow already exists', e=grpc_e,
-                                   flow=flow)
+                    self.log.warn('flow already exists', e=grpc_e, flow=flow)
                 else:
                     self.log.error('failed to add flow', flow=flow,
                                    grpc_error=grpc_e)
             except Exception as e:
                 self.log.error('failed to add flow', flow=flow, e=e)
 
-
         for flow in flows_to_remove:
 
             try:
@@ -977,7 +964,6 @@
             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(
@@ -991,7 +977,7 @@
 
     def disable(self):
         self.log.debug('sending-deactivate-olt-message',
-                      device_id=self.device_id)
+                       device_id=self.device_id)
 
         try:
             # Send grpc call
@@ -1002,7 +988,6 @@
         except Exception as e:
             self.log.error('Failure to disable openolt device', error=e)
 
-
     def delete(self):
         self.log.info('deleting-olt', device_id=self.device_id,
                       logical_device_id=self.logical_device_id)
@@ -1019,7 +1004,6 @@
         else:
             self.log.info('successfully-deleted-olt', device_id=self.device_id)
 
-
     def reenable(self):
         self.log.debug('reenabling-olt', device_id=self.device_id)
 
@@ -1037,14 +1021,13 @@
                      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)
+                       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 delete_child_device(self, child_device):
         self.log.debug('sending-deactivate-onu',
                        olt_device_id=self.device_id,
@@ -1052,7 +1035,8 @@
                        onu_serial_number=child_device.serial_number)
         try:
             self.adapter_agent.delete_child_device(self.device_id,
-                                               child_device.id, child_device)
+                                                   child_device.id,
+                                                   child_device)
         except Exception as e:
             self.log.error('adapter_agent error', error=e)
         try:
@@ -1063,14 +1047,15 @@
             self.delete_port(child_device.serial_number)
         except Exception as e:
             self.log.error('port delete error', error=e)
-        serial_number = self.destringify_serial_number(child_device.serial_number)
+        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)
         self.stub.DeleteOnu(onu)
 
     def reboot(self):
-        self.log.debug('rebooting openolt device', device_id = self.device_id)
+        self.log.debug('rebooting openolt device', device_id=self.device_id)
         try:
             self.stub.Reboot(openolt_pb2.Empty())
         except Exception as e:
@@ -1086,4 +1071,3 @@
                            error=e)
         else:
             self.log.info('statistics requested')
-