VOL-1477 Migrate OpenOltAlarmMgr adapter_agent

ONU's serial_number is included in the alarm context
deviceinfo is saved in the when the class is instantiated.

Change-Id: I4d145d6c0dc1fc4bad973fce2dd435bdafa389f7
diff --git a/python/adapters/openolt/openolt_alarms.py b/python/adapters/openolt/openolt_alarms.py
index 932855c..84bd7b8 100644
--- a/python/adapters/openolt/openolt_alarms.py
+++ b/python/adapters/openolt/openolt_alarms.py
@@ -36,7 +36,7 @@
 
 class OpenOltAlarmMgr(object):
     def __init__(self, log, adapter_agent, device_id, logical_device_id,
-                 platform):
+                 platform, serial_number):
         """
         20180711 -  Addition of adapter_agent and device_id
             to facilitate alarm processing and kafka posting
@@ -49,6 +49,7 @@
         self.device_id = device_id
         self.logical_device_id = logical_device_id
         self.platform = platform
+        self.serial_number = serial_number
         """
         The following is added to reduce the continual posting of OLT LOS alarming
         to Kafka.   Set enable_alarm_suppress = true to enable  otherwise the
@@ -58,7 +59,7 @@
         self.enable_alarm_suppress = True
         self.alarm_suppress = {"olt_los_clear": 0, "onu_disc_raised": []}  # Keep count of alarms to limit.
         try:
-            self.alarms = AdapterAlarms(self.adapter_agent, self.device_id, self.logical_device_id)
+            self.alarms = AdapterAlarms(self.adapter_agent, self.device_id, self.logical_device_id, self.serial_number)
             self.simulator = AdapterAlarmSimulator(self.alarms)
         except Exception as initerr:
             self.log.exception("alarmhandler-init-error", errmsg=initerr.message)
@@ -139,7 +140,7 @@
             try:
                 """
                 Get the specific onu device information for the onu generating the alarm.
-                Extract the id.   In the future extract the serial number as well
+                Extract the id.
                 """
                 onu_device_id = "unresolved"
                 onu_serial_number = "unresolved"
@@ -150,10 +151,10 @@
 
                 if dying_gasp_ind.status == 1 or dying_gasp_ind.status == "on":
                     OnuDyingGaspAlarm(self.alarms, dying_gasp_ind.intf_id,
-                                      onu_device_id).raise_alarm()
+                                      onu_device_id, serial_number=onu_serial_number).raise_alarm()
                 else:
                     OnuDyingGaspAlarm(self.alarms, dying_gasp_ind.intf_id,
-                                      onu_device_id).clear_alarm()
+                                      onu_device_id, serial_number=onu_serial_number).clear_alarm()
             except Exception as alarm_err:
                 self.log.exception('dying-gasp-indication', errmsg=alarm_err.message)
 
@@ -187,7 +188,7 @@
             try:
                 """
                     Get the specific onu device information for the onu generating the alarm.
-                    Extract the id.   In the future extract the serial number as well
+                    Extract the id.
                 """
                 onu_device_id = "unresolved"
                 serial_number = "unresolved"
@@ -197,30 +198,30 @@
                     serial_number = onu_device.serial_number
 
                 if onu_alarm_ind.los_status == 1 or onu_alarm_ind.los_status == "on":
-                    OnuLosAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).raise_alarm()
+                    OnuLosAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).raise_alarm()
                 elif onu_alarm_ind.los_status == 0 or onu_alarm_ind.los_status == "off":
-                    OnuLosAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).clear_alarm()
+                    OnuLosAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).clear_alarm()
                 else:     # No Change
                     pass
 
                 if onu_alarm_ind.lopc_miss_status == 1 or onu_alarm_ind.lopc_miss_status == "on":
-                    OnuLopcMissAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).raise_alarm()
+                    OnuLopcMissAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).raise_alarm()
                 elif (onu_alarm_ind.lopc_miss_status == 0 or onu_alarm_ind.lopc_miss_status == "off"):
-                    OnuLopcMissAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).clear_alarm()
+                    OnuLopcMissAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).clear_alarm()
                 else:     # No Change
                     pass
 
                 if onu_alarm_ind.lopc_mic_error_status == 1 or onu_alarm_ind.lopc_mic_error_status == "on":
-                    OnuLopcMicErrorAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).raise_alarm()
+                    OnuLopcMicErrorAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).raise_alarm()
                 elif onu_alarm_ind.lopc_mic_error_status == 0 or onu_alarm_ind.lopc_mic_error_status == "off":
-                    OnuLopcMicErrorAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).clear_alarm()
+                    OnuLopcMicErrorAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).clear_alarm()
                 else:     # No Change
                     pass
 
                 if onu_alarm_ind.lob_status == 1 or onu_alarm_ind.lob_status == "on":
-                    OnuLobAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).raise_alarm()
+                    OnuLobAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).raise_alarm()
                 elif onu_alarm_ind.lob_status == 0 or onu_alarm_ind.lob_status == "off":
