320c306c388fea96ddb34451ca1b62d8c7290aea
[ccsdk/cds.git] /
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 object BluePrintMetadataUtils {\r
35     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())\r
36 \r
37     @JvmStatic\r
38     fun toscaMetaData(basePath: String): ToscaMetaData {\r
39         val toscaMetaPath = basePath.plus(BluePrintConstants.PATH_DIVIDER).plus("TOSCA-Metadata/TOSCA.meta")\r
40         return toscaMetaDataFromMetaFile(toscaMetaPath)\r
41     }\r
42 \r
43     @JvmStatic\r
44     fun toscaMetaDataFromMetaFile(metaFilePath: String): ToscaMetaData {\r
45         val toscaMetaData = ToscaMetaData()\r
46         val lines: MutableList<String> = FileUtils.readLines(File(metaFilePath), Charset.defaultCharset())\r
47         lines.forEach { line ->\r
48             if (line.contains(":")) {\r
49                 val keyValue = line.split(":")\r
50                 if (keyValue.size == 2) {\r
51                     val value: String = keyValue[1].trim()\r
52                     when (keyValue[0]) {\r
53                         "TOSCA-Meta-File-Version" -> toscaMetaData.toscaMetaFileVersion = value\r
54                         "CSAR-Version" -> toscaMetaData.csarVersion = value\r
55                         "Created-By" -> toscaMetaData.createdBy = value\r
56                         "Entry-Definitions" -> toscaMetaData.entityDefinitions = value\r
57                         "Template-Tags" -> toscaMetaData.templateTags = value\r
58                     }\r
59                 }\r
60             }\r
61 \r
62         }\r
63         return toscaMetaData\r
64     }\r
65 \r
66     @JvmStatic\r
67     fun getBluePrintContext(blueprintBasePath: String): BluePrintContext {\r
68 \r
69         val toscaMetaData: ToscaMetaData = toscaMetaData(blueprintBasePath)\r
70 \r
71         log.info("Processing blueprint base path ($blueprintBasePath) and entry definition file (${toscaMetaData.entityDefinitions})")\r
72 \r
73         return readBlueprintFile(toscaMetaData.entityDefinitions, blueprintBasePath)\r
74     }\r
75 \r
76     @JvmStatic\r
77     fun getBluePrintRuntime(id: String, blueprintBasePath: String): BluePrintRuntimeService<MutableMap<String, JsonNode>> {\r
78 \r
79         val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)\r
80 \r
81         val context: MutableMap<String, JsonNode> = hashMapOf()\r
82         context[BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] = blueprintBasePath.asJsonPrimitive()\r
83         context[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = id.asJsonPrimitive()\r
84 \r
85         val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)\r
86         bluePrintRuntimeService.setExecutionContext(context)\r
87 \r
88         return bluePrintRuntimeService\r
89     }\r
90 \r
91     @JvmStatic\r
92     fun getBluePrintRuntime(id: String, blueprintBasePath: String, executionContext: MutableMap<String, JsonNode>): BluePrintRuntimeService<MutableMap<String, JsonNode>> {\r
93         val bluePrintContext: BluePrintContext = getBluePrintContext(blueprintBasePath)\r
94         val bluePrintRuntimeService = DefaultBluePrintRuntimeService(id, bluePrintContext)\r
95         bluePrintRuntimeService.setExecutionContext(executionContext)\r
96         return bluePrintRuntimeService\r
97     }\r
98 \r
99     @JvmStatic\r
100     fun readBlueprintFile(entityDefinitions: String, basePath: String): BluePrintContext {\r
101         val rootFilePath: String = basePath.plus(File.separator).plus(entityDefinitions)\r
102         val rootServiceTemplate = ServiceTemplateUtils.getServiceTemplate(rootFilePath)\r
103         // Recursively Import Template files\r
104         val schemaImportResolverUtils = BluePrintImportService(rootServiceTemplate, basePath)\r
105         val completeServiceTemplate = schemaImportResolverUtils.getImportResolvedServiceTemplate()\r
106         return BluePrintContext(completeServiceTemplate)\r
107     }\r
108 }