From 48926e13233260ac15b57d9560b30bb90bb307d4 Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Fri, 20 Sep 2019 15:28:57 +0100 Subject: Fixing vServer relationship endpoint Change-Id: I1a4baee44284958876d6d7e71338e7fd6d9aac04 Issue-ID: SO-2342 Signed-off-by: waqas.ikram --- .../controller/CloudRegionsController.java | 76 +++++++++++++++- .../providers/CloudRegionCacheServiceProvider.java | 7 ++ .../CloudRegionCacheServiceProviderImpl.java | 100 +++++++++++++++++++++ .../org/onap/so/aaisimulator/utils/Constants.java | 10 ++- .../so/aaisimulator/utils/HttpServiceUtils.java | 5 ++ .../controller/CloudRegionsControllerTest.java | 38 ++++++++ .../src/test/resources/test-data/vServer.json | 23 ++++- 7 files changed, 256 insertions(+), 3 deletions(-) diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java index 29701ed4..8e007de3 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java @@ -24,6 +24,7 @@ import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION; import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGIONS; import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST; import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.onap.so.aaisimulator.utils.Constants.VSERVER; import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders; import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; @@ -130,7 +131,7 @@ public class CloudRegionsController { } LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key); - return getRequestErrorResponseEntity(request, CLOUD_REGION); + return getRequestErrorResponseEntity(request, VSERVER); } @@ -265,6 +266,27 @@ public class CloudRegionsController { vServerId); if (cacheServiceProvider.putVserver(key, tenantId, vServerId, vServer)) { + + if (vServer.getRelationshipList() != null) { + for (final Relationship relationship : vServer.getRelationshipList().getRelationship()) { + if (relationship.getRelatedLink() != null) { + final String requestUri = request.getRequestURI(); + final String targetBaseUrl = + HttpServiceUtils.getBaseUrl(request.getRequestURL(), requestUri).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addVServerRelationShip(incomingHeader, + targetBaseUrl, requestUri, key, tenantId, vServerId, relationship); + if (!result) { + LOGGER.error( + "Unable to add Vserver relationship in cache using key: {}, tenantId: {}, vServerId: {}", + key, tenantId, vServerId); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + } + LOGGER.info("Successfully added relationship with {}", relationship.getRelatedLink()); + } + } + } + LOGGER.info("Successfully added Vserver for key: {}, tenantId: {}, vServerId: {} ...", key, tenantId, vServerId); return ResponseEntity.accepted().build(); @@ -319,4 +341,56 @@ public class CloudRegionsController { key, tenantId, vServerId, resourceVersion); return getRequestErrorResponseEntity(request, CLOUD_REGION); } + + @PutMapping( + value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}" + + RELATIONSHIP_LIST_RELATIONSHIP_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity putVserverRelationShip(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + if (relationship.getRelatedLink() != null) { + final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString(); + final HttpHeaders incomingHeader = getHeaders(request); + final boolean result = cacheServiceProvider.addVServerRelationShip(incomingHeader, targetBaseUrl, + request.getRequestURI(), key, tenantId, vServerId, relationship); + if (result) { + LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink()); + return ResponseEntity.accepted().build(); + } + } + LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + + } + + @PutMapping( + value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}" + + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL, + consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity putBiDirectionalVServerRelationShip(@PathVariable("cloud-owner") final String cloudOwner, + @PathVariable("cloud-region-id") final String cloudRegionId, + @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId, + @RequestBody final Relationship relationship, final HttpServletRequest request) { + final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId); + LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo()); + + final Optional optional = cacheServiceProvider.addvServerRelationShip(key, tenantId, vServerId, + relationship, request.getRequestURI()); + + if (optional.isPresent()) { + final Relationship resultantRelationship = optional.get(); + LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship); + return ResponseEntity.accepted().body(resultantRelationship); + } + LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key); + return getRequestErrorResponseEntity(request, CLOUD_REGION); + + } } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java index 412da1c6..6facac82 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java @@ -59,4 +59,11 @@ public interface CloudRegionCacheServiceProvider extends Clearable { boolean deleteVserver(final CloudRegionKey key, final String tenantId, final String vServerId, final String resourceVersion); + + Optional addvServerRelationShip(final CloudRegionKey key, final String tenantId, + final String vServerId, final Relationship relationship, final String requestUri); + + boolean addVServerRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl, final String requestURI, final CloudRegionKey key, + final String tenantId, final String vServerId, final Relationship relationship); + } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java index 6fb1eb0f..f1f782a0 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java @@ -25,10 +25,14 @@ import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION; import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_OWNER; import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_REGION_ID; import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_OWNER_DEFINED_TYPE; +import static org.onap.so.aaisimulator.utils.Constants.HOSTED_ON; import static org.onap.so.aaisimulator.utils.Constants.LOCATED_IN; import static org.onap.so.aaisimulator.utils.Constants.TENANT; import static org.onap.so.aaisimulator.utils.Constants.TENANT_TENANT_ID; import static org.onap.so.aaisimulator.utils.Constants.TENANT_TENANT_NAME; +import static org.onap.so.aaisimulator.utils.Constants.VSERVER; +import static org.onap.so.aaisimulator.utils.Constants.VSERVER_VSERVER_ID; +import static org.onap.so.aaisimulator.utils.Constants.VSERVER_VSERVER_NAME; import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink; import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink; import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl; @@ -63,6 +67,8 @@ import org.springframework.stereotype.Service; public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider implements CloudRegionCacheServiceProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class); private final HttpRestServiceProvider httpRestServiceProvider; @@ -325,6 +331,100 @@ public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServicePro return false; } + @Override + public Optional addvServerRelationShip(final CloudRegionKey key, final String tenantId, + final String vServerId, final Relationship relationship, final String requestUri) { + final Optional optional = getVserver(key, tenantId, vServerId); + if (optional.isPresent()) { + final Vserver vServer = optional.get(); + RelationshipList relationshipList = vServer.getRelationshipList(); + if (relationshipList == null) { + relationshipList = new RelationshipList(); + vServer.setRelationshipList(relationshipList); + } + relationshipList.getRelationship().add(relationship); + LOGGER.info("Successfully added relation to Vserver with key: {}, tenantId: {} and vServerId: {}", key, + tenantId, vServerId); + final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestUri); + + final Relationship resultantRelationship = getVserverRelationship(key, tenantId, vServer, relatedLink); + + return Optional.of(resultantRelationship); + } + + LOGGER.error("Unable to find Vserver using key: {}, tenantId: {} and vServerId: {}...", key, tenantId, + vServerId); + return Optional.empty(); + } + + private Relationship getVserverRelationship(final CloudRegionKey key, final String tenantId, final Vserver vServer, + final String relatedLink) { + final Relationship resultantRelationship = new Relationship(); + resultantRelationship.setRelatedTo(VSERVER); + resultantRelationship.setRelationshipLabel(HOSTED_ON); + resultantRelationship.setRelatedLink(relatedLink); + + final List relationshipDataList = resultantRelationship.getRelationshipData(); + relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, key.getCloudOwner())); + relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, key.getCloudRegionId())); + relationshipDataList.add(getRelationshipData(TENANT_TENANT_ID, tenantId)); + relationshipDataList.add(getRelationshipData(VSERVER_VSERVER_ID, vServer.getVserverId())); + + final List relatedToPropertyList = resultantRelationship.getRelatedToProperty(); + + final RelatedToProperty relatedToProperty = new RelatedToProperty(); + relatedToProperty.setPropertyKey(VSERVER_VSERVER_NAME); + relatedToProperty.setPropertyValue(vServer.getVserverName()); + relatedToPropertyList.add(relatedToProperty); + return resultantRelationship; + } + + @Override + public boolean addVServerRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl, + final String requestUriString, final CloudRegionKey key, final String tenantId, final String vServerId, + final Relationship relationship) { + try { + final Optional optional = getVserver(key, tenantId, vServerId); + if (optional.isPresent()) { + final Vserver vServer = optional.get(); + final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink()); + final Relationship outGoingRelationShip = getVserverRelationship(key, tenantId, vServer, + getRelationShipListRelatedLink(requestUriString)); + final Optional optionalRelationship = httpRestServiceProvider.put(incomingHeader, + outGoingRelationShip, targetUrl, Relationship.class); + if (optionalRelationship.isPresent()) { + final Relationship resultantRelationship = optionalRelationship.get(); + + RelationshipList relationshipList = vServer.getRelationshipList(); + if (relationshipList == null) { + relationshipList = new RelationshipList(); + vServer.setRelationshipList(relationshipList); + } + + final Optional relationShipExists = relationshipList.getRelationship().stream() + .filter(relation -> relation.getRelatedTo().equals(resultantRelationship.getRelatedTo()) + && relation.getRelatedLink().equals(resultantRelationship.getRelatedLink())) + .findAny(); + + if (relationShipExists.isPresent()) { + LOGGER.info("relationship {} already exists in cache ", resultantRelationship); + return true; + } + + LOGGER.info("added relationship {} in cache successfully", resultantRelationship); + return relationshipList.getRelationship().add(resultantRelationship); + } + + } + } catch (final Exception exception) { + LOGGER.error("Unable to add two-way relationship for key: {}, tenantId: {} and vServerId: {}", key, + tenantId, vServerId, exception); + } + LOGGER.error("Unable to add Vserver relationship for key: {}, tenantId: {} and vServerId: {}...", key, tenantId, + vServerId); + return false; + } + private List getEsrSystemInfoList(final CloudRegion cloudRegion) { EsrSystemInfoList esrSystemInfoList = cloudRegion.getEsrSystemInfoList(); if (esrSystemInfoList == null) { diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java index b4b562be..48735309 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java @@ -67,7 +67,7 @@ public class Constants { public static final String OWNING_ENTITY_OWNING_ENTITY_ID = "owning-entity.owning-entity-id"; public static final String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override"; - + public static final String APPLICATION_MERGE_PATCH_JSON = "application/merge-patch+json"; public static final String SERVICE_RESOURCE_TYPE = "service-instance"; @@ -130,6 +130,14 @@ public class Constants { public static final String ESR_SYSTEM_INFO_LIST = "esr-system-info-list"; + public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name"; + + public static final String VSERVER_VSERVER_ID = "vserver.vserver-id"; + + public static final String HOSTED_ON = "tosca.relationships.HostedOn"; + + public static final String VSERVER = "vserver"; + private Constants() {} } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java index a03a2757..10c1f258 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java @@ -45,6 +45,11 @@ public class HttpServiceUtils { return UriComponentsBuilder.fromHttpUrl(url.substring(0, url.indexOf(uri))).path(BASE_URL).build().toUri(); } + public static URI getBaseUrl(final StringBuffer requestUrl, final String requestUri) { + return UriComponentsBuilder.fromHttpUrl(requestUrl.substring(0, requestUrl.indexOf(requestUri))).build() + .toUri(); + } + public static String getBaseServiceInstanceUrl(final HttpServletRequest request, final String relatedLink) { return UriComponentsBuilder.fromUri(getBaseUrl(request)).path(relatedLink).toUriString(); } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/CloudRegionsControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/CloudRegionsControllerTest.java index 0ed9d4ea..334a0ebb 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/CloudRegionsControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/CloudRegionsControllerTest.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; +import static org.onap.so.aaisimulator.utils.Constants.VSERVER; import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME; import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME; import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL; @@ -300,6 +301,7 @@ public class CloudRegionsControllerTest extends AbstractSpringBootTest { final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(url); + addCustomerServiceAndGenericVnf(); final String tenantUrl = url + TENANTS_TENANT + TENANT_ID; addTenantAndAssertResponse(tenantUrl); @@ -318,6 +320,41 @@ public class CloudRegionsControllerTest extends AbstractSpringBootTest { assertEquals(VSERVER_NAME, actualVserver.getVserverName()); assertEquals(VSERVER_ID, actualVserver.getVserverId()); assertEquals("active", actualVserver.getProvStatus()); + assertNotNull(actualVserver.getRelationshipList()); + assertFalse(actualVserver.getRelationshipList().getRelationship().isEmpty()); + + final Optional optional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID); + assertTrue(optional.isPresent()); + final GenericVnf genericVnf = optional.get(); + assertNotNull(genericVnf.getRelationshipList()); + assertFalse(genericVnf.getRelationshipList().getRelationship().isEmpty()); + + final Relationship expectedRelationShip = genericVnf.getRelationshipList().getRelationship().get(0); + assertEquals(VSERVER, expectedRelationShip.getRelatedTo()); + assertNotNull(expectedRelationShip.getRelationshipData()); + assertEquals(4, expectedRelationShip.getRelationshipData().size()); + + final List relationshipDataList = expectedRelationShip.getRelationshipData(); + final RelationshipData vServerrelationshipData = + getRelationshipData(relationshipDataList, Constants.VSERVER_VSERVER_ID); + assertNotNull(vServerrelationshipData); + assertEquals(VSERVER_ID, vServerrelationshipData.getRelationshipValue()); + + final RelationshipData cloudOwnerRelationshipData = + getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_OWNER); + assertNotNull(cloudOwnerRelationshipData); + assertEquals(CLOUD_OWNER_NAME, cloudOwnerRelationshipData.getRelationshipValue()); + + final RelationshipData cloudRegionIdRelationshipData = + getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_REGION_ID); + assertNotNull(cloudRegionIdRelationshipData); + assertEquals(CLOUD_REGION_NAME, cloudRegionIdRelationshipData.getRelationshipValue()); + + final RelationshipData tenantRelationshipData = + getRelationshipData(relationshipDataList, Constants.TENANT_TENANT_ID); + assertNotNull(tenantRelationshipData); + assertEquals(TENANT_ID, tenantRelationshipData.getRelationshipValue()); + } @Test @@ -325,6 +362,7 @@ public class CloudRegionsControllerTest extends AbstractSpringBootTest { final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(url); + addCustomerServiceAndGenericVnf(); final String tenantUrl = url + TENANTS_TENANT + TENANT_ID; addTenantAndAssertResponse(tenantUrl); diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/vServer.json b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/vServer.json index b3cbba65..5fa1843e 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/vServer.json +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/vServer.json @@ -1,5 +1,26 @@ { "vserver-id": "f84fdb9b-ad7c-49db-a08f-e443b4cbd033", "vserver-name": "CsitVServer", - "prov-status": "active" + "prov-status": "active", + "relationship-list": { + "relationship": [ + { + "related-to": "generic-vnf", + "relationship-label": "tosca.relationships.HostedOn", + "related-link": "/aai/v15/network/generic-vnfs/generic-vnf/dfd02fb5-d7fb-4aac-b3c4-cd6b60058701", + "relationship-data": [ + { + "relationship-key": "generic-vnf.vnf-id", + "relationship-value": "58157d7e-d50d-4a7d-aebe-ae6e41ca1d9f" + } + ], + "related-to-property": [ + { + "property-key": "generic-vnf.vnf-name", + "property-value": "Test" + } + ] + } + ] + } } -- cgit 1.2.3-korg