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