-                    OnuLobAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id).clear_alarm()
+                    OnuLobAlarm(self.alarms, onu_id=onu_device_id, intf_id=onu_alarm_ind.intf_id, serial_number=serial_number).clear_alarm()
                 else:     # No Change
                     pass
             except Exception as alarm_err:
@@ -246,10 +247,18 @@
             label = "onu-startup-failure-indication"
             self.log.debug(label + " received", onu_startup_fail_ind=ind, int_id=ind.intf_id, onu_id=ind.onu_id, status=ind.status)
             try:
+                """
+                    Get the specific onu device information for the onu generating the alarm.
+                """
+                serial_number = "unresolved"
+                onu_device = self.resolve_onu_id(ind.onu_id,  port_intf_id=ind.intf_id)
+                if onu_device != None:
+                    serial_number = onu_device.serial_number
+                    
                 if ind.status == 1 or ind.status == "on":
-                    OnuStartupAlarm(self.alarms, intf_id=ind.intf_id,onu_id=ind.onu_id).raise_alarm()
+                    OnuStartupAlarm(self.alarms, intf_id=ind.intf_id,onu_id=ind.onu_id, serial_number=serial_number).raise_alarm()
                 else:
-                    OnuStartupAlarm(self.alarms, intf_id=ind.intf_id, onu_id=ind.onu_id).clear_alarm()
+                    OnuStartupAlarm(self.alarms, intf_id=ind.intf_id, onu_id=ind.onu_id, serial_number=serial_number).clear_alarm()
             except Exception as alarm_err:
                 self.log.exception(label, errmsg=alarm_err.message)
 
@@ -278,12 +287,21 @@
                            inverse_bit_error_rate=ind.inverse_bit_error_rate,
                            status=ind.status)
             try:
+                """
+                    Get the specific onu device information for the onu generating the alarm.
+                    Extract the id.   In the future extract the serial number as well
+                """
+                serial_number = "unresolved"
+                onu_device = self.resolve_onu_id(ind.onu_id,  port_intf_id=ind.intf_id)
+                if onu_device != None:
+                    serial_number = onu_device.serial_number
+
                 if ind.status == 1 or ind.status == "on":
                     OnuSignalDegradeAlarm(self.alarms, intf_id=ind.intf_id, onu_id=ind.onu_id,
-                                          inverse_bit_error_rate=ind.inverse_bit_error_rate).raise_alarm()
+                                          inverse_bit_error_rate=ind.inverse_bit_error_rate, serial_number=serial_number).raise_alarm()
                 else:
                     OnuSignalDegradeAlarm(self.alarms, intf_id=ind.intf_id, onu_id=ind.onu_id,
-                                          inverse_bit_error_rate=ind.inverse_bit_error_rate).clear_alarm()
+                                          inverse_bit_error_rate=ind.inverse_bit_error_rate, serial_number=serial_number).clear_alarm()
             except Exception as alarm_err:
                 self.log.exception(label, errmsg=alarm_err.message)
 
@@ -324,12 +342,14 @@
                     OnuWindowDriftAlarm(self.alarms, intf_id=ind.intf_id,
                            onu_id=onu_device_id,
                            drift=ind.drift,
-                           new_eqd=ind.new_eqd).raise_alarm()
+                           new_eqd=ind.new_eqd,
+                           serial_number=onu_serial_number).raise_alarm()
                 else:
                     OnuWindowDriftAlarm(self.alarms, intf_id=ind.intf_id,
                            onu_id=onu_device_id,
                            drift=ind.drift,
-                           new_eqd=ind.new_eqd).clear_alarm()
+                           new_eqd=ind.new_eqd,
+                           serial_number=onu_serial_number).clear_alarm()
             except Exception as alarm_err:
                 self.log.exception(label, errmsg=alarm_err.message)
 
@@ -371,11 +391,13 @@
                 if ind.status == 1 or ind.status == "on":
                     OnuSignalFailAlarm(self.alarms, intf_id=ind.intf_id,
                            onu_id=onu_device_id,
-                           inverse_bit_error_rate=ind.inverse_bit_error_rate).raise_alarm()
+                           inverse_bit_error_rate=ind.inverse_bit_error_rate,
+                           serial_number=onu_serial_number).raise_alarm()
                 else:
                     OnuSignalFailAlarm(self.alarms, intf_id=ind.intf_id,
                            onu_id=onu_device_id,
-                           inverse_bit_error_rate=ind.inverse_bit_error_rate).clear_alarm()
+                           inverse_bit_error_rate=ind.inverse_bit_error_rate,
+                           serial_number=onu_serial_number).clear_alarm()
             except Exception as alarm_err:
                 self.log.exception(label, errmsg=alarm_err.message)
 
@@ -409,7 +431,7 @@
             try:
 
                 OnuActivationFailAlarm(self.alarms, intf_id=ind.intf_id,
-                       onu_id=onu_device_id).raise_alarm()
+                       onu_id=onu_device_id, serial_number=onu_serial_number).raise_alarm()
             except Exception as alarm_err:
                 self.log.exception(label, errmsg=alarm_err.message)
 
