6f090783ac93fae49e96e9e524775575f9eb79e2
[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.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.ToscaMetaData
26 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
27 import org.onap.ccsdk.cds.controllerblueprints.core.readNBLines
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintImportService
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
32 import org.slf4j.LoggerFactory
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                             val istream = it.inputStream()
69                             properties.load(istream)
70                             istream.close()
71                         }
72             }
73             return properties
74         }
75
76         private suspend fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
77             val toscaMetaData = ToscaMetaData()
78             val lines = normalizedFile(metaFilePath).readNBLines()
79             lines.forEach { line ->
80                 if (line.contains(":")) {
81                     val keyValue = line.split(":")
82                     if (keyValue.size == 2) {
83                         val value: String = keyValue[1].trim()
84                         when (keyValue[0]) {
85                             "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value
86                             "CSAR-Version" -> toscaMetaData.csarVersion = value
87                             "Created-By" -> toscaMetaData.createdBy = value
88                             "Entry-Definitions" -> toscaMetaData.entityDefinitions = value
89                             "Template-Name" -> toscaMetaData.templateName = value
90                             "Template-Version" -> toscaMetaData.templateVersion = value
91                             "Template-Tags" -> toscaMetaData.templateTags = value
92                         }
93                     }
94                 }
95
96             }
97             return toscaMetaData
98         }
99
100         fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
101
102             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
103
104             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
105             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
106             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
107
108             return bluePrintRuntimeService
109         }
110
111         suspend fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String)
112                 : BluePrintRuntimeService<MutableMap<String, JsonNode>> {
113
114             val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
115
116             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
117             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
118             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
119
120             return bluePrintRuntimeService
121         }
122
123         fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>):
124                 BluePrintRuntimeService<MutableMap<String, JsonNode>> {
125             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
126             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
127             executionContext.forEach {
128                 bluePrintRuntimeService.put(it.key, it.value)
129             }
130
131             bluePrintRuntimeService.setExecutionContext(executionContext)
132             return bluePrintRuntimeService
133         }
134
135         fun getBluePrintContext(blueprintBasePath: String): BluePrintContext = runBlocking {
136
137             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
138
139             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
140
141             readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
142         }
143
144         private suspend fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
145             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
146             // Clean Type files
147             BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
148             val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
149             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
150
151             // Clean the Import Definitions
152             BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
153
154             val blueprintContext = BluePrintContext(rootServiceTemplate)
155             blueprintContext.rootPath = blueprintBasePath
156             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
157             return blueprintContext
158         }
159
160         private suspend fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
161             val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
162             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
163             // Recursively Import Template files
164             val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
165             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
166             val blueprintContext = BluePrintContext(completeServiceTemplate)
167             blueprintContext.rootPath = basePath
168             blueprintContext.entryDefinition = entityDefinitions
169             return blueprintContext
170         }
171     }
172 }