58a8b1e69dfb577ccbbe13add10cd164caa91b54
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / OwnEntityCacheServiceProviderImpl.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.Constants.OWNING_ENTITY_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
24 import java.util.Optional;
25 import org.onap.aai.domain.yang.OwningEntity;
26 import org.onap.aai.domain.yang.Relationship;
27 import org.onap.aai.domain.yang.RelationshipList;
28 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.cache.Cache;
33 import org.springframework.cache.CacheManager;
34 import org.springframework.stereotype.Service;
35
36 /**
37  * @author waqas.ikram@ericsson.com
38  *
39  */
40 @Service
41 public class OwnEntityCacheServiceProviderImpl extends AbstractCacheServiceProvider
42         implements OwnEntityCacheServiceProvider {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(OwnEntityCacheServiceProviderImpl.class);
45     private static final String RELATIONSHIPS_LABEL = "org.onap.relationships.inventory.BelongsTo";
46
47     @Autowired
48     public OwnEntityCacheServiceProviderImpl(final CacheManager cacheManager) {
49         super(cacheManager);
50     }
51
52     @Override
53     public void putOwningEntity(final String owningEntityId, final OwningEntity owningEntity) {
54         LOGGER.info("Adding OwningEntity: {} with name to cache", owningEntityId, owningEntity);
55         final Cache cache = getCache(OWNING_ENTITY_CACHE);
56         cache.put(owningEntityId, owningEntity);
57     }
58
59     @Override
60     public Optional<OwningEntity> getOwningEntity(final String owningEntityId) {
61         LOGGER.info("getting OwningEntity from cache using key: {}", owningEntityId);
62         final Cache cache = getCache(OWNING_ENTITY_CACHE);
63         final OwningEntity value = cache.get(owningEntityId, OwningEntity.class);
64         if (value != null) {
65             return Optional.of(value);
66         }
67         return Optional.empty();
68     }
69
70     @Override
71     public boolean putOwningEntityRelationShip(final String owningEntityId, final Relationship relationship) {
72         final Cache cache = getCache(OWNING_ENTITY_CACHE);
73         final OwningEntity value = cache.get(owningEntityId, OwningEntity.class);
74         if (value != null) {
75             RelationshipList relationshipList = value.getRelationshipList();
76             if (relationshipList == null) {
77                 relationshipList = new RelationshipList();
78                 value.setRelationshipList(relationshipList);
79             }
80
81             if (relationship.getRelatedTo() == null) {
82                 relationship.setRelatedTo(SERVICE_RESOURCE_TYPE);
83             }
84             if (relationship.getRelationshipLabel() == null) {
85                 relationship.setRelationshipLabel(RELATIONSHIPS_LABEL);
86             }
87
88             return relationshipList.getRelationship().add(relationship);
89         }
90         LOGGER.error("OwningEntity not found in cache for {}", owningEntityId);
91         return false;
92     }
93
94     @Override
95     public void clearAll() {
96         clearCahce(OWNING_ENTITY_CACHE);
97     }
98
99 }