diff options
author | stark, steven <steven.stark@att.com> | 2020-09-17 13:04:35 -0700 |
---|---|---|
committer | stark, steven <steven.stark@att.com> | 2020-09-17 13:04:35 -0700 |
commit | 35375724d9657de99adb8e530e1be21305555a40 (patch) | |
tree | 28806a1a1317426546ffdb9755d6f632ce088605 /onap-client/onap_client/so | |
parent | 591c548e569423dad8d106d4d2cd26bc5ce24b19 (diff) |
[VVP] Updating onap-client
Removing dependency on distutils
moving etc/ to package resource
Adding support for volume creation and delete
Removing module level imports
Adding delete capability to spec-engine
Adding kwarg to pass Client instance to functions
Issue-ID: VVP-469
Signed-off-by: stark, steven <steven.stark@att.com>
Change-Id: Ie00065f54411c8ff40ea20c35fe919ce15f65e6a
Diffstat (limited to 'onap-client/onap_client/so')
-rw-r--r-- | onap-client/onap_client/so/catalog/service_instantiation.py | 126 | ||||
-rw-r--r-- | onap-client/onap_client/so/client.py | 4 | ||||
-rw-r--r-- | onap-client/onap_client/so/module_instance.py | 245 | ||||
-rw-r--r-- | onap-client/onap_client/so/service_instance.py | 89 | ||||
-rw-r--r-- | onap-client/onap_client/so/tests/test_module_instance.py | 11 | ||||
-rw-r--r-- | onap-client/onap_client/so/tests/test_vnf_instance.py | 2 | ||||
-rw-r--r-- | onap-client/onap_client/so/vnf_instance.py | 153 |
7 files changed, 421 insertions, 209 deletions
diff --git a/onap-client/onap_client/so/catalog/service_instantiation.py b/onap-client/onap_client/so/catalog/service_instantiation.py index d561317..3874aef 100644 --- a/onap-client/onap_client/so/catalog/service_instantiation.py +++ b/onap-client/onap_client/so/catalog/service_instantiation.py @@ -34,16 +34,11 @@ # limitations under the License. # # ============LICENSE_END============================================ - import uuid from functools import partial -from onap_client import config from onap_client.so.client import SOClient -PAYLOADS_DIR = config.PAYLOADS_DIR -application_id = config.APPLICATION_ID - class ServiceInstantiationClient(SOClient): @property @@ -61,7 +56,7 @@ class ServiceInstantiationClient(SOClient): endpoint=self.config.so.SO_ENDPOINT, service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, ), - "payload": "{}/so_service_instance.jinja".format(PAYLOADS_DIR), + "payload": "{}/so_service_instance.jinja".format(self.config.payload_directory), "payload-parameters": [ "service_instance_name", "requestor_id", @@ -84,7 +79,7 @@ class ServiceInstantiationClient(SOClient): "Accept": "application/json", "Content-Type": "application/json", # "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -97,7 +92,7 @@ class ServiceInstantiationClient(SOClient): service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, ), "uri-parameters": ["service_instance_id"], - "payload": "{}/so_delete_service.jinja".format(PAYLOADS_DIR), + "payload": "{}/so_delete_service.jinja".format(self.config.payload_directory), "payload-parameters": [ "service_invariant_id", "service_name", @@ -110,7 +105,7 @@ class ServiceInstantiationClient(SOClient): "Accept": "application/json", "Content-Type": "application/json", # "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -123,7 +118,7 @@ class ServiceInstantiationClient(SOClient): service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, ), "uri-parameters": ["service_instance_id"], - "payload": "{}/so_vnf_instance.jinja".format(PAYLOADS_DIR), + "payload": "{}/so_vnf_instance.jinja".format(self.config.payload_directory), "payload-parameters": [ "vnf_instance_name", "requestor_id", @@ -150,7 +145,7 @@ class ServiceInstantiationClient(SOClient): "Accept": "application/json", "Content-Type": "application/json", # "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -163,7 +158,7 @@ class ServiceInstantiationClient(SOClient): service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, ), "uri-parameters": ["service_instance_id", "vnf_instance_id"], - "payload": "{}/so_delete_vnf.jinja".format(PAYLOADS_DIR), + "payload": "{}/so_delete_vnf.jinja".format(self.config.payload_directory), "payload-parameters": [ "vnf_invariant_id", "vnf_name", @@ -178,8 +173,50 @@ class ServiceInstantiationClient(SOClient): "headers": { "Accept": "application/json", "Content-Type": "application/json", - # "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, + }, + "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), + }, + "CREATE_VOLUME_MODULE_INSTANCE": { + "verb": "POST", + "description": "Creates a VNF Volume Module Instance.", + "uri": partial( + "{endpoint}{service_path}/{service_instance_id}/vnfs/{vnf_instance_id}/volumeGroups".format, + endpoint=self.config.so.SO_ENDPOINT, + service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, + ), + "uri-parameters": ["service_instance_id", "vnf_instance_id"], + "payload": "{}/so_create_volume_module.jinja".format(self.config.payload_directory), + "payload-parameters": [ + "volume_module_instance_name", + "model_invariant_id", + "model_version_id", + "model_name", + "model_version", + "model_customization_id", + "model_name", + "api_type", + "tenant_id", + "cloud_owner", + "cloud_region", + "service_instance_id", + "service_model_name", + "service_model_invariant_id", + "service_model_version", + "service_model_version_id", + "vnf_instance_id", + "vnf_model_name", + "vnf_model_invariant_id", + "vnf_model_version", + "vnf_model_version_id", + "vnf_model_customization_id", + ], + "header-parameters": ["X-TransactionId"], + "success_code": 202, + "headers": { + "Accept": "application/json", + "Content-Type": "application/json", + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -192,7 +229,7 @@ class ServiceInstantiationClient(SOClient): service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, ), "uri-parameters": ["service_instance_id", "vnf_instance_id"], - "payload": "{}/so_create_module.jinja".format(PAYLOADS_DIR), + "payload": "{}/so_create_module.jinja".format(self.config.payload_directory), "payload-parameters": [ "module_instance_name", "model_invariant_id", @@ -222,8 +259,7 @@ class ServiceInstantiationClient(SOClient): "headers": { "Accept": "application/json", "Content-Type": "application/json", - # "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -236,7 +272,7 @@ class ServiceInstantiationClient(SOClient): service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, ), "uri-parameters": ["service_instance_id", "vnf_instance_id", "vf_module_id"], - "payload": "{}/so_delete_module.jinja".format(PAYLOADS_DIR), + "payload": "{}/so_delete_module.jinja".format(self.config.payload_directory), "payload-parameters": [ "module_invariant_id", "module_name", @@ -251,8 +287,35 @@ class ServiceInstantiationClient(SOClient): "headers": { "Accept": "application/json", "Content-Type": "application/json", - # "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, + }, + "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), + }, + "DELETE_VOLUME_MODULE_INSTANCE": { + "verb": "DELETE", + "description": "Deletes a VNF Volume Module Instance.", + "uri": partial( + "{endpoint}{service_path}/{service_instance_id}/vnfs/{vnf_instance_id}/volumeGroups/{volume_module_id}".format, + endpoint=self.config.so.SO_ENDPOINT, + service_path=self.config.so.SO_SERVICE_INSTANCE_PATH, + ), + "uri-parameters": ["service_instance_id", "vnf_instance_id", "volume_module_id"], + "payload": "{}/so_delete_volume_module.jinja".format(self.config.payload_directory), + "payload-parameters": [ + "module_invariant_id", + "module_name", + "module_version", + "cloud_region", + "cloud_owner", + "tenant_id", + "api_type", + ], + "header-parameters": ["X-TransactionId"], + "success_code": 202, + "headers": { + "Accept": "application/json", + "Content-Type": "application/json", + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -270,7 +333,7 @@ class ServiceInstantiationClient(SOClient): "Accept": "application/json", "Content-Type": "application/json", "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -287,7 +350,7 @@ class ServiceInstantiationClient(SOClient): "Accept": "application/json", "Content-Type": "application/json", "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, @@ -304,7 +367,24 @@ class ServiceInstantiationClient(SOClient): "Accept": "application/json", "Content-Type": "application/json", "X-TransactionId": str(uuid.uuid4()), - "X-FromAppId": application_id, + "X-FromAppId": self.config.application_id, + }, + "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), + }, + "GET_MODULE_CUSTOMIZATION": { + "verb": "GET", + "description": "Searches the SO catalog for module customization object.", + "uri": partial( + "{endpoint}vfModuleCustomization/search/findFirstByModelCustomizationUUIDOrderByCreatedDesc?MODEL_CUSTOMIZATION_UUID={module_customization_id}".format, + endpoint=self.config.so.SO_ENDPOINT, + ), + "uri-parameters": ["module_customization_id"], + "success_code": 200, + "headers": { + "Accept": "application/json", + "Content-Type": "application/json", + "X-TransactionId": str(uuid.uuid4()), + "X-FromAppId": self.config.application_id, }, "auth": (self.config.so.SO_USERNAME, self.config.so.SO_PASSWORD), }, diff --git a/onap-client/onap_client/so/client.py b/onap-client/onap_client/so/client.py index 7e79fe1..3fa48ec 100644 --- a/onap-client/onap_client/so/client.py +++ b/onap-client/onap_client/so/client.py @@ -34,12 +34,8 @@ # limitations under the License. # # ============LICENSE_END============================================ - from functools import partial from onap_client.client.clients import Client -from onap_client import config - -application_id = config.APPLICATION_ID class SOClient(Client): diff --git a/onap-client/onap_client/so/module_instance.py b/onap-client/onap_client/so/module_instance.py index 67da272..59aba64 100644 --- a/onap-client/onap_client/so/module_instance.py +++ b/onap-client/onap_client/so/module_instance.py @@ -34,11 +34,13 @@ # limitations under the License. # # ============LICENSE_END============================================ - import uuid from onap_client.resource import Resource from onap_client.client.clients import get_client as Client -from onap_client.exceptions import ServiceInstanceNotFound, VNFInstanceNotFound, ModuleInstanceNotFound +from onap_client.exceptions import ( + ModuleModelNameNotFound, + NoArtifactFoundInModel, +) from onap_client import so from onap_client import sdnc from onap_client.util import utility @@ -57,25 +59,21 @@ class ModuleInstance(Resource): "cloud_owner": {"type": str, "required": True}, "cloud_region": {"type": str, "required": True}, "api_type": {"type": str, "required": False, "default": "GR_API"}, + "volume_group_name": {"type": str, "required": False}, } def _create(self, instance_input): tenant_id = so.service_instance.get_tenant_id( instance_input.get("cloud_region"), instance_input.get("cloud_owner"), - instance_input.get("tenant_name") + instance_input.get("tenant_name"), + oc=self.oc ) instance_input["tenant_id"] = tenant_id - service_instance = so.vnf_instance.get_service_instance( - instance_input.get("service_instance_name") + service_instance = so.service_instance.get_service_instance( + instance_input.get("service_instance_name"), oc=self.oc ) - if not service_instance: - raise ServiceInstanceNotFound( - "No service instance found for {}".format( - instance_input.get("service_instance_name") - ) - ) service_instance_id = service_instance.get("service-instance-id") model_information = ( service_instance.get("service-data") @@ -87,15 +85,10 @@ class ModuleInstance(Resource): service_model_version = model_information["model-version"] service_model_name = model_information["model-name"] - vnf_instance = so.vnf_instance.get_vnf_instance( + vnf_instance = so.service_instance.get_vnf_instance( service_instance, instance_input.get("vnf_instance_name") ) - if not vnf_instance: - raise VNFInstanceNotFound( - "No vnf instance found for {}".format( - instance_input.get("vnf_instance_name") - ) - ) + vnf_model_information = vnf_instance.get("vnf-data").get("vnf-information") vnf_instance_id = vnf_model_information.get("vnf-id") vnf_model_name = vnf_model_information.get("onap-model-information").get( @@ -115,10 +108,10 @@ class ModuleInstance(Resource): ) vnf_model = so.vnf_instance.get_vnf_model_component( - service_model_name, vnf_model_name + service_model_name, vnf_model_name, oc=self.oc ) - module_model = so.vnf_instance.get_module_model( + module_model = get_module_model( vnf_model, instance_input.get("heat_template_name") ) model_invariant_id = module_model.get("invariantUUID") @@ -127,6 +120,8 @@ class ModuleInstance(Resource): model_name = module_model.get("groupName") model_version = module_model.get("version") + volume_group = module_uses_volume_group(module_model) and instance_input.get("volume_group_name") + instance_input["model_invariant_id"] = model_invariant_id instance_input["model_version_id"] = model_version_id instance_input["model_name"] = model_name @@ -144,11 +139,38 @@ class ModuleInstance(Resource): instance_input["vnf_model_version"] = vnf_model_version instance_input["vnf_model_customization_id"] = vnf_model_customization_id - return create_module_instance(instance_input) + return create_module_instance(instance_input, volume_group=volume_group, oc=self.oc) + + def _delete(self, instance_input): + if instance_input.get("volume_group_name"): + request = delete_volume_module_instance( + instance_input.get("service_instance_name"), + instance_input.get("vnf_instance_name"), + instance_input.get("volume_group_name"), + instance_input.get("api_type"), + oc=self.oc, + ) + request_id = request.get("requestReferences", {}).get( + "requestId" + ) + so.service_instance.poll_request(request_id, oc=self.oc) + + request = delete_module_instance( + instance_input.get("service_instance_name"), + instance_input.get("vnf_instance_name"), + instance_input.get("module_instance_name"), + instance_input.get("api_type"), + oc=self.oc, + ) + request_id = request.get("requestReferences", {}).get( + "requestId" + ) + so.service_instance.poll_request(request_id, oc=self.oc) -def create_module_instance(instance_input): - oc = Client() +def create_module_instance(instance_input, volume_group=False, oc=None): + if not oc: + oc = Client() preload = sdnc.preload.Preload( preload_path=instance_input.get("preload_path"), @@ -156,11 +178,24 @@ def create_module_instance(instance_input): service_instance_name=instance_input.get("service_instance_name"), module_instance_name=instance_input.get("module_instance_name"), heat_template_name=instance_input.get("heat_template_name"), - api_type=instance_input.get("api_type") + api_type=instance_input.get("api_type"), + oc=oc ) preload.create() headers = {"X-TransactionId": str(uuid.uuid4())} + + if volume_group: + volume_instance = oc.so.service_instantiation.create_volume_module_instance( + volume_module_instance_name=instance_input.get("volume_group_name"), + **instance_input, + **headers + ) + request_id = volume_instance.response_data.get("requestReferences", {}).get( + "requestId" + ) + so.service_instance.poll_request(request_id, oc=oc) + module_instance = oc.so.service_instantiation.create_module_instance( **instance_input, **headers ) @@ -169,41 +204,143 @@ def create_module_instance(instance_input): "requestId" ) - instance_input["request_info"] = so.service_instance.poll_request(request_id) + instance_input["request_info"] = so.service_instance.poll_request(request_id, oc=oc) return instance_input @utility -def delete_module_instance(service_instance_name, vnf_instance_name, module_instance_name, api_type="GR_API"): +def delete_module_instance(service_instance_name, vnf_instance_name, module_instance_name, api_type="GR_API", oc=None): + """Delete a Module Instance from SO""" + if not oc: + oc = Client() + + si = so.service_instance.get_service_instance(service_instance_name, oc=oc) + vnfi = so.service_instance.get_vnf_instance(si, vnf_instance_name) + modulei = so.service_instance.get_module_instance(vnfi, module_instance_name) + + si_id = si.get("service-instance-id") + vnfi_id = vnfi.get("vnf-id") + module_id = modulei.get("vf-module-id") + module_invariant_id = modulei.get("vf-module-data").get("vf-module-topology").get("onap-model-information").get("model-invariant-uuid") + module_version = modulei.get("vf-module-data").get("vf-module-topology").get("onap-model-information").get("model-version") + module_name = modulei.get("vf-module-data").get("vf-module-topology").get("onap-model-information").get("model-name") + tenant_id = modulei.get("vf-module-data").get("vf-module-request-input").get("tenant") + cloud_owner = modulei.get("vf-module-data").get("vf-module-request-input").get("cloud-owner") + cloud_region = modulei.get("vf-module-data").get("vf-module-request-input").get("aic-cloud-region") + + return oc.so.service_instantiation.delete_module_instance( + module_invariant_id=module_invariant_id, + module_name=module_name, + module_version=module_version, + cloud_region=cloud_region, + cloud_owner=cloud_owner, + tenant_id=tenant_id, + vnf_instance_id=vnfi_id, + service_instance_id=si_id, + vf_module_id=module_id, + api_type=api_type + ).response_data + + +@utility +def delete_volume_module_instance(service_instance_name, vnf_instance_name, volume_module_name, api_type="GR_API", oc=None): """Delete a Module Instance from SO""" - oc = Client() + if not oc: + oc = Client() + + si = so.service_instance.get_service_instance(service_instance_name, oc=oc) + vnfi = so.service_instance.get_vnf_instance(si, vnf_instance_name) + + vnf_model_information = vnfi.get("vnf-data").get("vnf-information") + service_model_information = ( + si.get("service-data") + .get("service-information") + .get("onap-model-information") + ) + service_model_name = service_model_information["model-name"] + vnf_model_name = vnf_model_information.get("onap-model-information").get( + "model-name" + ) - si = so.service_instance.get_service_instance(service_instance_name) si_id = si.get("service-instance-id") - for vnfi in si.get("service-data", {}).get("vnfs", {}).get("vnf", []): - vnfi_id = vnfi.get("vnf-id") - if vnfi.get("vnf-data", {}).get("vnf-request-input", {}).get("vnf-name") == vnf_instance_name: - for modulei in vnfi.get("vnf-data").get("vf-modules", {}).get("vf-module", []): - if modulei.get("vf-module-data", {}).get("vf-module-request-input", {}).get("vf-module-name") == module_instance_name: - module_id = modulei.get("vf-module-id") - module_invariant_id = modulei.get("vf-module-data").get("vf-module-topology").get("onap-model-information").get("model-invariant-uuid") - module_version = modulei.get("vf-module-data").get("vf-module-topology").get("onap-model-information").get("model-version") - module_name = modulei.get("vf-module-data").get("vf-module-topology").get("onap-model-information").get("model-name") - tenant_id = modulei.get("vf-module-data").get("vf-module-request-input").get("tenant") - cloud_owner = modulei.get("vf-module-data").get("vf-module-request-input").get("cloud-owner") - cloud_region = modulei.get("vf-module-data").get("vf-module-request-input").get("aic-cloud-region") - return oc.so.service_instantiation.delete_module_instance( - module_invariant_id=module_invariant_id, - module_name=module_name, - module_version=module_version, - cloud_region=cloud_region, - cloud_owner=cloud_owner, - tenant_id=tenant_id, - vnf_instance_id=vnfi_id, - service_instance_id=si_id, - vf_module_id=module_id, - api_type=api_type - ).response_data - - raise ModuleInstanceNotFound("Module Instance was not found: {} {} {}".format(service_instance_name, vnf_instance_name, module_instance_name)) + vnfi_id = vnfi.get("vnf-id") + + tenant_id = vnfi.get("vnf-data").get("vnf-request-input").get("tenant") + cloud_owner = vnfi.get("vnf-data").get("vnf-request-input").get("cloud-owner") + cloud_region = vnfi.get("vnf-data").get("vnf-request-input").get("aic-cloud-region") + + volume_group_instance = get_volume_group_instance(volume_module_name, cloud_owner, cloud_region, oc=oc) + volume_customization_id = volume_group_instance.get("model-customization-id") + vnf_component = so.vnf_instance.get_vnf_model_component( + service_model_name, vnf_model_name, oc=oc + ) + volume_group_component = get_volume_group_from_vnf_model(vnf_component, volume_customization_id) + + return oc.so.service_instantiation.delete_volume_module_instance( + volume_module_id=volume_group_instance.get("volume-group-id"), + module_invariant_id=volume_group_component.get("invariantUUID"), + module_name=volume_group_component.get("groupName"), + module_version=volume_group_component.get("version"), + cloud_region=cloud_region, + cloud_owner=cloud_owner, + tenant_id=tenant_id, + vnf_instance_id=vnfi_id, + service_instance_id=si_id, + api_type=api_type + ).response_data + + +def module_uses_volume_group(module_model): + for prop in module_model.get("properties"): + if prop.get("name", "") == "volume_group" and prop.get("value") == "true": + return True + return False + + +def get_volume_group_instance(volume_group_name, cloud_owner, cloud_region, oc=None): + if not oc: + oc = Client() + + volume_group_list = oc.aai.cloud_infrastructure.get_volume_groups( + cloud_owner=cloud_owner, + cloud_region=cloud_region + ).response_data + + for volume_group in volume_group_list.get("volume-group", []): + if volume_group.get("volume-group-name") == volume_group_name: + return volume_group + + return {} + + +def get_volume_group_from_vnf_model(vnf_component, volume_group_customization_id): + for group_instance in vnf_component.get("groupInstances", []): + if group_instance.get("customizationUUID") == volume_group_customization_id: + return group_instance + return {} + + +def get_module_model(vnf_model, heat_template_name): + artifact_uuid = None + deployment_artifacts = vnf_model.get("deploymentArtifacts", {}) + for artifact_name, artifact_data in deployment_artifacts.items(): + if artifact_data.get("artifactName") == heat_template_name: + artifact_uuid = artifact_data.get("artifactUUID") + + if not artifact_uuid: + raise NoArtifactFoundInModel( + "Heat Template {} was not found in service model".format(heat_template_name) + ) + + group_instances = vnf_model.get("groupInstances", []) + for instance in group_instances: + if artifact_uuid in instance.get("artifactsUuid", []): + # return instance.get("groupName") + return instance + + raise ModuleModelNameNotFound( + "Module Model Name for {} was not found in service model".format( + heat_template_name + ) + ) diff --git a/onap-client/onap_client/so/service_instance.py b/onap-client/onap_client/so/service_instance.py index 3675a4e..5441d84 100644 --- a/onap-client/onap_client/so/service_instance.py +++ b/onap-client/onap_client/so/service_instance.py @@ -46,6 +46,8 @@ from onap_client.exceptions import ( SORequestTimeout, TenantNotFound, ServiceInstanceNotFound, + VNFInstanceNotFound, + ModuleInstanceNotFound, ) from onap_client import sdc from onap_client.util import utility @@ -77,14 +79,16 @@ class ServiceInstance(Resource): tenant_id = get_tenant_id( instance_input.get("cloud_region"), instance_input.get("cloud_owner"), - instance_input.get("tenant_name") + instance_input.get("tenant_name"), + oc=self.oc ) instance_input["tenant_id"] = tenant_id instance_input["customer_id"] = instance_input.get("customer_name") service_model = self.oc.sdc.service.get_sdc_service( catalog_service_id=sdc.service.get_service_id( - instance_input.get("model_name") + instance_input.get("model_name"), + oc=self.oc ) ).response_data @@ -97,25 +101,23 @@ class ServiceInstance(Resource): instance_input["owning_entity_id"] = entity.get("id") break - return create_service_instance(instance_input) + return create_service_instance(instance_input, oc=self.oc) - -@utility -def get_service_instance(instance_name): - """Queries SDNC for a list of all service instances and returns - The service instance that matches <instance name>""" - oc = Client() - - service_instances = oc.sdnc.config.get_service_instances().response_data - for si in service_instances.get("services", {}).get("service", []): - if si.get("service-data", {}).get("service-request-input", {}).get("service-instance-name") == instance_name: - return si - - raise ServiceInstanceNotFound("Service Instance {} was not found".format(instance_name)) + def _delete(self, instance_input): + request = delete_service_instance( + instance_input.get("service_instance_name"), + instance_input.get("api_type"), + oc=self.oc + ) + request_id = request.get("requestReferences", {}).get( + "requestId" + ) + poll_request(request_id, oc=self.oc) -def get_tenant_id(cloud_region, cloud_owner, tenant_name): - oc = Client() +def get_tenant_id(cloud_region, cloud_owner, tenant_name, oc=None): + if not oc: + oc = Client() tenants = oc.aai.cloud_infrastructure.get_cloud_region_tenants( cloud_owner=cloud_owner, @@ -129,8 +131,9 @@ def get_tenant_id(cloud_region, cloud_owner, tenant_name): raise TenantNotFound("Tenant {} was not found in AAI".format(tenant_name)) -def create_service_instance(instance_input): - oc = Client() +def create_service_instance(instance_input, oc=None): + if not oc: + oc = Client() headers = {"X-TransactionId": str(uuid.uuid4())} service_instance = oc.so.service_instantiation.create_service_instance( @@ -141,15 +144,16 @@ def create_service_instance(instance_input): "requestId" ) - instance_input["request_info"] = poll_request(request_id) + instance_input["request_info"] = poll_request(request_id, oc=oc) return instance_input @utility -def poll_request(request_id): +def poll_request(request_id, oc=None): """Poll an SO request until completion""" - oc = Client() + if not oc: + oc = Client() poll_interval = oc.config.so.POLL_INTERVAL or 30 request = None @@ -181,11 +185,12 @@ def poll_request(request_id): @utility -def delete_service_instance(service_instance_name, api_type="GR_API"): +def delete_service_instance(service_instance_name, api_type="GR_API", oc=None): """Delete a Service Instance from SO""" - oc = Client() + if not oc: + oc = Client() - si = get_service_instance(service_instance_name) + si = get_service_instance(service_instance_name, oc=oc) si_id = si.get("service-instance-id") invariant_id = si.get("service-data").get("service-information").get("onap-model-information").get("model-invariant-uuid") version = si.get("service-data").get("service-information").get("onap-model-information").get("model-version") @@ -197,3 +202,35 @@ def delete_service_instance(service_instance_name, api_type="GR_API"): service_instance_id=si_id, api_type=api_type, ).response_data + + +@utility +def get_service_instance(instance_name, oc=None): + """Queries SDNC for a list of all service instances and returns + The service instance that matches <instance name>""" + if not oc: + oc = Client() + + service_instances = oc.sdnc.configuration.get_service_instances().response_data + for si in service_instances.get("services", {}).get("service", []): + if si.get("service-data", {}).get("service-request-input", {}).get("service-instance-name") == instance_name: + return si + + raise ServiceInstanceNotFound("Service Instance {} was not found".format(instance_name)) + + +def get_vnf_instance(service_instance_dict, vnf_instance_name): + for vnfi in service_instance_dict.get("service-data", {}).get("vnfs", {}).get("vnf", []): + if vnfi.get("vnf-data", {}).get("vnf-request-input", {}).get("vnf-name") == vnf_instance_name: + return vnfi + + raise VNFInstanceNotFound("VNF Instance was not found: {}".format(vnf_instance_name)) + + +def get_module_instance(vnf_instance_dict, module_instance_name): + for modulei in vnf_instance_dict.get("vnf-data").get("vf-modules", {}).get("vf-module", []): + if modulei.get("vf-module-data", {}).get("vf-module-request-input", {}).get("vf-module-name") == module_instance_name: + return modulei + + raise ModuleInstanceNotFound("Module Instance was not found: {}".format(module_instance_name)) + diff --git a/onap-client/onap_client/so/tests/test_module_instance.py b/onap-client/onap_client/so/tests/test_module_instance.py index e102702..0e8c655 100644 --- a/onap-client/onap_client/so/tests/test_module_instance.py +++ b/onap-client/onap_client/so/tests/test_module_instance.py @@ -130,7 +130,8 @@ def test_module_instance(): "groupUUID": MODULE_MODEL_VERSION_ID, "customizationUUID": MODULE_MODEL_CUSTOMIZATION_ID, "groupName": MODULE_MODEL_NAME, - "version": MODULE_MODEL_VERSION + "version": MODULE_MODEL_VERSION, + "properties": [] } ] } @@ -148,7 +149,7 @@ def test_module_instance(): oc.sdnc.operations.catalog_items["GR_API_PRELOAD"], ) mockup_catalog_item( - oc.sdnc.config.catalog_items["GET_SERVICE_INSTANCES"], + oc.sdnc.configuration.catalog_items["GET_SERVICE_INSTANCES"], override_return_data={ "services": { "service": [ @@ -179,6 +180,12 @@ def test_module_instance(): "model-customization-uuid": VNF_CUSTOMIZATION_UUID, "model-version": "1.0" } + }, + "vnf-request-input": { + "vnf-name": VNF_INSTANCE_NAME, + "tenant": TENANT_ID, + "cloud-owner": CLOUD_OWNER, + "aic-cloud-region": CLOUD_REGION } } } diff --git a/onap-client/onap_client/so/tests/test_vnf_instance.py b/onap-client/onap_client/so/tests/test_vnf_instance.py index cecdb39..88334b0 100644 --- a/onap-client/onap_client/so/tests/test_vnf_instance.py +++ b/onap-client/onap_client/so/tests/test_vnf_instance.py @@ -114,7 +114,7 @@ def test_vnf_instance(): override_uri_params={"catalog_resource_id": VNF_ACTUAL_UUID}, ) mockup_catalog_item( - oc.sdnc.config.catalog_items["GET_SERVICE_INSTANCES"], + oc.sdnc.configuration.catalog_items["GET_SERVICE_INSTANCES"], override_return_data={ "services": { "service": [ diff --git a/onap-client/onap_client/so/vnf_instance.py b/onap-client/onap_client/so/vnf_instance.py index 0dbd87f..32fcda7 100644 --- a/onap-client/onap_client/so/vnf_instance.py +++ b/onap-client/onap_client/so/vnf_instance.py @@ -40,13 +40,7 @@ import uuid from onap_client.lib import generate_dummy_string from onap_client.resource import Resource from onap_client.client.clients import get_client as Client -from onap_client.exceptions import ( - ServiceInstanceNotFound, - VNFComponentNotFound, - ModuleModelNameNotFound, - NoArtifactFoundInModel, - VNFInstanceNotFound, -) +from onap_client.exceptions import VNFComponentNotFound from onap_client import sdc from onap_client import so from onap_client.util import utility @@ -75,19 +69,15 @@ class VNFInstance(Resource): tenant_id = so.service_instance.get_tenant_id( instance_input.get("cloud_region"), instance_input.get("cloud_owner"), - instance_input.get("tenant_name") + instance_input.get("tenant_name"), + oc=self.oc ) instance_input["tenant_id"] = tenant_id - service_instance = get_service_instance( - instance_input.get("service_instance_name") + service_instance = so.service_instance.get_service_instance( + instance_input.get("service_instance_name"), + oc=self.oc ) - if not service_instance: - raise ServiceInstanceNotFound( - "No service instance found for {}".format( - instance_input.get("service_instance_name") - ) - ) service_instance_id = service_instance.get("service-instance-id") model_information = ( service_instance.get("service-data") @@ -100,7 +90,7 @@ class VNFInstance(Resource): service_model_name = model_information["model-name"] vnf_component = get_vnf_model_component( - service_model_name, instance_input.get("model_name") + service_model_name, instance_input.get("model_name"), oc=self.oc ) if not vnf_component: raise VNFComponentNotFound( @@ -125,13 +115,27 @@ class VNFInstance(Resource): instance_input["service_model_version_id"] = service_model_id instance_input["service_instance_id"] = service_instance_id - return create_vnf_instance(instance_input) + return create_vnf_instance(instance_input, oc=self.oc) + + def _delete(self, instance_input): + request = delete_vnf_instance( + instance_input.get("service_instance_name"), + instance_input.get("vnf_instance_name"), + instance_input.get("api_type"), + oc=self.oc + ) + request_id = request.get("requestReferences", {}).get( + "requestId" + ) + so.service_instance.poll_request(request_id, oc=self.oc) + +def get_vnf_model_component(service_model_name, vnf_model_name, oc=None): + if not oc: + oc = Client() -def get_vnf_model_component(service_model_name, vnf_model_name): - oc = Client() service_model = oc.sdc.service.get_sdc_service( - catalog_service_id=sdc.service.get_service_id(service_model_name) + catalog_service_id=sdc.service.get_service_id(service_model_name, oc=oc) ).response_data for component in service_model.get("componentInstances", []): @@ -140,59 +144,9 @@ def get_vnf_model_component(service_model_name, vnf_model_name): return None -def get_service_instance(service_instance_name): - oc = Client() - service_instances = oc.sdnc.config.get_service_instances().response_data - for si in service_instances.get("services", {}).get("service", []): - si_name = ( - si.get("service-data", {}) - .get("service-request-input", {}) - .get("service-instance-name") - ) - if si_name == service_instance_name: - return si - return None - - -def get_module_model(vnf_model, heat_template_name): - artifact_uuid = None - deployment_artifacts = vnf_model.get("deploymentArtifacts", {}) - for artifact_name, artifact_data in deployment_artifacts.items(): - if artifact_data.get("artifactName") == heat_template_name: - artifact_uuid = artifact_data.get("artifactUUID") - - if not artifact_uuid: - raise NoArtifactFoundInModel( - "Heat Template {} was not found in service model".format(heat_template_name) - ) - - group_instances = vnf_model.get("groupInstances", []) - for instance in group_instances: - if artifact_uuid in instance.get("artifactsUuid", []): - # return instance.get("groupName") - return instance - - raise ModuleModelNameNotFound( - "Module Model Name for {} was not found in service model".format( - heat_template_name - ) - ) - - -def get_vnf_instance(service_instance, vnf_instance_name): - for vnf_instance in ( - service_instance.get("service-data", {}).get("vnfs", {}).get("vnf", []) - ): - vi_name = ( - vnf_instance.get("vnf-data", {}).get("vnf-information", {}).get("vnf-name") - ) - if vi_name == vnf_instance_name: - return vnf_instance - return None - - -def create_vnf_instance(instance_input): - oc = Client() +def create_vnf_instance(instance_input, oc=None): + if not oc: + oc = Client() headers = {"X-TransactionId": str(uuid.uuid4())} vnf_instance = oc.so.service_instantiation.create_vnf_instance( @@ -203,35 +157,36 @@ def create_vnf_instance(instance_input): "requestId" ) - instance_input["request_info"] = so.service_instance.poll_request(request_id) + instance_input["request_info"] = so.service_instance.poll_request(request_id, oc=oc) return instance_input @utility -def delete_vnf_instance(service_instance_name, vnf_instance_name, api_type="GR_API"): +def delete_vnf_instance(service_instance_name, vnf_instance_name, api_type="GR_API", oc=None): """Delete a VNF Instance from SO""" - oc = Client() - si = so.service_instance.get_service_instance(service_instance_name) + if not oc: + oc = Client() + + si = so.service_instance.get_service_instance(service_instance_name, oc=oc) + vnfi = so.service_instance.get_vnf_instance(si, vnf_instance_name) + si_id = si.get("service-instance-id") - for vnfi in si.get("service-data", {}).get("vnfs", {}).get("vnf", []): - vnfi_id = vnfi.get("vnf-id") - if vnfi.get("vnf-data", {}).get("vnf-request-input", {}).get("vnf-name") == vnf_instance_name: - invariant_id = vnfi.get("vnf-data").get("vnf-information").get("onap-model-information").get("model-invariant-uuid") - vnf_version = vnfi.get("vnf-data").get("vnf-information").get("onap-model-information").get("model-version") - tenant_id = vnfi.get("vnf-data").get("vnf-request-input").get("tenant") - cloud_owner = vnfi.get("vnf-data").get("vnf-request-input").get("cloud-owner") - cloud_region = vnfi.get("vnf-data").get("vnf-request-input").get("aic-cloud-region") - return oc.so.service_instantiation.delete_vnf_instance( - vnf_invariant_id=invariant_id, - vnf_version=vnf_version, - vnf_name=vnf_instance_name, - cloud_region=cloud_region, - cloud_owner=cloud_owner, - tenant_id=tenant_id, - vnf_instance_id=vnfi_id, - service_instance_id=si_id, - api_type=api_type, - ).response_data - - raise VNFInstanceNotFound("VNF Instance was not found: {} {}".format(service_instance_name, vnf_instance_name)) + vnfi_id = vnfi.get("vnf-id") + invariant_id = vnfi.get("vnf-data").get("vnf-information").get("onap-model-information").get("model-invariant-uuid") + vnf_version = vnfi.get("vnf-data").get("vnf-information").get("onap-model-information").get("model-version") + tenant_id = vnfi.get("vnf-data").get("vnf-request-input").get("tenant") + cloud_owner = vnfi.get("vnf-data").get("vnf-request-input").get("cloud-owner") + cloud_region = vnfi.get("vnf-data").get("vnf-request-input").get("aic-cloud-region") + + return oc.so.service_instantiation.delete_vnf_instance( + vnf_invariant_id=invariant_id, + vnf_version=vnf_version, + vnf_name=vnf_instance_name, + cloud_region=cloud_region, + cloud_owner=cloud_owner, + tenant_id=tenant_id, + vnf_instance_id=vnfi_id, + service_instance_id=si_id, + api_type=api_type, + ).response_data |