Merge "Check cross-references in automated tests"
[osm/NBI.git] / osm_nbi / instance_topics.py
index ac0dba9..de5bfd1 100644 (file)
@@ -21,6 +21,7 @@ from copy import copy, deepcopy
 from validation import validate_input, ValidationError, ns_instantiate, ns_action, ns_scale, nsi_instantiate
 from base_topic import BaseTopic, EngineException, get_iterable
 from descriptor_topics import DescriptorTopic
 from validation import validate_input, ValidationError, ns_instantiate, ns_action, ns_scale, nsi_instantiate
 from base_topic import BaseTopic, EngineException, get_iterable
 from descriptor_topics import DescriptorTopic
+from yaml import safe_dump
 
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
 
 
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
 
@@ -74,6 +75,7 @@ class NsrTopic(BaseTopic):
         if dry_run:
             return
 
         if dry_run:
             return
 
+        self.fs.file_delete(_id, ignore_non_exist=True)
         v = self.db.del_one("nsrs", {"_id": _id})
         self.db.del_list("nslcmops", {"nsInstanceId": _id})
         self.db.del_list("vnfrs", {"nsr-id-ref": _id})
         v = self.db.del_one("nsrs", {"_id": _id})
         self.db.del_list("nslcmops", {"nsInstanceId": _id})
         self.db.del_list("vnfrs", {"nsr-id-ref": _id})
@@ -83,6 +85,61 @@ class NsrTopic(BaseTopic):
         self._send_msg("deleted", {"_id": _id})
         return v
 
         self._send_msg("deleted", {"_id": _id})
         return v
 
+    @staticmethod
+    def _format_ns_request(ns_request):
+        formated_request = copy(ns_request)
+        formated_request.pop("additionalParamsForNs", None)
+        formated_request.pop("additionalParamsForVnf", None)
+        return formated_request
+
+    @staticmethod
+    def _format_addional_params(ns_request, member_vnf_index=None, descriptor=None):
+        """
+        Get and format user additional params for NS or VNF
+        :param ns_request: User instantiation additional parameters
+        :param member_vnf_index: None for extract NS params, or member_vnf_index to extract VNF params
+        :param descriptor: If not None it check that needed parameters of descriptor are supplied
+        :return: a formated copy of additional params or None if not supplied
+        """
+        additional_params = None
+        if not member_vnf_index:
+            additional_params = copy(ns_request.get("additionalParamsForNs"))
+            where_ = "additionalParamsForNs"
+        elif ns_request.get("additionalParamsForVnf"):
+            for additionalParamsForVnf in get_iterable(ns_request.get("additionalParamsForVnf")):
+                if additionalParamsForVnf["member-vnf-index"] == member_vnf_index:
+                    additional_params = copy(additionalParamsForVnf.get("additionalParams"))
+                    where_ = "additionalParamsForVnf[member-vnf-index={}]".format(
+                        additionalParamsForVnf["member-vnf-index"])
+                    break
+        if additional_params:
+            for k, v in additional_params.items():
+                if not isinstance(k, str):
+                    raise EngineException("Invalid param at {}:{}. Only string keys are allowed".format(where_, k))
+                if "." in k or "$" in k:
+                    raise EngineException("Invalid param at {}:{}. Keys must not contain dots or $".format(where_, k))
+                if isinstance(v, (dict, tuple, list)):
+                    additional_params[k] = "!!yaml " + safe_dump(v)
+
+        if descriptor:
+            # check that enough parameters are supplied for the initial-config-primitive
+            # TODO: check for cloud-init
+            if member_vnf_index:
+                if descriptor.get("vnf-configuration"):
+                    for initial_primitive in get_iterable(
+                            descriptor["vnf-configuration"].get("initial-config-primitive")):
+                        for param in get_iterable(initial_primitive.get("parameter")):
+                            if param["value"].startswith("<") and param["value"].endswith(">"):
+                                if param["value"] in ("<rw_mgmt_ip>", "<VDU_SCALE_INFO>"):
+                                    continue
+                                if not additional_params or param["value"][1:-1] not in additional_params:
+                                    raise EngineException("Parameter '{}' needed for vnfd[id={}]:vnf-configuration:"
+                                                          "initial-config-primitive[name={}] not supplied".
+                                                          format(param["value"], descriptor["id"],
+                                                                 initial_primitive["name"]))
+
+        return additional_params
+
     def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False):
         """
         Creates a new nsr into database. It also creates needed vnfrs
     def new(self, rollback, session, indata=None, kwargs=None, headers=None, force=False, make_public=False):
         """
         Creates a new nsr into database. It also creates needed vnfrs
@@ -110,6 +167,7 @@ class NsrTopic(BaseTopic):
             nsd = self.db.get_one("nsds", _filter)
 
             nsr_id = str(uuid4())
             nsd = self.db.get_one("nsds", _filter)
 
             nsr_id = str(uuid4())
