Merge "Descriptor Unification"
authorvelandy <rajesh.velandy@riftio.com>
Mon, 28 Nov 2016 20:07:02 +0000 (21:07 +0100)
committerGerrit Code Review <root@osm.etsi.org>
Mon, 28 Nov 2016 20:07:02 +0000 (21:07 +0100)
1  2 
rwcal/plugins/vala/rwcal_openstack/rift/rwcal/openstack/openstack_drv.py
rwlaunchpad/plugins/rwnsm/rift/tasklets/rwnsmtasklet/rwnsmtasklet.py
rwlaunchpad/plugins/rwvnfm/rift/tasklets/rwvnfmtasklet/rwvnfmtasklet.py

@@@ -123,8 -123,6 +123,8 @@@ class KeystoneDriver(object)
          try:
              ksconn = self._get_keystone_connection()
              service_endpoint  = ksconn.service_catalog.url_for(**endpoint_kwargs)
 +        except (KeystoneExceptions.Unauthorized, KeystoneExceptions.AuthorizationFailure) as e:
 +            raise
          except Exception as e:
              logger.error("OpenstackDriver: Service Catalog discovery operation failed for service_type: %s, endpoint_type: %s. Exception: %s" %(service_type, endpoint_type, str(e)))
              raise
@@@ -1647,6 -1645,7 +1647,7 @@@ class OpenstackDriver(object)
              logger.error("Could not identity the version information for openstack service endpoints. Auth_URL should contain \"/v2\" or \"/v3\" string in it")
              raise NotImplementedError("Auth URL is wrong or invalid. Only Keystone v2 & v3 supported")
  
+         self._mgmt_network_id = None
          if mgmt_network != None:
              self._mgmt_network = mgmt_network
  
              try:
                  ntconn   = self.neutron_drv._get_neutron_connection()
                  networks = ntconn.list_networks()
 +            except (KeystoneExceptions.Unauthorized, KeystoneExceptions.AuthorizationFailure) as e:
 +                raise
              except Exception as e:
                  logger.error("OpenstackDriver: List Network operation failed. Exception: %s" %(str(e)))
                  raise
@@@ -460,7 -460,6 +460,7 @@@ class VnffgRecord(object)
  
  class VirtualLinkRecord(object):
      """ Virtual Link Records class"""
 +    XPATH = "D,/vlr:vlr-catalog/vlr:vlr"
      @staticmethod
      @asyncio.coroutine
      def create_record(dts, log, loop, nsr_name, vld_msg, cloud_account_name, om_datacenter, ip_profile, nsr_id, restart_mode=False):
          self._vlr_id = str(uuid.uuid4())
          self._state = VlRecordState.INIT
          self._prev_state = None
 +        self._create_time = int(time.time())
  
      @property
      def xpath(self):
                      "nsr_id_ref": self._nsr_id,
                      "vld_ref": self.vld_msg.id,
                      "name": self.name,
 +                    "create_time": self._create_time,
                      "cloud_account": self.cloud_account_name,
                      "om_datacenter": self.om_datacenter_name,
                      }
      @asyncio.coroutine
      def instantiate(self):
          """ Instantiate this VL """
 -
          self._log.debug("Instaniating VLR key %s, vld %s",
                          self.xpath, self._vld_msg)
          vlr = None
