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)
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
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
: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.
"""
# 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)
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
# 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)
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)
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"):
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
: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)
-0.1.4
-May 2018
+0.1.5
+2018-05-14
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):
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)
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:
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)
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")
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"):
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)
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,
# 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)
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: