2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import kotlinx.coroutines.runBlocking
22 import org.apache.commons.io.FileUtils
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.ImportDefinition
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
29 import java.io.FileFilter
30 import java.nio.file.Files
31 import java.nio.file.StandardOpenOption
33 class BluePrintFileUtils {
36 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
38 fun createEmptyBluePrint(basePath: String) {
40 val blueprintDir = File(basePath)
41 FileUtils.deleteDirectory(blueprintDir)
43 Files.createDirectories(blueprintDir.toPath())
45 val metaDataDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_METADATA_DIR))
46 Files.createDirectories(metaDataDir.toPath())
48 val metafile = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE))
49 Files.write(metafile.toPath(), getMetaDataContent().toByteArray(), StandardOpenOption.CREATE_NEW)
51 val definitionsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR))
52 Files.createDirectories(definitionsDir.toPath())
54 val scriptsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_SCRIPTS_DIR))
55 Files.createDirectories(scriptsDir.toPath())
57 val plansDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_PLANS_DIR))
58 Files.createDirectories(plansDir.toPath())
60 val templatesDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_TEMPLATES_DIR))
61 Files.createDirectories(templatesDir.toPath())
65 fun copyBluePrint(sourcePath: String, targetPath: String) {
66 val sourceFile = File(sourcePath)
67 val targetFile = File(targetPath)
68 sourceFile.copyRecursively(targetFile, true)
71 fun deleteBluePrintTypes(basePath: String) {
72 val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
73 log.info("deleting definition types under : $definitionPath")
75 val definitionDir = File(definitionPath)
76 // Find the Type Definitions
77 val fileFilter = FileFilter { pathname -> pathname.absolutePath.endsWith("_types.json") }
78 // Delete the Type Files
79 definitionDir.listFiles(fileFilter).forEach {
80 Files.deleteIfExists(it.toPath())
84 fun populateDefaultImports(blueprintContext: BluePrintContext) {
85 // Get the Default Types
86 val types = arrayListOf(BluePrintConstants.PATH_DATA_TYPES, BluePrintConstants.PATH_ARTIFACT_TYPES,
87 BluePrintConstants.PATH_NODE_TYPES, BluePrintConstants.PATH_POLICY_TYPES)
90 cleanImportTypes(blueprintContext.serviceTemplate)
92 val imports = mutableListOf<ImportDefinition>()
93 types.forEach { typeName ->
94 val import = ImportDefinition()
95 import.file = BluePrintConstants.TOSCA_DEFINITIONS_DIR.plus("/$typeName.json")
99 blueprintContext.serviceTemplate.imports = imports
102 fun cleanImportTypes(serviceTemplate: ServiceTemplate) {
103 // Clean the Type imports
104 val toDeleteTypes = serviceTemplate.imports?.filter {
105 it.file.endsWith("_types.json")
108 if (toDeleteTypes != null && toDeleteTypes.isNotEmpty()) {
109 serviceTemplate.imports?.removeAll(toDeleteTypes)
113 fun writeEnhancedBluePrint(blueprintContext: BluePrintContext) {
115 // Write Blueprint Types
116 writeBluePrintTypes(blueprintContext)
117 // Re Populate the Imports
118 populateDefaultImports(blueprintContext)
119 // Rewrite the Entry Definition Files
120 writeEntryDefinitionFile(blueprintContext)
124 fun writeBluePrintTypes(blueprintContext: BluePrintContext) {
126 val basePath = blueprintContext.rootPath
127 val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
128 val definitionDir = File(definitionPath)
130 check(definitionDir.exists()) {
131 throw BluePrintException("couldn't get definition file under path(${definitionDir.absolutePath})")
134 blueprintContext.serviceTemplate.dataTypes?.let {
135 val dataTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_DATA_TYPES, it.toSortedMap(), true)
136 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_DATA_TYPES, dataTypesContent)
139 blueprintContext.serviceTemplate.artifactTypes?.let {
140 val artifactTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_ARTIFACT_TYPES, it.toSortedMap(), true)
141 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_ARTIFACT_TYPES, artifactTypesContent)
144 blueprintContext.serviceTemplate.nodeTypes?.let {
145 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_NODE_TYPES, it.toSortedMap(), true)
146 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_NODE_TYPES, nodeTypesContent)
149 blueprintContext.serviceTemplate.policyTypes?.let {
150 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_POLICY_TYPES, it.toSortedMap(), true)
151 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_POLICY_TYPES, nodeTypesContent)
155 fun writeEntryDefinitionFile(blueprintContext: BluePrintContext) {
157 val absoluteEntryDefinitionFile = blueprintContext.rootPath.plus(File.separator).plus(blueprintContext.entryDefinition)
159 val serviceTemplate = blueprintContext.serviceTemplate
161 // Clone the Service Template
162 val writeServiceTemplate = serviceTemplate.clone()
163 writeServiceTemplate.dataTypes = null
164 writeServiceTemplate.artifactTypes = null
165 writeServiceTemplate.policyTypes = null
166 writeServiceTemplate.nodeTypes = null
168 // Write the Serivice Template
169 writeDefinitionFile(absoluteEntryDefinitionFile, JacksonUtils.getJson(writeServiceTemplate, true))
172 fun writeDefinitionFile(definitionFile: String, content: String) = runBlocking {
173 val definitionFile = File(definitionFile)
174 // Delete the File If exists
175 Files.deleteIfExists(definitionFile.toPath())
177 Files.write(definitionFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
178 check(definitionFile.exists()) {
179 throw BluePrintException("couldn't write definition file under path(${definitionFile.absolutePath})")
183 private fun writeTypeFile(definitionPath: String, type: String, content: String) = runBlocking {
184 val typeFile = File(definitionPath.plus(File.separator).plus("$type.json"))
186 Files.write(typeFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
187 check(typeFile.exists()) {
188 throw BluePrintException("couldn't write $type.json file under path(${typeFile.absolutePath})")
192 private fun getMetaDataContent(): String {
193 return "TOSCA-Meta-File-Version: 1.0.0" +
194 "\nCSAR-Version: <VERSION>" +
195 "\nCreated-By: <AUTHOR NAME>" +
196 "\nEntry-Definitions: Definitions/<BLUEPRINT_NAME>.json" +
197 "\nTemplate-Tags: <TAGS>"