@@@ -1233,8 -1231,7 +1233,8 @@@ class NetworkServiceRecord(object)
      """ Network service record """
      XPATH = "D,/nsr:ns-instance-opdata/nsr:nsr"
  
 -    def __init__(self, dts, log, loop, nsm, nsm_plugin, nsr_cfg_msg, sdn_account_name, key_pairs, restart_mode=False):
 +    def __init__(self, dts, log, loop, nsm, nsm_plugin, nsr_cfg_msg, sdn_account_name, key_pairs, restart_mode=False,
 +                 vlr_handler=None):
          self._dts = dts
          self._log = log
          self._loop = loop
          self._nsr_cfg_msg = nsr_cfg_msg
          self._nsm_plugin = nsm_plugin
          self._sdn_account_name = sdn_account_name
 +        self._vlr_handler = vlr_handler
  
          self._nsd = None
          self._nsr_msg = None
          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))
 +
 +
 +    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)
 +
  
      @asyncio.coroutine
      def create(self, config_xact):
          """ This function creates VLs for every VLD in the NSD
          associated with this NSR"""
          for vld in self.nsd_msg.vld:
              self._log.debug("Found vld %s in nsr id %s", vld, self.id)
              cloud_account_list = self._extract_cloud_accounts_for_vl(vld)
              for cloud_account,om_datacenter in cloud_account_list:
          nsr.config_status = self.map_config_status()
          nsr.config_status_details = self._config_status_details
          nsr.create_time = self._create_time
 +        nsr.uptime = int(time.time()) - self._create_time
  
          for cfg_prim in self.nsd_msg.service_primitive:
              cfg_prim = NsrYang.YangData_Nsr_NsInstanceOpdata_Nsr_ServicePrimitive.from_dict(
@@@ -3852,8 -3836,7 +3853,8 @@@ class NsManager(object)
                                     nsr_msg,
                                     sdn_account_name,
                                     key_pairs,
 -                                   restart_mode=restart_mode
 +                                   restart_mode=restart_mode,
 +                                   vlr_handler=self._ro_plugin_selector._records_publisher._vlr_pub_hdlr
                                     )
          self._nsrs[nsr_msg.id] = nsr
          nsm_plugin.create_nsr(nsr_msg, nsr_msg.nsd, key_pairs)
@@@ -49,6 -49,7 +49,7 @@@ from gi.repository import 
  import rift.tasklets
  import rift.package.store
  import rift.package.cloud_init
+ import rift.mano.dts as mano_dts
  
  
  class VMResourceError(Exception):
