Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / LinesOfBusinessCacheServiceProviderImpl.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.LINES_OF_BUSINESS_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.LINE_OF_BUSINESS;
24 import static org.onap.so.aaisimulator.utils.Constants.LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME;
25 import static org.onap.so.aaisimulator.utils.Constants.USES;
26 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
27 import java.util.Optional;
28 import org.onap.aai.domain.yang.LineOfBusiness;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.aai.domain.yang.RelationshipData;
31 import org.onap.aai.domain.yang.RelationshipList;
32 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.cache.Cache;
37 import org.springframework.cache.CacheManager;
38 import org.springframework.stereotype.Service;
39
40 /**
41  * @author Waqas Ikram (waqas.ikram@est.tech)
42  *
43  */
44 @Service
45 public class LinesOfBusinessCacheServiceProviderImpl extends AbstractCacheServiceProvider
46         implements LinesOfBusinessCacheServiceProvider {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessCacheServiceProviderImpl.class);
49
50     @Autowired
51     public LinesOfBusinessCacheServiceProviderImpl(final CacheManager cacheManager) {
52         super(cacheManager);
53     }
54
55     @Override
56     public void putLineOfBusiness(final String lineOfBusinessName, final LineOfBusiness lineOfBusiness) {
57         LOGGER.info("Adding LineOfBusiness to cache with key: {} ...", lineOfBusinessName);
58         final Cache cache = getCache(LINES_OF_BUSINESS_CACHE.getName());
59         cache.put(lineOfBusinessName, lineOfBusiness);
60
61     }
62
63     @Override
64     public Optional<LineOfBusiness> getLineOfBusiness(final String lineOfBusinessName) {
65         LOGGER.info("getting LineOfBusiness from cache using key: {}", lineOfBusinessName);
66         final Cache cache = getCache(LINES_OF_BUSINESS_CACHE.getName());
67         final LineOfBusiness value = cache.get(lineOfBusinessName, LineOfBusiness.class);
68         if (value != null) {
69             return Optional.of(value);
70         }
71         LOGGER.error("Unable to find LineOfBusiness in cache using key:{} ", lineOfBusinessName);
72         return Optional.empty();
73     }
74
75     @Override
76     public Optional<Relationship> addRelationShip(final String lineOfBusinessName, final Relationship relationship,
77             final String requestUri) {
78         final Optional<LineOfBusiness> optional = getLineOfBusiness(lineOfBusinessName);
79         if (optional.isPresent()) {
80             final LineOfBusiness lineOfBusiness = optional.get();
81             RelationshipList relationshipList = lineOfBusiness.getRelationshipList();
82             if (relationshipList == null) {
83                 relationshipList = new RelationshipList();
84                 lineOfBusiness.setRelationshipList(relationshipList);
85             }
86             relationshipList.getRelationship().add(relationship);
87
88             LOGGER.info("Successfully added relation to LineOfBusiness with name: {}", lineOfBusinessName);
89             final Relationship resultantRelationship = new Relationship();
90             resultantRelationship.setRelatedTo(LINE_OF_BUSINESS);
91             resultantRelationship.setRelationshipLabel(USES);
92             resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
93
94             final RelationshipData relationshipData = new RelationshipData();
95             relationshipData.setRelationshipKey(LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME);
96             relationshipData.setRelationshipValue(lineOfBusiness.getLineOfBusinessName());
97             resultantRelationship.getRelationshipData().add(relationshipData);
98
99             return Optional.of(resultantRelationship);
100
101         }
102         LOGGER.error("Unable to find LineOfBusiness using name: {} ...", lineOfBusinessName);
103         return Optional.empty();
104     }
105
106     @Override
107     public void clearAll() {
108         clearCache(LINES_OF_BUSINESS_CACHE.getName());
109     }
110
111 }