c34a769e92051a8d85409e5b7c47954053e04e92
[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 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         fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
108
109             val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)
110
111             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
112             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH, blueprintBasePath.asJsonPrimitive())
113             bluePrintRuntimeService.put(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID, id.asJsonPrimitive())
114
115             return bluePrintRuntimeService
116         }
117
118         fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
119             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)
120             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)
121             executionContext.forEach{
122                 bluePrintRuntimeService.put(it.key,it.value)
123             }
124
125             bluePrintRuntimeService.setExecutionContext(executionContext)
126             return bluePrintRuntimeService
127         }
128
129         fun getBluePrintContext(blueprintBasePath: String): BluePrintContext {
130
131             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
132
133             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")
134
135             return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)
136         }
137
138         private fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {
139             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)
140             // Clean Type files
141             BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)
142             val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)
143             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
144
145             // Clean the Import Definitions
146             BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)
147
148             val blueprintContext = BluePrintContext(rootServiceTemplate)
149             blueprintContext.rootPath = blueprintBasePath
150             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions
151             return blueprintContext
152         }
153
154         private fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {
155             val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)
156             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)
157             // Recursively Import Template files
158             val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)
159             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()
160             val blueprintContext = BluePrintContext(completeServiceTemplate)
161             blueprintContext.rootPath = basePath
162             blueprintContext.entryDefinition = entityDefinitions
163             return blueprintContext
164         }
165     }
166 }