From 6541778af571666e11cfaea4953749bef94ca212 Mon Sep 17 00:00:00 2001 From: "Bonkur, Venkat" Date: Sat, 23 May 2020 17:33:33 -0400 Subject: Capture ids of vfModule and volumegroups Capture ids of vfModule and volumegroups Issue-ID: SO-2951 Signed-off-by: Benjamin, Max (mb388a) Change-Id: Ibe8af16c899a66ef007f74eb2c4e42afb0c2b42f --- .../infra/rest/AAIDataRetrieval.java | 54 ++++++++++++++++++++++ .../infra/rest/validators/VnfDeleteValidator.java | 12 ++++- 2 files changed, 64 insertions(+), 2 deletions(-) (limited to 'mso-api-handlers/mso-api-handler-infra/src/main') diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java index a87e94a6e0..fc0048bacf 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrieval.java @@ -1,7 +1,10 @@ package org.onap.so.apihandlerinfra.infra.rest; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import org.onap.aai.domain.yang.GenericVnf; import org.onap.aai.domain.yang.L3Network; import org.onap.aai.domain.yang.LInterface; @@ -9,13 +12,16 @@ import org.onap.aai.domain.yang.Service; import org.onap.aai.domain.yang.ServiceInstance; import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.VfModule; +import org.onap.aai.domain.yang.VfModules; import org.onap.aai.domain.yang.VolumeGroup; +import org.onap.aai.domain.yang.VolumeGroups; import org.onap.so.apihandlerinfra.infra.rest.exception.AAIEntityNotFound; import org.onap.aaiclient.client.aai.AAIDSLQueryClient; import org.onap.aaiclient.client.aai.AAIObjectPlurals; import org.onap.aaiclient.client.aai.AAIObjectType; import org.onap.aaiclient.client.aai.AAIResourcesClient; import org.onap.aaiclient.client.aai.entities.AAIResultWrapper; +import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; import org.onap.aaiclient.client.graphinventory.entities.DSLQuery; import org.onap.aaiclient.client.graphinventory.entities.DSLQueryBuilder; @@ -164,4 +170,52 @@ public class AAIDataRetrieval { return aaiResourcesClient; } + public boolean isVnfRelatedToVFModule(String vnfId) { + return !getVfModulesOfVnf(vnfId).isEmpty(); + } + + public List getVfModulesOfVnf(String vnfId) { + List vfModuleList = new ArrayList(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, vnfId); + Optional vfModules = getAaiResourcesClient().get(VfModules.class, uri); + if (!vfModules.isPresent() || vfModules.get().getVfModule().isEmpty()) { + logger.debug("No VfModules attached to Vnf in AAI : {}", vnfId); + } else { + vfModuleList = vfModules.get().getVfModule(); + } + return vfModuleList; + } + + public Optional getVfModuleIdsByVnfId(String vnfId) { + List vfModulesList = getVfModulesOfVnf(vnfId); + if (!vfModulesList.isEmpty()) { + return Optional.of(vfModulesList.stream().map(item -> item.getVfModuleId()).collect(Collectors.toList()) + .stream().sorted().collect(Collectors.joining(","))); + } else { + return Optional.empty(); + } + } + + public List getVolumeGroupsOfVnf(String vnfId) { + List volumeGroupList = new ArrayList(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId) + .relatedTo(AAIObjectPlurals.VOLUME_GROUP); + Optional volumeGroups = getAaiResourcesClient().get(VolumeGroups.class, uri); + if (!volumeGroups.isPresent() || volumeGroups.get().getVolumeGroup().isEmpty()) { + logger.debug("No VolumeGroups attached to Vnf in AAI : {}", vnfId); + } else { + volumeGroupList = volumeGroups.get().getVolumeGroup(); + } + return volumeGroupList; + } + + public Optional getVolumeGroupIdsByVnfId(String vnfId) { + List volumeGroupList = getVolumeGroupsOfVnf(vnfId); + if (!volumeGroupList.isEmpty()) { + return Optional.of(volumeGroupList.stream().map(item -> item.getVolumeGroupId()) + .collect(Collectors.toList()).stream().sorted().collect(Collectors.joining(","))); + } else { + return Optional.empty(); + } + } } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java index 37e3f0cb2e..a48fca8d0f 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/validators/VnfDeleteValidator.java @@ -25,8 +25,16 @@ public class VnfDeleteValidator implements RequestValidator { @Override public Optional validate(Map instanceIdMap, ServiceInstancesRequest request, Map queryParams) { - if (aaiDataRetrieval.isVnfRelatedToVolumes(instanceIdMap.get("vnfInstanceId"))) { - return Optional.of("Cannot delete vnf it is still related to existing volume groups"); + final Optional volumeGroupIds = + aaiDataRetrieval.getVolumeGroupIdsByVnfId(instanceIdMap.get("vnfInstanceId")); + final Optional vfModuleIds = aaiDataRetrieval.getVfModuleIdsByVnfId(instanceIdMap.get("vnfInstanceId")); + + if (volumeGroupIds.isPresent()) { + return Optional.of(String.format("Cannot delete vnf it is still related to existing volume group Ids - %s", + volumeGroupIds.get())); + } else if (vfModuleIds.isPresent()) { + return Optional.of(String.format("Cannot delete vnf it is still related to existing vfModule Ids - %s", + vfModuleIds.get())); } else { return Optional.empty(); } -- cgit 1.2.3-korg