Merge "Adjust SDC-DCAE-DS to integration test changes"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / GenericVnfCacheServiceProviderImpl.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.GENERIC_VNF_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
24 import static org.onap.so.aaisimulator.utils.Constants.COMPOSED_OF;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
27 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
28 import java.util.Optional;
29 import java.util.concurrent.ConcurrentHashMap;
30 import org.onap.aai.domain.yang.GenericVnf;
31 import org.onap.aai.domain.yang.RelatedToProperty;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.RelationshipData;
34 import org.onap.aai.domain.yang.RelationshipList;
35 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.cache.Cache;
40 import org.springframework.cache.CacheManager;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.stereotype.Service;
43 import org.springframework.web.util.UriComponentsBuilder;
44
45 /**
46  * @author Waqas Ikram (waqas.ikram@est.tech)
47  *
48  */
49 @Service
50 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
51         implements GenericVnfCacheServiceProvider {
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
54
55     private final HttpRestServiceProvider httpRestServiceProvider;
56
57     @Autowired
58     public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
59             final HttpRestServiceProvider httpRestServiceProvider) {
60         super(cacheManager);
61         this.httpRestServiceProvider = httpRestServiceProvider;
62     }
63
64     @Override
65     public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
66         LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
67         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
68         cache.put(vnfId, genericVnf);
69     }
70
71     @Override
72     public Optional<GenericVnf> getGenericVnf(final String vnfId) {
73         LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
74         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
75         final GenericVnf value = cache.get(vnfId, GenericVnf.class);
76         if (value != null) {
77             return Optional.of(value);
78         }
79         LOGGER.error("Unable to find GenericVnf ...");
80         return Optional.empty();
81     }
82
83     @Override
84     public Optional<String> getGenericVnfId(final String vnfName) {
85         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
86         if (cache != null) {
87             final Object nativeCache = cache.getNativeCache();
88             if (nativeCache instanceof ConcurrentHashMap) {
89                 @SuppressWarnings("unchecked")
90                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
91                         (ConcurrentHashMap<Object, Object>) nativeCache;
92                 for (final Object key : concurrentHashMap.keySet()) {
93                     final GenericVnf value = cache.get(key, GenericVnf.class);
94                     final String genericVnfName = value.getVnfName();
95                     if (value != null && genericVnfName.equals(vnfName)) {
96                         final String genericVnfId = value.getVnfId();
97                         LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
98                         return Optional.of(genericVnfId);
99                     }
100                 }
101             }
102         }
103         LOGGER.info("No match found for vnf name: {}", vnfName);
104         return Optional.empty();
105     }
106
107     @Override
108     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
109             final String requestUriString, final String vnfId, final Relationship relationship) {
110         try {
111             final Optional<GenericVnf> optional = getGenericVnf(vnfId);
112             if (optional.isPresent()) {
113                 final GenericVnf genericVnf = optional.get();
114                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
115                 final Relationship outGoingRelationShip = getRelationship(requestUriString, genericVnf);
116                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
117                         outGoingRelationShip, targetUrl, Relationship.class);
118                 if (optionalRelationship.isPresent()) {
119                     final Relationship resultantRelationship = optionalRelationship.get();
120
121                     RelationshipList relationshipList = genericVnf.getRelationshipList();
122                     if (relationshipList == null) {
123                         relationshipList = new RelationshipList();
124                         genericVnf.setRelationshipList(relationshipList);
125                     }
126                     if (relationshipList.getRelationship().add(resultantRelationship)) {
127                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
128                         return true;
129                     }
130                 }
131             }
132         } catch (final Exception exception) {
133             LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
134         }
135         LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
136         return false;
137     }
138
139     @Override
140     public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
141             final String requestURI) {
142         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
143         if (optional.isPresent()) {
144             final GenericVnf genericVnf = optional.get();
145             RelationshipList relationshipList = genericVnf.getRelationshipList();
146             if (relationshipList == null) {
147                 relationshipList = new RelationshipList();
148                 genericVnf.setRelationshipList(relationshipList);
149             }
150             relationshipList.getRelationship().add(relationship);
151             LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
152
153             final Relationship resultantRelationship = getRelationship(requestURI, genericVnf);
154             return Optional.of(resultantRelationship);
155         }
156         return Optional.empty();
157     }
158
159     @Override
160     public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
161         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
162         if (optional.isPresent()) {
163             final GenericVnf cachedGenericVnf = optional.get();
164             LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedGenericVnf.getOrchestrationStatus(),
165                     genericVnf.getOrchestrationStatus());
166             cachedGenericVnf.setOrchestrationStatus(genericVnf.getOrchestrationStatus());
167             return true;
168         }
169         LOGGER.error("Unable to find GenericVnf ...");
170         return false;
171     }
172
173     private String getTargetUrl(final String targetBaseUrl, final String relatedLink) {
174         return UriComponentsBuilder.fromUriString(targetBaseUrl).path(relatedLink)
175                 .path(BI_DIRECTIONAL_RELATIONSHIP_LIST_URL).toUriString();
176     }
177
178     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
179         final Relationship relationShip = new Relationship();
180         relationShip.setRelatedTo(GENERIC_VNF);
181         relationShip.setRelationshipLabel(COMPOSED_OF);
182         relationShip.setRelatedLink(relatedLink);
183
184         final RelationshipData relationshipData = new RelationshipData();
185         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
186         relationshipData.setRelationshipValue(genericVnf.getVnfId());
187         relationShip.getRelationshipData().add(relationshipData);
188
189         final RelatedToProperty relatedToProperty = new RelatedToProperty();
190         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
191         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
192         relationShip.getRelatedToProperty().add(relatedToProperty);
193         return relationShip;
194     }
195
196     @Override
197     public void clearAll() {
198         clearCahce(GENERIC_VNF_CACHE.getName());
199     }
200
201 }