@@@ -270,6 -271,7 +271,7 @@@ class VirtualDeploymentUnitRecord(objec
                   vdud,
                   vnfr,
                   mgmt_intf,
+                  mgmt_network,
                   cloud_account_name,
                   vnfd_package_store,
                   vdur_id=None,
          self._mgmt_intf = mgmt_intf
          self._cloud_account_name = cloud_account_name
          self._vnfd_package_store = vnfd_package_store
+         self._mgmt_network = mgmt_network
  
          self._vdur_id = vdur_id or str(uuid.uuid4())
          self._int_intf = []
          if config is not None:
              vm_create_msg_dict['vdu_init'] = {'userdata': config}
  
+         if self._mgmt_network:
+             vm_create_msg_dict['mgmt_network'] = self._mgmt_network
          cp_list = []
          for intf, cp, vlr in self._ext_intf:
              cp_info = {"name": cp,
@@@ -1046,7 -1052,7 +1052,7 @@@ class InternalVirtualLinkRecord(object)
  
  class VirtualNetworkFunctionRecord(object):
      """ Virtual Network Function Record """
-     def __init__(self, dts, log, loop, cluster_name, vnfm, vcs_handler, vnfr_msg):
+     def __init__(self, dts, log, loop, cluster_name, vnfm, vcs_handler, vnfr_msg, mgmt_network=None):
          self._dts = dts
          self._log = log
          self._loop = loop
          self._vnfm = vnfm
          self._vcs_handler = vcs_handler
          self._vnfr = vnfr_msg
+         self._mgmt_network = mgmt_network
  
          self._vnfd = None
          self._state = VirtualNetworkFunctionRecordState.INIT
          vnfr_dict.update(vnfd_copy_dict)
  
          vnfr_msg = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr.from_dict(vnfr_dict)
 +        vnfr_msg.uptime = int(time.time()) - self._create_time
          vnfr_msg.mgmt_interface = mgmt_intf
  
          # Add all the VLRs  to  VNFR
                  vdud=vdu,
                  vnfr=vnfr,
                  mgmt_intf=self.has_mgmt_interface(vdu),
+                 mgmt_network=self._mgmt_network,
                  cloud_account_name=self.cloud_account_name,
                  vnfd_package_store=self._vnfd_package_store,
                  vdur_id=vdur_id,
  
          self._log.debug("VNFR-ID %s: Instantiation Done", self._vnfr_id)
  
 +        # create task updating uptime for this vnfr
 +        self._log.debug("VNFR-ID %s: Starting task to update uptime", self._vnfr_id)
 +        self._loop.create_task(self.vnfr_uptime_update(xact))
 +
      @asyncio.coroutine
      def terminate(self, xact):
          """ Terminate this virtual network function """
          self._log.debug("Terminated  VNF id %s", self.vnfr_id)
          self.set_state(VirtualNetworkFunctionRecordState.TERMINATED)
  
 +    @asyncio.coroutine
 +    def vnfr_uptime_update(self, xact):
 +        while True:
 +            # Return when vnfr state is FAILED or TERMINATED etc
 +            if self._state not in [VirtualNetworkFunctionRecordState.INIT,
 +                                   VirtualNetworkFunctionRecordState.VL_INIT_PHASE,
 +                                   VirtualNetworkFunctionRecordState.VM_INIT_PHASE,
 +                                   VirtualNetworkFunctionRecordState.READY]:
 +                return
 +            yield from self.publish(xact)
 +            yield from asyncio.sleep(2, loop=self._loop)
 +
 +
  
  class VnfdDtsHandler(object):
      """ DTS handler for VNFD config changes """
@@@ -2476,13 -2466,15 +2484,15 @@@ class VnfManager(object)
  
          self._vcs_handler = VcsComponentDtsHandler(dts, log, loop, self)
          self._vnfr_handler = VnfrDtsHandler(dts, log, loop, self)
+         self._vnfr_ref_handler = VnfdRefCountDtsHandler(dts, log, loop, self)
+         self._nsr_handler = mano_dts.NsInstanceConfigSubscriber(log, dts, loop, callback=self.handle_nsr)
  
          self._dts_handlers = [VnfdDtsHandler(dts, log, loop, self),
-                               self._vnfr_handler,
                                self._vcs_handler,
-                               VnfdRefCountDtsHandler(dts, log, loop, self)]
+                               self._nsr_handler]
          self._vnfrs = {}
          self._vnfds = {}
+         self._nsrs = {}
  
      @property
      def vnfr_handler(self):
          self._log.debug("Run VNFManager - registering static DTS handlers""")
          yield from self.register()
  
+     def handle_nsr(self, nsr, action):
+         if action in [rwdts.QueryAction.CREATE]:
+             self._nsrs[nsr.id] = nsr
+         elif action == rwdts.QueryAction.DELETE:
+             if nsr.id in self._nsrs:
+                 del self._nsrs[nsr.id]
+     def get_linked_mgmt_network(self, vnfr):
+         """For the given VNFR get the related mgmt network from the NSD, if
+         available.
+         """
+         vnfd_id = vnfr.vnfd_ref
+         nsr_id = vnfr.nsr_id_ref
+         # for the given related VNFR, get the corresponding NSR-config
+         nsr_obj = None
+         try:
+             nsr_obj = self._nsrs[nsr_id]
+         except KeyError:
+             raise("Unable to find the NS with the ID: {}".format(nsr_id))
+         # for the related NSD check if a VLD exists such that it's a mgmt
+         # network
+         for vld in nsr_obj.nsd.vld:
+             if vld.mgmt_network:
+                 return vld.name
+         return None
      def get_vnfr(self, vnfr_id):
          """ get VNFR by vnfr id """
  
                         vnfr.id,
                         vnfr.vnfd_ref)
  
+         mgmt_network = self.get_linked_mgmt_network(vnfr)
          self._vnfrs[vnfr.id] = VirtualNetworkFunctionRecord(
-             self._dts, self._log, self._loop, self._cluster_name, self, self.vcs_handler, vnfr
+             self._dts, self._log, self._loop, self._cluster_name, self, self.vcs_handler, vnfr,
+             mgmt_network=mgmt_network
              )
          return self._vnfrs[vnfr.id]