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