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
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.service.providers;
21
22 import static org.onap.so.aaisimulator.utils.CacheName.PROJECT_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.PROJECT;
24 import static org.onap.so.aaisimulator.utils.Constants.PROJECT_PROJECT_NAME;
25 import static org.onap.so.aaisimulator.utils.Constants.USES;
26 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
27 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
28 import java.util.List;
29 import java.util.Optional;
30 import org.onap.aai.domain.yang.Project;
31 import org.onap.aai.domain.yang.Relationship;
32 import org.onap.aai.domain.yang.RelationshipData;
33 import org.onap.aai.domain.yang.RelationshipList;
34 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.cache.Cache;
39 import org.springframework.cache.CacheManager;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.stereotype.Service;
42
43 /**
44  * @author waqas.ikram@ericsson.com
45  *
46  */
47 @Service
48 public class ProjectCacheServiceProviderImpl extends AbstractCacheServiceProvider
49         implements ProjectCacheServiceProvider {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(ProjectCacheServiceProviderImpl.class);
52
53     private final HttpRestServiceProvider httpRestServiceProvider;
54
55     @Autowired
56     public ProjectCacheServiceProviderImpl(final CacheManager cacheManager,
57             final HttpRestServiceProvider httpRestServiceProvider) {
58         super(cacheManager);
59         this.httpRestServiceProvider = httpRestServiceProvider;
60     }
61
62     @Override
63     public void putProject(final String projectName, final Project project) {
64         LOGGER.info("Adding project: {} with name to cache", project, projectName);
65         final Cache cache = getCache(PROJECT_CACHE.getName());
66         cache.put(projectName, project);
67     }
68
69
70     @Override
71     public Optional<Project> getProject(final String projectName) {
72         LOGGER.info("getting project from cache using key: {}", projectName);
73         final Cache cache = getCache(PROJECT_CACHE.getName());
74         final Project value = cache.get(projectName, Project.class);
75         if (value != null) {
76             return Optional.of(value);
77         }
78         return Optional.empty();
79     }
80
81     @Override
82     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
83             final String requestUriString, final String projectName, final Relationship relationship) {
84         try {
85             final Optional<Project> optional = getProject(projectName);
86
87             if (optional.isPresent()) {
88                 final Project project = optional.get();
89                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
90                 final Relationship outGoingRelationShip = getRelationship(requestUriString, project);
91
92                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
93                         outGoingRelationShip, targetUrl, Relationship.class);
94
95                 if (optionalRelationship.isPresent()) {
96                     final Relationship resultantRelationship = optionalRelationship.get();
97
98                     RelationshipList relationshipList = project.getRelationshipList();
99                     if (relationshipList == null) {
100                         relationshipList = new RelationshipList();
101                         project.setRelationshipList(relationshipList);
102                     }
103                     if (relationshipList.getRelationship().add(resultantRelationship)) {
104                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
105                         return true;
106                     }
107                 }
108             }
109         } catch (final Exception exception) {
110             LOGGER.error("Unable to add two-way relationship for project name: {}", projectName, exception);
111         }
112         LOGGER.error("Unable to add relationship in cache for project name: {}", projectName);
113         return false;
114     }
115
116     @Override
117     public void clearAll() {
118         clearCache(PROJECT_CACHE.getName());
119     }
120
121     private Relationship getRelationship(final String requestUriString, final Project project) {
122
123         final Relationship relationShip = new Relationship();
124         relationShip.setRelatedTo(PROJECT);
125         relationShip.setRelationshipLabel(USES);
126         relationShip.setRelatedLink(getRelationShipListRelatedLink(requestUriString));
127
128         final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
129
130         final RelationshipData relationshipData = new RelationshipData();
131         relationshipData.setRelationshipKey(PROJECT_PROJECT_NAME);
132         relationshipData.setRelationshipValue(project.getProjectName());
133
134         relationshipDataList.add(relationshipData);
135
136
137         return relationShip;
138     }
139
140 }