Adding tenant endpoints
[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 boolean addRelationShip(final String vnfId, final Relationship relationship) {
85         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
86         if (optional.isPresent()) {
87             final GenericVnf genericVnf = optional.get();
88             RelationshipList relationshipList = genericVnf.getRelationshipList();
89             if (relationshipList == null) {
90                 relationshipList = new RelationshipList();
91                 genericVnf.setRelationshipList(relationshipList);
92             }
93             return relationshipList.getRelationship().add(relationship);
94         }
95         LOGGER.error("Unable to find GenericVnf ...");
96         return false;
97     }
98
99     @Override
100     public Optional<String> getGenericVnfId(final String vnfName) {
101         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
102         if (cache != null) {
103             final Object nativeCache = cache.getNativeCache();
104             if (nativeCache instanceof ConcurrentHashMap) {
105                 @SuppressWarnings("unchecked")
106                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
107                         (ConcurrentHashMap<Object, Object>) nativeCache;
108                 for (final Object key : concurrentHashMap.keySet()) {
109                     final GenericVnf value = cache.get(key, GenericVnf.class);
110                     final String genericVnfName = value.getVnfName();
111                     if (value != null && genericVnfName.equals(vnfName)) {
112                         final String genericVnfId = value.getVnfId();
113                         LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
114                         return Optional.of(genericVnfId);
115                     }
116                 }
117             }
118         }
119         LOGGER.info("No match found for vnf name: {}", vnfName);
120         return Optional.empty();
121     }
122
123     @Override
124     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
125             final String requestUriString, final String vnfId, final Relationship relationship) {
126         try {
127             final Optional<GenericVnf> optional = getGenericVnf(vnfId);
128             if (optional.isPresent()) {
129                 final GenericVnf genericVnf = optional.get();
130                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
131                 final Relationship outGoingRelationShip = getRelationship(requestUriString, genericVnf);
132                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
133                         outGoingRelationShip, targetUrl, Relationship.class);
134                 if (optionalRelationship.isPresent()) {
135                     final Relationship resultantRelationship = optionalRelationship.get();
136                     if (addRelationShip(vnfId, resultantRelationship)) {
137                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
138                         return true;
139                     }
140                 }
141             }
142         } catch (final Exception exception) {
143             LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
144         }
145         LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
146         return false;
147     }
148
149     private String getTargetUrl(final String targetBaseUrl, final String relatedLink) {
150         return UriComponentsBuilder.fromUriString(targetBaseUrl).path(relatedLink)
151                 .path(BI_DIRECTIONAL_RELATIONSHIP_LIST_URL).toUriString();
152     }
153
154     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
155         final Relationship relationShip = new Relationship();
156         relationShip.setRelatedTo(GENERIC_VNF);
157         relationShip.setRelationshipLabel(COMPOSED_OF);
158         relationShip.setRelatedLink(relatedLink);
159
160         final RelationshipData relationshipData = new RelationshipData();
161         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
162         relationshipData.setRelationshipValue(genericVnf.getVnfId());
163         relationShip.getRelationshipData().add(relationshipData);
164
165         final RelatedToProperty relatedToProperty = new RelatedToProperty();
166         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
167         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
168         relationShip.getRelatedToProperty().add(relatedToProperty);
169         return relationShip;
170     }
171
172     @Override
173     public void clearAll() {
174         clearCahce(GENERIC_VNF_CACHE.getName());
175     }
176
177 }