5ca43952d21915371853a477abbd700477a0eb0f
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.core.service\r
19 \r
20 import com.att.eelf.configuration.EELFLogger\r
21 import com.att.eelf.configuration.EELFManager\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.*\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils\r
26 import java.io.Serializable\r
27 \r
28 /**\r
29  * BluePrintRepoFileService\r
30  * @author Brinda Santh\r
31  *\r
32  */\r
33 \r
34 interface BluePrintRepoService : Serializable {\r
35 \r
36     @Throws(BluePrintException::class)\r
37     fun getNodeType(nodeTypeName: String): NodeType\r
38 \r
39     @Throws(BluePrintException::class)\r
40     fun getDataType(dataTypeName: String): DataType\r
41 \r
42     @Throws(BluePrintException::class)\r
43     fun getArtifactType(artifactTypeName: String): ArtifactType\r
44 \r
45     @Throws(BluePrintException::class)\r
46     fun getRelationshipType(relationshipTypeName: String): RelationshipType\r
47 \r
48     @Throws(BluePrintException::class)\r
49     fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition\r
50 \r
51 }\r
52 \r
53 \r
54 open class BluePrintRepoFileService(modelTypePath: String) : BluePrintRepoService {\r
55 \r
56     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintRepoFileService::class.toString())\r
57 \r
58     private val dataTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)\r
59     private val nodeTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE)\r
60     private val artifactTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE)\r
61     private val capabilityTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE)\r
62     private val relationshipTypePath = modelTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE)\r
63     private val extension = ".json"\r
64 \r
65     override fun getDataType(dataTypeName: String): DataType {\r
66         val fileName = dataTypePath.plus(BluePrintConstants.PATH_DIVIDER)\r
67                 .plus(dataTypeName).plus(extension)\r
68         return getModelType(fileName, DataType::class.java)\r
69     }\r
70 \r
71     override fun getNodeType(nodeTypeName: String): NodeType {\r
72         val fileName = nodeTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(nodeTypeName).plus(extension)\r
73         return getModelType(fileName, NodeType::class.java)\r
74     }\r
75 \r
76     override fun getArtifactType(artifactTypeName: String): ArtifactType {\r
77         val fileName = artifactTypePath.plus(BluePrintConstants.PATH_DIVIDER)\r
78                 .plus(artifactTypeName).plus(extension)\r
79         return getModelType(fileName, ArtifactType::class.java)\r
80     }\r
81 \r
82     override fun getRelationshipType(relationshipTypeName: String): RelationshipType {\r
83         val fileName = relationshipTypePath.plus(BluePrintConstants.PATH_DIVIDER)\r
84                 .plus(relationshipTypeName).plus(extension)\r
85         return getModelType(fileName, RelationshipType::class.java)\r
86     }\r
87 \r
88     override fun getCapabilityDefinition(capabilityDefinitionName: String): CapabilityDefinition {\r
89         val fileName = capabilityTypePath.plus(BluePrintConstants.PATH_DIVIDER)\r
90                 .plus(capabilityDefinitionName).plus(extension)\r
91         return getModelType(fileName, CapabilityDefinition::class.java)\r
92     }\r
93 \r
94     private fun <T> getModelType(fileName: String, valueType: Class<T>): T {\r
95         return JacksonUtils.readValueFromFile(fileName, valueType)\r
96                 ?: throw BluePrintException("couldn't get file($fileName) for type(${valueType.name}")\r
97     }\r
98 }