Feature shared network slice subnet
[osm/NBI.git] / osm_nbi / tests / test.py
index 8621d6b..74242d8 100755 (executable)
@@ -72,28 +72,20 @@ def usage():
 
 
 r_header_json = {"Content-type": "application/json"}
-headers_json = {
-    "Content-type": "application/json",
-    "Accept": "application/json",
-}
+headers_json = {"Content-type": "application/json", "Accept": "application/json"}
 r_header_yaml = {"Content-type": "application/yaml"}
-headers_yaml = {
-    "Content-type": "application/yaml",
-    "Accept": "application/yaml",
-}
+headers_yaml = {"Content-type": "application/yaml", "Accept": "application/yaml"}
 r_header_text = {"Content-type": "text/plain"}
 r_header_octect = {"Content-type": "application/octet-stream"}
-headers_text = {
-    "Accept": "text/plain",
-}
+headers_text = {"Accept": "text/plain,application/yaml"}
 r_header_zip = {"Content-type": "application/zip"}
-headers_zip = {
-    "Accept": "application/zip",
-}
-headers_zip_yaml = {
-    "Accept": "application/yaml", "Content-type": "application/zip"
-}
-
+headers_zip = {"Accept": "application/zip,application/yaml"}
+headers_zip_yaml = {"Accept": "application/yaml", "Content-type": "application/zip"}
+r_headers_yaml_location_vnfd = {"Location": "/vnfpkgm/v1/vnf_packages_content/", "Content-Type": "application/yaml"}
+r_headers_yaml_location_nsd = {"Location": "/nsd/v1/ns_descriptors_content/", "Content-Type": "application/yaml"}
+r_headers_yaml_location_nst = {"Location": "/nst/v1/netslice_templates_content", "Content-Type": "application/yaml"}
+r_headers_yaml_location_nslcmop = {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}
+r_headers_yaml_location_nsilcmop = {"Location": "/osm/nsilcm/v1/nsi_lcm_op_occs/", "Content-Type": "application/yaml"}
 
 # test ones authorized
 test_authorized_list = (
@@ -199,7 +191,18 @@ class TestRest:
             stream = False
             if expected_payload in ("zip", "octet-string") or store_file:
                 stream = True
-            r = getattr(self.s, method.lower())(url, data=payload, headers=headers, verify=self.verify, stream=stream)
+            __retry = 0
+            while True:
+                try:
+                    r = getattr(self.s, method.lower())(url, data=payload, headers=headers, verify=self.verify,
+                                                        stream=stream)
+                    break
+                except requests.exceptions.ConnectionError as e:
+                    if __retry == 2:
+                        raise
+                    logger.error("Exception {}. Retrying".format(e))
+                    __retry += 1
+
             if expected_payload in ("zip", "octet-string") or store_file:
                 logger.debug("RX {}".format(r.status_code))
             else:
@@ -279,6 +282,8 @@ class TestRest:
             self.failed_tests += 1
             return None
             # exit(1)
+        except requests.exceptions.RequestException as e:
+            logger.error("Exception: {}".format(e))
 
     def get_autorization(self):  # user=None, password=None, project=None):
         if self.token:  # and self.user == user and self.password == password and self.project == project:
@@ -364,7 +369,8 @@ class TestRest:
 
         wait = timeout_delete
         while wait >= 0:
-            r = self.test(description, "GET", url, headers_yaml, (200, 404), None, r_header_yaml, "yaml", pooling=True)
+            r = self.test(description, "GET", url_op, headers_yaml, None, (200, 404), None, r_header_yaml, "yaml",
+                          pooling=True)
             if not r:
                 return
             if r.status_code == 404:
@@ -403,8 +409,7 @@ class TestRest:
             nslcmop = r.json()
             if "COMPLETED" in nslcmop["operationState"]:
                 if expected_fail:
-                    logger.error("NS terminate has success, expecting failing: {}".format(
-                        nslcmop["detailed-status"]))
+                    logger.error("NS terminate has success, expecting failing: {}".format(nslcmop["detailed-status"]))
                     self.failed_tests += 1
                 else:
                     self.passed_tests += 1
@@ -412,6 +417,7 @@ class TestRest:
             elif "FAILED" in nslcmop["operationState"]:
                 if not expected_fail:
                     logger.error("NS terminate has failed: {}".format(nslcmop["detailed-status"]))
+                    self.failed_tests += 1
                 else:
                     self.passed_tests += 1
                 break
@@ -427,7 +433,7 @@ class TestRest:
 
 
 class TestNonAuthorized:
-    description = "test invalid URLs. methods and no authorization"
+    description = "Test invalid URLs. methods and no authorization"
 
     @staticmethod
     def run(engine, test_osm, manual_check, test_params=None):
@@ -618,6 +624,17 @@ class TestVIMSDN(TestFakeVim):
 
     def __init__(self):
         TestFakeVim.__init__(self)
+        self.wim = {
+            "schema_version": "1.0",
+            "schema_type": "No idea",
+            "name": "myWim",
+            "description": "Descriptor name",
+            "wim_type": "odl",
+            "wim_url": "http://localhost:/wim",
+            "user": "user",
+            "password": "password",
+            "config": {"config_param": 1}
+        }
 
     def run(self, engine, test_osm, manual_check, test_params=None):
         engine.set_test_name("VimSdn")
@@ -644,6 +661,10 @@ class TestVIMSDN(TestFakeVim):
         engine.test("Edit VIM remove port-mapping", "PUT", "/admin/v1/vim_accounts/{}".format(vim_id), headers_json,
                     {"config": {"sdn-port-mapping": None}}, 204, None, None)
 
+        engine.test("Create WIM", "POST", "/admin/v1/wim_accounts", headers_json, self.wim, (200, 204, 201),
+                    {"Location": "/admin/v1/wim_accounts/", "Content-Type": "application/json"}, "json"),
+        wim_id = engine.last_id
+
         if not test_osm:
             # delete with FORCE
             engine.test("Delete VIM remove port-mapping", "DELETE",
@@ -651,18 +672,27 @@ class TestVIMSDN(TestFakeVim):
             engine.test("Delete SDNC", "DELETE", "/admin/v1/sdns/{}?FORCE=True".format(sdnc_id), headers_json, None,
                         202, None, 0)
 
+            engine.test("Delete WIM", "DELETE",
+                        "/admin/v1/wim_accounts/{}?FORCE=True".format(wim_id), headers_json, None, 202, None, 0)
             engine.test("Check VIM is deleted", "GET", "/admin/v1/vim_accounts/{}".format(vim_id), headers_yaml,
                         None, 404, r_header_yaml, "yaml")
             engine.test("Check SDN is deleted", "GET", "/admin/v1/sdns/{}".format(sdnc_id), headers_yaml, None,
                         404, r_header_yaml, "yaml")
+            engine.test("Check WIM is deleted", "GET", "/admin/v1/wim_accounts/{}".format(wim_id), headers_yaml,
+                        None, 404, r_header_yaml, "yaml")
         else:
+            if manual_check:
+                input('VIM, SDN, WIM has been deployed. Perform manual check and press enter to resume')
             # delete and wait until is really deleted
             engine.test("Delete VIM remove port-mapping", "DELETE", "/admin/v1/vim_accounts/{}".format(vim_id),
                         headers_json, None, (202, 201, 204), None, 0)
             engine.test("Delete SDN", "DELETE", "/admin/v1/sdns/{}".format(sdnc_id), headers_json, None,
                         (202, 201, 204), None, 0)
+            engine.test("Delete VIM", "DELETE", "/admin/v1/wim_accounts/{}".format(wim_id),
+                        headers_json, None, (202, 201, 204), None, 0)
             engine.wait_until_delete("/admin/v1/vim_accounts/{}".format(vim_id), timeout)
             engine.wait_until_delete("/admin/v1/sdns/{}".format(sdnc_id), timeout)
+            engine.wait_until_delete("/admin/v1/wim_accounts/{}".format(wim_id), timeout)
 
 
 class TestDeploy:
@@ -679,12 +709,13 @@ class TestDeploy:
         self.nsd_filename = "cirros_2vnf_ns.tar.gz"
         self.descriptor_edit = None
         self.uses_configuration = False
-        self.uss = {}
-        self.passwds = {}
-        self.cmds = {}
+        self.users = {}
+        self.passwords = {}
+        self.commands = {}
         self.keys = {}
         self.timeout = 120
         self.qforce = ""
+        self.ns_params = None
 
     def create_descriptors(self, engine):
         temp_dir = os.path.dirname(os.path.abspath(__file__)) + "/temp/"
@@ -712,7 +743,7 @@ class TestDeploy:
                 # vnfd CREATE AND UPLOAD in one step:
                 engine.test("Onboard VNFD in one step", "POST",
                             "/vnfpkgm/v1/vnf_packages_content" + self.qforce, headers, "@b" + vnfd_filename_path, 201,
-                            {"Location": "/vnfpkgm/v1/vnf_packages_content/", "Content-Type": "application/yaml"},
+                            r_headers_yaml_location_vnfd,
                             "yaml")
                 self.vnfds_id.append(engine.last_id)
             else:
@@ -754,7 +785,7 @@ class TestDeploy:
             # nsd CREATE AND UPLOAD in one step:
             engine.test("Onboard NSD in one step", "POST",
                         "/nsd/v1/ns_descriptors_content" + self.qforce, headers, "@b" + nsd_filename_path, 201,
-                        {"Location": "/nsd/v1/ns_descriptors_content/", "Content-Type": "application/yaml"}, yaml)
+                        r_headers_yaml_location_nsd, yaml)
             self.nsd_id = engine.last_id
         else:
             # nsd CREATE AND UPLOAD ZIP
@@ -792,7 +823,7 @@ class TestDeploy:
         self.ns_id = engine.last_id
         engine.test("Instantiate NS step 2", "POST",
                     "/nslcm/v1/ns_instances/{}/instantiate".format(self.ns_id), headers_yaml, ns_data_text,
-                    201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+                    201, r_headers_yaml_location_nslcmop, "yaml")
         nslcmop_id = engine.last_id
 
         if test_osm:
@@ -804,7 +835,7 @@ class TestDeploy:
         # remove deployment
         if test_osm:
             engine.test("Terminate NS", "POST", "/nslcm/v1/ns_instances/{}/terminate".format(self.ns_id), headers_yaml,
-                        None, 201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+                        None, 201, r_headers_yaml_location_nslcmop, "yaml")
             nslcmop2_id = engine.last_id
             # Wait until status is Ok
             engine.wait_operation_ready("ns", nslcmop2_id, timeout_deploy)
@@ -838,6 +869,11 @@ class TestDeploy:
 
         vnfr_list = ns_data['constituent-vnfr-ref']
         time = 0
+        _commands = commands if commands is not None else self.commands
+        _users = users if users is not None else self.users
+        _passwds = passwds if passwds is not None else self.passwords
+        _keys = keys if keys is not None else self.keys
+        _timeout = timeout if timeout != 0 else self.timeout
 
         for vnfr_id in vnfr_list:
             r = engine.test("Get VNFR to get IP_ADDRESS", "GET",
@@ -848,19 +884,19 @@ class TestDeploy:
             vnfr_data = r.json()
 
             vnf_index = str(vnfr_data["member-vnf-index-ref"])
-            if not commands.get(vnf_index):
+            if not _commands.get(vnf_index):
                 continue
             if vnfr_data.get("ip-address"):
-                description = "Exec command='{}' at VNFR={} IP={}".format(commands.get(vnf_index)[0], vnf_index,
+                description = "Exec command='{}' at VNFR={} IP={}".format(_commands.get(vnf_index)[0], vnf_index,
                                                                           vnfr_data['ip-address'])
                 engine.step += 1
                 test_description = "{}{} {}".format(engine.test_name, engine.step, description)
                 logger.warning(test_description)
-                while timeout >= time:
+                while _timeout >= time:
                     result, message = self.do_checks([vnfr_data["ip-address"]],
                                                      vnf_index=vnfr_data["member-vnf-index-ref"],
-                                                     commands=commands.get(vnf_index), user=users.get(vnf_index),
-                                                     passwd=passwds.get(vnf_index), key=keys.get(vnf_index))
+                                                     commands=_commands.get(vnf_index), user=_users.get(vnf_index),
+                                                     passwd=_passwds.get(vnf_index), key=_keys.get(vnf_index))
                     if result == 1:
                         engine.passed_tests += 1
                         logger.debug(message)
@@ -907,16 +943,17 @@ class TestDeploy:
                 output = client.run_command(cmd)
                 client.join(output)
                 if output[ip[0]].exit_code:
-                    return -1, "    VNFR {} command '{}' returns error: {}".format(ip[0], cmd, output[ip[0]].stderr)
+                    return -1, "VNFR {} command '{}' returns error: '{}'".format(ip[0], cmd,
+                                                                                 "\n".join(output[ip[0]].stderr))
                 else:
-                    return 1, "     VNFR {} command '{}' successful".format(ip[0], cmd)
+                    return 1, "VNFR {} command '{}' successful".format(ip[0], cmd)
         except (ssh2Exception.ChannelFailure, ssh2Exception.SocketDisconnectError, ssh2Exception.SocketTimeout,
                 ssh2Exception.SocketRecvError) as e:
             return 0, "Timeout accessing the VNFR {}: {}".format(ip[0], str(e))
         except Exception as e:
             return -1, "ERROR checking the VNFR {}: {}".format(ip[0], str(e))
 
-    def aditional_operations(self, engine, test_osm, manual_check):
+    def additional_operations(self, engine, test_osm, manual_check):
         pass
 
     def run(self, engine, test_osm, manual_check, test_params=None):
@@ -936,6 +973,8 @@ class TestDeploy:
         self.vim_id = engine.get_create_vim(test_osm)
         ns_data = {"nsDescription": "default description", "nsName": nsname, "nsdId": self.nsd_id,
                    "vimAccountId": self.vim_id}
+        if self.ns_params:
+            ns_data.update(self.ns_params)
         if test_params and test_params.get("ns-config"):
             if isinstance(test_params["ns-config"], str):
                 ns_data.update(yaml.load(test_params["ns-config"]))
@@ -945,9 +984,9 @@ class TestDeploy:
 
         if manual_check:
             input('NS has been deployed. Perform manual check and press enter to resume')
-        if test_osm and self.cmds:
-            self.test_ns(engine, test_osm, self.cmds, self.uss, self.pss, self.keys, self.timeout)
-        self.aditional_operations(engine, test_osm, manual_check)
+        if test_osm and self.commands:
+            self.test_ns(engine, test_osm)
+        self.additional_operations(engine, test_osm, manual_check)
         self.terminate(engine)
         self.delete_descriptors(engine)
 
@@ -960,9 +999,32 @@ class TestDeployHackfestCirros(TestDeploy):
         self.test_name = "CIRROS"
         self.vnfd_filenames = ("cirros_vnf.tar.gz",)
         self.nsd_filename = "cirros_2vnf_ns.tar.gz"
-        self.cmds = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
-        self.uss = {'1': "cirros", '2': "cirros"}
-        self.pss = {'1': "cubswin:)", '2': "cubswin:)"}
+        self.commands = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
+        self.users = {'1': "cirros", '2': "cirros"}
+        self.passwords = {'1': "cubswin:)", '2': "cubswin:)"}
+
+    def terminate(self, engine):
+        # Make a delete in one step, overriding the normal two step of TestDeploy that launched terminate and delete
+        if test_osm:
+            engine.test("Terminate and delete NS in one step", "DELETE", "/nslcm/v1/ns_instances_content/{}".
+                        format(self.ns_id), headers_yaml, None, 202, None, "yaml")
+
+            engine .wait_until_delete("/nslcm/v1/ns_instances/{}".format(self.ns_id), timeout_deploy)
+        else:
+            engine.test("Delete NS with FORCE", "DELETE", "/nslcm/v1/ns_instances/{}?FORCE=True".format(self.ns_id),
+                        headers_yaml, None, 204, None, 0)
+
+        # check all it is deleted
+        engine.test("Check NS is deleted", "GET", "/nslcm/v1/ns_instances/{}".format(self.ns_id), headers_yaml, None,
+                    404, None, "yaml")
+        r = engine.test("Check NSLCMOPs are deleted", "GET",
+                        "/nslcm/v1/ns_lcm_op_occs?nsInstanceId={}".format(self.ns_id), headers_json, None,
+                        200, None, "json")
+        if not r:
+            return
+        nslcmops = r.json()
+        if not isinstance(nslcmops, list) or nslcmops:
+            raise TestException("NS {} deleted but with ns_lcm_op_occ active: {}".format(self.ns_id, nslcmops))
 
 
 class TestDeployHackfest1(TestDeploy):
@@ -973,9 +1035,9 @@ class TestDeployHackfest1(TestDeploy):
         self.test_name = "HACKFEST1-"
         self.vnfd_filenames = ("hackfest_1_vnfd.tar.gz",)
         self.nsd_filename = "hackfest_1_nsd.tar.gz"
-        # self.cmds = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
-        # self.uss = {'1': "cirros", '2': "cirros"}
-        # self.pss = {'1': "cubswin:)", '2': "cubswin:)"}
+        # self.commands = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
+        # self.users = {'1': "cirros", '2': "cirros"}
+        # self.passwords = {'1': "cubswin:)", '2': "cubswin:)"}
 
 
 class TestDeployHackfestCirrosScaling(TestDeploy):
@@ -1006,7 +1068,7 @@ class TestDeployHackfestCirrosScaling(TestDeploy):
             }
         }
 