+
             now = time()
             step = "filling nsr from input data"
             nsr_descriptor = {
             now = time()
             step = "filling nsr from input data"
             nsr_descriptor = {
@@ -134,7 +192,8 @@ class NsrTopic(BaseTopic):
                 "nsd-name-ref": nsd["name"],
                 "operational-events": [],   # "id", "timestamp", "description", "event",
                 "nsd-ref": nsd["id"],
                 "nsd-name-ref": nsd["name"],
                 "operational-events": [],   # "id", "timestamp", "description", "event",
                 "nsd-ref": nsd["id"],
-                "instantiate_params": ns_request,
+                "instantiate_params": self._format_ns_request(ns_request),
+                "additionalParamsForNs": self._format_addional_params(ns_request),
                 "ns-instance-config-ref": nsr_id,
                 "id": nsr_id,
                 "_id": nsr_id,
                 "ns-instance-config-ref": nsr_id,
                 "id": nsr_id,
                 "_id": nsr_id,
@@ -170,6 +229,8 @@ class NsrTopic(BaseTopic):
                     "_id": vnfr_id,
                     "nsr-id-ref": nsr_id,
                     "member-vnf-index-ref": member_vnf["member-vnf-index"],
                     "_id": vnfr_id,
                     "nsr-id-ref": nsr_id,
                     "member-vnf-index-ref": member_vnf["member-vnf-index"],
+                    "additionalParamsForVnf": self._format_addional_params(ns_request, member_vnf["member-vnf-index"],
+                                                                           vnfd),
                     "created-time": now,
                     # "vnfd": vnfd,        # at OSM model.but removed to avoid data duplication TODO: revise
                     "vnfd-ref": vnfd_id,
                     "created-time": now,
                     # "vnfd": vnfd,        # at OSM model.but removed to avoid data duplication TODO: revise
                     "vnfd-ref": vnfd_id,
@@ -277,6 +338,10 @@ class NsrTopic(BaseTopic):
             self.format_on_new(nsr_descriptor, session["project_id"], make_public=make_public)
             self.db.create("nsrs", nsr_descriptor)
             rollback.append({"topic": "nsrs", "_id": nsr_id})
             self.format_on_new(nsr_descriptor, session["project_id"], make_public=make_public)
             self.db.create("nsrs", nsr_descriptor)
             rollback.append({"topic": "nsrs", "_id": nsr_id})
+
+            step = "creating nsr temporal folder"
+            self.fs.mkdir(nsr_id)
+
             return nsr_id
         except Exception as e:
             self.logger.exception("Exception {} at NsrTopic.new()".format(e), exc_info=True)
             return nsr_id
         except Exception as e:
             self.logger.exception("Exception {} at NsrTopic.new()".format(e), exc_info=True)
@@ -517,10 +582,10 @@ class NsLcmOpTopic(BaseTopic):
             }
             self.db.set_one("pdus", {"_id": pdu["_id"]},
                             {"_admin.usageState": "IN_USE",
             }
             self.db.set_one("pdus", {"_id": pdu["_id"]},
                             {"_admin.usageState": "IN_USE",
-                             "_admin.usage.vnfr_id": vnfr["_id"],
-                             "_admin.usage.nsr_id": vnfr["nsr-id-ref"],
-                             "_admin.usage.vdur": vdur["vdu-id-ref"]}
-                            )
+                             "_admin.usage": {"vnfr_id": vnfr["_id"],
+                                              "nsr_id": vnfr["nsr-id-ref"],
+                                              "vdur": vdur["vdu-id-ref"]}
+                             })
             rollback.append({"topic": "pdus", "_id": pdu["_id"], "operation": "set", "content": rollback_pdu})
 
             # step 3. Fill vnfr info by filling vdur
             rollback.append({"topic": "pdus", "_id": pdu["_id"], "operation": "set", "content": rollback_pdu})
 
             # step 3. Fill vnfr info by filling vdur
@@ -668,7 +733,7 @@ class NsLcmOpTopic(BaseTopic):
             if not nsr["_admin"].get("nsState") or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
                 if operation == "terminate" and indata.get("autoremove"):
                     # NSR must be deleted
             if not nsr["_admin"].get("nsState") or nsr["_admin"]["nsState"] == "NOT_INSTANTIATED":
                 if operation == "terminate" and indata.get("autoremove"):
                     # NSR must be deleted
-                    return self.delete(session, nsInstanceId)
+                    return None    # a none in this case is used to indicate not instantiated. It can be removed
                 if operation != "instantiate":
                     raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
                         nsInstanceId, operation), HTTPStatus.CONFLICT)
                 if operation != "instantiate":
                     raise EngineException("ns_instance '{}' cannot be '{}' because it is not instantiated".format(
                         nsInstanceId, operation), HTTPStatus.CONFLICT)
@@ -1037,7 +1102,7 @@ class NsiLcmOpTopic(BaseTopic):
             if not nsir["_admin"].get("nsiState") or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
                 if operation == "terminate" and indata.get("autoremove"):
                     # NSIR must be deleted
             if not nsir["_admin"].get("nsiState") or nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
                 if operation == "terminate" and indata.get("autoremove"):
                     # NSIR must be deleted
-                    return self.delete(session, nsiInstanceId)
+                    return None    # a none in this case is used to indicate not instantiated. It can be removed
                 if operation != "instantiate":
                     raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
                         nsiInstanceId, operation), HTTPStatus.CONFLICT)
                 if operation != "instantiate":
                     raise EngineException("netslice_instance '{}' cannot be '{}' because it is not instantiated".format(
                         nsiInstanceId, operation), HTTPStatus.CONFLICT)