Merge from OSM SO master
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / rwnsm_conman.py
index e29f491..7d05457 100644 (file)
@@ -46,6 +46,7 @@ class ROConfigManager(object):
         self._loop = loop
         self._dts = dts
         self.nsm = parent
+        self.project = parent._project
         self._log.debug("Initialized ROConfigManager")
 
     def is_ready(self):
@@ -53,7 +54,7 @@ class ROConfigManager(object):
 
     @property
     def cm_state_xpath(self):
-        return ("/rw-conman:cm-state/rw-conman:cm-nsr")
+        return self.project.add_project("/rw-conman:cm-state/rw-conman:cm-nsr")
 
     @classmethod
     def map_config_status(cls, status):
@@ -103,9 +104,13 @@ class ROConfigManager(object):
 
             # Update the NSR's config status
             new_status = ROConfigManager.map_config_status(cm_nsr['state'])
-            self._log.debug("Updating config status of NSR {} to {}({})".
-                                format(nsrid, new_status, cm_nsr['state']))
-            yield from self.nsm.nsrs[nsrid].set_config_status(new_status, cm_nsr.get('state_details'))
+            self._log.info("Updating config status of NSR {} to {}({})".
+                           format(nsrid, new_status, cm_nsr['state']))
+
+            # If terminate nsr request comes when NS instantiation is in 'Configuring state'; self.nsm.nsrs dict
+            # is already empty when self.nsm.nsrs[nsrid].set_config_status gets executed. So adding a check here.
+            if nsrid in self.nsm.nsrs:
+                yield from self.nsm.nsrs[nsrid].set_config_status(new_status, cm_nsr.get('state_details'))
 
         except Exception as e:
             self._log.error("Failed to process cm-state for nsr {}: {}".
@@ -115,7 +120,7 @@ class ROConfigManager(object):
     @asyncio.coroutine
     def register(self):
         """ Register for cm-state changes """
-        
+
         @asyncio.coroutine
         def on_prepare(xact_info, query_action, ks_path, msg):
             """ cm-state changed """
@@ -147,4 +152,9 @@ class ROConfigManager(object):
                                                              handler=handler)
         except Exception as e:
             self._log.error("Failed to register for cm-state changes as %s", str(e))
-            
+
+
+    def deregister(self):
+        if self.dts_reg_hdl:
+            self.dts_reg_hdl.deregister()
+            self.dts_reg_hdl = None