Bug 67 SO is not publishing VLR records. Uptime update for vnfr when openmano used... 60/760/1
authorAnanda Baitharu <ananda.baitharu@riftio.com>
Wed, 7 Dec 2016 07:59:39 +0000 (02:59 -0500)
committerAnanda Baitharu <ananda.baitharu@riftio.com>
Wed, 7 Dec 2016 07:59:39 +0000 (02:59 -0500)
Signed-off-by: Ananda Baitharu <ananda.baitharu@riftio.com>
rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/openmano_nsm.py
rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/rwnsmtasklet.py

index e4c5f93..d3ff7f2 100644 (file)
@@ -416,6 +416,7 @@ class OpenmanoNsr(object):
     @asyncio.coroutine
     def add_vlr(self, vlr):
         self._vlrs.append(vlr)
+        yield from self._publisher.publish_vlr(None, vlr.vlr_msg)
         yield from asyncio.sleep(1, loop=self._loop)
 
     @asyncio.coroutine
@@ -428,6 +429,7 @@ class OpenmanoNsr(object):
                     self._cli_api.ns_vim_network_delete,
                     vlr.name,
                     vlr.om_datacenter_name)
+            yield from self._publisher.unpublish_vlr(None, vlr.vlr_msg)
         yield from asyncio.sleep(1, loop=self._loop)
 
     @asyncio.coroutine
@@ -782,6 +784,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
         self._cli_api = None
         self._http_api = None
         self._openmano_nsrs = {}
+        self._vnfr_uptime_tasks = {}
 
         self._set_ro_account(ro_account)
 
@@ -848,6 +851,18 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
         self._log.debug("Attempting to publish openmano vnf: %s", vnfr_msg)
         with self._dts.transaction() as xact:
             yield from self._publisher.publish_vnfr(xact, vnfr_msg)
+        self._log.debug("Creating a task to update uptime for vnfr: %s", vnfr.id)
+        self._vnfr_uptime_tasks[vnfr.id] = self._loop.create_task(self.vnfr_uptime_update(vnfr))
+
+    def vnfr_uptime_update(self, vnfr):
+        try:
+            vnfr_ = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr.from_dict({'id': vnfr.id})
+            while True:
+                vnfr_.uptime = int(time.time()) - vnfr._create_time
+                yield from self._publisher.publish_vnfr(None, vnfr_)
+                yield from asyncio.sleep(2, loop=self._loop)
+        except asyncio.CancelledError:
+            self._log.debug("Received cancellation request for vnfr_uptime_update task")
 
     @asyncio.coroutine
     def instantiate_vl(self, nsr, vlr):
@@ -868,6 +883,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
         """
         nsr_id = nsr.id
         openmano_nsr = self._openmano_nsrs[nsr_id]
+
         yield from openmano_nsr.terminate()
         yield from openmano_nsr.delete()
 
@@ -883,7 +899,8 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
         """
         Terminate the network service
         """
-        pass
+        if vnfr.id in self._vnfr_uptime_tasks:
+            self._vnfr_uptime_tasks[vnfr.id].cancel()
 
     @asyncio.coroutine
     def terminate_vl(self, vlr):
index 718d2b1..f43b0cb 100755 (executable)
@@ -796,6 +796,7 @@ class VirtualNetworkFunctionRecord(object):
         self._group_instance_id = group_instance_id
         self._placement_groups = placement_groups
         self._config_status = NsrYang.ConfigStates.INIT
+        self._create_time = int(time.time())
 
         self._prev_state = VnfRecordState.INIT
         self._state = VnfRecordState.INIT
@@ -1266,6 +1267,7 @@ class NetworkServiceRecord(object):
         self._is_active = False
         self._vl_phase_completed = False
         self._vnf_phase_completed = False
+        self.vlr_uptime_tasks = {}
 
 
         # Initalise the state to init
@@ -1457,17 +1459,20 @@ class NetworkServiceRecord(object):
         for vlr in self._vlrs:
             yield from self.nsm_plugin.instantiate_vl(self, vlr)
             vlr.state = VlRecordState.ACTIVE
-            self._loop.create_task(self.vlr_uptime_update(vlr))
+            self.vlr_uptime_tasks[vlr.id] = self._loop.create_task(self.vlr_uptime_update(vlr))
 
 
     def vlr_uptime_update(self, vlr):
-        vlr_ = RwVlrYang.YangData_Vlr_VlrCatalog_Vlr.from_dict({'id': vlr.id})
-        while True:
-            if vlr.state not in [VlRecordState.INIT, VlRecordState.INSTANTIATION_PENDING, VlRecordState.ACTIVE]:
-                return
-            vlr_.uptime = int(time.time()) - vlr._create_time
-            yield from self._vlr_handler.update(None, VirtualLinkRecord.vlr_xpath(vlr), vlr_)
-            yield from asyncio.sleep(2, loop=self._loop)
+        try:
+
+            vlr_ = RwVlrYang.YangData_Vlr_VlrCatalog_Vlr.from_dict({'id': vlr.id})
+            while True:
+                vlr_.uptime = int(time.time()) - vlr._create_time
+                yield from self._vlr_handler.update(None, VirtualLinkRecord.vlr_xpath(vlr), vlr_)
+                yield from asyncio.sleep(2, loop=self._loop)
+        except asyncio.CancelledError:
+            self._log.debug("Received cancellation request for vlr_uptime_update task")
+            yield from self._vlr_handler.delete(None, VirtualLinkRecord.vlr_xpath(vlr))
 
 
     @asyncio.coroutine
@@ -2417,6 +2422,8 @@ class NetworkServiceRecord(object):
             for vlr in self.vlrs:
                 yield from self.nsm_plugin.terminate_vl(vlr)
                 vlr.state = VlRecordState.TERMINATED
+                if vlr.id in self.vlr_uptime_tasks:
+                    self.vlr_uptime_tasks[vlr.id].cancel()
 
         self._log.debug("Terminating network service id %s", self.id)