2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.google.common.base.Preconditions
22 import org.apache.commons.lang3.StringUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ModelTypeRepository
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
25 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
31 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintRepoService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
34 import org.springframework.stereotype.Service
36 interface ResourceDefinitionRepoService : BlueprintRepoService {
38 @Throws(BlueprintException::class)
39 fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
43 open class BlueprintRepoFileService(
44 private val modelTypeRepository: ModelTypeRepository,
45 private val resourceDictionaryRepository: ResourceDictionaryRepository
46 ) : ResourceDefinitionRepoService {
48 @Throws(BlueprintException::class)
49 override fun getNodeType(nodeTypeName: String): NodeType {
50 return getModelType(nodeTypeName, NodeType::class.java)
51 ?: throw BlueprintException("couldn't get NodeType($nodeTypeName)")
54 @Throws(BlueprintException::class)
55 override fun getDataType(dataTypeName: String): DataType {
56 return getModelType(dataTypeName, DataType::class.java)
57 ?: throw BlueprintException("couldn't get DataType($dataTypeName)")
60 @Throws(BlueprintException::class)
61 override fun getArtifactType(artifactTypeName: String): ArtifactType {
62 return getModelType(artifactTypeName, ArtifactType::class.java)
63 ?: throw BlueprintException("couldn't get ArtifactType($artifactTypeName)")
66 @Throws(BlueprintException::class)
67 override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
68 return getModelType(relationshipTypeName, RelationshipType::class.java)
69 ?: throw BlueprintException("couldn't get RelationshipType($relationshipTypeName)")
72 @Throws(BlueprintException::class)
73 override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
74 return getModelType(capabilityDefinitionName, CapabilityDefinition::class.java)
75 ?: throw BlueprintException("couldn't get CapabilityDefinition($capabilityDefinitionName)")
78 @Throws(BlueprintException::class)
79 override fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition {
80 val dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName)
81 return if (dbResourceDictionary != null) {
82 dbResourceDictionary.definition
84 throw BlueprintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
88 @Throws(BlueprintException::class)
89 private fun <T> getModelType(modelName: String, valueClass: Class<T>): T? {
90 Preconditions.checkArgument(
91 StringUtils.isNotBlank(modelName),
92 "Failed to get model from repo, model name is missing"
95 val modelDefinition = getModelDefinition(modelName)
96 Preconditions.checkNotNull(
98 String.format("Failed to get model content for model name (%s)", modelName)
101 return JacksonUtils.readValue(modelDefinition, valueClass)
104 @Throws(BlueprintException::class)
105 private fun getModelDefinition(modelName: String): JsonNode {
106 val modelDefinition: JsonNode
107 val modelTypeDb = modelTypeRepository.findByModelName(modelName)
108 if (modelTypeDb != null) {
109 modelDefinition = modelTypeDb.definition
111 throw BlueprintException(String.format("failed to get model definition (%s) from repo", modelName))
113 return modelDefinition