-    def aditional_operations(self, engine, test_osm, manual_check):
+    def additional_operations(self, engine, test_osm, manual_check):
         if not test_osm:
             return
         # 2 perform scale out twice
@@ -1015,7 +1077,7 @@ class TestDeployHackfestCirrosScaling(TestDeploy):
         for i in range(0, 2):
             engine.test("Execute scale action over NS", "POST",
                         "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
-                        201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+                        201, r_headers_yaml_location_nslcmop, "yaml")
             nslcmop2_scale_out = engine.last_id
             engine.wait_operation_ready("ns", nslcmop2_scale_out, timeout_deploy)
             if manual_check:
@@ -1028,7 +1090,7 @@ class TestDeployHackfestCirrosScaling(TestDeploy):
         for i in range(0, 2):
             engine.test("Execute scale IN action over NS", "POST",
                         "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
-                        201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+                        201, r_headers_yaml_location_nslcmop, "yaml")
             nslcmop2_scale_in = engine.last_id
             engine.wait_operation_ready("ns", nslcmop2_scale_in, timeout_deploy)
             if manual_check:
@@ -1038,7 +1100,7 @@ class TestDeployHackfestCirrosScaling(TestDeploy):
         # perform scale in that must fail as reached limit
         engine.test("Execute scale IN out of limit action over NS", "POST",
                     "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
-                    201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+                    201, r_headers_yaml_location_nslcmop, "yaml")
         nslcmop2_scale_in = engine.last_id
         engine.wait_operation_ready("ns", nslcmop2_scale_in, timeout_deploy, expected_fail=True)
 
