VOL-1319: Watch for valid KEY_ERROR exceptions due to AVC and ONU created ME Entities

Change-Id: I09868b49686225408d6cceb452a5d2fe51378942
diff --git a/voltha/extensions/omci/database/mib_db_ext.py b/voltha/extensions/omci/database/mib_db_ext.py
index 0e1857a..1a895c5 100644
--- a/voltha/extensions/omci/database/mib_db_ext.py
+++ b/voltha/extensions/omci/database/mib_db_ext.py
@@ -382,6 +382,11 @@
 
         except KeyError:
             if not create:
+                # This can occur right after a MIB Reset if the ONU publishes AVCs right away
+                # and during the MIB audit resync for ONU created MEs in response to an OLT
+                # created ME.  Fail since for these test cases they occur during a verification
+                # 'query' and not the ME creation during resync. Calling code should handle
+                # they exception if it is expected to occur on occasion.
                 self.log.debug('class-proxy-does-not-exist', device_id=device_id,
                                class_id=class_id)
                 raise
@@ -425,8 +430,13 @@
 
         except KeyError:
             if not create:
-                self.log.error('instance-proxy-does-not-exist', device_id=device_id,
-                               class_id=class_id, instance_id=instance_id)
+                # This can occur right after a MIB Reset if the ONU publishes AVCs right away
+                # and during the MIB audit resync for ONU created MEs in response to an OLT
+                # created ME.  Fail since for these test cases they occur during a verification
+                # 'query' and not the ME creation during resync. Calling code should handle
+                # they exception if it is expected to occur on occasion.
+                self.log.info('instance-proxy-does-not-exist', device_id=device_id,
+                              class_id=class_id, instance_id=instance_id)
                 raise
 
         # Create instance, first make sure class exists
diff --git a/voltha/extensions/omci/tasks/mib_reconcile_task.py b/voltha/extensions/omci/tasks/mib_reconcile_task.py
index 721b225..38e29dc 100644
--- a/voltha/extensions/omci/tasks/mib_reconcile_task.py
+++ b/voltha/extensions/omci/tasks/mib_reconcile_task.py
@@ -219,7 +219,13 @@
 
     @inlineCallbacks
     def fix_onu_only_save_to_db(self, undecodable, onu_created, onu_db):
-        """ In ONU database and needs to be saved to OLT/OpenOMCI database """
+        """
+        In ONU database and needs to be saved to OLT/OpenOMCI database.
+
+        Note that some, perhaps all, of these instances could be ONU create
+        in response to the OLT creating some other ME instance. So treat
+        the Database operation as a create.
+        """
         successes = 0
         failures = 0
 
@@ -229,7 +235,11 @@
             try:
                 # If in current MIB, had an audit issue or other MIB operation
                 # put it into the database, declare it a failure so we audit again
-                olt_entry = self._sync_sm.query_mib(class_id=cid, instance_id=eid)
+                try:
+                    olt_entry = self._sync_sm.query_mib(class_id=cid, instance_id=eid)
+
+                except KeyError:        # Common for ONU created MEs during audit
+                    olt_entry = None
 
                 if olt_entry is not None and len(olt_entry):
                     self.log.debug('onu-only-in-current', cid=cid, eid=eid)
@@ -271,7 +281,12 @@
             try:
                 # If in current MIB, had an audit issue, declare it an error
                 # and next audit should clear it up
-                current_entry = self._sync_sm.query_mib(class_id=cid, instance_id=eid)
+                try:
+                    current_entry = self._sync_sm.query_mib(class_id=cid, instance_id=eid)
+
+                except KeyError:
+                    # Expected if no other entities with same class present in MIB
+                    current_entry = None
 
                 if current_entry is not None and len(current_entry):
                     self.log.debug('onu-only-in-current', cid=cid, eid=eid)
@@ -530,7 +545,7 @@
             current_entry = self._device.query_mib(cid, eid, attributes)
 
             if current_entry is not None and len(current_entry):
-                clashes = {k: v for k, v in current_entry[ATTRIBUTES_KEY].items()
+                clashes = {k: v for k, v in current_entry.items()
                            if k in attributes and v != mib_data[k]}
 
                 if len(clashes):
@@ -669,10 +684,10 @@
 
     def _onu_created(self, cid_eid_list, me_map):
         return [(cid, eid) for cid, eid in cid_eid_list if cid in me_map and
-                OP.Create not in me_map[cid].mandatory_operations and
-                OP.Create not in me_map[cid].optional_operations]
+                (OP.Create not in me_map[cid].mandatory_operations and
+                 OP.Create not in me_map[cid].optional_operations)]
 
     def _olt_created(self, cid_eid_list, me_map):
         return [(cid, eid) for cid, eid in cid_eid_list if cid in me_map and
-                OP.Create in me_map[cid].mandatory_operations or
-                OP.Create in me_map[cid].optional_operations]
+                (OP.Create in me_map[cid].mandatory_operations or
+                 OP.Create in me_map[cid].optional_operations)]