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.controllerblueprints.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.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.service.repository.ModelTypeRepository
29 import org.onap.ccsdk.cds.controllerblueprints.service.repository.ResourceDictionaryRepository
30 import org.springframework.stereotype.Service
32 interface ResourceDefinitionRepoService : BluePrintRepoService {
34 @Throws(BluePrintException::class)
35 fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
39 open class BluePrintRepoFileService(private val modelTypeRepository: ModelTypeRepository,
40 private val resourceDictionaryRepository: ResourceDictionaryRepository) : ResourceDefinitionRepoService {
42 @Throws(BluePrintException::class)
43 override fun getNodeType(nodeTypeName: String): NodeType {
44 return getModelType(nodeTypeName, NodeType::class.java)
45 ?: throw BluePrintException("couldn't get NodeType($nodeTypeName)")
48 @Throws(BluePrintException::class)
49 override fun getDataType(dataTypeName: String): DataType {
50 return getModelType(dataTypeName, DataType::class.java)
51 ?: throw BluePrintException("couldn't get DataType($dataTypeName)")
54 @Throws(BluePrintException::class)
55 override fun getArtifactType(artifactTypeName: String): ArtifactType {
56 return getModelType(artifactTypeName, ArtifactType::class.java)
57 ?: throw BluePrintException("couldn't get ArtifactType($artifactTypeName)")
60 @Throws(BluePrintException::class)
61 override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
62 return getModelType(relationshipTypeName, RelationshipType::class.java)
63 ?: throw BluePrintException("couldn't get RelationshipType($relationshipTypeName)")
66 @Throws(BluePrintException::class)
67 override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
68 return getModelType(capabilityDefinitionName, CapabilityDefinition::class.java)
69 ?: throw BluePrintException("couldn't get CapabilityDefinition($capabilityDefinitionName)")
72 @Throws(BluePrintException::class)
73 override fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition {
74 val dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName)
75 return if (dbResourceDictionary.isPresent) {
76 dbResourceDictionary.get().definition
78 throw BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
82 @Throws(BluePrintException::class)
83 private fun <T> getModelType(modelName: String, valueClass: Class<T>): T? {
84 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
85 "Failed to get model from repo, model name is missing")
87 val modelDefinition = getModelDefinition(modelName)
88 Preconditions.checkNotNull(modelDefinition,
89 String.format("Failed to get model content for model name (%s)", modelName))
91 return JacksonUtils.readValue(modelDefinition, valueClass)
94 @Throws(BluePrintException::class)
95 private fun getModelDefinition(modelName: String): JsonNode {
96 val modelDefinition: JsonNode
97 val modelTypeDb = modelTypeRepository.findByModelName(modelName)
98 if (modelTypeDb != null) {
99 modelDefinition = modelTypeDb.definition
101 throw BluePrintException(String.format("failed to get model definition (%s) from repo", modelName))
103 return modelDefinition