2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.apps.controllerblueprints.core.service
\r
20 import com.google.common.base.Preconditions
\r
21 import org.apache.commons.io.FileUtils
\r
22 import org.apache.commons.lang3.StringUtils
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
\r
27 import org.slf4j.Logger
\r
28 import org.slf4j.LoggerFactory
\r
29 import reactor.core.publisher.Mono
\r
31 import java.io.Serializable
\r
32 import java.nio.charset.Charset
\r
35 * BluePrintRepoFileService
\r
36 * @author Brinda Santh
\r
40 interface BluePrintRepoService : Serializable {
\r
42 @Throws(BluePrintException::class)
\r
43 fun getNodeType(nodeTypeName: String): Mono<NodeType>?
\r
45 @Throws(BluePrintException::class)
\r
46 fun getDataType(dataTypeName: String): Mono<DataType>?
\r
48 @Throws(BluePrintException::class)
\r
49 fun getArtifactType(artifactTypeName: String): Mono<ArtifactType>?
\r
51 @Throws(BluePrintException::class)
\r
52 fun getRelationshipType(relationshipTypeName: String): Mono<RelationshipType>?
\r
54 @Throws(BluePrintException::class)
\r
55 fun getCapabilityDefinition(capabilityDefinitionName: String): Mono<CapabilityDefinition>?
\r
60 class BluePrintRepoFileService(val basePath: String) : BluePrintRepoService {
\r
62 private val log: Logger = LoggerFactory.getLogger(BluePrintRepoFileService::class.java)
\r
64 private val dataTypePath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)
\r
65 private val nodeTypePath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE)
\r
66 private val artifactTypePath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE)
\r
67 private val capabilityTypePath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE)
\r
68 private val relationshipTypePath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus(BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE)
\r
69 private val extension = ".json"
\r
71 override fun getDataType(dataTypeName: String): Mono<DataType>? {
\r
72 val fileName = dataTypePath.plus(BluePrintConstants.PATH_DIVIDER)
\r
73 .plus(dataTypeName).plus(extension)
\r
74 return getModelType(fileName, DataType::class.java)
\r
77 override fun getNodeType(nodeTypeName: String): Mono<NodeType>? {
\r
78 val fileName = nodeTypePath.plus(BluePrintConstants.PATH_DIVIDER).plus(nodeTypeName).plus(extension)
\r
79 return getModelType(fileName, NodeType::class.java)
\r
82 override fun getArtifactType(artifactTypeName: String): Mono<ArtifactType>? {
\r
83 val fileName = artifactTypePath.plus(BluePrintConstants.PATH_DIVIDER)
\r
84 .plus(artifactTypeName).plus(extension)
\r
85 return getModelType(fileName, ArtifactType::class.java)
\r
88 override fun getRelationshipType(relationshipTypeName: String): Mono<RelationshipType>? {
\r
89 val fileName = relationshipTypePath.plus(BluePrintConstants.PATH_DIVIDER)
\r
90 .plus(relationshipTypeName).plus(extension)
\r
91 return getModelType(fileName, RelationshipType::class.java)
\r
94 override fun getCapabilityDefinition(capabilityDefinitionName: String): Mono<CapabilityDefinition>? {
\r
95 val fileName = capabilityTypePath.plus(BluePrintConstants.PATH_DIVIDER)
\r
96 .plus(capabilityDefinitionName).plus(extension)
\r
97 return getModelType(fileName, CapabilityDefinition::class.java)
\r
100 private fun <T> getModelType(fileName: String, valueType: Class<T>): Mono<T> {
\r
101 return getFileContent(fileName).map { content ->
\r
102 Preconditions.checkArgument(StringUtils.isNotBlank(content),
\r
103 String.format("Failed to get model content for file (%s)", fileName))
\r
105 JacksonUtils.readValue(content, valueType)
\r
106 ?: throw BluePrintException(String.format("Failed to get model file from content for file (%s)", fileName))
\r
111 private fun getFileContent(fileName: String): Mono<String> {
\r
112 return Mono.just(FileUtils.readFileToString(File(fileName), Charset.defaultCharset()))
\r