2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.service
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
30 open class BluePrintRepoFileService(modelTypePath: String) : BluePrintRepoService {
32 private val log = LoggerFactory.getLogger(BluePrintRepoFileService::class.toString())
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"
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)
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)
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)
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)
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)
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}")