f9ac876007c8df21eade61bb187da093ca6712f0
[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.serviceTemplate.dataTypes?.let {
93                 val dataTypesContent = JacksonUtils.getWrappedJson(BluePrintConstants.PATH_DATA_TYPES, it.toSortedMap(), true)
94                 writeTypeFile(definitionDir.absolutePath, BluePrintConstants.PATH_DATA_TYPES, dataTypesContent)
95             }
96
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)
100             }
101
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)
105             }
106
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)
110             }
111         }
112
113         fun writeDefinitionFile(definitionFile: String, content: String) = runBlocking {
114             val definitionFile = File(definitionFile)
115
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})")
119             }
120         }
121
122         private fun writeTypeFile(definitionPath: String, type: String, content: String) = runBlocking {
123             val typeFile = File(definitionPath.plus(File.separator).plus("$type.json"))
124
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})")
128             }
129         }
130
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>"
137         }
138
139     }
140 }