Fixing vServer relationship endpoint 85/96085/1
authorwaqas.ikram <waqas.ikram@est.tech>
Fri, 20 Sep 2019 14:28:57 +0000 (15:28 +0100)
committerwaqas.ikram <waqas.ikram@est.tech>
Fri, 20 Sep 2019 14:28:59 +0000 (15:28 +0100)
Change-Id: I1a4baee44284958876d6d7e71338e7fd6d9aac04
Issue-ID: SO-2342
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/CloudRegionsController.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProvider.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/HttpServiceUtils.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/CloudRegionsControllerTest.java
plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/vServer.json

index 29701ed..8e007de 100644 (file)
@@ -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<Relationship> 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);
+
+    }
 }
index 412da1c..6facac8 100644 (file)
@@ -59,4 +59,11 @@ public interface CloudRegionCacheServiceProvider extends Clearable {
 
     boolean deleteVserver(final CloudRegionKey key, final String tenantId, final String vServerId,
             final String resourceVersion);
+
+    Optional<Relationship> 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);
+
 }
index 6fb1eb0..f1f782a 100644 (file)
@@ -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<Relationship> addvServerRelationShip(final CloudRegionKey key, final String tenantId,
+            final String vServerId, final Relationship relationship, final String requestUri) {
+        final Optional<Vserver> 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<RelationshipData> 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<RelatedToProperty> 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<Vserver> 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<Relationship> 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<Relationship> 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<EsrSystemInfo> getEsrSystemInfoList(final CloudRegion cloudRegion) {
         EsrSystemInfoList esrSystemInfoList = cloudRegion.getEsrSystemInfoList();
         if (esrSystemInfoList == null) {
index b4b562b..4873530 100644 (file)
@@ -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() {}
 
 }
index a03a275..10c1f25 100644 (file)
@@ -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();
     }
index 0ed9d4e..334a0eb 100644 (file)
@@ -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<GenericVnf> 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<RelationshipData> 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);
index b3cbba6..5fa1843 100644 (file)
@@ -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"
+                    }
+                ]
+            }
+        ]
+    }
 }