2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.controllerblueprints.service
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.google.common.base.Preconditions
21 import org.apache.commons.lang3.StringUtils
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
24 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
27 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository
28 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository
29 import org.springframework.stereotype.Service
31 interface ResourceDefinitionRepoService : BluePrintRepoService {
33 @Throws(BluePrintException::class)
34 fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
38 open class BluePrintRepoFileService(private val modelTypeRepository: ModelTypeRepository,
39 private val resourceDictionaryRepository: ResourceDictionaryRepository) : ResourceDefinitionRepoService {
41 @Throws(BluePrintException::class)
42 override fun getNodeType(nodeTypeName: String): NodeType {
43 return getModelType(nodeTypeName, NodeType::class.java)
44 ?: throw BluePrintException("couldn't get NodeType($nodeTypeName)")
47 @Throws(BluePrintException::class)
48 override fun getDataType(dataTypeName: String): DataType {
49 return getModelType(dataTypeName, DataType::class.java)
50 ?: throw BluePrintException("couldn't get DataType($dataTypeName)")
53 @Throws(BluePrintException::class)
54 override fun getArtifactType(artifactTypeName: String): ArtifactType {
55 return getModelType(artifactTypeName, ArtifactType::class.java)
56 ?: throw BluePrintException("couldn't get ArtifactType($artifactTypeName)")
59 @Throws(BluePrintException::class)
60 override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
61 return getModelType(relationshipTypeName, RelationshipType::class.java)
62 ?: throw BluePrintException("couldn't get RelationshipType($relationshipTypeName)")
65 @Throws(BluePrintException::class)
66 override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
67 return getModelType(capabilityDefinitionName, CapabilityDefinition::class.java)
68 ?: throw BluePrintException("couldn't get CapabilityDefinition($capabilityDefinitionName)")
71 @Throws(BluePrintException::class)
72 override fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition {
73 val dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName)
74 return if (dbResourceDictionary.isPresent) {
75 dbResourceDictionary.get().definition
77 throw BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
81 @Throws(BluePrintException::class)
82 private fun <T> getModelType(modelName: String, valueClass: Class<T>): T? {
83 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
84 "Failed to get model from repo, model name is missing")
86 val modelDefinition = getModelDefinition(modelName)
87 Preconditions.checkNotNull(modelDefinition,
88 String.format("Failed to get model content for model name (%s)", modelName))
90 return JacksonUtils.readValue(modelDefinition, valueClass)
93 @Throws(BluePrintException::class)
94 private fun getModelDefinition(modelName: String): JsonNode {
95 val modelDefinition: JsonNode
96 val modelTypeDb = modelTypeRepository.findByModelName(modelName)
97 if (modelTypeDb.isPresent) {
98 modelDefinition = modelTypeDb.get().definition
100 throw BluePrintException(String.format("failed to get model definition (%s) from repo", modelName))
102 return modelDefinition