4b02492da0bfe4c237452b7d4691fd8bcdbf75b7
[ccsdk/cds.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.cds.controllerblueprints.core.service
18
19 import org.slf4j.LoggerFactory
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
25
26 open class BluePrintRepoFileService(modelTypePath: String) : BluePrintRepoService {
27
28     private val log= LoggerFactory.getLogger(BluePrintRepoFileService::class.toString())
29
30     private val dataTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)
31     private val nodeTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE)
32     private val artifactTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE)
33     private val capabilityTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE)
34     private val relationshipTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE)
35     private val extension = ".json"
36
37     override fun getDataType(dataTypeName: String): DataType {
38         val fileName = dataTypePath.plus(BluePrintConstants.PATH_DIVIDER)
39                 .plus(dataTypeName).plus(extension)
40         return getModelType(fileName, DataType::class.java)
41     }
42
43     override fun getNodeType(nodeTypeName: String): NodeType {
44         val fileName = nodeTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(nodeTypeName).plus(extension)
45         return getModelType(fileName, NodeType::class.java)
46     }
47
48     override fun getArtifactType(artifactTypeName: String): ArtifactType {
49         val fileName = artifactTypePath.plus(BluePrintConstants.PATH_DIVIDER)
50                 .plus(artifactTypeName).plus(extension)
51         return getModelType(fileName, ArtifactType::class.java)
52     }
53
54     override fun getRelationshipType(relationshipTypeName: String): RelationshipType {
55         val fileName = relationshipTypePath.plus(BluePrintConstants.PATH_DIVIDER)
56                 .plus(relationshipTypeName).plus(extension)
57         return getModelType(fileName, RelationshipType::class.java)
58     }
59
60     override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {
61         val fileName = capabilityTypePath.plus(BluePrintConstants.PATH_DIVIDER)
62                 .plus(capabilityDefinitionName).plus(extension)
63         return getModelType(fileName, CapabilityDefinition::class.java)
64     }
65
66     private fun <T> getModelType(fileName: String, valueType: Class<T>): T {
67         return JacksonUtils.readValueFromFile(fileName, valueType)
68                 ?: throw BluePrintException("couldn't get file($fileName) for type(${valueType.name}")
69     }
70 }