Bug 117
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / openmano_nsm.py
index d3ff7f2..0ad877e 100644 (file)
@@ -225,7 +225,6 @@ class OpenmanoVnfr(object):
 
         self._created = True
 
-    @asyncio.coroutine
     def delete(self):
         if not self._created:
             return
@@ -235,30 +234,28 @@ class OpenmanoVnfr(object):
             self._log.warning("Openmano vnf id not set.  Cannot delete.")
             return
 
-        yield from self._loop.run_in_executor(
-                None,
-                self._cli_api.vnf_delete,
-                self._vnf_id,
-                )
+        self._cli_api.vnf_delete(self._vnf_id)
 
 
 class OpenmanoNSRecordState(Enum):
     """ Network Service Record State """
+    # Make sure the values match with NetworkServiceRecordState
     INIT = 101
     INSTANTIATION_PENDING = 102
-    RUNNING = 103
-    SCALING_OUT = 104
-    SCALING_IN = 105
-    TERMINATE = 106
-    TERMINATE_RCVD = 107
-    TERMINATED = 108
-    FAILED = 109
-    VL_INSTANTIATE = 110
-    VL_TERMINATE = 111
+    RUNNING = 106
+    SCALING_OUT = 107
+    SCALING_IN = 108
+    TERMINATE = 109
+    TERMINATE_RCVD = 110
+    TERMINATED = 114
+    FAILED = 115
+    VL_INSTANTIATE = 116
+    VL_TERMINATE = 117
 
 
 class OpenmanoNsr(object):
     TIMEOUT_SECS = 300
+    INSTANCE_TERMINATE_TIMEOUT = 60
 
     def __init__(self, dts, log, loop, publisher, cli_api, http_api, nsd_msg, nsr_config_msg,key_pairs):
         self._dts = dts
@@ -421,6 +418,13 @@ class OpenmanoNsr(object):
 
     @asyncio.coroutine
     def remove_vlr(self, vlr):
+        if vlr in self._vlrs:
+            self._vlrs.remove(vlr)
+            yield from self._publisher.unpublish_vlr(None, vlr.vlr_msg)
+        yield from asyncio.sleep(1, loop=self._loop)
+
+    @asyncio.coroutine
+    def delete_vlr(self, vlr):
         if vlr in self._vlrs:
             self._vlrs.remove(vlr)
             if not  vlr.vld_msg.vim_network_name:
@@ -438,23 +442,20 @@ class OpenmanoNsr(object):
         yield from vnfr.create()
         self._vnfrs.append(vnfr)
 
-    @asyncio.coroutine
     def delete(self):
         if not self._created:
             self._log.debug("NSD wasn't created.  Skipping delete.")
             return
 
         self._log.debug("Deleting openmano nsr")
-
-        yield from self._loop.run_in_executor(
-               None,
-               self._cli_api.ns_delete,
-               self._nsd_uuid,
-               )
+        self._cli_api.ns_delete(self._nsd_uuid)
 
         self._log.debug("Deleting openmano vnfrs")
+        deleted_vnf_id_list = []
         for vnfr in self._vnfrs:
-            yield from vnfr.delete()
+            if vnfr.vnfr.vnfd.id not in deleted_vnf_id_list:
+                vnfr.delete()
+                deleted_vnf_id_list.append(vnfr.vnfr.vnfd.id)
 
 
     @asyncio.coroutine
@@ -550,6 +551,11 @@ class OpenmanoNsr(object):
                     return vnf["ip_address"].strip()
                 return None
 
+            def get_vnf_mac_address(vnf):
+                if "mac_address" in vnf:
+                    return vnf["mac_address"].strip()
+                return None
+
             def get_ext_cp_info(vnf):
                 cp_info_list = []
                 for vm in vnf["vms"]:
@@ -567,7 +573,11 @@ class OpenmanoNsr(object):
                         if ip_address is None:
                             ip_address = "0.0.0.0"
 
-                        cp_info_list.append((intf["external_name"], ip_address))
+                        mac_address = intf["mac_address"]
+                        if mac_address is None:
+                            mac_address="00:00:00:00:00:00"
+
+                        cp_info_list.append((intf["external_name"], ip_address, mac_address))
 
                 return cp_info_list
 
@@ -613,8 +623,16 @@ class OpenmanoNsr(object):
                         yield from self._publisher.publish_vnfr(None, vnfr_msg)
                         return
 
+                    if (time.time() - start_time) > OpenmanoNsr.TIMEOUT_SECS:
+                        self._log.error("NSR timed out before reaching running state")
+                        self._state = OpenmanoNSRecordState.FAILED
+                        vnfr_msg.operational_status = "failed"
+                        yield from self._publisher.publish_vnfr(None, vnfr_msg)
+                        return
+
                     if all_vms_active(vnf_status):
                         vnf_ip_address = get_vnf_ip_address(vnf_status)
