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.resource.dict.service.ResourceAssignmentValidationServiceImpl
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.service.ResourceDefinitionValidationServiceImpl
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ModelTypeRepository
30 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository
31 import org.springframework.stereotype.Service
33 // Resource Dictionary Validation Services
36 class DefaultResourceAssignmentValidationService : ResourceAssignmentValidationServiceImpl()
39 class DefalutResourceDefinitionValidationService(bluePrintRepoService: BluePrintRepoService)
40 : ResourceDefinitionValidationServiceImpl(bluePrintRepoService)
42 interface ResourceDefinitionRepoService : BluePrintRepoService {
44 @Throws(BluePrintException::class)
45 fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
49 open class BluePrintRepoFileService(private val modelTypeRepository: ModelTypeRepository,
50 private val resourceDictionaryRepository: ResourceDictionaryRepository) : ResourceDefinitionRepoService {
52 @Throws(BluePrintException::class)
53 override fun getNodeType(nodeTypeName: String): NodeType {
54 return getModelType(nodeTypeName, NodeType::class.java)
55 ?: throw BluePrintException("couldn't get NodeType($nodeTypeName)")
58 @Throws(BluePrintException::class)
59 override fun getDataType(dataTypeName: String): DataType {
60 return getModelType(dataTypeName, DataType::class.java)
61 ?: throw BluePrintException("couldn't get DataType($dataTypeName)")
64 @Throws(BluePrintException::class)
65 override fun getArtifactType(artifactTypeName: String): ArtifactType {
66 return getModelType(artifactTypeName, ArtifactType::class.java)
67 ?: throw BluePrintException("couldn't get ArtifactType($artifactTypeName)")
70 @Throws(BluePrintException::class)
71 override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
72 return getModelType(relationshipTypeName, RelationshipType::class.java)
73 ?: throw BluePrintException("couldn't get RelationshipType($relationshipTypeName)")
76 @Throws(BluePrintException::class)
77 override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
78 return getModelType(capabilityDefinitionName, CapabilityDefinition::class.java)
79 ?: throw BluePrintException("couldn't get CapabilityDefinition($capabilityDefinitionName)")
82 @Throws(BluePrintException::class)
83 override fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition {
84 val dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName)
85 return if (dbResourceDictionary.isPresent) {
86 dbResourceDictionary.get().definition
88 throw BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
92 @Throws(BluePrintException::class)
93 private fun <T> getModelType(modelName: String, valueClass: Class<T>): T? {
94 Preconditions.checkArgument(StringUtils.isNotBlank(modelName),
95 "Failed to get model from repo, model name is missing")
97 val modelDefinition = getModelDefinition(modelName)
98 Preconditions.checkNotNull(modelDefinition,
99 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