2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.service;
\r
19 import com.google.common.base.Preconditions;
\r
20 import org.apache.commons.lang3.StringUtils;
\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.data.*;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintEnhancerRepoService;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository;
\r
27 import org.springframework.stereotype.Service;
\r
29 import java.util.Optional;
\r
32 * BluePrintEnhancerRepoDBService
\r
34 * @author Brinda Santh
\r
37 public class BluePrintEnhancerRepoDBService implements BluePrintEnhancerRepoService {
\r
39 private ModelTypeRepository modelTypeRepository;
\r
41 public BluePrintEnhancerRepoDBService(ModelTypeRepository modelTypeRepository) {
\r
42 this.modelTypeRepository = modelTypeRepository;
\r
47 public NodeType getNodeType(String nodeTypeName) throws BluePrintException {
\r
48 Preconditions.checkArgument(StringUtils.isNotBlank(nodeTypeName), "NodeType name is missing");
\r
49 String content = getModelDefinitions(nodeTypeName);
\r
50 Preconditions.checkArgument(StringUtils.isNotBlank(content), "NodeType content is missing");
\r
51 return JacksonUtils.readValue(content, NodeType.class);
\r
56 public DataType getDataType(String dataTypeName) throws BluePrintException {
\r
57 Preconditions.checkArgument(StringUtils.isNotBlank(dataTypeName), "DataType name is missing");
\r
58 String content = getModelDefinitions(dataTypeName);
\r
59 Preconditions.checkArgument(StringUtils.isNotBlank(content), "DataType content is missing");
\r
60 return JacksonUtils.readValue(content, DataType.class);
\r
65 public ArtifactType getArtifactType(String artifactTypeName) throws BluePrintException {
\r
66 Preconditions.checkArgument(StringUtils.isNotBlank(artifactTypeName), "ArtifactType name is missing");
\r
67 String content = getModelDefinitions(artifactTypeName);
\r
68 Preconditions.checkArgument(StringUtils.isNotBlank(content), "ArtifactType content is missing");
\r
69 return JacksonUtils.readValue(content, ArtifactType.class);
\r
74 public RelationshipType getRelationshipType(String relationshipTypeName) throws BluePrintException {
\r
75 Preconditions.checkArgument(StringUtils.isNotBlank(relationshipTypeName), "RelationshipType name is missing");
\r
76 String content = getModelDefinitions(relationshipTypeName);
\r
77 Preconditions.checkArgument(StringUtils.isNotBlank(content), "RelationshipType content is missing");
\r
78 return JacksonUtils.readValue(content, RelationshipType.class);
\r
83 public CapabilityDefinition getCapabilityDefinition(String capabilityDefinitionName) throws BluePrintException {
\r
84 Preconditions.checkArgument(StringUtils.isNotBlank(capabilityDefinitionName), "CapabilityDefinition name is missing");
\r
85 String content = getModelDefinitions(capabilityDefinitionName);
\r
86 Preconditions.checkArgument(StringUtils.isNotBlank(content), "CapabilityDefinition content is missing");
\r
87 return JacksonUtils.readValue(content, CapabilityDefinition.class);
\r
90 private String getModelDefinitions(String modelName) throws BluePrintException {
\r
91 String modelDefinition = null;
\r
92 Optional<ModelType> modelTypedb = modelTypeRepository.findByModelName(modelName);
\r
93 if (modelTypedb.isPresent()) {
\r
94 modelDefinition = modelTypedb.get().getDefinition();
\r
96 throw new BluePrintException(String.format("failed to get model definition (%s) from repo", modelName));
\r
98 return modelDefinition;
\r