Fixing Blueprint Typo's and docs
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / utils / BlueprintEnhancerUtils.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils
20
21 import kotlinx.coroutines.reactive.awaitSingle
22 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
27 import org.onap.ccsdk.cds.controllerblueprints.core.deCompress
28 import org.onap.ccsdk.cds.controllerblueprints.core.deleteNBDir
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintRepoService
30 import org.onap.ccsdk.cds.controllerblueprints.core.logger
31 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
32 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
33 import org.onap.ccsdk.cds.controllerblueprints.core.reCreateNBDirs
34 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BlueprintArchiveUtils
36 import org.springframework.core.io.ByteArrayResource
37 import org.springframework.core.io.Resource
38 import org.springframework.http.HttpHeaders
39 import org.springframework.http.MediaType
40 import org.springframework.http.ResponseEntity
41 import org.springframework.http.codec.multipart.FilePart
42 import java.io.File
43 import java.nio.file.Paths
44
45 class BlueprintEnhancerUtils {
46     companion object {
47
48         val log = logger(BlueprintEnhancerUtils)
49
50         fun populateDataTypes(
51             bluePrintContext: BlueprintContext,
52             bluePrintRepoService: BlueprintRepoService,
53             dataTypeName: String
54         ): DataType {
55             val dataType = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
56                 ?: bluePrintRepoService.getDataType(dataTypeName)
57                 ?: throw BlueprintException("couldn't get DataType($dataTypeName) from repo.")
58             bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dataType)
59             return dataType
60         }
61
62         fun populateRelationshipType(
63             bluePrintContext: BlueprintContext,
64             bluePrintRepoService: BlueprintRepoService,
65             relationshipName: String
66         ): RelationshipType {
67
68             val relationshipType = bluePrintContext.serviceTemplate.relationshipTypes?.get(relationshipName)
69                 ?: bluePrintRepoService.getRelationshipType(relationshipName)
70                 ?: throw BlueprintException("couldn't get RelationshipType($relationshipName) from repo.")
71             bluePrintContext.serviceTemplate.relationshipTypes?.put(relationshipName, relationshipType)
72             return relationshipType
73         }
74
75         fun populateNodeType(
76             bluePrintContext: BlueprintContext,
77             bluePrintRepoService: BlueprintRepoService,
78             nodeTypeName: String
79         ): NodeType {
80
81             val nodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(nodeTypeName)
82                 ?: bluePrintRepoService.getNodeType(nodeTypeName)
83                 ?: throw BlueprintException("couldn't get NodeType($nodeTypeName) from repo.")
84             bluePrintContext.serviceTemplate.nodeTypes?.put(nodeTypeName, nodeType)
85             return nodeType
86         }
87
88         fun populateArtifactType(
89             bluePrintContext: BlueprintContext,
90             bluePrintRepoService: BlueprintRepoService,
91             artifactTypeName: String
92         ): ArtifactType {
93
94             val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
95                 ?: bluePrintRepoService.getArtifactType(artifactTypeName)
96                 ?: throw BlueprintException("couldn't get ArtifactType($artifactTypeName) from repo.")
97             bluePrintContext.serviceTemplate.artifactTypes?.put(artifactTypeName, artifactType)
98             return artifactType
99         }
100
101         suspend fun byteArrayAsFile(byteArray: ByteArray, targetFile: File): File {
102             // Recreate Folder
103             targetFile.parentFile.reCreateNBDirs()
104             targetFile.writeBytes(byteArray).apply {
105                 log.info("CBA file(${targetFile.absolutePath} written successfully")
106             }
107             return targetFile
108         }
109
110         suspend fun filePartAsFile(filePart: FilePart, targetFile: File): File {
111             // Delete the Directory
112             targetFile.parentFile.reCreateNBDirs()
113             return filePart.transferTo(targetFile)
114                 .thenReturn(targetFile)
115                 .awaitSingle()
116         }
117
118         private suspend fun byteArrayAsArchiveFile(byteArray: ByteArray, archiveDir: String, enhanceDir: String): File {
119             // Recreate the Base Directories
120             normalizedFile(archiveDir).reCreateNBDirs()
121             normalizedFile(enhanceDir).reCreateNBDirs()
122             val archiveFile = normalizedFile(archiveDir, "cba.zip")
123             // Copy the File Part to ZIP
124             return byteArrayAsFile(byteArray, archiveFile)
125         }
126
127         private suspend fun filePartAsArchiveFile(filePart: FilePart, archiveDir: String, enhanceDir: String): File {
128             // Recreate the Base Directories
129             normalizedFile(archiveDir).reCreateNBDirs()
130             normalizedFile(enhanceDir).reCreateNBDirs()
131             val archiveFile = normalizedFile(archiveDir, "cba.zip")
132             // Copy the File Part to ZIP
133             return filePartAsFile(filePart, archiveFile)
134         }
135
136         /** copy the [byteArray] zip file to [archiveDir] and then decompress to [enhanceDir] */
137         suspend fun copyByteArrayToEnhanceDir(byteArray: ByteArray, archiveDir: String, enhanceDir: String): File {
138             val archiveFile = byteArrayAsArchiveFile(byteArray, archiveDir, enhanceDir)
139             val deCompressFileName = normalizedPathName(enhanceDir)
140             return archiveFile.deCompress(deCompressFileName)
141         }
142
143         /** copy the [filePart] zip file to [archiveDir] and then decompress to [enhanceDir] */
144         suspend fun copyFilePartToEnhanceDir(filePart: FilePart, archiveDir: String, enhanceDir: String): File {
145             val filePartFile = filePartAsArchiveFile(filePart, archiveDir, enhanceDir)
146             val deCompressFileName = normalizedPathName(enhanceDir)
147             return filePartFile.deCompress(deCompressFileName)
148         }
149
150         /** compress [enhanceDir] to [archiveDir] and return ByteArray */
151         suspend fun compressEnhanceDirAndReturnByteArray(
152             enhanceDir: String,
153             archiveDir: String,
154             outputFileName: String = "enhanced-cba.zip"
155         ): ByteArray {
156             val compressedFile = normalizedFile(archiveDir, outputFileName)
157             BlueprintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
158             return compressedFile.readBytes()
159         }
160
161         /** compress [enhanceDir] to [archiveDir] and return ResponseEntity */
162         suspend fun compressEnhanceDirAndReturnFilePart(
163             enhanceDir: String,
164             archiveDir: String,
165             outputFileName: String = "enhanced-cba.zip"
166         ):
167             ResponseEntity<Resource> {
168                 val compressedFile = normalizedFile(archiveDir, outputFileName)
169                 BlueprintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
170                 return prepareResourceEntity(compressedFile)
171             }
172
173         /** convert [file] to ResourceEntity */
174         suspend fun prepareResourceEntity(file: File): ResponseEntity<Resource> {
175             return prepareResourceEntity(file.name, file.readBytes())
176         }
177
178         /** convert [byteArray] to ResourceEntity with [fileName]*/
179         fun prepareResourceEntity(fileName: String, byteArray: ByteArray): ResponseEntity<Resource> {
180             return ResponseEntity.ok()
181                 .contentType(MediaType.parseMediaType("text/plain"))
182                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"")
183                 .body(ByteArrayResource(byteArray))
184         }
185
186         suspend fun cleanEnhancer(archiveLocation: String, enhancementLocation: String) {
187             deleteNBDir(archiveLocation)
188             deleteNBDir(enhancementLocation)
189         }
190     }
191 }