Blueprint Processor Python Script Components
[ccsdk/cds.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / utils / BluePrintMetadataUtils.kt
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils\r
18 \r
19 \r
20 import com.att.eelf.configuration.EELFLogger\r
21 import com.att.eelf.configuration.EELFManager\r
22 import com.fasterxml.jackson.databind.JsonNode\r
23 import org.apache.commons.io.FileUtils\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.ToscaMetaData\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintImportService\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService\r
31 import java.io.File\r
32 import java.nio.charset.Charset\r
33 \r
34 class BluePrintMetadataUtils {\r
35     companion object {\r
36         private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())\r
37 \r
38 \r
39         fun toscaMetaData(basePath: String): ToscaMetaData {\r
40             val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)\r
41                     .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)\r
42             return toscaMetaDataFromMetaFile(toscaMetaPath)\r
43         }\r
44 \r
45         fun entryDefinitionFile(basePath: String): String {\r
46             val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER)\r
47                     .plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE)\r
48             return toscaMetaDataFromMetaFile(toscaMetaPath).entityDefinitions\r
49         }\r
50 \r
51         fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {\r
52             val toscaMetaData = ToscaMetaData()\r
53             val lines: MutableList<String> = FileUtils.readLines(File(metaFilePath), Charset.defaultCharset())\r
54             lines.forEach { line ->\r
55                 if (line.contains(":")) {\r
56                     val keyValue = line.split(":")\r
57                     if (keyValue.size == 2) {\r
58                         val value: String = keyValue[1].trim()\r
59                         when (keyValue[0]) {\r
60                             "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value\r
61                             "CSAR-Version" -> toscaMetaData.csarVersion = value\r
62                             "Created-By" -> toscaMetaData.createdBy = value\r
63                             "Entry-Definitions" -> toscaMetaData.entityDefinitions = value\r
64                             "Template-Tags" -> toscaMetaData.templateTags = value\r
65                         }\r
66                     }\r
67                 }\r
68 \r
69             }\r
70             return toscaMetaData\r
71         }\r
72 \r
73         fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {\r
74 \r
75             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)\r
76 \r
77             val context: MutableMap<String, JsonNode> = hashMapOf()\r
78             context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()\r
79             context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()\r
80 \r
81             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)\r
82             bluePrintRuntimeService.setExecutionContext(context)\r
83 \r
84             return bluePrintRuntimeService\r
85         }\r
86 \r
87         fun getBaseEnhancementBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {\r
88 \r
89             val bluePrintContext: BluePrintContext = getBaseEnhancementBluePrintContext(blueprintBasePath)\r
90             val context: MutableMap<String, JsonNode> = hashMapOf()\r
91             context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()\r
92             context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()\r
93 \r
94             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)\r
95             bluePrintRuntimeService.setExecutionContext(context)\r
96 \r
97             return bluePrintRuntimeService\r
98         }\r
99 \r
100         fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>): BluePrintRuntimeService<MutableMap<String, JsonNode>> {\r
101             val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)\r
102             val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)\r
103             bluePrintRuntimeService.setExecutionContext(executionContext)\r
104             return bluePrintRuntimeService\r
105         }\r
106 \r
107         fun getBluePrintContext(blueprintBasePath: String): BluePrintContext {\r
108 \r
109             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)\r
110 \r
111             log.info("Reading blueprint path($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")\r
112 \r
113             return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)\r
114         }\r
115 \r
116         private fun getBaseEnhancementBluePrintContext(blueprintBasePath: String): BluePrintContext {\r
117             val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)\r
118             // Clean Type files\r
119             BluePrintFileUtils.deleteBluePrintTypes(blueprintBasePath)\r
120             val rootFilePath: String = blueprintBasePath.plus(File.separator).plus(toscaMetaData.entityDefinitions)\r
121             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)\r
122 \r
123             // Clean the Import Definitions\r
124             BluePrintFileUtils.cleanImportTypes(rootServiceTemplate)\r
125 \r
126             val blueprintContext = BluePrintContext(rootServiceTemplate)\r
127             blueprintContext.rootPath = blueprintBasePath\r
128             blueprintContext.entryDefinition = toscaMetaData.entityDefinitions\r
129             return blueprintContext\r
130         }\r
131 \r
132         private fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {\r
133             val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)\r
134             val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)\r
135             // Recursively Import Template files\r
136             val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)\r
137             val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()\r
138             val blueprintContext = BluePrintContext(completeServiceTemplate)\r
139             blueprintContext.rootPath = basePath\r
140             blueprintContext.entryDefinition = entityDefinitions\r
141             return blueprintContext\r
142         }\r
143     }\r
144 }