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