fa5e164774493a65aabca9710f531ef161af3df8
[ccsdk/cds.git] /
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 org.apache.commons.io.FileUtils
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.service.BluePrintContext
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintImportService
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService
32 import java.io.File
33 import java.nio.charset.Charset
34 import java.util.*
35
36 class BluePrintMetadataUtils {
37     companion object {
38         private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
39
40
41         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         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 = File(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         fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {
75             val toscaMetaData = ToscaMetaData()
76             val lines: MutableList<String> = FileUtils.readLines(File(metaFilePath), Charset.defaultCharset())
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 context: MutableMap<String, JsonNode> = hashMapOf()
101             context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()
102             context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()
103
104             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
105             bluePrintRuntimeService.setExecutionContext(context)
106
107             return bluePrintRuntimeService
108         }
109
110         fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
111
112             val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
113             val context: MutableMap<String, JsonNode> = hashMapOf()
114             context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()
115             context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()
116
117             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
118             bluePrintRuntimeService.setExecutionContext(context)
119
120             return bluePrintRuntimeService
121         }
122
123         fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
124             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
125             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
126             bluePrintRuntimeService.setExecutionContext(executionContext)
127             return bluePrintRuntimeService
128         }
129
130         fun getBluePrintContext(blueprintBasePath: String): BluePrintContext {
131
132             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
133
134             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
135
136             return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
137         }
138
139         private fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
140             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
141             // Clean Type files
142             BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
143             val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
144             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
145
146             // Clean the Import Definitions
147             BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
148
149             val blueprintContext = BluePrintContext(rootServiceTemplate)
150             blueprintContext.rootPath = blueprintBasePath
151             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
152             return blueprintContext
153         }
154
155         private fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
156             val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
157             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
158             // Recursively Import Template files
159             val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
160             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
161             val blueprintContext = BluePrintContext(completeServiceTemplate)
162             blueprintContext.rootPath = basePath
163             blueprintContext.entryDefinition = entityDefinitions
164             return blueprintContext
165         }
166     }
167 }