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