Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases / pnf-sw-upgrade / so / simulator / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / ExternalSystemCacheServiceProviderImpl.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.ESR_VNFM_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.DEPENDS_ON;
24 import static org.onap.so.aaisimulator.utils.Constants.ESR_VNFM;
25 import static org.onap.so.aaisimulator.utils.Constants.ESR_VNFM_VNFM_ID;
26 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
27 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.concurrent.ConcurrentHashMap;
33 import org.onap.aai.domain.yang.EsrSystemInfo;
34 import org.onap.aai.domain.yang.EsrSystemInfoList;
35 import org.onap.aai.domain.yang.EsrVnfm;
36 import org.onap.aai.domain.yang.Relationship;
37 import org.onap.aai.domain.yang.RelationshipData;
38 import org.onap.aai.domain.yang.RelationshipList;
39 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.cache.Cache;
44 import org.springframework.cache.CacheManager;
45 import org.springframework.http.HttpHeaders;
46 import org.springframework.stereotype.Service;
47
48 /**
49  * @author Waqas Ikram (waqas.ikram@est.tech)
50  *
51  */
52 @Service
53 public class ExternalSystemCacheServiceProviderImpl extends AbstractCacheServiceProvider
54         implements ExternalSystemCacheServiceProvider {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemCacheServiceProviderImpl.class);
57
58     private final HttpRestServiceProvider httpRestServiceProvider;
59
60     @Autowired
61     public ExternalSystemCacheServiceProviderImpl(final CacheManager cacheManager,
62             final HttpRestServiceProvider httpRestServiceProvider) {
63         super(cacheManager);
64         this.httpRestServiceProvider = httpRestServiceProvider;
65
66     }
67
68     @Override
69     public void putEsrVnfm(final String vnfmId, final EsrVnfm esrVnfm) {
70         LOGGER.info("Adding esrVnfm: {} with name to cache", esrVnfm);
71         final Cache cache = getCache(ESR_VNFM_CACHE.getName());
72         cache.put(vnfmId, esrVnfm);
73     }
74
75     @Override
76     public Optional<EsrVnfm> getEsrVnfm(final String vnfmId) {
77         LOGGER.info("getting EsrVnfm from cache using key: {}", vnfmId);
78         final Cache cache = getCache(ESR_VNFM_CACHE.getName());
79         final EsrVnfm value = cache.get(vnfmId, EsrVnfm.class);
80         if (value != null) {
81             return Optional.of(value);
82         }
83         LOGGER.error("Unable to find EsrVnfm in cache using vnfmId: {} ", vnfmId);
84         return Optional.empty();
85     }
86
87     @Override
88     public List<EsrVnfm> getAllEsrVnfm() {
89         final Cache cache = getCache(ESR_VNFM_CACHE.getName());
90         if (cache != null) {
91             final Object nativeCache = cache.getNativeCache();
92             if (nativeCache instanceof ConcurrentHashMap) {
93                 @SuppressWarnings("unchecked")
94                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
95                         (ConcurrentHashMap<Object, Object>) nativeCache;
96                 final List<EsrVnfm> result = new ArrayList<>();
97                 concurrentHashMap.keySet().stream().forEach(key -> {
98                     final Optional<EsrVnfm> optional = getEsrVnfm(key.toString());
99                     if (optional.isPresent()) {
100                         result.add(optional.get());
101                     }
102                 });
103                 return result;
104             }
105         }
106         LOGGER.error("Unable to get all esr vnfms ... ");
107         return Collections.emptyList();
108
109     }
110
111     @Override
112     public Optional<EsrSystemInfoList> getEsrSystemInfoList(final String vnfmId) {
113         final Optional<EsrVnfm> optional = getEsrVnfm(vnfmId);
114         if (optional.isPresent()) {
115             final EsrVnfm esrVnfm = optional.get();
116             if (esrVnfm.getEsrSystemInfoList() != null) {
117                 return Optional.of(esrVnfm.getEsrSystemInfoList());
118             }
119             LOGGER.error("EsrSystemInfoList is null for vnfmId: {} ", vnfmId);
120         }
121         LOGGER.error("Unable to find EsrVnfm in cache using vnfmId: {} ", vnfmId);
122         return Optional.empty();
123     }
124
125     @Override
126     public boolean putEsrSystemInfo(final String vnfmId, final String esrSystemInfoId,
127             final EsrSystemInfo esrSystemInfo) {
128         final Optional<EsrVnfm> optional = getEsrVnfm(vnfmId);
129         if (optional.isPresent()) {
130             final EsrVnfm esrVnfm = optional.get();
131             final List<EsrSystemInfo> esrSystemInfoList = getEsrSystemInfoList(esrVnfm);
132
133             final Optional<EsrSystemInfo> existingEsrSystemInfo =
134                     esrSystemInfoList.stream().filter(existing -> existing.getEsrSystemInfoId() != null
135                             && existing.getEsrSystemInfoId().equals(esrSystemInfoId)).findFirst();
136             if (existingEsrSystemInfo.isPresent()) {
137                 LOGGER.error("EsrSystemInfo already exists {}", existingEsrSystemInfo.get());
138                 return false;
139             }
140
141             return esrSystemInfoList.add(esrSystemInfo);
142         }
143         LOGGER.error("Unable to add EsrSystemInfo in cache for vnfmId: {} ", vnfmId);
144         return false;
145     }
146
147     @Override
148     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
149             final String requestUriString, final String vnfmId, final Relationship relationship) {
150         try {
151             final Optional<EsrVnfm> optional = getEsrVnfm(vnfmId);
152             if (optional.isPresent()) {
153                 final EsrVnfm esrVnfm = optional.get();
154                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
155                 final Relationship outGoingRelationShip =
156                         getRelationship(getRelationShipListRelatedLink(requestUriString), esrVnfm);
157                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
158                         outGoingRelationShip, targetUrl, Relationship.class);
159                 if (optionalRelationship.isPresent()) {
160                     final Relationship resultantRelationship = optionalRelationship.get();
161
162                     RelationshipList relationshipList = esrVnfm.getRelationshipList();
163                     if (relationshipList == null) {
164                         relationshipList = new RelationshipList();
165                         esrVnfm.setRelationshipList(relationshipList);
166                     }
167                     if (relationshipList.getRelationship().add(resultantRelationship)) {
168                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
169                         return true;
170                     }
171                 }
172             }
173         } catch (final Exception exception) {
174             LOGGER.error("Unable to add two-way relationship for vnfmId: {}", vnfmId, exception);
175         }
176         LOGGER.error("Unable to add relationship in cache for vnfmId: {}", vnfmId);
177         return false;
178     }
179
180     private Relationship getRelationship(final String relatedLink, final EsrVnfm esrVnfm) {
181         final Relationship relationShip = new Relationship();
182         relationShip.setRelatedTo(ESR_VNFM);
183         relationShip.setRelationshipLabel(DEPENDS_ON);
184         relationShip.setRelatedLink(relatedLink);
185
186         final RelationshipData relationshipData = new RelationshipData();
187         relationshipData.setRelationshipKey(ESR_VNFM_VNFM_ID);
188         relationshipData.setRelationshipValue(esrVnfm.getVnfmId());
189         relationShip.getRelationshipData().add(relationshipData);
190
191         return relationShip;
192     }
193
194     private List<EsrSystemInfo> getEsrSystemInfoList(final EsrVnfm esrVnfm) {
195         EsrSystemInfoList esrSystemInfoList = esrVnfm.getEsrSystemInfoList();
196         if (esrSystemInfoList == null) {
197             esrSystemInfoList = new EsrSystemInfoList();
198             esrVnfm.setEsrSystemInfoList(esrSystemInfoList);
199         }
200         return esrSystemInfoList.getEsrSystemInfo();
201     }
202
203     @Override
204     public void clearAll() {
205         clearCache(ESR_VNFM_CACHE.getName());
206
207     }
208
209 }