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.GENERIC_VNF_CACHE;
 
  23 import static org.onap.so.aaisimulator.utils.Constants.COMPOSED_OF;
 
  24 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
 
  25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
 
  26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
 
  27 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
 
  28 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
 
  29 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
 
  30 import java.util.Optional;
 
  31 import java.util.concurrent.ConcurrentHashMap;
 
  32 import org.onap.aai.domain.yang.GenericVnf;
 
  33 import org.onap.aai.domain.yang.RelatedToProperty;
 
  34 import org.onap.aai.domain.yang.Relationship;
 
  35 import org.onap.aai.domain.yang.RelationshipData;
 
  36 import org.onap.aai.domain.yang.RelationshipList;
 
  37 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
 
  38 import org.slf4j.Logger;
 
  39 import org.slf4j.LoggerFactory;
 
  40 import org.springframework.beans.factory.annotation.Autowired;
 
  41 import org.springframework.cache.Cache;
 
  42 import org.springframework.cache.CacheManager;
 
  43 import org.springframework.http.HttpHeaders;
 
  44 import org.springframework.stereotype.Service;
 
  47  * @author Waqas Ikram (waqas.ikram@est.tech)
 
  51 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
 
  52         implements GenericVnfCacheServiceProvider {
 
  54     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
 
  56     private final HttpRestServiceProvider httpRestServiceProvider;
 
  59     public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
 
  60             final HttpRestServiceProvider httpRestServiceProvider) {
 
  62         this.httpRestServiceProvider = httpRestServiceProvider;
 
  66     public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
 
  67         LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
 
  68         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
 
  69         cache.put(vnfId, genericVnf);
 
  73     public Optional<GenericVnf> getGenericVnf(final String vnfId) {
 
  74         LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
 
  75         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
 
  76         final GenericVnf value = cache.get(vnfId, GenericVnf.class);
 
  78             return Optional.of(value);
 
  80         LOGGER.error("Unable to find GenericVnf ...");
 
  81         return Optional.empty();
 
  85     public Optional<String> getGenericVnfId(final String vnfName) {
 
  86         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
 
  88             final Object nativeCache = cache.getNativeCache();
 
  89             if (nativeCache instanceof ConcurrentHashMap) {
 
  90                 @SuppressWarnings("unchecked")
 
  91                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
 
  92                         (ConcurrentHashMap<Object, Object>) nativeCache;
 
  93                 for (final Object key : concurrentHashMap.keySet()) {
 
  94                     final GenericVnf value = cache.get(key, GenericVnf.class);
 
  95                     final String genericVnfName = value.getVnfName();
 
  96                     if (value != null && genericVnfName.equals(vnfName)) {
 
  97                         final String genericVnfId = value.getVnfId();
 
  98                         LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
 
  99                         return Optional.of(genericVnfId);
 
 104         LOGGER.info("No match found for vnf name: {}", vnfName);
 
 105         return Optional.empty();
 
 109     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
 
 110             final String requestUriString, final String vnfId, final Relationship relationship) {
 
 112             final Optional<GenericVnf> optional = getGenericVnf(vnfId);
 
 113             if (optional.isPresent()) {
 
 114                 final GenericVnf genericVnf = optional.get();
 
 115                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
 
 116                 final Relationship outGoingRelationShip =
 
 117                         getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf);
 
 118                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
 
 119                         outGoingRelationShip, targetUrl, Relationship.class);
 
 120                 if (optionalRelationship.isPresent()) {
 
 121                     final Relationship resultantRelationship = optionalRelationship.get();
 
 123                     RelationshipList relationshipList = genericVnf.getRelationshipList();
 
 124                     if (relationshipList == null) {
 
 125                         relationshipList = new RelationshipList();
 
 126                         genericVnf.setRelationshipList(relationshipList);
 
 128                     if (relationshipList.getRelationship().add(resultantRelationship)) {
 
 129                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
 
 134         } catch (final Exception exception) {
 
 135             LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
 
 137         LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
 
 142     public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
 
 143             final String requestURI) {
 
 144         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
 
 145         if (optional.isPresent()) {
 
 146             final GenericVnf genericVnf = optional.get();
 
 147             RelationshipList relationshipList = genericVnf.getRelationshipList();
 
 148             if (relationshipList == null) {
 
 149                 relationshipList = new RelationshipList();
 
 150                 genericVnf.setRelationshipList(relationshipList);
 
 152             relationshipList.getRelationship().add(relationship);
 
 153             LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
 
 155             final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
 
 156             final Relationship resultantRelationship = getRelationship(relatedLink, genericVnf);
 
 157             return Optional.of(resultantRelationship);
 
 159         return Optional.empty();
 
 163     public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
 
 164         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
 
 165         if (optional.isPresent()) {
 
 166             final GenericVnf cachedGenericVnf = optional.get();
 
 167             LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedGenericVnf.getOrchestrationStatus(),
 
 168                     genericVnf.getOrchestrationStatus());
 
 169             cachedGenericVnf.setOrchestrationStatus(genericVnf.getOrchestrationStatus());
 
 172         LOGGER.error("Unable to find GenericVnf ...");
 
 176     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
 
 177         final Relationship relationShip = new Relationship();
 
 178         relationShip.setRelatedTo(GENERIC_VNF);
 
 179         relationShip.setRelationshipLabel(COMPOSED_OF);
 
 180         relationShip.setRelatedLink(relatedLink);
 
 182         final RelationshipData relationshipData = new RelationshipData();
 
 183         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
 
 184         relationshipData.setRelationshipValue(genericVnf.getVnfId());
 
 185         relationShip.getRelationshipData().add(relationshipData);
 
 187         final RelatedToProperty relatedToProperty = new RelatedToProperty();
 
 188         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
 
 189         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
 
 190         relationShip.getRelatedToProperty().add(relatedToProperty);
 
 195     public void clearAll() {
 
 196         clearCahce(GENERIC_VNF_CACHE.getName());