2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils
\r
20 import com.att.eelf.configuration.EELFLogger
\r
21 import com.att.eelf.configuration.EELFManager
\r
22 import com.fasterxml.jackson.databind.JsonNode
\r
23 import org.apache.commons.io.FileUtils
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.ToscaMetaData
\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintImportService
\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService
\r
32 import java.nio.charset.Charset
\r
34 object BluePrintMetadataUtils {
\r
35 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
\r
38 fun toscaMetaData(basePath: String): ToscaMetaData {
\r
39 val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus("TOSCA-Metadata/TOSCA.meta")
\r
40 return toscaMetaDataFromMetaFile(toscaMetaPath)
\r
44 fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
\r
45 val toscaMetaData = ToscaMetaData()
\r
46 val lines: MutableList<String> = FileUtils.readLines(File(metaFilePath), Charset.defaultCharset())
\r
47 lines.forEach { line ->
\r
48 if (line.contains(":")) {
\r
49 val keyValue = line.split(":")
\r
50 if (keyValue.size == 2) {
\r
51 val value: String = keyValue[1].trim()
\r
52 when (keyValue[0]) {
\r
53 "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value
\r
54 "CSAR-Version" -> toscaMetaData.csarVersion = value
\r
55 "Created-By" -> toscaMetaData.createdBy = value
\r
56 "Entry-Definitions" -> toscaMetaData.entityDefinitions = value
\r
57 "Template-Tags" -> toscaMetaData.templateTags = value
\r
63 return toscaMetaData
\r
67 fun getBluePrintContext(blueprintBasePath: String): BluePrintContext {
\r
69 val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
\r
71 log.info("Processing blueprint base path ($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
\r
73 return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
\r
77 fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
\r
79 val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
\r
81 val context: MutableMap<String, JsonNode> = hashMapOf()
\r
82 context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()
\r
83 context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()
\r
85 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
\r
86 bluePrintRuntimeService.setExecutionContext(context)
\r
88 return bluePrintRuntimeService
\r
92 fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
\r
93 val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
\r
94 val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
\r
95 bluePrintRuntimeService.setExecutionContext(executionContext)
\r
96 return bluePrintRuntimeService
\r
100 fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
\r
101 val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
\r
102 val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
\r
103 // Recursively Import Template files
\r
104 val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
\r
105 val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
\r
106 val blueprintContext = BluePrintContext(completeServiceTemplate)
\r
107 blueprintContext.rootPath = basePath
\r
108 return blueprintContext
\r