Create based CSIT test for SO-CNFM - Simulator Changes.
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / CloudRegionCacheServiceProviderImpl.java
index 5024cc7..f15b389 100644 (file)
@@ -25,10 +25,18 @@ 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.K8S_RESOURCE;
+import static org.onap.so.aaisimulator.utils.Constants.K8S_RESOURCE_ID;
+import static org.onap.so.aaisimulator.utils.Constants.K8S_RESOURCE_NAME;
 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.USES;
+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;
@@ -37,6 +45,8 @@ import java.util.Optional;
 import org.onap.aai.domain.yang.CloudRegion;
 import org.onap.aai.domain.yang.EsrSystemInfo;
 import org.onap.aai.domain.yang.EsrSystemInfoList;
+import org.onap.aai.domain.yang.K8SResource;
+import org.onap.aai.domain.yang.K8SResources;
 import org.onap.aai.domain.yang.RelatedToProperty;
 import org.onap.aai.domain.yang.Relationship;
 import org.onap.aai.domain.yang.RelationshipData;
@@ -282,7 +292,7 @@ public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServicePro
         final Optional<Tenant> optional = getTenant(key, tenantId);
         if (optional.isPresent()) {
             final Tenant tenant = optional.get();
-            Vservers vServers = tenant.getVservers();
+            final Vservers vServers = tenant.getVservers();
             if (vServers != null) {
                 return vServers.getVserver().stream()
                         .filter(vServer -> vServer.getVserverId() != null && vServer.getVserverId().equals(vServerId))
@@ -293,6 +303,238 @@ public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServicePro
         return Optional.empty();
     }
 
+    @Override
+    public boolean deleteVserver(final CloudRegionKey key, final String tenantId, final String vServerId,
+            final String resourceVersion) {
+        final Optional<Vserver> optional = getVserver(key, tenantId, vServerId);
+        if (optional.isPresent()) {
+            final Optional<Tenant> tenantOptional = getTenant(key, tenantId);
+            if (tenantOptional.isPresent()) {
+                final Tenant tenant = tenantOptional.get();
+                final Vservers vServers = tenant.getVservers();
+                if (vServers != null) {
+                    return vServers.getVserver().removeIf(vServer -> {
+                        if (vServer.getVserverId() != null && vServer.getVserverId().equals(vServerId)
+                                && vServer.getResourceVersion() != null
+                                && vServer.getResourceVersion().equals(resourceVersion)) {
+                            LOGGER.info("Will remove Vserver from cache with vServerId: {} and resource-version: {} ",
+                                    vServerId, vServer.getResourceVersion());
+                            return true;
+                        }
+                        return false;
+                    });
+                }
+
+            }
+
+        }
+        LOGGER.error(
+                "Unable to find Vserver for using key: {}, tenant-id: {}, vserver-id: {} and resource-version: {} ...",
+                key, tenantId, vServerId, resourceVersion);
+
+        return false;
+    }
+
+    @Override
+    public boolean putK8sResource(final CloudRegionKey key, final String tenantId, final String id,
+            final K8SResource k8sResource) {
+        final Optional<Tenant> optional = getTenant(key, tenantId);
+        if (optional.isPresent()) {
+            final Tenant tenant = optional.get();
+            K8SResources k8sResources = tenant.getK8SResources();
+            if (k8sResources == null) {
+                k8sResources = new K8SResources();
+                tenant.setK8SResources(k8sResources);
+            }
+
+
+            final Optional<K8SResource> existingK8sResource = k8sResources.getK8SResource().stream()
+                    .filter(entry -> entry.getId() != null && entry.getId().equalsIgnoreCase(id)).findFirst();
+
+            if (existingK8sResource.isEmpty()) {
+                LOGGER.info("Adding k8sResources to cache...");
+                return k8sResources.getK8SResource().add(k8sResource);
+            }
+
+            LOGGER.warn("K8SResource already exists existingK8sResource: {}...", existingK8sResource.get());
+            return false;
+        }
+
+        LOGGER.error("Unable to add K8s Resource using key: {}, tenantId: {} and id: {}...", key, tenantId, id);
+        return false;
+    }
+
+    @Override
+    public Optional<K8SResource> getK8sResource(final CloudRegionKey key, final String tenantId, final String id) {
+        final Optional<Tenant> optional = getTenant(key, tenantId);
+
+        if (optional.isPresent()) {
+
+            final Tenant tenant = optional.get();
+            final K8SResources k8sResources = tenant.getK8SResources();
+            if (k8sResources != null) {
+                return k8sResources.getK8SResource().stream()
+                        .filter(entry -> entry.getId() != null && entry.getId().equalsIgnoreCase(id)).findFirst();
+            }
+
+        }
+        LOGGER.error("Unable to find K8sResource using key: {}, tenantId: {} and id: {}...", key, tenantId, id);
+        return Optional.empty();
+    }
+
+    @Override
+    public boolean addK8sResourceRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
+            final String requestUriString, final CloudRegionKey key, final String tenantId, final String id,
+            final Relationship relationship) {
+        try {
+            final Optional<K8SResource> optional = getK8sResource(key, tenantId, id);
+            if (optional.isPresent()) {
+                final K8SResource k8sResource = optional.get();
+                final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
+                final Relationship outGoingRelationShip =
+                        getRelationship(key, tenantId, k8sResource, getRelationShipListRelatedLink(requestUriString));
+                final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
+                        outGoingRelationShip, targetUrl, Relationship.class);
+
+                if (optionalRelationship.isPresent()) {
+                    final Relationship resultantRelationship = optionalRelationship.get();
+
+                    RelationshipList relationshipList = k8sResource.getRelationshipList();
+                    if (relationshipList == null) {
+                        relationshipList = new RelationshipList();
+                        k8sResource.setRelationshipList(relationshipList);
+                    }
+                    if (relationshipList.getRelationship().add(resultantRelationship)) {
+                        LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
+                        return true;
+                    }
+                }
+
+            }
+        } catch (final Exception exception) {
+            LOGGER.error("Unable to add two-way relationship for key: {}, tenantId: {} and id: {}", key, tenantId, id,
+                    exception);
+        }
+        LOGGER.error("Unable to add K8sResource relationship for key: {}, tenantId: {} and id: {}...", key, tenantId,
+                id);
+        return false;
+    }
+
+    private Relationship getRelationship(final CloudRegionKey key, final String tenantId, final K8SResource k8sResource,
+            final String relatedLink) {
+        final Relationship relationShip = new Relationship();
+        relationShip.setRelatedTo(K8S_RESOURCE);
+        relationShip.setRelationshipLabel(USES);
+        relationShip.setRelatedLink(relatedLink);
+
+        final List<RelationshipData> relationshipDataList = relationShip.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(K8S_RESOURCE_ID, k8sResource.getId()));
+
+        final RelatedToProperty relatedToProperty = new RelatedToProperty();
+        relatedToProperty.setPropertyKey(K8S_RESOURCE_NAME);
+        relatedToProperty.setPropertyValue(k8sResource.getName());
+        relationShip.getRelatedToProperty().add(relatedToProperty);
+
+        return relationShip;
+    }
+
+    @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) {
@@ -336,4 +578,29 @@ public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServicePro
 
     }
 
+    @Override
+    public boolean deleteK8sResource(final CloudRegionKey key, final String tenantId, final String id,
+                                     final String resourceVersion) {
+        final Optional<Tenant> optional = getTenant(key, tenantId);
+        if (optional.isPresent()) {
+            final Tenant tenant = optional.get();
+            K8SResources k8sResources = tenant.getK8SResources();
+            if (k8sResources != null) {
+                final Optional<K8SResource> existingK8sResource = k8sResources.getK8SResource().stream()
+                    .filter(entry -> entry.getId() != null && entry.getId().equalsIgnoreCase(id)).findFirst();
+
+                if (existingK8sResource.isPresent()
+                    && existingK8sResource.get().getResourceVersion().equals(resourceVersion)) {
+                    LOGGER.info("k8sResources found in cache and removing the same.");
+                    return k8sResources.getK8SResource().remove(existingK8sResource.get());
+                }
+            }
+            else {
+                return true;
+            }
+        }
+
+        LOGGER.error("Unable to add K8s Resource using key: {}, tenantId: {} and id: {}...", key, tenantId, id);
+        return false;
+    }
 }