50e19b2fe4346036a122e1eec99116070af87d14
[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.service.repository.ModelTypeRepository
28 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository
29 import org.springframework.stereotype.Service
30
31 interface ResourceDefinitionRepoService : BluePrintRepoService {
32
33     @Throws(BluePrintException::class)
34     fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
35 }
36
37 @Service
38 open class BluePrintRepoFileService(private val modelTypeRepository: ModelTypeRepository,
39                                     private val resourceDictionaryRepository: ResourceDictionaryRepository) : ResourceDefinitionRepoService {
40
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)")
45     }
46
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)")
51     }
52
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)")
57     }
58
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)")
63     }
64
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)")
69     }
70
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
76         } else {
77             throw BluePrintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
78         }
79     }
80
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")
85
86         val modelDefinition = getModelDefinition(modelName)
87         Preconditions.checkNotNull(modelDefinition,
88                 String.format("Failed to get model content for model name (%s)", modelName))
89
90         return JacksonUtils.readValue(modelDefinition, valueClass)
91     }
92
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
99         } else {
100             throw BluePrintException(String.format("failed to get model definition (%s) from repo", modelName))
101         }
102         return modelDefinition
103     }
104 }