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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  20 package org.onap.so.aaisimulator.service.providers;
 
  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;
 
  44  * @author waqas.ikram@ericsson.com
 
  48 public class OwnEntityCacheServiceProviderImpl extends AbstractCacheServiceProvider
 
  49         implements OwnEntityCacheServiceProvider {
 
  51     private static final Logger LOGGER = LoggerFactory.getLogger(OwnEntityCacheServiceProviderImpl.class);
 
  53     private final HttpRestServiceProvider httpRestServiceProvider;
 
  57     public OwnEntityCacheServiceProviderImpl(final CacheManager cacheManager,
 
  58             final HttpRestServiceProvider httpRestServiceProvider) {
 
  60         this.httpRestServiceProvider = httpRestServiceProvider;
 
  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);
 
  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);
 
  76             return Optional.of(value);
 
  78         return Optional.empty();
 
  82     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
 
  83             final String requestUriString, final String owningEntityId, final Relationship relationship) {
 
  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);
 
  91                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
 
  92                         outGoingRelationShip, targetUrl, Relationship.class);
 
  94                 if (optionalRelationship.isPresent()) {
 
  95                     final Relationship resultantRelationship = optionalRelationship.get();
 
  97                     RelationshipList relationshipList = owningEntity.getRelationshipList();
 
  98                     if (relationshipList == null) {
 
  99                         relationshipList = new RelationshipList();
 
 100                         owningEntity.setRelationshipList(relationshipList);
 
 102                     if (relationshipList.getRelationship().add(resultantRelationship)) {
 
 103                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
 
 109         } catch (final Exception exception) {
 
 110             LOGGER.error("Unable to add two-way relationship for owning entity id: {}", owningEntityId, exception);
 
 112         LOGGER.error("Unable to add relationship in cache for owning entity id: {}", owningEntityId);
 
 117     public void clearAll() {
 
 118         clearCahce(OWNING_ENTITY_CACHE.getName());
 
 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));
 
 127         final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
 
 129         final RelationshipData relationshipData = new RelationshipData();
 
 130         relationshipData.setRelationshipKey(OWNING_ENTITY_OWNING_ENTITY_ID);
 
 131         relationshipData.setRelationshipValue(owningEntity.getOwningEntityId());
 
 133         relationshipDataList.add(relationshipData);