From: tierno Date: Fri, 11 May 2018 11:44:22 +0000 (+0200) Subject: bug 495 check descriptor dependencies at create,delete,edit X-Git-Tag: v4.0.0~4 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F32%2F6132%2F4;p=osm%2FNBI.git bug 495 check descriptor dependencies at create,delete,edit Change-Id: I70e267c85ab77460ade0b7f2fa239f73ee07b262 Signed-off-by: tierno --- diff --git a/osm_nbi/engine.py b/osm_nbi/engine.py index d771f96..c4a2619 100644 --- a/osm_nbi/engine.py +++ b/osm_nbi/engine.py @@ -243,7 +243,48 @@ class Engine(object): clean_indata = clean_indata['userDefinedData'] return clean_indata - def _validate_new_data(self, session, item, indata, id=None): + def _check_dependencies_on_descriptor(self, session, item, descriptor_id): + """ + Check that the descriptor to be deleded is not a dependency of others + :param session: client session information + :param item: can be vnfds, nsds + :param descriptor_id: id of descriptor to be deleted + :return: None or raises exception + """ + if item == "vnfds": + _filter = {"constituent-vnfd.ANYINDEX.vnfd-id-ref": descriptor_id} + if self.get_item_list(session, "nsds", _filter): + raise EngineException("There are nsd that depends on this VNFD", http_code=HTTPStatus.CONFLICT) + elif item == "nsds": + _filter = {"nsdId": descriptor_id} + if self.get_item_list(session, "nsrs", _filter): + raise EngineException("There are nsr that depends on this NSD", http_code=HTTPStatus.CONFLICT) + + def _check_descriptor_dependencies(self, session, item, descriptor): + """ + Check that the dependent descriptors exist on a new descriptor or edition + :param session: client session information + :param item: can be nsds, nsrs + :param descriptor: descriptor to be inserted or edit + :return: None or raises exception + """ + if item == "nsds": + if not descriptor.get("constituent-vnfd"): + return + for vnf in descriptor["constituent-vnfd"]: + vnfd_id = vnf["vnfd-id-ref"] + if not self.get_item_list(session, "vnfds", {"id": vnfd_id}): + raise EngineException("Descriptor error at 'constituent-vnfd':'vnfd-id-ref'='{}' references a non " + "existing vnfd".format(vnfd_id), http_code=HTTPStatus.CONFLICT) + elif item == "nsrs": + if not descriptor.get("nsdId"): + return + nsd_id = descriptor["nsdId"] + if not self.get_item_list(session, "nsds", {"id": nsd_id}): + raise EngineException("Descriptor error at nsdId='{}' references a non exist nsd".format(nsd_id), + http_code=HTTPStatus.CONFLICT) + + def _validate_new_data(self, session, item, indata, id=None, force=False): if item == "users": if not indata.get("username"): raise EngineException("missing 'username'", HTTPStatus.UNPROCESSABLE_ENTITY) @@ -271,6 +312,8 @@ class Engine(object): HTTPStatus.CONFLICT) # TODO validate with pyangbind + if item == "nsds" and not force: + self._check_descriptor_dependencies(session, "nsds", indata) elif item == "userDefinedData": # TODO validate userDefinedData is a keypair values pass @@ -620,7 +663,7 @@ class Engine(object): raise EngineException( "Invalid query string '{}'. Index '{}' out of range".format(k, kitem_old)) - def new_item(self, session, item, indata={}, kwargs=None, headers={}): + def new_item(self, session, item, indata={}, kwargs=None, headers={}, force=False): """ Creates a new entry into database. For nsds and vnfds it creates an almost empty DISABLED entry, that must be completed with a call to method upload_content @@ -629,6 +672,7 @@ class Engine(object): :param indata: data to be inserted :param kwargs: used to override the indata descriptor :param headers: http request headers + :param force: If True avoid some dependence checks :return: _id: identity of the inserted data. """ @@ -649,7 +693,7 @@ class Engine(object): # in this case the input descriptor is not the data to be stored return self.new_nsr(session, ns_request=content) - self._validate_new_data(session, item_envelop, content) + self._validate_new_data(session, item_envelop, content, force) if item in ("nsds", "vnfds"): content = {"_admin": {"userDefinedData": content}} self._format_new_data(session, item, content) @@ -847,7 +891,7 @@ class Engine(object): def del_item(self, session, item, _id, force=False): """ - Get complete information on an items + Delete item by its internal id :param session: contains the used login username and working project :param item: it can be: users, projects, vnfds, nsds, ... :param _id: server id of the item @@ -858,6 +902,10 @@ class Engine(object): # data = self.get_item(item, _id) filter = {"_id": _id} self._add_delete_filter(session, item, filter) + if item in ("vnfds", "nsds") and not force: + descriptor = self.get_item(session, item, _id) + descriptor_id = descriptor["id"] + self._check_dependencies_on_descriptor(session, item, descriptor_id) if item == "nsrs": nsr = self.db.get_one(item, filter) @@ -934,7 +982,7 @@ class Engine(object): version["status"]), HTTPStatus.INTERNAL_SERVER_ERROR) return - def _edit_item(self, session, item, id, content, indata={}, kwargs=None): + def _edit_item(self, session, item, id, content, indata={}, kwargs=None, force=False): if indata: indata = self._remove_envelop(item, indata) @@ -971,7 +1019,7 @@ class Engine(object): raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) _deep_update(content, indata) - self._validate_new_data(session, item, content, id) + self._validate_new_data(session, item, content, id, force) # self._format_new_data(session, item, content) self.db.replace(item, id, content) if item in ("vim_accounts", "sdns"): @@ -983,7 +1031,7 @@ class Engine(object): self.msg.write("sdn", "edit", indata) return id - def edit_item(self, session, item, _id, indata={}, kwargs=None): + def edit_item(self, session, item, _id, indata={}, kwargs=None, force=False): """ Update an existing entry at database :param session: contains the used login username and working project @@ -991,9 +1039,10 @@ class Engine(object): :param _id: identifier to be updated :param indata: data to be inserted :param kwargs: used to override the indata descriptor + :param force: If True avoid some dependence checks :return: dictionary, raise exception if not found. """ content = self.get_item(session, item, _id) - return self._edit_item(session, item, _id, content, indata, kwargs) + return self._edit_item(session, item, _id, content, indata, kwargs, force) diff --git a/osm_nbi/html_public/version b/osm_nbi/html_public/version index b7021d1..ff270ea 100644 --- a/osm_nbi/html_public/version +++ b/osm_nbi/html_public/version @@ -1,3 +1,3 @@ -0.1.4 -May 2018 +0.1.5 +2018-05-14 diff --git a/osm_nbi/nbi.py b/osm_nbi/nbi.py index cb727d8..566fb2b 100644 --- a/osm_nbi/nbi.py +++ b/osm_nbi/nbi.py @@ -343,6 +343,8 @@ class Server(object): raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST) except KeyError as exc: raise NbiException("Query string error: " + str(exc), HTTPStatus.BAD_REQUEST) + except Exception as exc: + raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST) @staticmethod def _format_out(data, session=None, _format=None): @@ -621,6 +623,10 @@ class Server(object): method = kwargs.pop("METHOD") else: method = cherrypy.request.method + if kwargs and "FORCE" in kwargs: + force = kwargs.pop("FORCE") + else: + force = False self._check_valid_url_method(method, topic, version, item, _id, item2, *args) @@ -670,7 +676,8 @@ class Server(object): if item in ("ns_descriptors_content", "vnf_packages_content"): _id = cherrypy.request.headers.get("Transaction-Id") if not _id: - _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers) + _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers, + force=force) rollback = {"session": session, "item": engine_item, "_id": _id, "force": True} completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers) if completed: @@ -679,7 +686,7 @@ class Server(object): cherrypy.response.headers["Transaction-Id"] = _id outdata = {"id": _id} elif item == "ns_instances_content": - _id = self.engine.new_item(session, engine_item, indata, kwargs) + _id = self.engine.new_item(session, engine_item, indata, kwargs, force=force) rollback = {"session": session, "item": engine_item, "_id": _id, "force": True} self.engine.ns_action(session, _id, "instantiate", {}, None) self._set_location_header(topic, version, item, _id) @@ -690,7 +697,8 @@ class Server(object): outdata = {"id": _id} cherrypy.response.status = HTTPStatus.ACCEPTED.value else: - _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers) + _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers, + force=force) self._set_location_header(topic, version, item, _id) outdata = {"id": _id} # TODO form NsdInfo when item in ("ns_descriptors", "vnf_packages") @@ -706,7 +714,6 @@ class Server(object): outdata = {"_id": opp_id} cherrypy.response.status = HTTPStatus.ACCEPTED.value else: - force = kwargs.get("FORCE") self.engine.del_item(session, engine_item, _id, force) cherrypy.response.status = HTTPStatus.NO_CONTENT.value if engine_item in ("vim_accounts", "sdns"): @@ -723,12 +730,12 @@ class Server(object): cherrypy.response.status = HTTPStatus.NO_CONTENT.value outdata = None else: - outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs)} + outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs, force=force)} elif method == "PATCH": if not indata and not kwargs: raise NbiException("Nothing to update. Provide payload and/or query string", HTTPStatus.BAD_REQUEST) - outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs)} + outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs, force=force)} else: raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED) return self._format_out(outdata, session, _format) diff --git a/osm_nbi/test/test.py b/osm_nbi/test/test.py index 979af44..2519953 100755 --- a/osm_nbi/test/test.py +++ b/osm_nbi/test/test.py @@ -290,6 +290,50 @@ if __name__ == "__main__": for t in test_admin_list1: test_rest.test(*t) + # vnfd CREATE + r = test_rest.test("VNFD1", "Onboard VNFD step 1", "POST", "/vnfpkgm/v1/vnf_packages", headers_json, None, + 201, {"Location": "/vnfpkgm/v1/vnf_packages/", "Content-Type": "application/json"}, "json") + location = r.headers["Location"] + vnfd_id = location[location.rfind("/")+1:] + # print(location, vnfd_id) + + # vnfd UPLOAD test + r = test_rest.test("VNFD2", "Onboard VNFD step 2 as TEXT", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), + r_header_text, "@./cirros_vnf/cirros_vnfd.yaml", 204, None, 0) + + # vnfd SHOW OSM format + r = test_rest.test("VNFD3", "Show VNFD OSM format", "GET", "/vnfpkgm/v1/vnf_packages_content/{}".format(vnfd_id), + headers_json, None, 200, r_header_json, "json") + + # vnfd SHOW text + r = test_rest.test("VNFD4", "Show VNFD SOL005 text", "GET", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), + headers_text, None, 200, r_header_text, "text") + + # vnfd UPLOAD ZIP + makedirs("temp", exist_ok=True) + tar = tarfile.open("temp/cirros_vnf.tar.gz", "w:gz") + tar.add("cirros_vnf") + tar.close() + r = test_rest.test("VNFD5", "Onboard VNFD step 3 replace with ZIP", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), + r_header_zip, "@b./temp/cirros_vnf.tar.gz", 204, None, 0) + + # vnfd SHOW OSM format + r = test_rest.test("VNFD6", "Show VNFD OSM format", "GET", "/vnfpkgm/v1/vnf_packages_content/{}".format(vnfd_id), + headers_json, None, 200, r_header_json, "json") + + # vnfd SHOW zip + r = test_rest.test("VNFD7", "Show VNFD SOL005 zip", "GET", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), + headers_zip, None, 200, r_header_zip, "zip") + # vnfd SHOW descriptor + r = test_rest.test("VNFD8", "Show VNFD descriptor", "GET", "/vnfpkgm/v1/vnf_packages/{}/vnfd".format(vnfd_id), + headers_text, None, 200, r_header_text, "text") + # vnfd SHOW actifact + r = test_rest.test("VNFD9", "Show VNFD artifact", "GET", "/vnfpkgm/v1/vnf_packages/{}/artifacts/icons/cirros-64.png".format(vnfd_id), + headers_text, None, 200, r_header_octect, "text") + + # # vnfd DELETE + # r = test_rest.test("VNFD10", "Delete VNFD SOL005 text", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(vnfd_id), + # headers_yaml, None, 204, None, 0) # nsd CREATE r = test_rest.test("NSD1", "Onboard NSD step 1", "POST", "/nsd/v1/ns_descriptors", headers_json, None, @@ -299,6 +343,13 @@ if __name__ == "__main__": # print(location, nsd_id) # nsd UPLOAD test + r = test_rest.test("NSD2", "Onboard NSD with missing vnfd", "PUT", "/nsd/v1/ns_descriptors/{}/nsd_content?constituent-vnfd.0.vnfd-id-ref=NONEXISTING-VNFD".format(nsd_id), + r_header_text, "@./cirros_ns/cirros_nsd.yaml", 409, r_header_yaml, "yaml") + + # # VNF_CREATE + # r = test_rest.test("VNFD5", "Onboard VNFD step 3 replace with ZIP", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), + # r_header_zip, "@b./temp/cirros_vnf.tar.gz", 204, None, 0) + r = test_rest.test("NSD2", "Onboard NSD step 2 as TEXT", "PUT", "/nsd/v1/ns_descriptors/{}/nsd_content".format(nsd_id), r_header_text, "@./cirros_ns/cirros_nsd.yaml", 204, None, 0) @@ -333,55 +384,19 @@ if __name__ == "__main__": r = test_rest.test("NSD9", "Show NSD artifact", "GET", "/nsd/v1/ns_descriptors/{}/artifacts/icons/osm_2x.png".format(nsd_id), headers_text, None, 200, r_header_octect, "text") + # vnfd DELETE + r = test_rest.test("VNFD10", "Delete VNFD conflict", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(vnfd_id), + headers_yaml, None, 409, r_header_yaml, "yaml") + # nsd DELETE r = test_rest.test("NSD10", "Delete NSD SOL005 text", "DELETE", "/nsd/v1/ns_descriptors/{}".format(nsd_id), headers_yaml, None, 204, None, 0) - # vnfd CREATE - r = test_rest.test("VNFD1", "Onboard VNFD step 1", "POST", "/vnfpkgm/v1/vnf_packages", headers_json, None, - 201, {"Location": "/vnfpkgm/v1/vnf_packages/", "Content-Type": "application/json"}, "json") - location = r.headers["Location"] - vnfd_id = location[location.rfind("/")+1:] - # print(location, vnfd_id) - - # vnfd UPLOAD test - r = test_rest.test("VNFD2", "Onboard VNFD step 2 as TEXT", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), - r_header_text, "@./cirros_vnf/cirros_vnfd.yaml", 204, None, 0) - - # vnfd SHOW OSM format - r = test_rest.test("VNFD3", "Show VNFD OSM format", "GET", "/vnfpkgm/v1/vnf_packages_content/{}".format(vnfd_id), - headers_json, None, 200, r_header_json, "json") - - # vnfd SHOW text - r = test_rest.test("VNFD4", "Show VNFD SOL005 text", "GET", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), - headers_text, None, 200, r_header_text, "text") - - # vnfd UPLOAD ZIP - makedirs("temp", exist_ok=True) - tar = tarfile.open("temp/cirros_vnf.tar.gz", "w:gz") - tar.add("cirros_vnf") - tar.close() - r = test_rest.test("VNFD5", "Onboard VNFD step 3 replace with ZIP", "PUT", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), - r_header_zip, "@b./temp/cirros_vnf.tar.gz", 204, None, 0) - - # vnfd SHOW OSM format - r = test_rest.test("VNFD6", "Show VNFD OSM format", "GET", "/vnfpkgm/v1/vnf_packages_content/{}".format(vnfd_id), - headers_json, None, 200, r_header_json, "json") - - # vnfd SHOW zip - r = test_rest.test("VNFD7", "Show VNFD SOL005 zip", "GET", "/vnfpkgm/v1/vnf_packages/{}/package_content".format(vnfd_id), - headers_zip, None, 200, r_header_zip, "zip") - # vnfd SHOW descriptor - r = test_rest.test("VNFD8", "Show VNFD descriptor", "GET", "/vnfpkgm/v1/vnf_packages/{}/vnfd".format(vnfd_id), - headers_text, None, 200, r_header_text, "text") - # vnfd SHOW actifact - r = test_rest.test("VNFD9", "Show VNFD artifact", "GET", "/vnfpkgm/v1/vnf_packages/{}/artifacts/icons/cirros-64.png".format(vnfd_id), - headers_text, None, 200, r_header_octect, "text") - # vnfd DELETE r = test_rest.test("VNFD10", "Delete VNFD SOL005 text", "DELETE", "/vnfpkgm/v1/vnf_packages/{}".format(vnfd_id), headers_yaml, None, 204, None, 0) + print("PASS") except Exception as e: