d9222d754395aa614f23e785f705685c5d3febdb
[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 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
26 import java.io.File
27 import java.io.FileFilter
28 import java.nio.file.Files
29 import java.nio.file.StandardOpenOption
30
31 class BluePrintFileUtils {
32     companion object {
33
34         private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
35
36         fun createEmptyBluePrint(basePath: String) {
37
38             val blueprintDir = File(basePath)
39             FileUtils.deleteDirectory(blueprintDir)
40
41             Files.createDirectories(blueprintDir.toPath())
42
43             val metaDataDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_METADATA_DIR))
44             Files.createDirectories(metaDataDir.toPath())
45
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)
48
49             val definitionsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR))
50             Files.createDirectories(definitionsDir.toPath())
51
52             val scriptsDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_SCRIPTS_DIR))
53             Files.createDirectories(scriptsDir.toPath())
54
55             val plansDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_PLANS_DIR))
56             Files.createDirectories(plansDir.toPath())
57
58             val templatesDir = File(blueprintDir.absolutePath.plus(File.separator).plus(BluePrintConstants.TOSCA_TEMPLATES_DIR))
59             Files.createDirectories(templatesDir.toPath())
60
61         }
62
63         fun copyBluePrint(sourcePath: String, targetPath: String) {
64             val sourceFile = File(sourcePath)
65             val targetFile = File(targetPath)
66             sourceFile.copyRecursively(targetFile, true)
67         }
68
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")
72
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())
79             }
80         }
81
82         fun writeBluePrintTypes(blueprintContext: BluePrintContext) {
83
84             val basePath = blueprintContext.rootPath
85             val definitionPath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
86             val definitionDir = File(definitionPath)
87
88             check(definitionDir.exists()) {
89                 throw BluePrintException("couldn't get definition file under path(${definitionDir.absolutePath})")
90             }
91
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)
95
96             }
97
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)
101             }
102
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)
106             }
107
108         }
109
110         private fun writeFile(definitionPath: String, type: String, content: String) = runBlocking {
111             val typeFile = File(definitionPath.plus(File.separator).plus("$type.json"))
112
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})")
116             }
117         }
118
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>"
125         }
126
127     }
128 }