3a1edccc05f6f8ff371f15677770acddb90b91d7
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018-2019 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.cds.controllerblueprints.core.utils
19
20
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.interfaces.BluePrintDefinitions
26 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintImportService
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
31 import org.slf4j.LoggerFactory
32 import java.io.File
33 import java.util.*
34
35 class BluePrintMetadataUtils {
36     companion object {
37         private val log = LoggerFactory.getLogger(this::class.toString())
38
39
40         suspend fun toscaMetaData(basePath: String): ToscaMetaData {
41             val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
42                     .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
43             return toscaMetaDataFromMetaFile(toscaMetaPath)
44         }
45
46         suspend fun entryDefinitionFile(basePath: String): String {
47             val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)
48                     .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)
49             return toscaMetaDataFromMetaFile(toscaMetaPath).entityDefinitions
50         }
51
52         fun bluePrintEnvProperties(basePath: String): Properties {
53             val blueprintsEnvFilePath = basePath.plus(File.separator)
54                     .plus(BluePrintConstants.TOSCA_ENVIRONMENTS_DIR)
55             return environmentFileProperties(blueprintsEnvFilePath)
56         }
57
58         fun environmentFileProperties(pathName: String): Properties {
59             val properties = Properties()
60             val envDir = normalizedFile(pathName)
61             // Verify if the environment directory exists
62             if (envDir.exists() && envDir.isDirectory) {
63                 //Find all available environment files
64                 envDir.listFiles()
65                         .filter { it.name.endsWith(".properties") }
66                         .forEach {
67                             val istream = it.inputStream()
68                             properties.load(istream)
69                             istream.close()
70                         }
71             }
72             return properties
73         }
74
75         private suspend fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
76             val toscaMetaData = ToscaMetaData()
77             val lines = normalizedFile(metaFilePath).readNBLines()
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-Name" -> toscaMetaData.templateName = value
89                             "Template-Version" -> toscaMetaData.templateVersion = value
90                             "Template-Tags" -> toscaMetaData.templateTags = value
91                         }
92                     }
93                 }
94
95             }
96             return toscaMetaData
97         }
98
99         fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
100
101             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
102
103             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
104             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
105             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
106
107             return bluePrintRuntimeService
108         }
109
110         suspend fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String)
111                 : BluePrintRuntimeService<MutableMap<String, JsonNode>> {
112
113             val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
114
115             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
116             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
117             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
118
119             return bluePrintRuntimeService
120         }
121
122         fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>):
123                 BluePrintRuntimeService<MutableMap<String, JsonNode>> {
124             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
125             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
126             executionContext.forEach {
127                 bluePrintRuntimeService.put(it.key, it.value)
128             }
129
130             bluePrintRuntimeService.setExecutionContext(executionContext)
131             return bluePrintRuntimeService
132         }
133
134         fun getBluePrintContext(blueprintBasePath: String): BluePrintContext = runBlocking {
135
136             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
137
138             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
139
140             // If the EntryDefinition is Kotlin file, compile and get Service Template
141             if (toscaMetaData.entityDefinitions.endsWith("kt")) {
142                 readBlueprintKotlinFile(toscaMetaData, blueprintBasePath)
143             } else {
144                 readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
145             }
146         }
147
148         private suspend fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
149             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
150             // Clean Type files
151             BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
152             val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
153             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
154
155             // Clean the Import Definitions
156             BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
157
158             val blueprintContext = BluePrintContext(rootServiceTemplate)
159             blueprintContext.rootPath = blueprintBasePath
160             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
161             return blueprintContext
162         }
163
164         private suspend fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
165             val normalizedBasePath = normalizedPathName(basePath)
166             val rootFilePath = normalizedPathName(normalizedBasePath, entityDefinitions)
167             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
168             // Recursively Import Template files
169             val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, normalizedBasePath)
170             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
171             val blueprintContext = BluePrintContext(completeServiceTemplate)
172             blueprintContext.rootPath = normalizedBasePath
173             blueprintContext.entryDefinition = entityDefinitions
174             return blueprintContext
175         }
176
177         /** Reade the Service Template Definitions from the Kotlin file */
178         private suspend fun readBlueprintKotlinFile(toscaMetaData: ToscaMetaData, basePath: String): BluePrintContext {
179
180             checkNotNull(toscaMetaData.templateName) { "couldn't find 'Template-Name' key in TOSCA.meta" }
181             checkNotNull(toscaMetaData.templateVersion) { "couldn't find 'Template-Version' key in TOSCA.meta" }
182
183             val definitionClassName = toscaMetaData.entityDefinitions.removeSuffix(".kt")
184             val normalizedBasePath = normalizedPathName(basePath)
185
186             val bluePrintScriptsService = BluePrintScriptsServiceImpl()
187             val bluePrintDefinitions = bluePrintScriptsService
188                     .scriptInstance<BluePrintDefinitions>(normalizedBasePath, toscaMetaData.templateName!!,
189                             toscaMetaData.templateVersion!!, definitionClassName, true)
190             // Get the Service Template
191             val serviceTemplate = bluePrintDefinitions.serviceTemplate()
192
193             // Clean the Default type import Definitions
194             BluePrintFileUtils.cleanImportTypes(serviceTemplate)
195
196             val blueprintContext = BluePrintContext(serviceTemplate)
197             blueprintContext.rootPath = normalizedBasePath
198             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
199             blueprintContext.otherDefinitions = bluePrintDefinitions.otherDefinitions()
200             return blueprintContext
201         }
202     }
203 }