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
35 import java.util.Optional;
\r
38 * ResourceDefinitionRepoDBService
\r
40 * @author Brinda Santh
\r
43 @SuppressWarnings("unused")
\r
44 public class ResourceDefinitionRepoDBService implements ResourceDefinitionRepoService {
\r
46 private ModelTypeRepository modelTypeRepository;
\r
47 private ResourceDictionaryRepository resourceDictionaryRepository;
\r
49 @SuppressWarnings("unused")
\r
50 public ResourceDefinitionRepoDBService(ModelTypeRepository modelTypeRepository,
\r
51 ResourceDictionaryRepository resourceDictionaryRepository) {
\r
52 this.modelTypeRepository = modelTypeRepository;
\r
53 this.resourceDictionaryRepository = resourceDictionaryRepository;
\r
57 public NodeType getNodeType(@NotNull String nodeTypeName) throws BluePrintException {
\r
58 return getModelType(nodeTypeName, NodeType.class);
\r
62 public DataType getDataType(@NotNull String dataTypeName) throws BluePrintException {
\r
63 return getModelType(dataTypeName, DataType.class);
\r
67 public ArtifactType getArtifactType(@NotNull String artifactTypeName) throws BluePrintException {
\r
68 return getModelType(artifactTypeName, ArtifactType.class);
\r
72 public RelationshipType getRelationshipType(@NotNull String relationshipTypeName) throws BluePrintException {
\r
73 return getModelType(relationshipTypeName, RelationshipType.class);
\r
77 public CapabilityDefinition getCapabilityDefinition(@NotNull String capabilityDefinitionName) throws BluePrintException {
\r
78 return getModelType(capabilityDefinitionName, CapabilityDefinition.class);
\r
83 public ResourceDefinition getResourceDefinition(@NotNull String resourceDefinitionName) throws BluePrintException {
\r
84 Optional<ResourceDictionary> dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName);
\r
85 if (dbResourceDictionary.isPresent()) {
\r
86 return dbResourceDictionary.get().getDefinition();
\r
88 throw new BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName));
\r
92 private <T> T getModelType(String modelName, Class<T> valueClass) throws BluePrintException {
\r
93 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
\r
94 "Failed to get model from repo, model name is missing");
\r
96 JsonNode modelDefinition = getModelDefinition(modelName);
\r
97 Preconditions.checkNotNull(modelDefinition,
\r
98 String.format("Failed to get model content for model name (%s)", modelName));
\r
100 return JacksonUtils.readValue(modelDefinition, valueClass);
\r
103 private JsonNode getModelDefinition(String modelName) throws BluePrintException {
\r
104 JsonNode modelDefinition;
\r
105 Optional<ModelType> modelTypeDb = modelTypeRepository.findByModelName(modelName);
\r
106 if (modelTypeDb.isPresent()) {
\r
107 modelDefinition = modelTypeDb.get().getDefinition();
\r
109 throw new BluePrintException(String.format("failed to get model definition (%s) from repo", modelName));
\r
111 return modelDefinition;
\r