2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.core.utils
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import kotlinx.coroutines.runBlocking
23 import org.apache.commons.io.FileUtils
24 import org.apache.commons.lang3.StringUtils
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.ImportDefinition
28 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
31 import java.io.FileFilter
32 import java.nio.file.Files
33 import java.nio.file.Path
34 import java.nio.file.Paths
35 import java.nio.file.StandardOpenOption
38 class BluePrintFileUtils {
41 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
43 fun createEmptyBluePrint(basePath: String) {
45 val blueprintDir = File(basePath)
46 FileUtils.deleteDirectory(blueprintDir)
48 Files.createDirectories(blueprintDir.toPath())
50 val metaDataDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_METADATA_DIR))
51 Files.createDirectories(metaDataDir.toPath())
53 val metaFile = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants
54 .TOSCA_METADATA_ENTRY_DEFINITION_FILE))
55 Files.write(metaFile.toPath(), getMetaDataContent().toByteArray(), StandardOpenOption.CREATE_NEW)
57 val definitionsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR))
58 Files.createDirectories(definitionsDir.toPath())
60 val scriptsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_SCRIPTS_DIR))
61 Files.createDirectories(scriptsDir.toPath())
63 val plansDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_PLANS_DIR))
64 Files.createDirectories(plansDir.toPath())
66 val templatesDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_TEMPLATES_DIR))
67 Files.createDirectories(templatesDir.toPath())
71 fun copyBluePrint(sourcePath: String, targetPath: String) {
72 val sourceFile = File(sourcePath)
73 val targetFile = File(targetPath)
74 sourceFile.copyRecursively(targetFile, true)
77 fun deleteBluePrintTypes(basePath: String) {
78 val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
79 log.info("deleting definition types under : $definitionPath")
81 val definitionDir = File(definitionPath)
82 // Find the Type Definitions
83 val fileFilter = FileFilter { pathname -> pathname.absolutePath.endsWith("_types.json") }
84 // Delete the Type Files
85 definitionDir.listFiles(fileFilter).forEach {
86 Files.deleteIfExists(it.toPath())
90 fun writeEnhancedBluePrint(blueprintContext: BluePrintContext) {
92 // Write Blueprint Types
93 writeBluePrintTypes(blueprintContext)
94 // Re Populate the Imports
95 populateDefaultImports(blueprintContext)
96 // Rewrite the Entry Definition Files
97 writeEntryDefinitionFile(blueprintContext)
101 fun writeBluePrintTypes(blueprintContext: BluePrintContext) {
103 val basePath = blueprintContext.rootPath
104 val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
105 val definitionDir = File(definitionPath)
107 check(definitionDir.exists()) {
108 throw BluePrintException("couldn't get definition file under path(${definitionDir.absolutePath})")
111 blueprintContext.serviceTemplate.dataTypes?.let {
112 val dataTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_DATA_TYPES, it.toSortedMap(), true)
113 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_DATA_TYPES, dataTypesContent)
116 blueprintContext.serviceTemplate.relationshipTypes?.let {
117 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_RELATIONSHIP_TYPES, it.toSortedMap(), true)
118 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_RELATIONSHIP_TYPES, nodeTypesContent)
121 blueprintContext.serviceTemplate.artifactTypes?.let {
122 val artifactTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_ARTIFACT_TYPES, it.toSortedMap(), true)
123 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_ARTIFACT_TYPES, artifactTypesContent)
126 blueprintContext.serviceTemplate.nodeTypes?.let {
127 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_NODE_TYPES, it.toSortedMap(), true)
128 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_NODE_TYPES, nodeTypesContent)
131 blueprintContext.serviceTemplate.policyTypes?.let {
132 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_POLICY_TYPES, it.toSortedMap(), true)
133 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_POLICY_TYPES, nodeTypesContent)
137 private fun populateDefaultImports(blueprintContext: BluePrintContext) {
138 // Get the Default Types
139 val types = arrayListOf(BluePrintConstants.PATH_DATA_TYPES, BluePrintConstants.PATH_RELATIONSHIP_TYPES,
140 BluePrintConstants.PATH_ARTIFACT_TYPES, BluePrintConstants.PATH_NODE_TYPES,
141 BluePrintConstants.PATH_POLICY_TYPES)
143 // Clean Type Imports
144 cleanImportTypes(blueprintContext.serviceTemplate)
146 val imports = mutableListOf<ImportDefinition>()
147 types.forEach { typeName ->
148 val import = ImportDefinition()
149 import.file = BluePrintConstants.TOSCA_DEFINITIONS_DIR.plus("/$typeName.json")
153 blueprintContext.serviceTemplate.imports = imports
156 fun cleanImportTypes(serviceTemplate: ServiceTemplate) {
157 // Clean the Type imports
158 val toDeleteTypes = serviceTemplate.imports?.filter {
159 it.file.endsWith("_types.json")
162 if (toDeleteTypes != null && toDeleteTypes.isNotEmpty()) {
163 serviceTemplate.imports?.removeAll(toDeleteTypes)
168 * Re Generate the Blueprint Service Template Definition file based on BluePrint Context.
170 private fun writeEntryDefinitionFile(blueprintContext: BluePrintContext) {
172 val absoluteEntryDefinitionFile = blueprintContext.rootPath.plus(File.separator).plus(blueprintContext.entryDefinition)
174 val serviceTemplate = blueprintContext.serviceTemplate
176 // Clone the Service Template
177 val writeServiceTemplate = serviceTemplate.clone()
178 writeServiceTemplate.dataTypes = null
179 writeServiceTemplate.artifactTypes = null
180 writeServiceTemplate.policyTypes = null
181 writeServiceTemplate.relationshipTypes = null
182 writeServiceTemplate.nodeTypes = null
184 // Write the Service Template
185 writeDefinitionFile(absoluteEntryDefinitionFile, JacksonUtils.getJson(writeServiceTemplate, true))
188 fun writeDefinitionFile(definitionFileName: String, content: String) = runBlocking {
189 val definitionFile = File(definitionFileName)
190 // Delete the File If exists
191 Files.deleteIfExists(definitionFile.toPath())
193 Files.write(definitionFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
194 check(definitionFile.exists()) {
195 throw BluePrintException("couldn't write definition file under path(${definitionFile.absolutePath})")
199 private fun writeTypeFile(definitionPath: String, type: String, content: String) = runBlocking {
200 val typeFile = File(definitionPath.plus(File.separator).plus("$type.json"))
202 Files.write(typeFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
203 check(typeFile.exists()) {
204 throw BluePrintException("couldn't write $type.json file under path(${typeFile.absolutePath})")
208 private fun getMetaDataContent(): String {
209 return "TOSCA-Meta-File-Version: 1.0.0" +
210 "\nCSAR-Version: <VERSION>" +
211 "\nCreated-By: <AUTHOR NAME>" +
212 "\nEntry-Definitions: Definitions/<BLUEPRINT_NAME>.json" +
213 "\nTemplate-Tags: <TAGS>"
216 fun getCbaStorageDirectory(path: String): Path {
217 check(StringUtils.isNotBlank(path)) {
218 throw BluePrintException("CBA Path is missing.")
221 val fileStorageLocation = Paths.get(path).toAbsolutePath().normalize()
223 if (!Files.exists(fileStorageLocation))
224 Files.createDirectories(fileStorageLocation)
226 return fileStorageLocation
229 fun stripFileExtension(fileName: String): String {
230 val dotIndexe = fileName.lastIndexOf('.')
232 // In case dot is in first position, we are dealing with a hidden file rather than an extension
233 return if (dotIndexe > 0) fileName.substring(0, dotIndexe) else fileName