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.apps.controllerblueprints.core.utils
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import com.fasterxml.jackson.databind.JsonNode
23 import org.apache.commons.io.FileUtils
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.ToscaMetaData
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintImportService
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService
32 import java.nio.charset.Charset
34 class BluePrintMetadataUtils {
36 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
39 fun toscaMetaData(basePath: String): ToscaMetaData {
40 val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
41 .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
42 return toscaMetaDataFromMetaFile(toscaMetaPath)
45 fun entryDefinitionFile(basePath: String): String {
46 val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
47 .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
48 return toscaMetaDataFromMetaFile(toscaMetaPath).entityDefinitions
51 fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
52 val toscaMetaData = ToscaMetaData()
53 val lines: MutableList<String> = FileUtils.readLines(File(metaFilePath), Charset.defaultCharset())
54 lines.forEach { line ->
55 if (line.contains(":")) {
56 val keyValue = line.split(":")
57 if (keyValue.size == 2) {
58 val value: String = keyValue[1].trim()
60 "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value
61 "CSAR-Version" -> toscaMetaData.csarVersion = value
62 "Created-By" -> toscaMetaData.createdBy = value
63 "Entry-Definitions" -> toscaMetaData.entityDefinitions = value
64 "Template-Tags" -> toscaMetaData.templateTags = value
73 fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
75 val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
77 val context: MutableMap<String, JsonNode> = hashMapOf()
78 context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()
79 context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()
81 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
82 bluePrintRuntimeService.setExecutionContext(context)
84 return bluePrintRuntimeService
87 fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
89 val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
90 val context: MutableMap<String, JsonNode> = hashMapOf()
91 context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()
92 context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()
94 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
95 bluePrintRuntimeService.setExecutionContext(context)
97 return bluePrintRuntimeService
100 fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
101 val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
102 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
103 bluePrintRuntimeService.setExecutionContext(executionContext)
104 return bluePrintRuntimeService
107 fun getBluePrintContext(blueprintBasePath: String): BluePrintContext {
109 val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
111 log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
113 return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
116 private fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
117 val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
119 BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
120 val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
121 val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
123 // Clean the Import Definitions
124 BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
126 val blueprintContext = BluePrintContext(rootServiceTemplate)
127 blueprintContext.rootPath = blueprintBasePath
128 blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
129 return blueprintContext
132 private fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
133 val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
134 val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
135 // Recursively Import Template files
136 val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
137 val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
138 val blueprintContext = BluePrintContext(completeServiceTemplate)
139 blueprintContext.rootPath = basePath
140 blueprintContext.entryDefinition = entityDefinitions
141 return blueprintContext