@@ -1053,9 +1115,9 @@ class TestDeployIpMac(TestDeploy):
         self.nsd_filename = "scenario_2vdu_set_ip_mac.yaml"
         self.descriptor_url = \
             "https://osm.etsi.org/gitweb/?p=osm/RO.git;a=blob_plain;f=test/RO_tests/v3_2vdu_set_ip_mac/"
-        self.cmds = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
-        self.uss = {'1': "osm", '2': "osm"}
-        self.pss = {'1': "osm4u", '2': "osm4u"}
+        self.commands = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
+        self.users = {'1': "osm", '2': "osm"}
+        self.passwords = {'1': "osm4u", '2': "osm4u"}
         self.timeout = 360
 
     def run(self, engine, test_osm, manual_check, test_params=None):
@@ -1127,60 +1189,56 @@ class TestDeployHackfest4(TestDeploy):
         self.vnfd_filenames = ("hackfest_4_vnfd.tar.gz",)
         self.nsd_filename = "hackfest_4_nsd.tar.gz"
         self.uses_configuration = True
-        self.cmds = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
-        self.uss = {'1': "ubuntu", '2': "ubuntu"}
-        self.pss = {'1': "osm4u", '2': "osm4u"}
-
-    def create_descriptors(self, engine):
-        super().create_descriptors(engine)
+        self.commands = {'1': ['ls -lrt', ], '2': ['ls -lrt', ]}
+        self.users = {'1': "ubuntu", '2': "ubuntu"}
+        self.passwords = {'1': "osm4u", '2': "osm4u"}
         # Modify VNFD to add scaling
-        self.descriptor_edit = {
-            "vnfd0": {
-                'vnf-configuration': {
-                    'config-primitive': [{
-                        'name': 'touch',
-                        'parameter': [{
-                            'name': 'filename',
-                            'data-type': 'STRING',
-                            'default-value': '/home/ubuntu/touched'
-                        }]
-                    }]
-                },
-                'scaling-group-descriptor': [{
-                    'name': 'scale_dataVM',
-                    'scaling-policy': [{
-                        'threshold-time': 0,
-                        'name': 'auto_cpu_util_above_threshold',
-                        'scaling-type': 'automatic',
-                        'scaling-criteria': [{
-                            'name': 'cpu_util_above_threshold',
-                            'vnf-monitoring-param-ref': 'all_aaa_cpu_util',
-                            'scale-out-relational-operation': 'GE',
-                            'scale-in-threshold': 15,
-                            'scale-out-threshold': 60,
-                            'scale-in-relational-operation': 'LE'
-                        }],
-                        'cooldown-time': 60
-                    }],
-                    'max-instance-count': 10,
-                    'scaling-config-action': [
-                        {'vnf-config-primitive-name-ref': 'touch',
-                         'trigger': 'post-scale-out'},
-                        {'vnf-config-primitive-name-ref': 'touch',
-                         'trigger': 'pre-scale-in'}
-                    ],
-                    'vdu': [{
-                        'vdu-id-ref': 'dataVM',
-                        'count': 1
-                    }]
-                }]
-            }
-        }
+        self.descriptor_edit = {
+            "vnfd0": {
+                'vnf-configuration': {
+                    'config-primitive': [{
+                        'name': 'touch',
+                        'parameter': [{
+                            'name': 'filename',
+                            'data-type': 'STRING',
+                            'default-value': '/home/ubuntu/touched'
+                        }]
+                    }]
+                },
+                'scaling-group-descriptor': [{
+                    'name': 'scale_dataVM',
+                    'scaling-policy': [{
+                        'threshold-time': 0,
+                        'name': 'auto_cpu_util_above_threshold',
+                        'scaling-type': 'automatic',
+                        'scaling-criteria': [{
+                            'name': 'cpu_util_above_threshold',
+                            'vnf-monitoring-param-ref': 'all_aaa_cpu_util',
+                            'scale-out-relational-operation': 'GE',
+                            'scale-in-threshold': 15,
+                            'scale-out-threshold': 60,
+                            'scale-in-relational-operation': 'LE'
+                        }],
+                        'cooldown-time': 60
+                    }],
+                    'max-instance-count': 10,
+                    'scaling-config-action': [
+                        {'vnf-config-primitive-name-ref': 'touch',
+                         'trigger': 'post-scale-out'},
+                        {'vnf-config-primitive-name-ref': 'touch',
+                         'trigger': 'pre-scale-in'}
+                    ],
+                    'vdu': [{
+                        'vdu-id-ref': 'dataVM',
+                        'count': 1
+                    }]
+                }]
+            }
+        }
 
 
 class TestDeployHackfest3Charmed(TestDeploy):
-    description = "Load and deploy Hackfest 3charmed_ns example. Modifies it for adding scaling and performs " \
-                  "primitive actions and scaling"
+    description = "Load and deploy Hackfest 3charmed_ns example"
 
     def __init__(self):
         super().__init__()
@@ -1188,52 +1246,18 @@ class TestDeployHackfest3Charmed(TestDeploy):
         self.vnfd_filenames = ("hackfest_3charmed_vnfd.tar.gz",)
         self.nsd_filename = "hackfest_3charmed_nsd.tar.gz"
         self.uses_configuration = True
