diff options
author | rope252 <gareth.roper@est.tech> | 2019-04-09 18:20:13 +0000 |
---|---|---|
committer | rope252 <gareth.roper@est.tech> | 2019-04-09 18:20:13 +0000 |
commit | 8105de4c4b02cc1706c7065305e9f0a9a5052591 (patch) | |
tree | 6dc410d9470062d10309be4801559ecafe9d908d /adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter | |
parent | c6d3ec1230b2d68548587b28d4a9b02e9e96101f (diff) |
SO-ETSI Alignment. Handle Grant Request
Change-Id: Ic54892fd52f4125f0d1515db3224e99b5c40f47f
Issue-ID: SO-1625
Signed-off-by: rope252 <gareth.roper@est.tech>
Diffstat (limited to 'adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter')
7 files changed, 440 insertions, 10 deletions
diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiHelper.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiHelper.java index ee04819ca4..b9ffd8bac5 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiHelper.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiHelper.java @@ -31,13 +31,14 @@ import org.onap.aai.domain.yang.GenericVnf; import org.onap.aai.domain.yang.Relationship; import org.onap.aai.domain.yang.RelationshipData; import org.onap.aai.domain.yang.RelationshipList; -import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.Vserver; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs; +import org.onap.so.adapters.vnfmadapter.rest.exceptions.TenantNotFoundException; import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException; import org.onap.so.client.aai.AAIObjectType; import org.onap.so.client.aai.AAIVersion; import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.vnfmadapter.v1.model.Tenant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -124,8 +125,11 @@ public class AaiHelper { final String cloudOwner = getRelationshipKey(relationship, "cloud-region.cloud-owner"); final String cloudRegion = getRelationshipKey(relationship, "cloud-region.cloud-region-id"); final String tenantId = getRelationshipKey(relationship, "tenant.tenant-id"); - return cloudOwner == null || cloudRegion == null || tenantId == null ? null - : aaiServiceProvider.invokeGetTenant(cloudOwner, cloudRegion, tenantId); + if (cloudOwner == null || cloudRegion == null || tenantId == null) { + throw new TenantNotFoundException("No matching Tenant found in AAI. VNFID: " + vnf.getVnfId()); + } else { + return new Tenant().cloudOwner(cloudOwner).regionName(cloudRegion).tenantId(tenantId); + } } private Relationship getRelationship(final GenericVnf vnf, final String relationshipRelatedToValue) { @@ -233,5 +237,4 @@ public class AaiHelper { public OamIpAddressSource getOamIpAddressSource(final String vnfId) { return mapOfVnfIdToOamIpAddressHolder.get(vnfId); } - } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProvider.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProvider.java index b3767a3b62..2708742e6a 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProvider.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProvider.java @@ -25,8 +25,8 @@ import org.onap.aai.domain.yang.EsrSystemInfoList; import org.onap.aai.domain.yang.EsrVnfm; import org.onap.aai.domain.yang.EsrVnfmList; import org.onap.aai.domain.yang.GenericVnf; -import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.Vserver; +import org.onap.vnfmadapter.v1.model.Tenant; /** * Provides methods for invoking REST calls to AAI. diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProviderImpl.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProviderImpl.java index 6c3d7c2ca6..55a2cc39ec 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProviderImpl.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/aai/AaiServiceProviderImpl.java @@ -25,10 +25,10 @@ import org.onap.aai.domain.yang.EsrSystemInfoList; import org.onap.aai.domain.yang.EsrVnfm; import org.onap.aai.domain.yang.EsrVnfmList; import org.onap.aai.domain.yang.GenericVnf; -import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.Vserver; import org.onap.so.client.aai.AAIObjectType; import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.vnfmadapter.v1.model.Tenant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmHelper.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmHelper.java index c0d3cf8567..b370dc35c8 100644 --- a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmHelper.java +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/extclients/vnfm/VnfmHelper.java @@ -22,10 +22,6 @@ package org.onap.so.adapters.vnfmadapter.extclients.vnfm; import static org.onap.so.adapters.vnfmadapter.Constants.BASE_URL; import static org.onap.so.adapters.vnfmadapter.Constants.OPERATION_NOTIFICATION_ENDPOINT; -import com.google.common.reflect.TypeToken; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,6 +30,7 @@ import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider; import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo; import org.onap.so.adapters.vnfmadapter.extclients.vim.model.InterfaceInfo; import org.onap.so.adapters.vnfmadapter.extclients.vim.model.VimCredentials; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest; import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthentication; @@ -53,6 +50,10 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; /** * Provides helper methods for interactions with VNFM. @@ -199,4 +200,25 @@ public class VnfmHelper { return authentication; } + /** + * Get the VIM connections for a tenant + * + * @param tenant the tenant + * @return the VIM connections + */ + public InlineResponse201VimConnections getVimConnections(final Tenant tenant) { + final EsrSystemInfo esrSystemInfo = + aaiServiceProvider.invokeGetCloudRegionEsrSystemInfoList(tenant.getCloudOwner(), tenant.getRegionName()) + .getEsrSystemInfo().iterator().next(); + + final InlineResponse201VimConnections vimConnection = new InlineResponse201VimConnections(); + vimConnection.setId(createVimId(tenant.getCloudOwner(), tenant.getRegionName())); + vimConnection.setVimId(vimConnection.getId()); + vimConnection.setVimType(esrSystemInfo.getType()); + vimConnection.setInterfaceInfo(getInterfaceInfo(esrSystemInfo.getServiceUrl())); + vimConnection.setAccessInfo(getAccessInfo(esrSystemInfo, tenant.getTenantId())); + return vimConnection; + } + + } diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java new file mode 100644 index 0000000000..82e72a7630 --- /dev/null +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantController.java @@ -0,0 +1,130 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.vnfmadapter.rest; + +import static org.onap.so.adapters.vnfmadapter.Constants.BASE_URL; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import javax.ws.rs.core.MediaType; +import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper; +import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmHelper; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201AddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections; +import org.onap.vnfmadapter.v1.model.Tenant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping(value = BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON) +public class Sol003GrantController { + + private static final String SEPARATOR = "_"; + private static final String VIM_TYPE = "OPENSTACK"; + private static final String CLOUD_OWNER = "myTestCloudOwner"; + private static final String REGION = "myTestRegion"; + private static final String TENANT_ID = "myTestTenantId"; + private static final Logger logger = LoggerFactory.getLogger(Sol003GrantController.class); + public final AaiServiceProvider aaiServiceProvider; + public final AaiHelper aaiHelper; + public final VnfmHelper vnfmHelper; + + @Autowired + public Sol003GrantController(final AaiServiceProvider aaiServiceProvider, final AaiHelper aaiHelper, + final VnfmHelper vnfmHelper) { + this.aaiServiceProvider = aaiServiceProvider; + this.aaiHelper = aaiHelper; + this.vnfmHelper = vnfmHelper; + } + + @GetMapping(value = "/grants/{grantId}") + public ResponseEntity<InlineResponse201> grantsGrantIdGet(@PathVariable("grantId") final String grantId) { + logger.info("Get grant received from VNFM, grant id: " + grantId); + return new ResponseEntity<InlineResponse201>(HttpStatus.NOT_IMPLEMENTED); + } + + @PostMapping(value = "/grants") + public ResponseEntity<InlineResponse201> grantsPost(@RequestBody final GrantRequest grantRequest) { + logger.info("Grant request received from VNFM: " + grantRequest); + + final InlineResponse201 grantResponse = createGrantResponse(grantRequest); + logger.info("Grant request returning to VNFM: " + grantResponse); + return new ResponseEntity<InlineResponse201>(grantResponse, HttpStatus.CREATED); + } + + private InlineResponse201 createGrantResponse(final GrantRequest grantRequest) { + final InlineResponse201 grantResponse = new InlineResponse201(); + grantResponse.setId(UUID.randomUUID().toString()); + grantResponse.setVnfInstanceId(grantRequest.getVnfInstanceId()); + grantResponse.setVnfLcmOpOccId(grantRequest.getVnfLcmOpOccId()); + final Tenant tenant = + aaiHelper.getAssignedTenant(aaiServiceProvider.invokeGetGenericVnf((grantRequest.getVnfInstanceId()))); + + String vimConnectionId = ""; + final InlineResponse201VimConnections vimConnection = vnfmHelper.getVimConnections(tenant); + grantResponse.addVimConnectionsItem(vimConnection); + vimConnectionId = vimConnection.getId(); + + if (grantRequest.getOperation().equals(GrantRequest.OperationEnum.INSTANTIATE)) { + grantResponse.addResources(getResources(grantRequest.getAddResources(), vimConnectionId)); + } else if (grantRequest.getOperation().equals(GrantRequest.OperationEnum.TERMINATE)) { + grantResponse.addResources(getResources(grantRequest.getRemoveResources(), vimConnectionId)); + } + return grantResponse; + } + + private InlineResponse201VimConnections getVimConnectionsItem(final Tenant tenant) { + final InlineResponse201VimConnections vimConnection = new InlineResponse201VimConnections(); + vimConnection.setId(createVimConnectionId(tenant.getCloudOwner(), tenant.getRegionName())); + vimConnection.setVimId(vimConnection.getId()); + vimConnection.setVimType(VIM_TYPE); + return vimConnection; + } + + private List<InlineResponse201AddResources> getResources(final List<GrantsAddResources> requestResources, + final String vimId) { + final List<InlineResponse201AddResources> resources = new ArrayList<>(); + for (final GrantsAddResources requestResource : requestResources) { + final InlineResponse201AddResources responseResource = new InlineResponse201AddResources(); + responseResource.setResourceDefinitionId(requestResource.getId()); + responseResource.setVimConnectionId(vimId); + resources.add(responseResource); + } + return resources; + } + + private String createVimConnectionId(String cloudOwner, String cloudRegionId) { + return cloudOwner + SEPARATOR + cloudRegionId; + } +} diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/TenantNotFoundException.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/TenantNotFoundException.java new file mode 100644 index 0000000000..215e68adcf --- /dev/null +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/main/java/org/onap/so/adapters/vnfmadapter/rest/exceptions/TenantNotFoundException.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.vnfmadapter.rest.exceptions; + +/** + * Exception for Tenant not found. + */ + +public class TenantNotFoundException extends RuntimeException { + + private static final long serialVersionUID = 6398018034431666933L; + + public TenantNotFoundException(final String message) { + super(message); + } + +} diff --git a/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantControllerTest.java b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantControllerTest.java new file mode 100644 index 0000000000..21c6eff6b0 --- /dev/null +++ b/adapters/mso-vnfm-adapter/mso-vnfm-etsi-adapter/src/test/java/org/onap/so/adapters/vnfmadapter/rest/Sol003GrantControllerTest.java @@ -0,0 +1,240 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.adapters.vnfmadapter.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE; +import java.util.Optional; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.hamcrest.MockitoHamcrest; +import org.onap.aai.domain.yang.EsrSystemInfo; +import org.onap.aai.domain.yang.EsrSystemInfoList; +import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aai.domain.yang.Relationship; +import org.onap.aai.domain.yang.RelationshipData; +import org.onap.aai.domain.yang.RelationshipList; +import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest.OperationEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources.TypeEnum; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201; +import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections; +import org.onap.so.client.aai.AAIResourcesClient; +import org.onap.so.client.aai.entities.uri.AAIResourceUri; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; +import com.google.gson.Gson; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +public class Sol003GrantControllerTest { + + private static final String CLOUD_OWNER = "myTestCloudOwner"; + private static final String REGION = "myTestRegion"; + private static final String TENANT_ID = "myTestTenantId"; + private static final String SEPARATOR = "_"; + private static final String vimConnectionId = CLOUD_OWNER + SEPARATOR + REGION; + + @LocalServerPort + private int port; + @Autowired + @Qualifier(CONFIGURABLE_REST_TEMPLATE) + private RestTemplate testRestTemplate; + private MockRestServiceServer mockRestServer; + + @MockBean + private AAIResourcesClient aaiResourcesClient; + + @Autowired + private Sol003GrantController controller; + private final Gson gson = new Gson(); + + @Before + public void setUp() throws Exception { + mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build(); + setUpVimInMockAai(); + } + + @Test + public void grantRequest_ValidRequestInstantiate_GrantApproved() { + GrantRequest grantRequest = createGrantRequest("INSTANTIATE"); + setUpGenericVnfWithVnfmRelationshipInMockAai("vnfmType", "vnfm1"); + final ResponseEntity<InlineResponse201> response = controller.grantsPost(grantRequest); + assertEquals(HttpStatus.CREATED, response.getStatusCode()); + assertEquals(1, response.getBody().getAddResources().size()); + + assertEquals(vimConnectionId, response.getBody().getAddResources().get(0).getVimConnectionId()); + assertEquals("myTestVnfId", response.getBody().getVnfInstanceId()); + assertEquals("123456", response.getBody().getVnfLcmOpOccId()); + + InlineResponse201VimConnections vimConnections = response.getBody().getVimConnections().get(0); + assertEquals(vimConnectionId, vimConnections.getVimId()); + assertEquals("OPENSTACK", vimConnections.getVimType()); + assertNotNull(vimConnections.getAccessInfo()); + assertNotNull(vimConnections.getInterfaceInfo()); + assertEquals("INSTANTIATE", grantRequest.getOperation().toString()); + } + + @Test + public void getGrant_notSupported_returns501() { + final ResponseEntity<InlineResponse201> response2 = controller.grantsGrantIdGet("myTestGrantId"); + assertEquals(HttpStatus.NOT_IMPLEMENTED, response2.getStatusCode()); + } + + @Test + public void grantRequest_ValidRequestTerminate_GrantApproved() { + GrantRequest grantRequest = createGrantRequest("TERMINATE"); + setUpGenericVnfWithVnfmRelationshipInMockAai("vnfmType", "vnfm1"); + final ResponseEntity<InlineResponse201> response = controller.grantsPost(grantRequest); + + assertEquals(HttpStatus.CREATED, response.getStatusCode()); + assertEquals(1, response.getBody().getAddResources().size()); + assertEquals(vimConnectionId, response.getBody().getAddResources().get(0).getVimConnectionId()); + assertEquals("myTestVnfId", response.getBody().getVnfInstanceId()); + assertEquals("123456", response.getBody().getVnfLcmOpOccId()); + + InlineResponse201VimConnections vimConnections = response.getBody().getVimConnections().get(0); + assertEquals(vimConnectionId, vimConnections.getVimId()); + assertEquals("OPENSTACK", vimConnections.getVimType()); + assertNotNull(vimConnections.getAccessInfo()); + assertNotNull(vimConnections.getInterfaceInfo()); + assertEquals("TERMINATE", grantRequest.getOperation().toString()); + + } + + private GrantRequest createGrantRequest(String operation) { + GrantRequest grantRequest = new GrantRequest(); + grantRequest.setVnfInstanceId("myTestVnfId"); + grantRequest.setVnfLcmOpOccId("123456"); + if (operation == "INSTANTIATE") { + grantRequest.setOperation(OperationEnum.INSTANTIATE); + GrantsAddResources resource = new GrantsAddResources(); + resource.setId("123"); + resource.setType(TypeEnum.COMPUTE); + grantRequest.addAddResourcesItem(resource); + } else if (operation == "TERMINATE") { + grantRequest.setOperation(OperationEnum.TERMINATE); + GrantsAddResources resource = new GrantsAddResources(); + resource.setId("123"); + resource.setType(TypeEnum.COMPUTE); + grantRequest.addRemoveResourcesItem(resource); + } + + return grantRequest; + } + + private void setUpVimInMockAai() { + final EsrSystemInfo esrSystemInfo = new EsrSystemInfo(); + esrSystemInfo.setServiceUrl("http://myVim:8080"); + esrSystemInfo.setType("OPENSTACK"); + esrSystemInfo.setSystemType("VIM"); + esrSystemInfo.setCloudDomain("myDomain"); + esrSystemInfo.setUserName("myUser"); + esrSystemInfo.setPassword("myPassword"); + + final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList(); + esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo); + + doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class), + MockitoHamcrest.argThat(new AaiResourceUriMatcher("/cloud-infrastructure/cloud-regions/cloud-region/" + + CLOUD_OWNER + "/" + REGION + "/esr-system-info-list"))); + } + + private GenericVnf createGenericVnf(final String type) { + final GenericVnf genericVnf = new GenericVnf(); + genericVnf.setVnfId("myTestVnfId"); + genericVnf.setNfType(type); + return genericVnf; + } + + private void setUpGenericVnfWithVnfmRelationshipInMockAai(final String type, final String vnfmId) { + final GenericVnf genericVnf = createGenericVnf(type); + + final Relationship relationshipToVnfm = new Relationship(); + relationshipToVnfm.setRelatedTo("tenant"); + final RelationshipData relationshipData1 = new RelationshipData(); + final RelationshipData relationshipData2 = new RelationshipData(); + final RelationshipData relationshipData3 = new RelationshipData(); + + relationshipData1.setRelationshipKey("cloud-region.cloud-owner"); + relationshipData1.setRelationshipValue(CLOUD_OWNER); + relationshipData2.setRelationshipKey("cloud-region.cloud-region-id"); + relationshipData2.setRelationshipValue(REGION); + relationshipData3.setRelationshipKey("tenant.tenant-id"); + relationshipData3.setRelationshipValue(TENANT_ID); + + relationshipToVnfm.getRelationshipData().add(relationshipData1); + relationshipToVnfm.getRelationshipData().add(relationshipData2); + relationshipToVnfm.getRelationshipData().add(relationshipData3); + + final RelationshipList relationshipList = new RelationshipList(); + relationshipList.getRelationship().add(relationshipToVnfm); + genericVnf.setRelationshipList(relationshipList); + + doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), + MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId"))); + } + + private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> { + + final String uriAsString; + + public AaiResourceUriMatcher(final String uriAsString) { + this.uriAsString = uriAsString; + } + + @Override + public boolean matches(final Object item) { + if (item instanceof AAIResourceUri) { + if (uriAsString.endsWith("...")) { + return ((AAIResourceUri) item).build().toString() + .startsWith(uriAsString.substring(0, uriAsString.indexOf("..."))); + } + return ((AAIResourceUri) item).build().toString().equals(uriAsString); + } + return false; + } + + @Override + public void describeTo(final Description description) {} + + } + +} |