diff options
4 files changed, 206 insertions, 6 deletions
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<VfModule> getVfModulesOfVnf(String vnfId) { + List<VfModule> vfModuleList = new ArrayList<VfModule>(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, vnfId); + Optional<VfModules> 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<String> getVfModuleIdsByVnfId(String vnfId) { + List<VfModule> 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<VolumeGroup> getVolumeGroupsOfVnf(String vnfId) { + List<VolumeGroup> volumeGroupList = new ArrayList<VolumeGroup>(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId) + .relatedTo(AAIObjectPlurals.VOLUME_GROUP); + Optional<VolumeGroups> 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<String> getVolumeGroupIdsByVnfId(String vnfId) { + List<VolumeGroup> 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<String> validate(Map<String, String> instanceIdMap, ServiceInstancesRequest request, Map<String, String> queryParams) { - if (aaiDataRetrieval.isVnfRelatedToVolumes(instanceIdMap.get("vnfInstanceId"))) { - return Optional.of("Cannot delete vnf it is still related to existing volume groups"); + final Optional<String> volumeGroupIds = + aaiDataRetrieval.getVolumeGroupIdsByVnfId(instanceIdMap.get("vnfInstanceId")); + final Optional<String> 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(); } diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrievalTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrievalTest.java new file mode 100644 index 0000000000..9d5d80592d --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/AAIDataRetrievalTest.java @@ -0,0 +1,122 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.apihandlerinfra.infra.rest; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +import java.util.List; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aai.domain.yang.GenericVnf; +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.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.uri.AAIPluralResourceUri; +import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; + +@RunWith(MockitoJUnitRunner.class) +public class AAIDataRetrievalTest { + @Mock + private AAIResourcesClient aaiResourcesClient; + + @InjectMocks + private AAIDataRetrieval aaiDataRetrieval = new AAIDataRetrieval(); + + @Test + public void getVfModulesOfVnfTest() { + VfModules vfModules = getVfModules(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, "vnfId"); + doReturn(Optional.of(vfModules)).when(aaiResourcesClient).get(VfModules.class, uri); + List<VfModule> vfModulesList = aaiDataRetrieval.getVfModulesOfVnf("vnfId"); + assertEquals("vfm1", vfModulesList.get(0).getVfModuleId()); + + Optional<String> vfModuleIds = aaiDataRetrieval.getVfModuleIdsByVnfId("vnfId"); + assertEquals(Optional.of("vfm1,vfm2"), vfModuleIds); + } + + @Test + public void getVfModulesOfVnfWhenNoneTest() { + VfModules vfModules = new VfModules(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, "vnfId"); + doReturn(Optional.of(vfModules)).when(aaiResourcesClient).get(VfModules.class, uri); + List<VfModule> vfModulesList = aaiDataRetrieval.getVfModulesOfVnf("vnfId"); + assertEquals(true, vfModulesList.isEmpty()); + + Optional<String> vfModuleIds = aaiDataRetrieval.getVfModuleIdsByVnfId("vnfId"); + assertEquals(false, vfModuleIds.isPresent()); + } + + @Test + public void getVolumeGroupsOfVnfTest() throws Exception { + VolumeGroups volumeGroups = getVolumeGroups(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId") + .relatedTo(AAIObjectPlurals.VOLUME_GROUP); + doReturn(Optional.of(volumeGroups)).when(aaiResourcesClient).get(VolumeGroups.class, uri); + List<VolumeGroup> volumeGroupList = aaiDataRetrieval.getVolumeGroupsOfVnf("vnfId"); + assertEquals("vg1", volumeGroupList.get(0).getVolumeGroupId()); + Optional<String> volumeGroupIds = aaiDataRetrieval.getVolumeGroupIdsByVnfId("vnfId"); + assertEquals(Optional.of("vg1,vg2"), volumeGroupIds); + } + + @Test + public void getVolumeGroupsOfVnfWhenNoneTest() throws Exception { + VolumeGroups volumeGroups = new VolumeGroups(); + AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "vnfId") + .relatedTo(AAIObjectPlurals.VOLUME_GROUP); + doReturn(Optional.of(volumeGroups)).when(aaiResourcesClient).get(VolumeGroups.class, uri); + List<VolumeGroup> volumeGroupList = aaiDataRetrieval.getVolumeGroupsOfVnf("vnfId"); + assertEquals(true, volumeGroupList.isEmpty()); + Optional<String> volumeGroupIds = aaiDataRetrieval.getVolumeGroupIdsByVnfId("vnfId"); + assertEquals(false, volumeGroupIds.isPresent()); + } + + + private VfModules getVfModules() { + VfModule vfModule1 = new VfModule(); + VfModule vfModule2 = new VfModule(); + vfModule1.setVfModuleId("vfm1"); + vfModule2.setVfModuleId("vfm2"); + VfModules vfModules = new VfModules(); + vfModules.getVfModule().add(vfModule1); + vfModules.getVfModule().add(vfModule2); + return vfModules; + } + + private VolumeGroups getVolumeGroups() { + VolumeGroup volumeGroup1 = new VolumeGroup(); + volumeGroup1.setVolumeGroupId("vg1"); + VolumeGroup volumeGroup2 = new VolumeGroup(); + volumeGroup2.setVolumeGroupId("vg2"); + VolumeGroups volumeGroups = new VolumeGroups(); + volumeGroups.getVolumeGroup().add(volumeGroup1); + volumeGroups.getVolumeGroup().add(volumeGroup2); + return volumeGroups; + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java index d4f57d3146..65ce355ad1 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/validator/VnfDeleteValidatorTest.java @@ -2,6 +2,7 @@ package org.onap.so.apihandlerinfra.infra.rest.validator; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doReturn; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -49,19 +50,34 @@ public class VnfDeleteValidatorTest { } @Test - public void validateSuccessTest() { + public void validateFailureVfModuleVnfTest() { instanceIdMap.put("vnfInstanceId", "1"); - when(aaiDataRetrieval.isVnfRelatedToVolumes("1")).thenReturn(false); + doReturn(Optional.of("test")).when(aaiDataRetrieval).getVfModuleIdsByVnfId("1"); + Optional<String> result = vnfValidator.validate(instanceIdMap, null, null); + assertEquals(true, result.isPresent()); + } + + @Test + public void validateSuccessVfModuleVnfTest() { + instanceIdMap.put("vnfInstanceId", "1"); + doReturn(Optional.empty()).when(aaiDataRetrieval).getVfModuleIdsByVnfId("1"); Optional<String> result = vnfValidator.validate(instanceIdMap, null, null); assertEquals(false, result.isPresent()); } @Test - public void validateFailureVnfTest() { + public void validateFailureVolumeGroupVnfTest() { instanceIdMap.put("vnfInstanceId", "1"); - when(aaiDataRetrieval.isVnfRelatedToVolumes("1")).thenReturn(true); + doReturn(Optional.of("test")).when(aaiDataRetrieval).getVolumeGroupIdsByVnfId("1"); Optional<String> result = vnfValidator.validate(instanceIdMap, null, null); assertEquals(true, result.isPresent()); } + @Test + public void validateSuccessVolumeGroupVnfTest() { + instanceIdMap.put("vnfInstanceId", "1"); + doReturn(Optional.empty()).when(aaiDataRetrieval).getVolumeGroupIdsByVnfId("1"); + Optional<String> result = vnfValidator.validate(instanceIdMap, null, null); + assertEquals(false, result.isPresent()); + } } |