+                        vnf_mac_address = get_vnf_mac_address(vnf_status)
 
                         if vnf_ip_address is None:
                             self._log.warning("No IP address obtained "
@@ -625,7 +643,7 @@ class OpenmanoNsr(object):
                         self._log.debug("All VMs in VNF are active.  Marking as running.")
                         vnfr_msg.operational_status = "running"
 
-                        self._log.debug("Got VNF ip address: %s", vnf_ip_address)
+                        self._log.debug("Got VNF ip address: %s, mac-address: %s", vnf_ip_address, vnf_mac_address)
                         vnfr_msg.mgmt_interface.ip_address = vnf_ip_address
                         vnfr_msg.vnf_configuration.config_access.mgmt_ip_address = vnf_ip_address
 
@@ -643,22 +661,16 @@ class OpenmanoNsr(object):
 
                         # Add connection point information for the config manager
                         cp_info_list = get_ext_cp_info(vnf_status)
-                        for (cp_name, cp_ip) in cp_info_list:
+                        for (cp_name, cp_ip, cp_mac_addr) in cp_info_list:
                             cp = vnfr_msg.connection_point.add()
                             cp.name = cp_name
                             cp.short_name = cp_name
                             cp.ip_address = cp_ip
+                            cp.mac_address = cp_mac_addr
 
                         yield from self._publisher.publish_vnfr(None, vnfr_msg)
                         active_vnfs.append(vnfr)
 
-                    if (time.time() - start_time) > OpenmanoNsr.TIMEOUT_SECS:
-                        self._log.error("NSR timed out before reaching running state")
-                        self._state = OpenmanoNSRecordState.FAILED
-                        vnfr_msg.operational_status = "failed"
-                        yield from self._publisher.publish_vnfr(None, vnfr_msg)
-                        return
-
                 except Exception as e:
                     vnfr_msg.operational_status = "failed"
                     self._state = OpenmanoNSRecordState.FAILED
@@ -705,26 +717,22 @@ class OpenmanoNsr(object):
                 self.instance_monitor_task(), loop=self._loop
                 )
 
-    @asyncio.coroutine
     def terminate(self):
-
-        for _,handler in  self._vdur_console_handler.items():
-            handler._regh.deregister()
-
         if self._nsr_uuid is None:
-            self._log.warning("Cannot terminate an un-instantiated nsr")
-            return
+            start_time = time.time()
+            while ((time.time() - start_time) < OpenmanoNsr.INSTANCE_TERMINATE_TIMEOUT) and (self._nsr_uuid is None):
+                time.sleep(5)
+                self._log.warning("Waiting for nsr to get instatiated")
+            if self._nsr_uuid is None:
+                self._log.warning("Cannot terminate an un-instantiated nsr")
+                return
 
         if self._monitor_task is not None:
             self._monitor_task.cancel()
             self._monitor_task = None
 
         self._log.debug("Terminating openmano nsr")
-        yield from self._loop.run_in_executor(
-               None,
-               self._cli_api.ns_terminate,
-               self._nsr_uuid,
-               )
+        self._cli_api.ns_terminate(self._nsr_uuid)
 
     @asyncio.coroutine
     def create_vlr(self,vlr):
@@ -804,6 +812,15 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
                 ro_account.openmano.tenant_id,
                 )
 
+    def set_state(self, nsr_id, state):
+        # Currently we update only during terminate to
+        # decide how to handle VL terminate
+        if state.value == OpenmanoNSRecordState.TERMINATE.value:
+            self._openmano_nsrs[nsr_id]._state = \
+                [member.value for name, member in \
+                 OpenmanoNSRecordState.__members__.items() \
+                 if member.value == state.value]
+
     def create_nsr(self, nsr_config_msg, nsd_msg, key_pairs=None):
         """
         Create Network service record
@@ -873,6 +890,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
         openmano_nsr = self._openmano_nsrs[nsr.id]
         if openmano_nsr._state == OpenmanoNSRecordState.RUNNING:
             yield from openmano_nsr.create_vlr(vlr)
+            yield from self._publisher.publish_vlr(None, vlr.vlr_msg)
         else: 
             yield from openmano_nsr.add_vlr(vlr)
 
@@ -884,8 +902,14 @@ 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()
+        for _,handler in openmano_nsr._vdur_console_handler.items():
+            handler._regh.deregister()
+
+        yield from self._loop.run_in_executor(
+               None,
+               self.terminate,
+               openmano_nsr,
+               )
 
         with self._dts.transaction() as xact:
             for vnfr in openmano_nsr.vnfrs:
@@ -894,6 +918,10 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
 
         del self._openmano_nsrs[nsr_id]
 
+    def terminate(self, openmano_nsr):
+        openmano_nsr.terminate()
+        openmano_nsr.delete()
+
     @asyncio.coroutine
     def terminate_vnf(self, vnfr):
         """
@@ -909,6 +937,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
         """
         self._log.debug("Received terminate VL for VLR {}".format(vlr))
         openmano_nsr = self._openmano_nsrs[vlr._nsr_id]
-        yield from openmano_nsr.remove_vlr(vlr)
-
-
+        if openmano_nsr._state == OpenmanoNSRecordState.RUNNING:
+            yield from openmano_nsr.delete_vlr(vlr)
+        else:
+            yield from openmano_nsr.remove_vlr(vlr)