-        self.cmds = {'1': [''], '2': ['ls -lrt /home/ubuntu/first-touch', ]}
-        self.uss = {'1': "ubuntu", '2': "ubuntu"}
-        self.pss = {'1': "osm4u", '2': "osm4u"}
-        # self.descriptor_edit = {
-        #     "vnfd0": yaml.load("""
-        #         scaling-group-descriptor:
-        #             -   name: "scale_dataVM"
-        #                 max-instance-count: 10
-        #                 scaling-policy:
-        #                 -   name: "auto_cpu_util_above_threshold"
-        #                     scaling-type: "automatic"
-        #                     threshold-time: 0
-        #                     cooldown-time: 60
-        #                     scaling-criteria:
-        #                     -   name: "cpu_util_above_threshold"
-        #                         scale-in-threshold: 15
-        #                         scale-in-relational-operation: "LE"
-        #                         scale-out-threshold: 60
-        #                         scale-out-relational-operation: "GE"
-        #                         vnf-monitoring-param-ref: "all_aaa_cpu_util"
-        #                 vdu:
-        #                 -   vdu-id-ref: dataVM
-        #                     count: 1
-        #                 scaling-config-action:
-        #                 -   trigger: post-scale-out
-        #                     vnf-config-primitive-name-ref: touch
-        #                 -   trigger: pre-scale-in
-        #                     vnf-config-primitive-name-ref: touch
-        #         vnf-configuration:
-        #             config-primitive:
-        #             -   name: touch
-        #                 parameter:
-        #                 -   name: filename
-        #                     data-type: STRING
-        #                     default-value: '/home/ubuntu/touched'
-        #         """)
-        #     }
+        self.commands = {'1': ['ls -lrt /home/ubuntu/first-touch'], '2': ['ls -lrt /home/ubuntu/first-touch']}
+        self.users = {'1': "ubuntu", '2': "ubuntu"}
+        self.passwords = {'1': "osm4u", '2': "osm4u"}
 
-    def aditional_operations(self, engine, test_osm, manual_check):
+    def additional_operations(self, engine, test_osm, manual_check):
         if not test_osm:
             return
         # 1 perform action
         payload = '{member_vnf_index: "2", primitive: touch, primitive_params: { filename: /home/ubuntu/OSMTESTNBI }}'
         engine.test("Exec service primitive over NS", "POST",
                     "/nslcm/v1/ns_instances/{}/action".format(self.ns_id), headers_yaml, payload,
-                    201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+                    201, r_headers_yaml_location_nslcmop, "yaml")
         nslcmop2_action = engine.last_id
         # Wait until status is Ok
         engine.wait_operation_ready("ns", nslcmop2_action, timeout_deploy)
@@ -1241,17 +1265,15 @@ class TestDeployHackfest3Charmed(TestDeploy):
             input('NS service primitive has been executed. Check that file /home/ubuntu/OSMTESTNBI is present at '
                   'TODO_PUT_IP')
         if test_osm:
-            cmds = {'1': [''], '2': ['ls -lrt /home/ubuntu/OSMTESTNBI', ]}
-            uss = {'1': "ubuntu", '2': "ubuntu"}
-            pss = {'1': "osm4u", '2': "osm4u"}
-            self.test_ns(engine, test_osm, cmds, uss, pss, self.keys, self.timeout)
+            commands = {'1': [''], '2': ['ls -lrt /home/ubuntu/OSMTESTNBI', ]}
+            self.test_ns(engine, test_osm, commands=commands)
 
         # # 2 perform scale out
         # payload = '{scaleType: SCALE_VNF, scaleVnfData: {scaleVnfType: SCALE_OUT, scaleByStepData: ' \
         #           '{scaling-group-descriptor: scale_dataVM, member-vnf-index: "1"}}}'
         # engine.test("Execute scale action over NS", "POST",
         #             "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
-        #             201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+        #             201, r_headers_yaml_location_nslcmop, "yaml")
         # nslcmop2_scale_out = engine.last_id
         # engine.wait_operation_ready("ns", nslcmop2_scale_out, timeout_deploy)
         # if manual_check:
@@ -1263,7 +1285,7 @@ class TestDeployHackfest3Charmed(TestDeploy):
         #           '{scaling-group-descriptor: scale_dataVM, member-vnf-index: "1"}}}'
         # engine.test("Execute scale action over NS", "POST",
         #             "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
-        #             201, {"Location": "nslcm/v1/ns_lcm_op_occs/", "Content-Type": "application/yaml"}, "yaml")
+        #             201, r_headers_yaml_location_nslcmop, "yaml")
         # nslcmop2_scale_in = engine.last_id
         # engine.wait_operation_ready("ns", nslcmop2_scale_in, timeout_deploy)
         # if manual_check:
@@ -1271,65 +1293,13 @@ class TestDeployHackfest3Charmed(TestDeploy):
         # # TODO check automatic
 
 
