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