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