2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018-2019 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.controllerblueprints.core.utils
21 import com.fasterxml.jackson.databind.JsonNode
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.controllerblueprints.core.*
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.ToscaMetaData
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintImportService
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
29 import org.slf4j.LoggerFactory
33 class BluePrintMetadataUtils {
35 private val log = LoggerFactory.getLogger(this::class.toString())
38 suspend fun toscaMetaData(basePath: String): ToscaMetaData {
39 val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
40 .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
41 return toscaMetaDataFromMetaFile(toscaMetaPath)
44 suspend fun entryDefinitionFile(basePath: String): String {
45 val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
46 .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
47 return toscaMetaDataFromMetaFile(toscaMetaPath).entityDefinitions
50 fun bluePrintEnvProperties(basePath: String): Properties {
51 val blueprintsEnvFilePath = basePath.plus(File.separator)
52 .plus(BluePrintConstants.TOSCA_ENVIRONMENTS_DIR)
53 return environmentFileProperties(blueprintsEnvFilePath)
56 fun environmentFileProperties(pathName: String): Properties {
57 val properties = Properties()
58 val envDir = normalizedFile(pathName)
59 // Verify if the environment directory exists
60 if (envDir.exists() && envDir.isDirectory) {
61 //Find all available environment files
63 .filter { it.name.endsWith(".properties") }
65 val istream = it.inputStream()
66 properties.load(istream)
73 private suspend fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
74 val toscaMetaData = ToscaMetaData()
75 val lines = normalizedFile(metaFilePath).readNBLines()
76 lines.forEach { line ->
77 if (line.contains(":")) {
78 val keyValue = line.split(":")
79 if (keyValue.size == 2) {
80 val value: String = keyValue[1].trim()
82 "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value
83 "CSAR-Version" -> toscaMetaData.csarVersion = value
84 "Created-By" -> toscaMetaData.createdBy = value
85 "Entry-Definitions" -> toscaMetaData.entityDefinitions = value
86 "Template-Tags" -> toscaMetaData.templateTags = value
95 fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
97 val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
99 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
100 bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
101 bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
103 return bluePrintRuntimeService
106 suspend fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String)
107 : BluePrintRuntimeService<MutableMap<String, JsonNode>> {
109 val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
111 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
112 bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
113 bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
115 return bluePrintRuntimeService
118 fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>):
119 BluePrintRuntimeService<MutableMap<String, JsonNode>> {
120 val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
121 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
122 executionContext.forEach {
123 bluePrintRuntimeService.put(it.key, it.value)
126 bluePrintRuntimeService.setExecutionContext(executionContext)
127 return bluePrintRuntimeService
130 fun getBluePrintContext(blueprintBasePath: String): BluePrintContext = runBlocking {
132 val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
134 log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
136 readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
139 private suspend fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
140 val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
142 BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
143 val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
144 val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
146 // Clean the Import Definitions
147 BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
149 val blueprintContext = BluePrintContext(rootServiceTemplate)
150 blueprintContext.rootPath = blueprintBasePath
151 blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
152 return blueprintContext
155 private suspend fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
156 val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
157 val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
158 // Recursively Import Template files
159 val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
160 val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
161 val blueprintContext = BluePrintContext(completeServiceTemplate)
162 blueprintContext.rootPath = basePath
163 blueprintContext.entryDefinition = entityDefinitions
164 return blueprintContext