Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / utils / BluePrintMetadataUtils.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.controllerblueprints.core.utils
19
20
21 import com.att.eelf.configuration.EELFLogger
22 import com.att.eelf.configuration.EELFManager
23 import com.fasterxml.jackson.databind.JsonNode
24 import kotlinx.coroutines.runBlocking
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.ToscaMetaData
28 import org.onap.ccsdk.apps.controllerblueprints.core.normalizedFile
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintImportService
31 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
32 import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService
33 import java.io.File
34 import java.nio.charset.Charset
35 import java.util.*
36
37 class BluePrintMetadataUtils {
38     companion object {
39         private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
40
41
42         fun toscaMetaData(basePath: String): ToscaMetaData {
43             val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
44                     .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
45             return toscaMetaDataFromMetaFile(toscaMetaPath)
46         }
47
48         fun entryDefinitionFile(basePath: String): String {
49             val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
50                     .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
51             return toscaMetaDataFromMetaFile(toscaMetaPath).entityDefinitions
52         }
53
54         fun bluePrintEnvProperties(basePath: String): Properties {
55             val blueprintsEnvFilePath = basePath.plus(File.separator)
56                     .plus(BluePrintConstants.TOSCA_ENVIRONMENTS_DIR)
57             return environmentFileProperties(blueprintsEnvFilePath)
58         }
59
60         fun environmentFileProperties(pathName: String): Properties {
61             val properties = Properties()
62             val envDir = File(pathName)
63             // Verify if the environment directory exists
64             if (envDir.exists() && envDir.isDirectory) {
65                 //Find all available environment files
66                 envDir.listFiles()
67                         .filter { it.name.endsWith(".properties") }
68                         .forEach {
69                             properties.load(it.inputStream())
70                         }
71             }
72             return properties
73         }
74
75         fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
76             val toscaMetaData = ToscaMetaData()
77             val lines = normalizedFile(metaFilePath).readLines(Charset.defaultCharset())
78             lines.forEach { line ->
79                 if (line.contains(":")) {
80                     val keyValue = line.split(":")
81                     if (keyValue.size == 2) {
82                         val value: String = keyValue[1].trim()
83                         when (keyValue[0]) {
84                             "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value
85                             "CSAR-Version" -> toscaMetaData.csarVersion = value
86                             "Created-By" -> toscaMetaData.createdBy = value
87                             "Entry-Definitions" -> toscaMetaData.entityDefinitions = value
88                             "Template-Tags" -> toscaMetaData.templateTags = value
89                         }
90                     }
91                 }
92
93             }
94             return toscaMetaData
95         }
96
97         fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
98
99             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
100
101             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
102             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
103             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
104
105             return bluePrintRuntimeService
106         }
107
108         suspend fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String)
109                 : BluePrintRuntimeService<MutableMap<String, JsonNode>> {
110
111             val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
112
113             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
114             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
115             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
116
117             return bluePrintRuntimeService
118         }
119
120         fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>):
121                 BluePrintRuntimeService<MutableMap<String, JsonNode>> {
122             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
123             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
124             executionContext.forEach {
125                 bluePrintRuntimeService.put(it.key, it.value)
126             }
127
128             bluePrintRuntimeService.setExecutionContext(executionContext)
129             return bluePrintRuntimeService
130         }
131
132         fun getBluePrintContext(blueprintBasePath: String): BluePrintContext = runBlocking {
133
134             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
135
136             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
137
138             readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
139         }
140
141         private suspend fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
142             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
143             // Clean Type files
144             BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
145             val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
146             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
147
148             // Clean the Import Definitions
149             BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
150
151             val blueprintContext = BluePrintContext(rootServiceTemplate)
152             blueprintContext.rootPath = blueprintBasePath
153             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
154             return blueprintContext
155         }
156
157         private suspend fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
158             val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
159             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
160             // Recursively Import Template files
161             val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
162             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
163             val blueprintContext = BluePrintContext(completeServiceTemplate)
164             blueprintContext.rootPath = basePath
165             blueprintContext.entryDefinition = entityDefinitions
166             return blueprintContext
167         }
168     }
169 }