-class TestDeploySimpleCharm(TestDeploy):
-    description = "Deploy hackfest-4 hackfest_simplecharm example"
-
-    def __init__(self):
-        super().__init__()
-        self.test_name = "HACKFEST-SIMPLE"
-        self.descriptor_url = "https://osm-download.etsi.org/ftp/osm-4.0-four/4th-hackfest/packages/"
-        self.vnfd_filenames = ("hackfest_simplecharm_vnf.tar.gz",)
-        self.nsd_filename = "hackfest_simplecharm_ns.tar.gz"
-        self.uses_configuration = True
-        self.cmds = {'1': [''], '2': ['ls -lrt /home/ubuntu/first-touch', ]}
-        self.uss = {'1': "ubuntu", '2': "ubuntu"}
-        self.pss = {'1': "osm4u", '2': "osm4u"}
-
-
-class TestDeploySimpleCharm2(TestDeploySimpleCharm):
-    description = "Deploy hackfest-4 hackfest_simplecharm example changing naming to contain dots on ids and " \
-                  "vnf-member-index"
-
-    def __init__(self):
-        super().__init__()
-        self.test_name = "HACKFEST-SIMPLE2-"
-        self.qforce = "?FORCE=True"
-        self.descriptor_edit = {
-            "vnfd0": {
-                "id": "hackfest.simplecharm.vnf"
-            },
-
-            "nsd": {
-                "id": "hackfest.simplecharm.ns",
-                "constituent-vnfd": {
-                    "$[0]": {"vnfd-id-ref": "hackfest.simplecharm.vnf", "member-vnf-index": "$1"},
-                    "$[1]": {"vnfd-id-ref": "hackfest.simplecharm.vnf", "member-vnf-index": "$2"},
-                },
-                "vld": {
-                    "$[0]": {
-                        "vnfd-connection-point-ref": {"$[0]": {"member-vnf-index-ref": "$1",
-                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"},
-                                                      "$[1]": {"member-vnf-index-ref": "$2",
-                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"}},
-                    },
-                    "$[1]": {
-                        "vnfd-connection-point-ref": {"$[0]": {"member-vnf-index-ref": "$1",
-                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"},
-                                                      "$[1]": {"member-vnf-index-ref": "$2",
-                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"}},
-                    },
-                }
-            }
-        }
-
-
 class TestDeployHackfest3Charmed2(TestDeployHackfest3Charmed):
     description = "Load and deploy Hackfest 3charmed_ns example modified version of descriptors to have dots in " \
-                  "ids and member-vnf-index"
+                  "ids and member-vnf-index."
 
     def __init__(self):
         super().__init__()
-        self.test_name = "HACKFEST3bis"
+        self.test_name = "HACKFEST3v2-"
         self.qforce = "?FORCE=True"
         self.descriptor_edit = {
             "vnfd0": {
@@ -1392,6 +1362,159 @@ class TestDeployHackfest3Charmed2(TestDeployHackfest3Charmed):
         }
 
 
+class TestDeployHackfest3Charmed3(TestDeployHackfest3Charmed):
+    description = "Load and deploy Hackfest 3charmed_ns example modified version to test scaling and NS parameters"
+
+    def __init__(self):
+        super().__init__()
+        self.test_name = "HACKFEST3v3-"
+        self.commands = {'1': ['ls -lrt /home/ubuntu/first-touch-1'], '2': ['ls -lrt /home/ubuntu/first-touch-2']}
+        self.descriptor_edit = {
+            "vnfd0": yaml.load(
+                """
+                scaling-group-descriptor:
+                    -   name: "scale_dataVM"
+                        max-instance-count: 10
+                        scaling-policy:
+                        -   name: "auto_cpu_util_above_threshold"
+                            scaling-type: "automatic"
+                            threshold-time: 0
+                            cooldown-time: 60
+                            scaling-criteria:
+                            -   name: "cpu_util_above_threshold"
+                                scale-in-threshold: 15
+                                scale-in-relational-operation: "LE"
+                                scale-out-threshold: 60
+                                scale-out-relational-operation: "GE"
+                                vnf-monitoring-param-ref: "monitor1"
+                        vdu:
+                        -   vdu-id-ref: dataVM
+                            count: 1
+                        scaling-config-action:
+                        -   trigger: post-scale-out
+                            vnf-config-primitive-name-ref: touch
+                        -   trigger: pre-scale-in
+                            vnf-config-primitive-name-ref: touch
+                vdu:
+                    "$id: dataVM":
+                        monitoring-param:
+                        -   id: "dataVM_cpu_util"
+                            nfvi-metric: "cpu_utilization"
+
+                monitoring-param:
+                -   id: "monitor1"
+                    name: "monitor1"
+                    aggregation-type: AVERAGE
+                    vdu-monitoring-param:
+                      vdu-ref: "dataVM"
+                      vdu-monitoring-param-ref: "dataVM_cpu_util"
+                vnf-configuration:
+                    initial-config-primitive:
+                        "$[1]":
+                            parameter:
+                                "$[0]":
+                                    value: "<touch-filename>"   # default-value: /home/ubuntu/first-touch
+                    config-primitive:
+                        "$[0]":
+                            parameter:
+                                "$[0]":
+                                    default-value: "<touch-filename2>"
+                """)
+        }
+        self.ns_params = {
+            "additionalParamsForVnf": [
+                {"member-vnf-index": "1", "additionalParams": {"touch-filename": "/home/ubuntu/first-touch-1",
+                                                               "touch-filename2": "/home/ubuntu/second-touch-1"}},
+                {"member-vnf-index": "2", "additionalParams": {"touch-filename": "/home/ubuntu/first-touch-2",
+                                                               "touch-filename2": "/home/ubuntu/second-touch-2"}},
+            ]
+        }
+
+    def additional_operations(self, engine, test_osm, manual_check):
+        super().additional_operations(engine, test_osm, manual_check)
+        if not test_osm:
+            return
+
+        # 2 perform scale out
+        payload = '{scaleType: SCALE_VNF, scaleVnfData: {scaleVnfType: SCALE_OUT, scaleByStepData: ' \
+                  '{scaling-group-descriptor: scale_dataVM, member-vnf-index: "1"}}}'
+        engine.test("Execute scale action over NS", "POST",
+                    "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
+                    201, r_headers_yaml_location_nslcmop, "yaml")
+        nslcmop2_scale_out = engine.last_id
+        engine.wait_operation_ready("ns", nslcmop2_scale_out, timeout_deploy)
+        if manual_check:
+            input('NS scale out done. Check that file /home/ubuntu/second-touch-1 is present and new VM is created')
+        if test_osm:
+            commands = {'1': ['ls -lrt /home/ubuntu/second-touch-1', ]}
+            self.test_ns(engine, test_osm, commands=commands)
+            # TODO check automatic connection to scaled VM
+
+        # 2 perform scale in
+        payload = '{scaleType: SCALE_VNF, scaleVnfData: {scaleVnfType: SCALE_IN, scaleByStepData: ' \
+                  '{scaling-group-descriptor: scale_dataVM, member-vnf-index: "1"}}}'
+        engine.test("Execute scale action over NS", "POST",
+                    "/nslcm/v1/ns_instances/{}/scale".format(self.ns_id), headers_yaml, payload,
+                    201, r_headers_yaml_location_nslcmop, "yaml")
+        nslcmop2_scale_in = engine.last_id
+        engine.wait_operation_ready("ns", nslcmop2_scale_in, timeout_deploy)
+        if manual_check:
+            input('NS scale in done. Check that file /home/ubuntu/second-touch-1 is updated and new VM is deleted')
+        # TODO check automatic
+
+
+class TestDeploySimpleCharm(TestDeploy):
+    description = "Deploy hackfest-4 hackfest_simplecharm example"
+
+    def __init__(self):
+        super().__init__()
+        self.test_name = "HACKFEST-SIMPLE"
+        self.descriptor_url = "https://osm-download.etsi.org/ftp/osm-4.0-four/4th-hackfest/packages/"
+        self.vnfd_filenames = ("hackfest_simplecharm_vnf.tar.gz",)
+        self.nsd_filename = "hackfest_simplecharm_ns.tar.gz"
+        self.uses_configuration = True
+        self.commands = {'1': [''], '2': ['ls -lrt /home/ubuntu/first-touch', ]}
+        self.users = {'1': "ubuntu", '2': "ubuntu"}
+        self.passwords = {'1': "osm4u", '2': "osm4u"}
+
+
+class TestDeploySimpleCharm2(TestDeploySimpleCharm):
+    description = "Deploy hackfest-4 hackfest_simplecharm example changing naming to contain dots on ids and " \
+                  "vnf-member-index"
+
+    def __init__(self):
+        super().__init__()
+        self.test_name = "HACKFEST-SIMPLE2-"
+        self.qforce = "?FORCE=True"
+        self.descriptor_edit = {
+            "vnfd0": {
+                "id": "hackfest.simplecharm.vnf"
+            },
+
+            "nsd": {
+                "id": "hackfest.simplecharm.ns",
+                "constituent-vnfd": {
+                    "$[0]": {"vnfd-id-ref": "hackfest.simplecharm.vnf", "member-vnf-index": "$1"},
+                    "$[1]": {"vnfd-id-ref": "hackfest.simplecharm.vnf", "member-vnf-index": "$2"},
+                },
+                "vld": {
+                    "$[0]": {
+                        "vnfd-connection-point-ref": {"$[0]": {"member-vnf-index-ref": "$1",
+                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"},
+                                                      "$[1]": {"member-vnf-index-ref": "$2",
+                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"}},
+                    },
+                    "$[1]": {
+                        "vnfd-connection-point-ref": {"$[0]": {"member-vnf-index-ref": "$1",
+                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"},
+                                                      "$[1]": {"member-vnf-index-ref": "$2",
+                                                               "vnfd-id-ref": "hackfest.simplecharm.vnf"}},
+                    },
+                }
+            }
+        }
+
+
 class TestDeploySingleVdu(TestDeployHackfest3Charmed):
     description = "Generate a single VDU base on editing Hackfest3Charmed descriptors and deploy"
 
@@ -1569,8 +1692,7 @@ class TestDeployHnfd(TestDeployHackfest3Charmed):
         if manual_check:
             input('VNF to be used as PDU has been deployed. Perform manual check and press enter to resume')
         if test_osm:
-            self.pduDeploy.test_ns(engine, test_osm, self.pduDeploy.cmds, self.pduDeploy.uss, self.pduDeploy.pss,
-                                   self.pduDeploy.keys, self.pduDeploy.timeout)
+            self.pduDeploy.test_ns(engine, test_osm)
 
         if test_osm:
             r = engine.test("Get VNFR to obtain IP_ADDRESS", "GET",
@@ -1607,8 +1729,8 @@ class TestDeployHnfd(TestDeployHackfest3Charmed):
         if manual_check:
             input('NS has been deployed. Perform manual check and press enter to resume')
         if test_osm:
-            self.test_ns(engine, test_osm, self.cmds, self.uss, self.pss, self.keys, self.timeout)
-        self.aditional_operations(engine, test_osm, manual_check)
+            self.test_ns(engine, test_osm)
+        self.additional_operations(engine, test_osm, manual_check)
         self.terminate(engine)
         self.pduDeploy.terminate(engine)
         self.delete_descriptors(engine)
@@ -1631,6 +1753,31 @@ class TestDescriptors:
         self.descriptor_url = "https://osm-download.etsi.org/ftp/osm-3.0-three/2nd-hackfest/packages/"
         self.vnfd_id = None
         self.nsd_id = None
+        self.vnfd_empty = """vnfd:vnfd-catalog:
+            vnfd:
+            -   name: prova
+                short-name: prova
+                id: prova
+        """
+        self.vnfd_prova = """vnfd:vnfd-catalog:
+            vnfd:
+            -   connection-point:
+                -   name: cp_0h8m
+                    type: VPORT
+                id: prova
+                name: prova
+                short-name: prova
+                vdu:
+                -   id: vdu_z4bm
+                    image: ubuntu
+                    interface:
+                    -   external-connection-point-ref: cp_0h8m
+                        name: eth0
+                        virtual-interface:
+                        type: VIRTIO
+                    name: vdu_z4bm
+                version: '1.0'
+        """
 
     def run(self, engine, test_osm, manual_check, test_params=None):
         engine.set_test_name("Descriptors")
@@ -1653,20 +1800,50 @@ class TestDescriptors:
         vnfd_filename_path = temp_dir + self.vnfd_filename
         nsd_filename_path = temp_dir + self.nsd_filename
 
-        # vnfd CREATE AND UPLOAD in one step:
-        engine.test("Onboard VNFD in one step", "POST",
-                    "/vnfpkgm/v1/vnf_packages_content", headers_zip_yaml, "@b" + vnfd_filename_path, 201,
-                    {"Location": "/vnfpkgm/v1/vnf_packages_content/", "Content-Type": "application/yaml"}, "yaml")
+        engine.test("Onboard empty VNFD in one step", "POST", "/vnfpkgm/v1/vnf_packages_content", headers_yaml,
+                    self.vnfd_empty, 201, r_headers_yaml_location_vnfd, "yaml")
         self.vnfd_id = engine.last_id
 
+        # test bug 605
+        engine.test("Upload invalid VNFD ", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(self.vnfd_id),
+                    headers_yaml, self.vnfd_prova, 422, r_header_yaml, "yaml")
+
+        engine.test("Upload VNFD {}".format(self.vnfd_filename), "PUT",
+                    "/vnfpkgm/v1/vnf_packages/{}/package_content".format(self.vnfd_id), headers_zip_yaml,
+                    "@b" + vnfd_filename_path, 204, None, 0)
+
+        queries = ["mgmt-interface.cp=mgmt", "vdu.0.interface.0.external-connection-point-ref=mgmt",
+                   "vdu.0.interface.1.internal-connection-point-ref=internal",
+                   "internal-vld.0.internal-connection-point.0.id-ref=internal",
+                   # Detection of duplicated VLD names in VNF Descriptors
+                   # URL: internal-vld=[
+                   #        {id: internal1, name: internal, type:ELAN,
+                   #            internal-connection-point: [{id-ref: mgmtVM-internal}, {id-ref: dataVM-internal}]},
+                   #        {id: internal2, name: internal, type:ELAN,
+                   #            internal-connection-point: [{id-ref: mgmtVM-internal}, {id-ref: dataVM-internal}]}
+                   #        ]
+                   "internal-vld=%5B%7Bid%3A%20internal1%2C%20name%3A%20internal%2C%20type%3A%20ELAN%2C%20"
+                   "internal-connection-point%3A%20%5B%7Bid-ref%3A%20mgmtVM-internal%7D%2C%20%7Bid-ref%3A%20"
+                   "dataVM-internal%7D%5D%7D%2C%20%7Bid%3A%20internal2%2C%20name%3A%20internal%2C%20type%3A%20"
+                   "ELAN%2C%20internal-connection-point%3A%20%5B%7Bid-ref%3A%20mgmtVM-internal%7D%2C%20%7B"
+                   "id-ref%3A%20dataVM-internal%7D%5D%7D%5D"
+                   ]
+        for query in queries:
+            engine.test("Upload invalid VNFD ", "PUT",
+                        "/vnfpkgm/v1/vnf_packages/{}/package_content?{}".format(self.vnfd_id, query),
+                        headers_zip_yaml, "@b" + vnfd_filename_path, 422, r_header_yaml, "yaml")
+
+        # test bug 605
+        engine.test("Upload invalid VNFD ", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(self.vnfd_id),
+                    headers_yaml, self.vnfd_prova, 422, r_header_yaml, "yaml")
+
         # get vnfd descriptor
-        engine.test("Get VNFD descriptor", "GET",
-                    "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_id), headers_yaml, None, 200, r_header_yaml, "yaml")
+        engine.test("Get VNFD descriptor", "GET", "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_id),
+                    headers_yaml, None, 200, r_header_yaml, "yaml")
 
         # get vnfd file descriptor
-        engine.test("Get VNFD file descriptor", "GET",
-                    "/vnfpkgm/v1/vnf_packages/{}/vnfd".format(self.vnfd_id), headers_text, None, 200,
-                    r_header_text, "text", temp_dir+"vnfd-yaml")
+        engine.test("Get VNFD file descriptor", "GET", "/vnfpkgm/v1/vnf_packages/{}/vnfd".format(self.vnfd_id),
+                    headers_text, None, 200, r_header_text, "text", temp_dir+"vnfd-yaml")
         # TODO compare files: diff vnfd-yaml hackfest_3charmed_vnfd/hackfest_3charmed_vnfd.yaml
 
         # get vnfd zip file package
@@ -1682,25 +1859,28 @@ class TestDescriptors:
         # TODO compare files: diff vnfd-icon hackfest_3charmed_vnfd/icons/osm.png
 
         # nsd CREATE AND UPLOAD in one step:
-        engine.test("Onboard NSD in one step", "POST",
-                    "/nsd/v1/ns_descriptors_content", headers_zip_yaml, "@b" + nsd_filename_path, 201,
-                    {"Location": "/nsd/v1/ns_descriptors_content/", "Content-Type": "application/yaml"}, "yaml")
+        engine.test("Onboard NSD in one step", "POST", "/nsd/v1/ns_descriptors_content", headers_zip_yaml,
+                    "@b" + nsd_filename_path, 201, r_headers_yaml_location_nsd, "yaml")
         self.nsd_id = engine.last_id
 
+        queries = ["vld.0.vnfd-connection-point-ref.0.vnfd-id-ref=hf"]
+        for query in queries:
+            engine.test("Upload invalid NSD ", "PUT",
+                        "/nsd/v1/ns_descriptors/{}/nsd_content?{}".format(self.nsd_id, query),
+                        headers_zip_yaml, "@b" + nsd_filename_path, 422, r_header_yaml, "yaml")
+
         # get nsd descriptor
-        engine.test("Get NSD descriptor", "GET",
-                    "/nsd/v1/ns_descriptors/{}".format(self.nsd_id), headers_yaml, None, 200, r_header_yaml, "yaml")
+        engine.test("Get NSD descriptor", "GET", "/nsd/v1/ns_descriptors/{}".format(self.nsd_id), headers_yaml,
+                    None, 200, r_header_yaml, "yaml")
 
         # get nsd file descriptor
-        engine.test("Get NSD file descriptor", "GET",
-                    "/nsd/v1/ns_descriptors/{}/nsd".format(self.nsd_id), headers_text, None, 200,
-                    r_header_text, "text", temp_dir+"nsd-yaml")
+        engine.test("Get NSD file descriptor", "GET", "/nsd/v1/ns_descriptors/{}/nsd".format(self.nsd_id), headers_text,
+                    None, 200, r_header_text, "text", temp_dir+"nsd-yaml")
         # TODO compare files: diff nsd-yaml hackfest_3charmed_nsd/hackfest_3charmed_nsd.yaml
 
         # get nsd zip file package
-        engine.test("Get NSD zip package", "GET",
-                    "/nsd/v1/ns_descriptors/{}/nsd_content".format(self.nsd_id), headers_zip, None, 200,
-                    r_header_zip, "zip", temp_dir+"nsd-zip")
+        engine.test("Get NSD zip package", "GET", "/nsd/v1/ns_descriptors/{}/nsd_content".format(self.nsd_id),
+                    headers_zip, None, 200, r_header_zip, "zip", temp_dir+"nsd-zip")
         # TODO compare files: diff nsd-zip hackfest_3charmed_nsd.tar.gz
 
         # get nsd artifact
@@ -1710,29 +1890,56 @@ class TestDescriptors:
         # TODO compare files: diff nsd-icon hackfest_3charmed_nsd/icons/osm.png
 
         # vnfd DELETE
-        test_rest.test("Delete VNFD conflict", "DELETE",
-                       "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_id), headers_yaml, None, 409, None, None)
+        test_rest.test("Delete VNFD conflict", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_id),
+                       headers_yaml, None, 409, None, None)
 
-        test_rest.test("Delete VNFD force", "DELETE",
-                       "/vnfpkgm/v1/vnf_packages/{}?FORCE=TRUE".format(self.vnfd_id), headers_yaml, None, 204, None, 0)
+        test_rest.test("Delete VNFD force", "DELETE", "/vnfpkgm/v1/vnf_packages/{}?FORCE=TRUE".format(self.vnfd_id),
+                       headers_yaml, None, 204, None, 0)
 
         # nsd DELETE
-        test_rest.test("Delete NSD", "DELETE",
-                       "/nsd/v1/ns_descriptors/{}".format(self.nsd_id), headers_yaml, None, 204, None, 0)
+        test_rest.test("Delete NSD", "DELETE", "/nsd/v1/ns_descriptors/{}".format(self.nsd_id), headers_yaml, None, 204,
+                       None, 0)
 
 
 class TestNetSliceTemplates:
     description = "Upload a NST to OSM"
 
     def __init__(self):
-        self.nst_filenames = ("@./cirros_slice/cirros_slice_vld.yaml")
+        self.vnfd_filename = ("@./slice_shared/vnfd/slice_shared_vnfd.yaml")
+        self.vnfd_filename_middle = ("@./slice_shared/vnfd/slice_shared_middle_vnfd.yaml")
+        self.nsd_filename = ("@./slice_shared/nsd/slice_shared_nsd.yaml")
+        self.nsd_filename_middle = ("@./slice_shared/nsd/slice_shared_middle_nsd.yaml")
+        self.nst_filenames = ("@./slice_shared/slice_shared_nstd.yaml")
 
     def run(self, engine, test_osm, manual_check, test_params=None):
         # nst CREATE
-        engine.set_test_name("NST")
+        engine.set_test_name("NST step ")
         engine.get_autorization()
+        temp_dir = os.path.dirname(os.path.abspath(__file__)) + "/temp/"
+        if not os.path.exists(temp_dir):
+            os.makedirs(temp_dir)
+
+        # Onboard VNFDs
+        engine.test("Onboard edge VNFD", "POST", "/vnfpkgm/v1/vnf_packages_content", headers_yaml,
+                    self.vnfd_filename, 201, r_headers_yaml_location_vnfd, "yaml")
+        self.vnfd_edge_id = engine.last_id
+
+        engine.test("Onboard middle VNFD", "POST", "/vnfpkgm/v1/vnf_packages_content", headers_yaml,
+                    self.vnfd_filename_middle, 201, r_headers_yaml_location_vnfd, "yaml")
+        self.vnfd_middle_id = engine.last_id
+
+        # Onboard NSDs
+        engine.test("Onboard NSD edge", "POST", "/nsd/v1/ns_descriptors_content", headers_yaml,
+                    self.nsd_filename, 201, r_headers_yaml_location_nsd, "yaml")
+        self.nsd_edge_id = engine.last_id
+
+        engine.test("Onboard NSD middle", "POST", "/nsd/v1/ns_descriptors_content", headers_yaml,
+                    self.nsd_filename_middle, 201, r_headers_yaml_location_nsd, "yaml")
+        self.nsd_middle_id = engine.last_id
+
+        # Onboard NST
         engine.test("Onboard NST", "POST", "/nst/v1/netslice_templates_content", headers_yaml, self.nst_filenames,
-                    201, {"Location": "/nst/v1/netslice_templates_content", "Content-Type": "application/yaml"}, "yaml")
+                    201, r_headers_yaml_location_nst, "yaml")
         nst_id = engine.last_id
 
         # nstd SHOW OSM format
@@ -1743,51 +1950,221 @@ class TestNetSliceTemplates:
         engine.test("Delete NSTD", "DELETE", "/nst/v1/netslice_templates/{}".format(nst_id), headers_json, None,
                     204, None, 0)
 
+        # NSDs DELETE
+        test_rest.test("Delete NSD middle", "DELETE", "/nsd/v1/ns_descriptors/{}".format(self.nsd_middle_id),
+                       headers_json, None, 204, None, 0)
+
+        test_rest.test("Delete NSD edge", "DELETE", "/nsd/v1/ns_descriptors/{}".format(self.nsd_edge_id), headers_json,
+                       None, 204, None, 0)
+
+        # VNFDs DELETE
+        test_rest.test("Delete VNFD edge", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_edge_id),
+                       headers_yaml, None, 204, None, 0)
+
+        test_rest.test("Delete VNFD middle", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_middle_id),
+                       headers_yaml, None, 204, None, 0)
+
 
 class TestNetSliceInstances:
+    '''
+    Test procedure:
+    1. Populate databases with VNFD, NSD, NST with the following scenario
+       +-----------------management-----------------+
+       |                     |                      |
+    +--+---+            +----+----+             +---+--+
+    |      |            |         |             |      |
+    | edge +---data1----+  middle +---data2-----+ edge |
+    |      |            |         |             |      |
+    +------+            +---------+             +------+
+                        shared-nss
+    2. Create NSI-1
+    3. Instantiate NSI-1
+    4. Create NSI-2
+    5. Instantiate NSI-2
+        Manual check - Are 2 slices instantiated correctly?
+        NSI-1 3 nss (2 nss-edges + 1 nss-middle)
+        NSI-2 2 nss (2 nss-edge sharing nss-middle)
+    6. Terminate NSI-1
+    7. Delete NSI-1
+        Manual check - Is slice NSI-1 deleted correctly?
+        NSI-2 with 2 nss-edge + 1 nss-middle (The one from NSI-1)
+    8. Create NSI-3
+    9. Instantiate NSI-3
+        Manual check - Is slice NSI-3 instantiated correctly?
+        NSI-3 reuse nss-middle. NSI-3 only create 2 nss-edge
+    10. Delete NSI-2
+    11. Terminate NSI-2
+    12. Delete NSI-3
+    13. Terminate NSI-3
+        Manual check - All cleaned correctly?
+        NSI-2 and NSI-3 were terminated and deleted
+    14. Cleanup database
+    '''
+
     description = "Upload a NST to OSM"
 
     def __init__(self):
         self.vim_id = None
-        self.nst_filenames = ("@./cirros_slice/cirros_slice.yaml")
+        self.vnfd_filename = ("@./slice_shared/vnfd/slice_shared_vnfd.yaml")
+        self.vnfd_filename_middle = ("@./slice_shared/vnfd/slice_shared_middle_vnfd.yaml")
+        self.nsd_filename = ("@./slice_shared/nsd/slice_shared_nsd.yaml")
+        self.nsd_filename_middle = ("@./slice_shared/nsd/slice_shared_middle_nsd.yaml")
+        self.nst_filenames = ("@./slice_shared/slice_shared_nstd.yaml")
+
+    def create_slice(self, engine, nsi_data, name):
+        ns_data_text = yaml.safe_dump(nsi_data, default_flow_style=True, width=256)
+        r = engine.test(name, "POST", "/nsilcm/v1/netslice_instances",
+                        headers_yaml, ns_data_text, 201,
+                        {"Location": "nsilcm/v1/netslice_instances/", "Content-Type": "application/yaml"}, "yaml")
+        return r
+
+    def instantiate_slice(self, engine, nsi_data, nsi_id, name):
+        ns_data_text = yaml.safe_dump(nsi_data, default_flow_style=True, width=256)
+        engine.test(name, "POST",
+                    "/nsilcm/v1/netslice_instances/{}/instantiate".format(nsi_id), headers_yaml, ns_data_text,
+                    201, r_headers_yaml_location_nsilcmop, "yaml")
+
+    def terminate_slice(self, engine, nsi_id, name):
+        engine.test(name, "POST", "/nsilcm/v1/netslice_instances/{}/terminate".format(nsi_id),
+                    headers_yaml, None, 201, r_headers_yaml_location_nsilcmop, "yaml")
+
+    def delete_slice(self, engine, nsi_id, name):
+        engine.test(name, "DELETE", "/nsilcm/v1/netslice_instances/{}".format(nsi_id), headers_yaml, None,
+                    204, None, 0)
 
     def run(self, engine, test_osm, manual_check, test_params=None):
         # nst CREATE
         engine.set_test_name("NSI")
         engine.get_autorization()
-        engine.test("Onboard NST", "POST", "/nst/v1/netslice_templates_content", headers_yaml, self.nst_filenames, 201,
-                    {"Location": "/nst/v1/netslice_templates_content", "Content-Type": "application/yaml"}, "yaml")
+
+        # Onboard VNFDs
+        engine.test("Onboard edge VNFD", "POST", "/vnfpkgm/v1/vnf_packages_content", headers_yaml,
+                    self.vnfd_filename, 201, r_headers_yaml_location_vnfd, "yaml")
+        self.vnfd_edge_id = engine.last_id
+
+        engine.test("Onboard middle VNFD", "POST", "/vnfpkgm/v1/vnf_packages_content", headers_yaml,
+                    self.vnfd_filename_middle, 201, r_headers_yaml_location_vnfd, "yaml")
+        self.vnfd_middle_id = engine.last_id
+
+        # Onboard NSDs
+        engine.test("Onboard NSD edge", "POST", "/nsd/v1/ns_descriptors_content", headers_yaml,
+                    self.nsd_filename, 201, r_headers_yaml_location_nsd, "yaml")
+        self.nsd_edge_id = engine.last_id
+
+        engine.test("Onboard NSD middle", "POST", "/nsd/v1/ns_descriptors_content", headers_yaml,
+                    self.nsd_filename_middle, 201, r_headers_yaml_location_nsd, "yaml")
+        self.nsd_middle_id = engine.last_id
+
+        # Onboard NST
+        engine.test("Onboard NST", "POST", "/nst/v1/netslice_templates_content", headers_yaml, self.nst_filenames,
+                    201, r_headers_yaml_location_nst, "yaml")
         nst_id = engine.last_id
 
-        # nsi CREATE
         self.vim_id = engine.get_create_vim(test_osm)
 
-        ns_data = {"nsiDescription": "default description", "nsiName": "my_slice", "nstId": nst_id,
-                   "vimAccountId": self.vim_id}
-        ns_data_text = yaml.safe_dump(ns_data, default_flow_style=True, width=256)
+        # CREATE NSI-1
+        ns_data = {'nsiName': 'Deploy-NSI-1', 'vimAccountId': self.vim_id, 'nstId': nst_id, 'nsiDescription': 'default'}
+        r = self.create_slice(engine, ns_data, "Create NSI-1 step 1")
+        if not r:
+            return
+        self.nsi_id1 = engine.last_id
+
+        # INSTANTIATE NSI-1
+        self.instantiate_slice(engine, ns_data, self.nsi_id1, "Instantiate NSI-1 step 2")
+        nsilcmop_id1 = engine.last_id
+
+        # Waiting for NSI-1
+        engine.wait_operation_ready("nsi", nsilcmop_id1, timeout_deploy)
 
-        engine.test("Onboard NSI", "POST", "/nsilcm/v1/netslice_instances_content", headers_yaml, ns_data_text, 201,
-                    {"Location": "/nsilcm/v1/netslice_instances_content", "Content-Type": "application/yaml"}, "yaml")
-        nsi_id = engine.last_id
+        # CREATE NSI-2
+        ns_data = {'nsiName': 'Deploy-NSI-2', 'vimAccountId': self.vim_id, 'nstId': nst_id, 'nsiDescription': 'default'}
+        r = self.create_slice(engine, ns_data, "Create NSI-2 step 1")
+        if not r:
+            return
+        self.nsi_id2 = engine.last_id
+
+        # INSTANTIATE NSI-2
+        self.instantiate_slice(engine, ns_data, self.nsi_id2, "Instantiate NSI-2 step 2")
+        nsilcmop_id2 = engine.last_id
+
+        # Waiting for NSI-2
+        engine.wait_operation_ready("nsi", nsilcmop_id2, timeout_deploy)
+
+        if manual_check:
+            input('NSI-1 AND NSI-2 has been deployed. Perform manual check and press enter to resume')
+
+        # TERMINATE NSI-1
+        self.terminate_slice(engine, self.nsi_id1, "Terminate NSI-1")
+        nsilcmop1_id = engine.last_id
+
+        # Wait terminate NSI-1
+        engine.wait_operation_ready("nsi", nsilcmop1_id, timeout_deploy)
+
+        # DELETE NSI-1
+        self.delete_slice(engine, self.nsi_id1, "Delete NS")
+
+        if manual_check:
+            input('NSI-1 has been deleted. Perform manual check and press enter to resume')
+
+        # CREATE NSI-3
+        ns_data = {'nsiName': 'Deploy-NSI-3', 'vimAccountId': self.vim_id, 'nstId': nst_id, 'nsiDescription': 'default'}
+        r = self.create_slice(engine, ns_data, "Create NSI-3 step 1")
+
+        if not r:
+            return
+        self.nsi_id3 = engine.last_id
+
+        # INSTANTIATE NSI-3
+        self.instantiate_slice(engine, ns_data, self.nsi_id3, "Instantiate NSI-3 step 2")
+        nsilcmop_id3 = engine.last_id
+
+        # Wait Instantiate NSI-3
+        engine.wait_operation_ready("nsi", nsilcmop_id3, timeout_deploy)
+
+        if manual_check:
+            input('NSI-3 has been deployed. Perform manual check and press enter to resume')
+
+        # TERMINATE NSI-2
+        self.terminate_slice(engine, self.nsi_id2, "Terminate NSI-2")
+        nsilcmop2_id = engine.last_id
+
+        # Wait terminate NSI-2
+        engine.wait_operation_ready("nsi", nsilcmop2_id, timeout_deploy)
         
-        # TODO: Improve the wait with a polling if NSI was deployed
-        wait = 120
-        sleep(wait)
-
-        # Check deployment
-        engine.test("Wait until NSI is deployed", "GET", "/nsilcm/v1/netslice_instances_content/{}".format(nsi_id),
-                    headers_json, None, 200, r_header_json, "json")
-        # nsi DELETE
-        engine.test("Delete NSI", "DELETE", "/nsilcm/v1/netslice_instances_content/{}".format(nsi_id), headers_json,
-                    None, 202, r_header_json, "json")
+        # DELETE NSI-2
+        self.delete_slice(engine, self.nsi_id2, "DELETE NSI-2")
+
+        # TERMINATE NSI-3
+        self. terminate_slice(engine, self.nsi_id3, "Terminate NSI-3")
+        nsilcmop3_id = engine.last_id
         
-        sleep(60)
+        # Wait terminate NSI-3
+        engine.wait_operation_ready("nsi", nsilcmop3_id, timeout_deploy)
+
+        # DELETE NSI-3
+        self.delete_slice(engine, self.nsi_id3, "DELETE NSI-3")
+
+        if manual_check:
+            input('NSI-2 and NSI-3 has been deleted. Perform manual check and press enter to resume')
 
         # nstd DELETE
         engine.test("Delete NSTD", "DELETE", "/nst/v1/netslice_templates/{}".format(nst_id), headers_json, None,
                     204, None, 0)
 
+        # NSDs DELETE
+        test_rest.test("Delete NSD middle", "DELETE", "/nsd/v1/ns_descriptors/{}".format(self.nsd_middle_id),
+                       headers_json, None, 204, None, 0)
+
+        test_rest.test("Delete NSD edge", "DELETE", "/nsd/v1/ns_descriptors/{}".format(self.nsd_edge_id), headers_json,
+                       None, 204, None, 0)
+
+        # VNFDs DELETE
+        test_rest.test("Delete VNFD edge", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_edge_id),
+                       headers_yaml, None, 204, None, 0)
+
+        test_rest.test("Delete VNFD middle", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(self.vnfd_middle_id),
+                       headers_yaml, None, 204, None, 0)
+
 
 if __name__ == "__main__":
     global logger
@@ -1819,6 +2196,8 @@ if __name__ == "__main__":
             "Deploy-Hackfest-Cirros": TestDeployHackfestCirros,
             "Deploy-Hackfest-Cirros-Scaling": TestDeployHackfestCirrosScaling,
             "Deploy-Hackfest-3Charmed": TestDeployHackfest3Charmed,
+            "Deploy-Hackfest-3Charmed2": TestDeployHackfest3Charmed2,
+            "Deploy-Hackfest-3Charmed3": TestDeployHackfest3Charmed3,
             "Deploy-Hackfest-4": TestDeployHackfest4,
             "Deploy-CirrosMacIp": TestDeployIpMac,
             "TestDescriptors": TestDescriptors,
@@ -1826,8 +2205,8 @@ if __name__ == "__main__":
             # "Deploy-MultiVIM": TestDeployMultiVIM,
             "DeploySingleVdu": TestDeploySingleVdu,
             "DeployHnfd": TestDeployHnfd,
-            "Upload-Slice-Template": TestNetSliceTemplates,
-            "Deploy-Slice-Instance": TestNetSliceInstances,
+            "Upload-Slice-Template": TestNetSliceTemplates,
+            "Deploy-Slice-Instance": TestNetSliceInstances,
             "TestDeploySimpleCharm": TestDeploySimpleCharm,
             "TestDeploySimpleCharm2": TestDeploySimpleCharm2,
         }
@@ -1840,8 +2219,8 @@ if __name__ == "__main__":
                 print("test version " + __version__ + ' ' + version_date)
                 exit()
             elif o == "--list":
-                for test, test_class in test_classes.items():
-                    print("{:20} {}".format(test + ":", test_class.description))
+                for test, test_class in sorted(test_classes.items()):
+                    print("{:32} {}".format(test + ":", test_class.description))
                 exit()
             elif o in ("-v", "--verbose"):
                 verbose += 1