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.service.BluePrintContext
27 import java.io.FileFilter
28 import java.nio.file.Files
29 import java.nio.file.StandardOpenOption
31 class BluePrintFileUtils {
34 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
36 fun createEmptyBluePrint(basePath: String) {
38 val blueprintDir = File(basePath)
39 FileUtils.deleteDirectory(blueprintDir)
41 Files.createDirectories(blueprintDir.toPath())
43 val metaDataDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_METADATA_DIR))
44 Files.createDirectories(metaDataDir.toPath())
46 val metafile = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_METADATA_ENTRY_DEFINITION_FILE))
47 Files.write(metafile.toPath(), getMetaDataContent().toByteArray(), StandardOpenOption.CREATE_NEW)
49 val definitionsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR))
50 Files.createDirectories(definitionsDir.toPath())
52 val scriptsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_SCRIPTS_DIR))
53 Files.createDirectories(scriptsDir.toPath())
55 val plansDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_PLANS_DIR))
56 Files.createDirectories(plansDir.toPath())
58 val templatesDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_TEMPLATES_DIR))
59 Files.createDirectories(templatesDir.toPath())
63 fun copyBluePrint(sourcePath: String, targetPath: String) {
64 val sourceFile = File(sourcePath)
65 val targetFile = File(targetPath)
66 sourceFile.copyRecursively(targetFile, true)
69 fun deleteBluePrintTypes(basePath: String) {
70 val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
71 log.info("deleting definition types under : $definitionPath")
73 val definitionDir = File(definitionPath)
74 // Find the Type Definitions
75 val fileFilter = FileFilter { pathname -> pathname.absolutePath.endsWith("_types.json") }
76 // Delete the Type Files
77 definitionDir.listFiles(fileFilter).forEach {
78 Files.deleteIfExists(it.toPath())
82 fun writeBluePrintTypes(blueprintContext: BluePrintContext) {
84 val basePath = blueprintContext.rootPath
85 val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
86 val definitionDir = File(definitionPath)
88 check(definitionDir.exists()) {
89 throw BluePrintException("couldn't get definition file under path(${definitionDir.absolutePath})")
92 blueprintContext.dataTypes.let {
93 val dataTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_DATA_TYPES, blueprintContext.dataTypes!!, true)
94 writeFile(definitionDir.absolutePath, BluePrintConstants.PATH_DATA_TYPES, dataTypesContent)
98 blueprintContext.artifactTypes.let {
99 val artifactTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_ARTIFACT_TYPES, blueprintContext.artifactTypes!!, true)
100 writeFile(definitionDir.absolutePath, BluePrintConstants.PATH_ARTIFACT_TYPES, artifactTypesContent)
103 blueprintContext.nodeTypes.let {
104 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_NODE_TYPES, blueprintContext.nodeTypes!!, true)
105 writeFile(definitionDir.absolutePath, BluePrintConstants.PATH_NODE_TYPES, nodeTypesContent)
110 private fun writeFile(definitionPath: String, type: String, content: String) = runBlocking {
111 val typeFile = File(definitionPath.plus(File.separator).plus("$type.json"))
113 Files.write(typeFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
114 check(typeFile.exists()) {
115 throw BluePrintException("couldn't write $type.json file under path(${typeFile.absolutePath})")
119 private fun getMetaDataContent(): String {
120 return "TOSCA-Meta-File-Version: 1.0.0" +
121 "\nCSAR-Version: <VERSION>" +
122 "\nCreated-By: <AUTHOR NAME>" +
123 "\nEntry-Definitions: Definitions/<BLUEPRINT_NAME>.json" +
124 "\nTemplate-Tags: <TAGS>"