Seperating usecase test suite dependencies
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / 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.CacheName.OWNING_ENTITY_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.BELONGS_TO;
24 import static org.onap.so.aaisimulator.utils.Constants.OWNING_ENTITY;
25 import static org.onap.so.aaisimulator.utils.Constants.OWNING_ENTITY_OWNING_ENTITY_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.List;
29 import java.util.Optional;
30 import org.onap.aai.domain.yang.OwningEntity;
31 import org.onap.aai.domain.yang.Relationship;
32 import org.onap.aai.domain.yang.RelationshipData;
33 import org.onap.aai.domain.yang.RelationshipList;
34 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.cache.Cache;
39 import org.springframework.cache.CacheManager;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.stereotype.Service;
42
43 /**
44  * @author waqas.ikram@ericsson.com
45  *
46  */
47 @Service
48 public class OwnEntityCacheServiceProviderImpl extends AbstractCacheServiceProvider
49         implements OwnEntityCacheServiceProvider {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(OwnEntityCacheServiceProviderImpl.class);
52
53     private final HttpRestServiceProvider httpRestServiceProvider;
54
55
56     @Autowired
57     public OwnEntityCacheServiceProviderImpl(final CacheManager cacheManager,
58             final HttpRestServiceProvider httpRestServiceProvider) {
59         super(cacheManager);
60         this.httpRestServiceProvider = httpRestServiceProvider;
61     }
62
63     @Override
64     public void putOwningEntity(final String owningEntityId, final OwningEntity owningEntity) {
65         LOGGER.info("Adding OwningEntity: {} with name to cache", owningEntityId, owningEntity);
66         final Cache cache = getCache(OWNING_ENTITY_CACHE.getName());
67         cache.put(owningEntityId, owningEntity);
68     }
69
70     @Override
71     public Optional<OwningEntity> getOwningEntity(final String owningEntityId) {
72         LOGGER.info("getting OwningEntity from cache using key: {}", owningEntityId);
73         final Cache cache = getCache(OWNING_ENTITY_CACHE.getName());
74         final OwningEntity value = cache.get(owningEntityId, OwningEntity.class);
75         if (value != null) {
76             return Optional.of(value);
77         }
78         return Optional.empty();
79     }
80
81     @Override
82     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
83             final String requestUriString, final String owningEntityId, final Relationship relationship) {
84         try {
85             final Optional<OwningEntity> optional = getOwningEntity(owningEntityId);
86             if (optional.isPresent()) {
87                 final OwningEntity owningEntity = optional.get();
88                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
89                 final Relationship outGoingRelationShip = getRelationship(requestUriString, owningEntity);
90
91                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
92                         outGoingRelationShip, targetUrl, Relationship.class);
93
94                 if (optionalRelationship.isPresent()) {
95                     final Relationship resultantRelationship = optionalRelationship.get();
96
97                     RelationshipList relationshipList = owningEntity.getRelationshipList();
98                     if (relationshipList == null) {
99                         relationshipList = new RelationshipList();
100                         owningEntity.setRelationshipList(relationshipList);
101                     }
102                     if (relationshipList.getRelationship().add(resultantRelationship)) {
103                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
104                         return true;
105                     }
106                 }
107             }
108
109         } catch (final Exception exception) {
110             LOGGER.error("Unable to add two-way relationship for owning entity id: {}", owningEntityId, exception);
111         }
112         LOGGER.error("Unable to add relationship in cache for owning entity id: {}", owningEntityId);
113         return false;
114     }
115
116     @Override
117     public void clearAll() {
118         clearCache(OWNING_ENTITY_CACHE.getName());
119     }
120
121     private Relationship getRelationship(final String requestUriString, final OwningEntity owningEntity) {
122         final Relationship relationShip = new Relationship();
123         relationShip.setRelatedTo(OWNING_ENTITY);
124         relationShip.setRelationshipLabel(BELONGS_TO);
125         relationShip.setRelatedLink(getRelationShipListRelatedLink(requestUriString));
126
127         final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
128
129         final RelationshipData relationshipData = new RelationshipData();
130         relationshipData.setRelationshipKey(OWNING_ENTITY_OWNING_ENTITY_ID);
131         relationshipData.setRelationshipValue(owningEntity.getOwningEntityId());
132
133         relationshipDataList.add(relationshipData);
134
135
136         return relationShip;
137     }
138 }