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.google.common.base.Preconditions;
\r
21 import org.apache.commons.lang3.StringUtils;
\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.*;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository;
\r
28 import org.springframework.stereotype.Service;
\r
29 import reactor.core.publisher.Mono;
\r
31 import java.util.Optional;
\r
34 * BluePrintRepoDBService
\r
36 * @author Brinda Santh
\r
39 public class BluePrintRepoDBService implements BluePrintRepoService {
\r
41 private ModelTypeRepository modelTypeRepository;
\r
43 public BluePrintRepoDBService(ModelTypeRepository modelTypeRepository) {
\r
44 this.modelTypeRepository = modelTypeRepository;
\r
48 public Mono<NodeType> getNodeType(String nodeTypeName) throws BluePrintException {
\r
49 return getModelType(nodeTypeName, NodeType.class);
\r
53 public Mono<DataType> getDataType(String dataTypeName) throws BluePrintException {
\r
54 return getModelType(dataTypeName, DataType.class);
\r
58 public Mono<ArtifactType> getArtifactType(String artifactTypeName) throws BluePrintException {
\r
59 return getModelType(artifactTypeName, ArtifactType.class);
\r
63 public Mono<RelationshipType> getRelationshipType(String relationshipTypeName) throws BluePrintException {
\r
64 return getModelType(relationshipTypeName, RelationshipType.class);
\r
68 public Mono<CapabilityDefinition> getCapabilityDefinition(String capabilityDefinitionName) throws BluePrintException {
\r
69 return getModelType(capabilityDefinitionName, CapabilityDefinition.class);
\r
72 private <T> Mono<T> getModelType(String modelName, Class<T> valueClass) throws BluePrintException {
\r
73 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
\r
74 "Failed to get model from repo, model name is missing");
\r
76 return getModelDefinitions(modelName).map(content -> {
\r
77 Preconditions.checkArgument(StringUtils.isNotBlank(content),
\r
78 String.format("Failed to get model content for model name (%s)", modelName));
\r
79 return JacksonUtils.readValue(content, valueClass);
\r
84 private Mono<String> getModelDefinitions(String modelName) throws BluePrintException {
\r
85 String modelDefinition;
\r
86 Optional<ModelType> modelTypeDb = modelTypeRepository.findByModelName(modelName);
\r
87 if (modelTypeDb.isPresent()) {
\r
88 modelDefinition = modelTypeDb.get().getDefinition();
\r
90 throw new BluePrintException(String.format("failed to get model definition (%s) from repo", modelName));
\r
92 return Mono.just(modelDefinition);
\r