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.jetbrains.annotations.NotNull;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.*;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository;
\r
29 import org.springframework.stereotype.Service;
\r
30 import reactor.core.publisher.Mono;
\r
32 import java.util.Optional;
\r
35 * BluePrintRepoDBService
\r
37 * @author Brinda Santh
\r
40 @SuppressWarnings("unused")
\r
41 public class BluePrintRepoDBService implements BluePrintRepoService {
\r
43 private ModelTypeRepository modelTypeRepository;
\r
44 @SuppressWarnings("unused")
\r
45 public BluePrintRepoDBService(ModelTypeRepository modelTypeRepository) {
\r
46 this.modelTypeRepository = modelTypeRepository;
\r
50 public Mono<NodeType> getNodeType(@NotNull String nodeTypeName) throws BluePrintException {
\r
51 return getModelType(nodeTypeName, NodeType.class);
\r
55 public Mono<DataType> getDataType(@NotNull String dataTypeName) throws BluePrintException {
\r
56 return getModelType(dataTypeName, DataType.class);
\r
60 public Mono<ArtifactType> getArtifactType(@NotNull String artifactTypeName) throws BluePrintException {
\r
61 return getModelType(artifactTypeName, ArtifactType.class);
\r
65 public Mono<RelationshipType> getRelationshipType(@NotNull String relationshipTypeName) throws BluePrintException {
\r
66 return getModelType(relationshipTypeName, RelationshipType.class);
\r
70 public Mono<CapabilityDefinition> getCapabilityDefinition(@NotNull String capabilityDefinitionName) throws BluePrintException {
\r
71 return getModelType(capabilityDefinitionName, CapabilityDefinition.class);
\r
74 private <T> Mono<T> getModelType(String modelName, Class<T> valueClass) throws BluePrintException {
\r
75 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
\r
76 "Failed to get model from repo, model name is missing");
\r
78 return getModelDefinition(modelName).map(content -> {
\r
79 Preconditions.checkArgument(StringUtils.isNotBlank(content),
\r
80 String.format("Failed to get model content for model name (%s)", modelName));
\r
81 return JacksonUtils.readValue(content, valueClass);
\r
86 private Mono<String> getModelDefinition(String modelName) throws BluePrintException {
\r
87 String modelDefinition;
\r
88 Optional<ModelType> modelTypeDb = modelTypeRepository.findByModelName(modelName);
\r
89 if (modelTypeDb.isPresent()) {
\r
90 modelDefinition = modelTypeDb.get().getDefinition();
\r
92 throw new BluePrintException(String.format("failed to get model definition (%s) from repo", modelName));
\r
94 return Mono.just(modelDefinition);
\r