2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.apps.controllerblueprints.service;
\r
20 import com.fasterxml.jackson.databind.JsonNode;
\r
21 import com.google.common.base.Preconditions;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.jetbrains.annotations.NotNull;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.*;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceDefinitionRepoService;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository;
\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository;
\r
33 import org.springframework.stereotype.Service;
\r
34 import reactor.core.publisher.Mono;
\r
36 import java.util.Optional;
\r
39 * ResourceDefinitionRepoDBService
\r
41 * @author Brinda Santh
\r
44 @SuppressWarnings("unused")
\r
45 public class ResourceDefinitionRepoDBService implements ResourceDefinitionRepoService {
\r
47 private ModelTypeRepository modelTypeRepository;
\r
48 private ResourceDictionaryRepository resourceDictionaryRepository;
\r
50 @SuppressWarnings("unused")
\r
51 public ResourceDefinitionRepoDBService(ModelTypeRepository modelTypeRepository,
\r
52 ResourceDictionaryRepository resourceDictionaryRepository) {
\r
53 this.modelTypeRepository = modelTypeRepository;
\r
54 this.resourceDictionaryRepository = resourceDictionaryRepository;
\r
58 public Mono<NodeType> getNodeType(@NotNull String nodeTypeName) throws BluePrintException {
\r
59 return getModelType(nodeTypeName, NodeType.class);
\r
63 public Mono<DataType> getDataType(@NotNull String dataTypeName) throws BluePrintException {
\r
64 return getModelType(dataTypeName, DataType.class);
\r
68 public Mono<ArtifactType> getArtifactType(@NotNull String artifactTypeName) throws BluePrintException {
\r
69 return getModelType(artifactTypeName, ArtifactType.class);
\r
73 public Mono<RelationshipType> getRelationshipType(@NotNull String relationshipTypeName) throws BluePrintException {
\r
74 return getModelType(relationshipTypeName, RelationshipType.class);
\r
78 public Mono<CapabilityDefinition> getCapabilityDefinition(@NotNull String capabilityDefinitionName) throws BluePrintException {
\r
79 return getModelType(capabilityDefinitionName, CapabilityDefinition.class);
\r
84 public Mono<ResourceDefinition> getResourceDefinition(@NotNull String resourceDefinitionName) throws BluePrintException{
\r
85 Optional<ResourceDictionary> dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName);
\r
86 if(dbResourceDictionary.isPresent()){
\r
87 return Mono.just(dbResourceDictionary.get().getDefinition());
\r
89 throw new BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName));
\r
93 private <T> Mono<T> getModelType(String modelName, Class<T> valueClass) throws BluePrintException {
\r
94 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
\r
95 "Failed to get model from repo, model name is missing");
\r
97 return getModelDefinition(modelName).map(modelDefinition -> {
\r
98 Preconditions.checkNotNull(modelDefinition,
\r
99 String.format("Failed to get model content for model name (%s)", modelName));
\r
100 return JacksonUtils.readValue(modelDefinition, valueClass);
\r
105 private Mono<JsonNode> getModelDefinition(String modelName) throws BluePrintException {
\r
106 JsonNode modelDefinition;
\r
107 Optional<ModelType> modelTypeDb = modelTypeRepository.findByModelName(modelName);
\r
108 if (modelTypeDb.isPresent()) {
\r
109 modelDefinition = modelTypeDb.get().getDefinition();
\r
111 throw new BluePrintException(String.format("failed to get model definition (%s) from repo", modelName));
\r
113 return Mono.just(modelDefinition);
\r