@@ -458,7 +480,6 @@
                 parent_port_no=self.platform.intf_id_to_port_no(
                     port_intf_id, device_pb2.Port.PON_OLT),
                 onu_id=onu_id)
-            onu_device_id = onu_device.id
         except Exception as inner:
             self.log.exception('resolve-onu-id', errmsg=inner.message)
 
diff --git a/python/adapters/openolt/openolt_device.py b/python/adapters/openolt/openolt_device.py
index 3242839..d7f9c1c 100644
--- a/python/adapters/openolt/openolt_device.py
+++ b/python/adapters/openolt/openolt_device.py
@@ -88,7 +88,7 @@
         self.core_proxy = kwargs['core_proxy']
         self.adapter_proxy = kwargs['adapter_proxy']
         self.device_num = kwargs['device_num']
-        device = kwargs['device']
+        self.device = kwargs['device']
 
         self.platform_class = kwargs['support_classes']['platform']
         self.resource_mgr_class = kwargs['support_classes']['resource_mgr']
@@ -101,9 +101,9 @@
         self.stub = None
         self.connected = False
         is_reconciliation = kwargs.get('reconciliation', False)
-        self.device_id = device.id
-        self.host_and_port = device.host_and_port
-        self.extra_args = device.extra_args
+        self.device_id = self.device.id
+        self.host_and_port = self.device.host_and_port
+        self.extra_args = self.device.extra_args
         self.device_info = None
         self.log = structlog.get_logger(id=self.device_id,
                                         ip=self.host_and_port)
@@ -119,16 +119,14 @@
             self.log.info('updating-device')
             # It is a new device
             # Update device
-            device.root = True
-            device.connect_status = ConnectStatus.UNREACHABLE
-            device.oper_status = OperStatus.ACTIVATING
-            # TODO NEW CORE. need to move this, cant have a constructor be a generator (yield)
-            #self.adapter_agent.device_update(device)
+            self.device.root = True
+            self.device.connect_status = ConnectStatus.UNREACHABLE
+            self.device.oper_status = OperStatus.ACTIVATING
 
         # If logical device does exist use it, else create one after connecting to device
-        if device.parent_id:
+        if self.device.parent_id:
             # logical device already exists
-            self.logical_device_id = device.parent_id
+            self.logical_device_id = self.device.parent_id
             if is_reconciliation:
                 self.adapter_agent.reconcile_logical_device(
                     self.logical_device_id)
@@ -242,9 +240,6 @@
     def do_state_connected(self, event):
         self.log.debug("do_state_connected")
         
-        device = yield self.core_proxy.get_device(self.device_id)
-
-
         self.stub = openolt_pb2_grpc.OpenoltStub(self.channel)
 
         delay = 1
@@ -269,28 +264,26 @@
         self.log.info('Device connected', device_info=self.device_info)
 
         # self.create_logical_device(device_info)
-        self.logical_device_id = 0
+        self.logical_device_id = '0'
 
         serial_number = self.device_info.device_serial_number
         if serial_number is None: 
             serial_number = self.serial_number
-        device.serial_number = serial_number
+        self.device.serial_number = serial_number
         
         self.serial_number = serial_number
-
-        device.root = True
-        device.vendor = self.device_info.vendor
-        device.model = self.device_info.model
-        device.hardware_version = self.device_info.hardware_version
-        device.firmware_version = self.device_info.firmware_version
+        
+        self.device.root = True
+        self.device.vendor = self.device_info.vendor
+        self.device.model = self.device_info.model
+        self.device.hardware_version = self.device_info.hardware_version
+        self.device.firmware_version = self.device_info.firmware_version
 
         # TODO: check for uptime and reboot if too long (VOL-1192)
 
-        device.connect_status = ConnectStatus.REACHABLE
-        # TODO NEW CORE: Gather this from DeviceInfo proto from openolt agent
-        device.mac_address = "AA:BB:CC:DD:EE:FF"
-        yield self.core_proxy.device_update(device)
-        
+        self.device.connect_status = ConnectStatus.REACHABLE
+        self.device.mac_address = "AA:BB:CC:DD:EE:FF"
+        yield self.core_proxy.device_update(self.device)
 
         self.resource_mgr = self.resource_mgr_class(self.device_id,
                                                     self.host_and_port,
@@ -301,11 +294,12 @@
                                             self.stub, self.device_id,
                                             self.logical_device_id,
                                             self.platform, self.resource_mgr)
-
+        
         self.alarm_mgr = self.alarm_mgr_class(self.log, self.core_proxy,
                                               self.device_id,
                                               self.logical_device_id,
-                                              self.platform)
+                                              self.platform,
+                                              self.serial_number)
         self.stats_mgr = self.stats_mgr_class(self, self.log, self.platform)
         self.bw_mgr = self.bw_mgr_class(self.log, self.core_proxy)