Bug 50 Cloud-init support in SO for ssh key injection
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / openmano_nsm.py
index d3b86a6..5bf86de 100644 (file)
@@ -102,7 +102,6 @@ class VnfrConsoleOperdataDtsHandler(object):
             if action == rwdts.QueryAction.READ:
                 schema = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur.schema()
                 path_entry = schema.keyspec_to_entry(ks_path)
-                self._log.debug("VDU Opdata path is {}".format(path_entry))
 
                 try:
                     console_url = yield from self._loop.run_in_executor(
@@ -242,7 +241,7 @@ class OpenmanoVnfr(object):
 class OpenmanoNsr(object):
     TIMEOUT_SECS = 120
 
-    def __init__(self, dts, log, loop, publisher, cli_api, http_api, nsd_msg, nsr_config_msg):
+    def __init__(self, dts, log, loop, publisher, cli_api, http_api, nsd_msg, nsr_config_msg,key_pairs):
         self._dts = dts
         self._log = log
         self._loop = loop
@@ -256,6 +255,7 @@ class OpenmanoNsr(object):
         self._vlrs = []
         self._vnfrs = []
         self._vdur_console_handler = {}
+        self._key_pairs = key_pairs
 
         self._nsd_uuid = None
         self._nsr_uuid = None
@@ -274,6 +274,10 @@ class OpenmanoNsr(object):
     def vnfds(self):
         return {v.rift_vnfd_id: v.vnfd for v in self._vnfrs}
 
+    @property
+    def vnfr_ids(self):
+        return {v.rift_vnfd_id: v.openmano_vnfd_id for v in self._vnfrs}
+
     @property
     def vnfrs(self):
         return self._vnfrs
@@ -281,9 +285,49 @@ class OpenmanoNsr(object):
     @property
     def openmano_nsd_yaml(self):
         self._log.debug("Converting nsd %s from rift to openmano", self.nsd.id)
-        openmano_nsd = rift2openmano.rift2openmano_nsd(self.nsd, self.vnfds)
+        openmano_nsd = rift2openmano.rift2openmano_nsd(self.nsd, self.vnfds,self.vnfr_ids)
         return yaml.safe_dump(openmano_nsd, default_flow_style=False)
 
+    def get_ssh_key_pairs(self):
+        cloud_config = {}
+        key_pairs = list()
+        for authorized_key in self._nsr_config_msg.ssh_authorized_key:
+            self._log.debug("Key pair ref present is %s",authorized_key.key_pair_ref)
+            if authorized_key.key_pair_ref in  self._key_pairs:
+                key_pairs.append(self._key_pairs[authorized_key.key_pair_ref].key)
+
+        for authorized_key in self._nsd_msg.key_pair:
+            self._log.debug("Key pair  NSD  is %s",authorized_key)
+            key_pairs.append(authorized_key.key)
+
+        if key_pairs: 
+            cloud_config["key-pairs"] = key_pairs 
+             
+        users = list()
+        for user_entry in self._nsr_config_msg.user:
+            self._log.debug("User present is  %s",user_entry)
+            user = {}
+            user["name"] = user_entry.name 
+            user["key-pairs"] = list()
+            for ssh_key in user_entry.key_pair:
+                user["key-pairs"].append(ssh_key.key)
+            users.append(user)
+
+        for user_entry in self._nsd_msg.user:
+            self._log.debug("User present in NSD is  %s",user_entry)
+            user = {}
+            user["name"] = user_entry.name 
+            user["key-pairs"] = list()
+            for ssh_key in user_entry.key_pair:
+                user["key-pairs"].append(ssh_key.key)
+            users.append(user)
+
+        if users:
+            cloud_config["users"] = users
+
+        self._log.debug("Cloud config formed is %s",cloud_config)
+        return cloud_config
+             
 
     @property
     def openmano_instance_create_yaml(self):
@@ -292,6 +336,10 @@ class OpenmanoNsr(object):
         openmano_instance_create["name"] = self._nsr_config_msg.name
         openmano_instance_create["description"] = self._nsr_config_msg.description
         openmano_instance_create["scenario"] = self._nsd_uuid
+
+        cloud_config = self.get_ssh_key_pairs()
+        if cloud_config:
+            openmano_instance_create["cloud-config"] = cloud_config
         if self._nsr_config_msg.has_field("om_datacenter"):
             openmano_instance_create["datacenter"] = self._nsr_config_msg.om_datacenter
         openmano_instance_create["vnfs"] = {}
@@ -312,7 +360,7 @@ class OpenmanoNsr(object):
                     ip_profile = {}
                     if vld_msg.vim_network_name:
                         network["netmap-use"] = vld_msg.vim_network_name
-                    elif vlr._ip_profile.has_field("ip_profile_params"):
+                    elif vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"):
                         ip_profile_params = vlr._ip_profile.ip_profile_params
                         if ip_profile_params.ip_version == "ipv6":
                             ip_profile['ip-version'] = "IPv6"
@@ -341,6 +389,7 @@ class OpenmanoNsr(object):
                         openmano_instance_create["networks"][vld_msg.name]["sites"].append(network) 
                     if ip_profile:
                         openmano_instance_create["networks"][vld_msg.name]['ip-profile'] = ip_profile 
+        
              
         return yaml.safe_dump(openmano_instance_create, default_flow_style=False)
 
@@ -670,7 +719,7 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
                 ro_account.openmano.tenant_id,
                 )
 
-    def create_nsr(self, nsr_config_msg, nsd_msg):
+    def create_nsr(self, nsr_config_msg, nsd_msg, key_pairs=None):
         """
         Create Network service record
         """
@@ -682,7 +731,8 @@ class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
                 self._cli_api,
                 self._http_api,
                 nsd_msg,
-                nsr_config_msg
+                nsr_config_msg,
+                key_pairs
                 )
         self._openmano_nsrs[nsr_config_msg.id] = openmano_nsr