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