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.serviceTemplate.dataTypes?.let {
93 val dataTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_DATA_TYPES, it.toSortedMap(), true)
94 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_DATA_TYPES, dataTypesContent)
97 blueprintContext.serviceTemplate.artifactTypes?.let {
98 val artifactTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_ARTIFACT_TYPES, it.toSortedMap(), true)
99 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_ARTIFACT_TYPES, artifactTypesContent)
102 blueprintContext.serviceTemplate.nodeTypes?.let {
103 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_NODE_TYPES, it.toSortedMap(), true)
104 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_NODE_TYPES, nodeTypesContent)
107 blueprintContext.serviceTemplate.policyTypes?.let {
108 val nodeTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_POLICY_TYPES, it.toSortedMap(), true)
109 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_POLICY_TYPES, nodeTypesContent)
113 fun writeDefinitionFile(definitionFile: String, content: String) = runBlocking {
114 val definitionFile = File(definitionFile)
116 Files.write(definitionFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE)
117 check(definitionFile.exists()) {
118 throw BluePrintException("couldn't write definition file under path(${definitionFile.absolutePath})")
122 private fun writeTypeFile(definitionPath: String, type: String, content: String) = runBlocking {
123 val typeFile = File(definitionPath.plus(File.separator).plus("$type.json"))
125 Files.write(typeFile.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
126 check(typeFile.exists()) {
127 throw BluePrintException("couldn't write $type.json file under path(${typeFile.absolutePath})")
131 private fun getMetaDataContent(): String {
132 return "TOSCA-Meta-File-Version: 1.0.0" +
133 "\nCSAR-Version: <VERSION>" +
134 "\nCreated-By: <AUTHOR NAME>" +
135 "\nEntry-Definitions: Definitions/<BLUEPRINT_NAME>.json" +
136 "\nTemplate-Tags: <TAGS>"