Adding vnf-topology-operation endpoint
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / ProjectCacheServiceProviderImpl.java
index 3ddefbe..a2eb7ee 100644 (file)
  */
 package org.onap.so.aaisimulator.service.providers;
 
-import static org.onap.so.aaisimulator.utils.Constants.PROJECT_CACHE;
-import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
+import static org.onap.so.aaisimulator.utils.CacheName.PROJECT_CACHE;
+import static org.onap.so.aaisimulator.utils.Constants.PROJECT;
+import static org.onap.so.aaisimulator.utils.Constants.PROJECT_PROJECT_NAME;
+import static org.onap.so.aaisimulator.utils.Constants.USES;
+import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
+import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
+import java.util.List;
 import java.util.Optional;
 import org.onap.aai.domain.yang.Project;
 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.simulator.cache.provider.AbstractCacheServiceProvider;
 import org.slf4j.Logger;
@@ -31,6 +37,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.Cache;
 import org.springframework.cache.CacheManager;
+import org.springframework.http.HttpHeaders;
 import org.springframework.stereotype.Service;
 
 /**
@@ -41,20 +48,21 @@ import org.springframework.stereotype.Service;
 public class ProjectCacheServiceProviderImpl extends AbstractCacheServiceProvider
         implements ProjectCacheServiceProvider {
 
-    private static final String RELATIONSHIPS_LABEL = "org.onap.relationships.inventory.Uses";
-
     private static final Logger LOGGER = LoggerFactory.getLogger(ProjectCacheServiceProviderImpl.class);
 
+    private final HttpRestServiceProvider httpRestServiceProvider;
 
     @Autowired
-    public ProjectCacheServiceProviderImpl(final CacheManager cacheManager) {
+    public ProjectCacheServiceProviderImpl(final CacheManager cacheManager,
+            final HttpRestServiceProvider httpRestServiceProvider) {
         super(cacheManager);
+        this.httpRestServiceProvider = httpRestServiceProvider;
     }
 
     @Override
     public void putProject(final String projectName, final Project project) {
         LOGGER.info("Adding project: {} with name to cache", project, projectName);
-        final Cache cache = getCache(PROJECT_CACHE);
+        final Cache cache = getCache(PROJECT_CACHE.getName());
         cache.put(projectName, project);
     }
 
@@ -62,7 +70,7 @@ public class ProjectCacheServiceProviderImpl extends AbstractCacheServiceProvide
     @Override
     public Optional<Project> getProject(final String projectName) {
         LOGGER.info("getting project from cache using key: {}", projectName);
-        final Cache cache = getCache(PROJECT_CACHE);
+        final Cache cache = getCache(PROJECT_CACHE.getName());
         final Project value = cache.get(projectName, Project.class);
         if (value != null) {
             return Optional.of(value);
@@ -71,32 +79,62 @@ public class ProjectCacheServiceProviderImpl extends AbstractCacheServiceProvide
     }
 
     @Override
-    public boolean putProjectRelationShip(final String projectName, final Relationship relationship) {
-        final Cache cache = getCache(PROJECT_CACHE);
-        final Project value = cache.get(projectName, Project.class);
-        if (value != null) {
-            RelationshipList relationshipList = value.getRelationshipList();
-            if (relationshipList == null) {
-                relationshipList = new RelationshipList();
-                value.setRelationshipList(relationshipList);
-            }
+    public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
+            final String requestUriString, final String projectName, final Relationship relationship) {
+        try {
+            final Optional<Project> optional = getProject(projectName);
 
-            if (relationship.getRelatedTo() == null) {
-                relationship.setRelatedTo(SERVICE_RESOURCE_TYPE);
-            }
-            if (relationship.getRelationshipLabel() == null) {
-                relationship.setRelationshipLabel(RELATIONSHIPS_LABEL);
-            }
+            if (optional.isPresent()) {
+                final Project project = optional.get();
+                final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
+                final Relationship outGoingRelationShip = getRelationship(requestUriString, project);
+
+                final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
+                        outGoingRelationShip, targetUrl, Relationship.class);
+
+                if (optionalRelationship.isPresent()) {
+                    final Relationship resultantRelationship = optionalRelationship.get();
 
-            return relationshipList.getRelationship().add(relationship);
+                    RelationshipList relationshipList = project.getRelationshipList();
+                    if (relationshipList == null) {
+                        relationshipList = new RelationshipList();
+                        project.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 project name: {}", projectName, exception);
         }
-        LOGGER.error("Project not found in cache for {}", projectName);
+        LOGGER.error("Unable to add relationship in cache for project name: {}", projectName);
         return false;
-
     }
 
     @Override
     public void clearAll() {
-        clearCahce(PROJECT_CACHE);
+        clearCache(PROJECT_CACHE.getName());
     }
+
+    private Relationship getRelationship(final String requestUriString, final Project project) {
+
+        final Relationship relationShip = new Relationship();
+        relationShip.setRelatedTo(PROJECT);
+        relationShip.setRelationshipLabel(USES);
+        relationShip.setRelatedLink(getRelationShipListRelatedLink(requestUriString));
+
+        final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
+
+        final RelationshipData relationshipData = new RelationshipData();
+        relationshipData.setRelationshipKey(PROJECT_PROJECT_NAME);
+        relationshipData.setRelationshipValue(project.getProjectName());
+
+        relationshipDataList.add(relationshipData);
+
+
+        return relationShip;
+    }
+
 }