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.aaisimulator.service.providers;
22 import static org.onap.aaisimulator.utils.CacheName.PROJECT_CACHE;
23 import static org.onap.aaisimulator.utils.Constants.PROJECT;
24 import static org.onap.aaisimulator.utils.Constants.PROJECT_PROJECT_NAME;
25 import static org.onap.aaisimulator.utils.Constants.USES;
26 import static org.onap.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
27 import static org.onap.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
28 import java.util.List;
29 import java.util.Optional;
30 import org.onap.aai.domain.yang.Project;
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.aaisimulator.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 ProjectCacheServiceProviderImpl extends AbstractCacheServiceProvider
49 implements ProjectCacheServiceProvider {
51 private static final Logger LOGGER = LoggerFactory.getLogger(ProjectCacheServiceProviderImpl.class);
53 private final HttpRestServiceProvider httpRestServiceProvider;
56 public ProjectCacheServiceProviderImpl(final CacheManager cacheManager,
57 final HttpRestServiceProvider httpRestServiceProvider) {
59 this.httpRestServiceProvider = httpRestServiceProvider;
63 public void putProject(final String projectName, final Project project) {
64 LOGGER.info("Adding project: {} with name to cache", project, projectName);
65 final Cache cache = getCache(PROJECT_CACHE.getName());
66 cache.put(projectName, project);
71 public Optional<Project> getProject(final String projectName) {
72 LOGGER.info("getting project from cache using key: {}", projectName);
73 final Cache cache = getCache(PROJECT_CACHE.getName());
74 final Project value = cache.get(projectName, Project.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 projectName, final Relationship relationship) {
85 final Optional<Project> optional = getProject(projectName);
87 if (optional.isPresent()) {
88 final Project project = optional.get();
89 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
90 final Relationship outGoingRelationShip = getRelationship(requestUriString, project);
92 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
93 outGoingRelationShip, targetUrl, Relationship.class);
95 if (optionalRelationship.isPresent()) {
96 final Relationship resultantRelationship = optionalRelationship.get();
98 RelationshipList relationshipList = project.getRelationshipList();
99 if (relationshipList == null) {
100 relationshipList = new RelationshipList();
101 project.setRelationshipList(relationshipList);
103 if (relationshipList.getRelationship().add(resultantRelationship)) {
104 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
109 } catch (final Exception exception) {
110 LOGGER.error("Unable to add two-way relationship for project name: {}", projectName, exception);
112 LOGGER.error("Unable to add relationship in cache for project name: {}", projectName);
117 public void clearAll() {
118 clearCache(PROJECT_CACHE.getName());
121 private Relationship getRelationship(final String requestUriString, final Project project) {
123 final Relationship relationShip = new Relationship();
124 relationShip.setRelatedTo(PROJECT);
125 relationShip.setRelationshipLabel(USES);
126 relationShip.setRelatedLink(getRelationShipListRelatedLink(requestUriString));
128 final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
130 final RelationshipData relationshipData = new RelationshipData();
131 relationshipData.setRelationshipKey(PROJECT_PROJECT_NAME);
132 relationshipData.setRelationshipValue(project.getProjectName());
134 relationshipDataList.add(relationshipData);