f856b9efe179dde27e8b5041fdafc0fb02a96fd1
[ccsdk/apps.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.service
18
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
32
33 // Resource Dictionary Validation Services
34
35 @Service
36 class DefaultResourceAssignmentValidationService : ResourceAssignmentValidationServiceImpl()
37
38 @Service
39 class DefalutResourceDefinitionValidationService(bluePrintRepoService: BluePrintRepoService)
40     : ResourceDefinitionValidationServiceImpl(bluePrintRepoService)
41
42 interface ResourceDefinitionRepoService : BluePrintRepoService {
43
44     @Throws(BluePrintException::class)
45     fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
46 }
47
48 @Service
49 open class BluePrintRepoFileService(private val modelTypeRepository: ModelTypeRepository,
50                                     private val resourceDictionaryRepository: ResourceDictionaryRepository) : ResourceDefinitionRepoService {
51
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)")
56     }
57
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)")
62     }
63
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)")
68     }
69
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)")
74     }
75
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)")
80     }
81
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
87         } else {
88             throw BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
89         }
90     }
91
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")
96
97         val modelDefinition = getModelDefinition(modelName)
98         Preconditions.checkNotNull(modelDefinition,
99                 String.format("Failed to get model content for model name (%s)", modelName))
100
101         return JacksonUtils.readValue(modelDefinition, valueClass)
102     }
103
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
110         } else {
111             throw BluePrintException(String.format("failed to get model definition (%s) from repo", modelName))
112         }
113         return modelDefinition
114     }
115 }