709f80d795e7aa3f404a775961c4bda1d7ae12e7
[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 java.util.Optional;
24 import org.onap.aai.domain.yang.GenericVnf;
25 import org.onap.aai.domain.yang.Relationship;
26 import org.onap.aai.domain.yang.RelationshipList;
27 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.cache.Cache;
32 import org.springframework.cache.CacheManager;
33 import org.springframework.stereotype.Service;
34
35 /**
36  * @author Waqas Ikram (waqas.ikram@est.tech)
37  *
38  */
39 @Service
40 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
41         implements GenericVnfCacheServiceProvider {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
44
45     @Autowired
46     public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager) {
47         super(cacheManager);
48     }
49
50     @Override
51     public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
52         LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
53         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
54         cache.put(vnfId, genericVnf);
55     }
56
57     @Override
58     public Optional<GenericVnf> getGenericVnf(final String vnfId) {
59         LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
60         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
61         final GenericVnf value = cache.get(vnfId, GenericVnf.class);
62         if (value != null) {
63             return Optional.of(value);
64         }
65         LOGGER.error("Unable to find GenericVnf ...");
66         return Optional.empty();
67     }
68
69     @Override
70     public boolean addRelationShip(final String vnfId, final Relationship relationship) {
71         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
72         if (optional.isPresent()) {
73             GenericVnf genericVnf = optional.get();
74             RelationshipList relationshipList = genericVnf.getRelationshipList();
75             if (relationshipList == null) {
76                 relationshipList = new RelationshipList();
77                 genericVnf.setRelationshipList(relationshipList);
78             }
79             return relationshipList.getRelationship().add(relationship);
80         }
81         LOGGER.error("Unable to find GenericVnf ...");
82         return false;
83     }
84
85     @Override
86     public void clearAll() {
87         clearCahce(GENERIC_VNF_CACHE.getName());
88     }
89 }