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.PLATFORM_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.PLATFORM;
24 import static org.onap.so.aaisimulator.utils.Constants.PLATFORM_PLATFORM_NAME;
25 import static org.onap.so.aaisimulator.utils.Constants.USES;
26 import java.util.Optional;
27 import org.onap.aai.domain.yang.Platform;
28 import org.onap.aai.domain.yang.Relationship;
29 import org.onap.aai.domain.yang.RelationshipData;
30 import org.onap.aai.domain.yang.RelationshipList;
31 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.cache.Cache;
36 import org.springframework.cache.CacheManager;
37 import org.springframework.stereotype.Service;
40 * @author Waqas Ikram (waqas.ikram@est.tech)
44 public class PlatformCacheServiceProviderImpl extends AbstractCacheServiceProvider
45 implements PlatformCacheServiceProvider {
47 private static final Logger LOGGER = LoggerFactory.getLogger(PlatformCacheServiceProviderImpl.class);
50 public PlatformCacheServiceProviderImpl(final CacheManager cacheManager) {
55 public void putPlatform(final String platformName, final Platform platform) {
56 LOGGER.info("Adding Platform to cache with key: {} ...", platformName);
57 final Cache cache = getCache(PLATFORM_CACHE.getName());
58 cache.put(platformName, platform);
62 public Optional<Platform> getPlatform(final String platformName) {
63 LOGGER.info("getting Platform from cache using key: {}", platformName);
64 final Cache cache = getCache(PLATFORM_CACHE.getName());
65 final Platform value = cache.get(platformName, Platform.class);
67 return Optional.of(value);
69 LOGGER.error("Unable to find Platform in cache using key:{} ", platformName);
70 return Optional.empty();
74 public Optional<Relationship> addRelationShip(final String platformName, final Relationship relationship,
75 final String requestUri) {
76 final Optional<Platform> optional = getPlatform(platformName);
77 if (optional.isPresent()) {
78 final Platform platform = optional.get();
79 RelationshipList relationshipList = platform.getRelationshipList();
80 if (relationshipList == null) {
81 relationshipList = new RelationshipList();
82 platform.setRelationshipList(relationshipList);
84 relationshipList.getRelationship().add(relationship);
86 LOGGER.info("Successfully add relation to Platform with name: {}", platformName);
88 final Relationship resultantRelationship = new Relationship();
89 resultantRelationship.setRelatedTo(PLATFORM);
90 resultantRelationship.setRelationshipLabel(USES);
91 resultantRelationship.setRelatedLink(requestUri);
93 final RelationshipData relationshipData = new RelationshipData();
94 relationshipData.setRelationshipKey(PLATFORM_PLATFORM_NAME);
95 relationshipData.setRelationshipValue(platform.getPlatformName());
96 resultantRelationship.getRelationshipData().add(relationshipData);
98 return Optional.of(resultantRelationship);
100 LOGGER.error("Unable to find Platform ...");
101 return Optional.empty();
105 public void clearAll() {
106 clearCahce(PLATFORM_CACHE.getName());