Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / service / BlueprintRepoServiceImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service
19
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.blueprintsprocessor.designer.api.repository.ModelTypeRepository
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
25 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
31 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintRepoService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
34 import org.springframework.stereotype.Service
35
36 interface ResourceDefinitionRepoService : BlueprintRepoService {
37
38     @Throws(BlueprintException::class)
39     fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition
40 }
41
42 @Service
43 open class BlueprintRepoFileService(
44     private val modelTypeRepository: ModelTypeRepository,
45     private val resourceDictionaryRepository: ResourceDictionaryRepository
46 ) : ResourceDefinitionRepoService {
47
48     @Throws(BlueprintException::class)
49     override fun getNodeType(nodeTypeName: String): NodeType {
50         return getModelType(nodeTypeName, NodeType::class.java)
51             ?: throw BlueprintException("couldn't get NodeType($nodeTypeName)")
52     }
53
54     @Throws(BlueprintException::class)
55     override fun getDataType(dataTypeName: String): DataType {
56         return getModelType(dataTypeName, DataType::class.java)
57             ?: throw BlueprintException("couldn't get DataType($dataTypeName)")
58     }
59
60     @Throws(BlueprintException::class)
61     override fun getArtifactType(artifactTypeName: String): ArtifactType {
62         return getModelType(artifactTypeName, ArtifactType::class.java)
63             ?: throw BlueprintException("couldn't get ArtifactType($artifactTypeName)")
64     }
65
66     @Throws(BlueprintException::class)
67     override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
68         return getModelType(relationshipTypeName, RelationshipType::class.java)
69             ?: throw BlueprintException("couldn't get RelationshipType($relationshipTypeName)")
70     }
71
72     @Throws(BlueprintException::class)
73     override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
74         return getModelType(capabilityDefinitionName, CapabilityDefinition::class.java)
75             ?: throw BlueprintException("couldn't get CapabilityDefinition($capabilityDefinitionName)")
76     }
77
78     @Throws(BlueprintException::class)
79     override fun getResourceDefinition(resourceDefinitionName: String): ResourceDefinition {
80         val dbResourceDictionary = resourceDictionaryRepository.findByName(resourceDefinitionName)
81         return if (dbResourceDictionary != null) {
82             dbResourceDictionary.definition
83         } else {
84             throw BlueprintException(String.format("failed to get resource dictionary (%s) from repo", resourceDefinitionName))
85         }
86     }
87
88     @Throws(BlueprintException::class)
89     private fun <T> getModelType(modelName: String, valueClass: Class<T>): T? {
90         Preconditions.checkArgument(
91             StringUtils.isNotBlank(modelName),
92             "Failed to get model from repo, model name is missing"
93         )
94
95         val modelDefinition = getModelDefinition(modelName)
96         Preconditions.checkNotNull(
97             modelDefinition,
98             String.format("Failed to get model content for model name (%s)", modelName)
99         )
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 }