c79d1b526a28943a78dffdb4c84214b57c7a93fa
[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.*
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.interfaces.BluePrintRepoService
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintArchiveUtils
30 import org.springframework.core.io.ByteArrayResource
31 import org.springframework.core.io.Resource
32 import org.springframework.http.HttpHeaders
33 import org.springframework.http.MediaType
34 import org.springframework.http.ResponseEntity
35 import org.springframework.http.codec.multipart.FilePart
36 import java.io.File
37 import java.nio.file.Paths
38
39
40 class BluePrintEnhancerUtils {
41     companion object {
42         val log = logger(BluePrintEnhancerUtils)
43
44         fun populateDataTypes(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
45                               dataTypeName: String): DataType {
46             val dataType = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
47                     ?: bluePrintRepoService.getDataType(dataTypeName)
48                     ?: throw BluePrintException("couldn't get DataType($dataTypeName) from repo.")
49             bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dataType)
50             return dataType
51         }
52
53         fun populateRelationshipType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
54                                      relationshipName: String): RelationshipType {
55
56             val relationshipType = bluePrintContext.serviceTemplate.relationshipTypes?.get(relationshipName)
57                     ?: bluePrintRepoService.getRelationshipType(relationshipName)
58                     ?: throw BluePrintException("couldn't get RelationshipType($relationshipName) from repo.")
59             bluePrintContext.serviceTemplate.relationshipTypes?.put(relationshipName, relationshipType)
60             return relationshipType
61         }
62
63
64         fun populateNodeType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
65                              nodeTypeName: String): NodeType {
66
67             val nodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(nodeTypeName)
68                     ?: bluePrintRepoService.getNodeType(nodeTypeName)
69                     ?: throw BluePrintException("couldn't get NodeType($nodeTypeName) from repo.")
70             bluePrintContext.serviceTemplate.nodeTypes?.put(nodeTypeName, nodeType)
71             return nodeType
72         }
73
74         fun populateArtifactType(bluePrintContext: BluePrintContext, bluePrintRepoService: BluePrintRepoService,
75                                  artifactTypeName: String): ArtifactType {
76
77             val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
78                     ?: bluePrintRepoService.getArtifactType(artifactTypeName)
79                     ?: throw BluePrintException("couldn't get ArtifactType($artifactTypeName) from repo.")
80             bluePrintContext.serviceTemplate.artifactTypes?.put(artifactTypeName, artifactType)
81             return artifactType
82         }
83
84         suspend fun byteArrayAsFile(byteArray: ByteArray, targetFile: File): File {
85             // Recreate Folder
86             targetFile.parentFile.reCreateNBDirs()
87             targetFile.writeBytes(byteArray).apply {
88                 log.info("CBA file(${targetFile.absolutePath} written successfully")
89             }
90             return targetFile
91         }
92
93
94         suspend fun filePartAsFile(filePart: FilePart, targetFile: File): File {
95             // Delete the Directory
96             targetFile.parentFile.reCreateNBDirs()
97             return filePart.transferTo(targetFile)
98                     .thenReturn(targetFile)
99                     .awaitSingle()
100         }
101
102         private suspend fun byteArrayAsArchiveFile(byteArray: ByteArray, archiveDir: String, enhanceDir: String): File {
103             //Recreate the Base Directories
104             normalizedFile(archiveDir).reCreateNBDirs()
105             normalizedFile(enhanceDir).reCreateNBDirs()
106             val archiveFile = normalizedFile(archiveDir, "cba.zip")
107             // Copy the File Part to ZIP
108             return byteArrayAsFile(byteArray, archiveFile)
109         }
110
111         private suspend fun filePartAsArchiveFile(filePart: FilePart, archiveDir: String, enhanceDir: String): File {
112             //Recreate the Base Directories
113             normalizedFile(archiveDir).reCreateNBDirs()
114             normalizedFile(enhanceDir).reCreateNBDirs()
115             val archiveFile = normalizedFile(archiveDir, "cba.zip")
116             // Copy the File Part to ZIP
117             return filePartAsFile(filePart, archiveFile)
118         }
119
120         /** copy the [byteArray] zip file to [archiveDir] and then decompress to [enhanceDir] */
121         suspend fun copyByteArrayToEnhanceDir(byteArray: ByteArray, archiveDir: String, enhanceDir: String): File {
122             val archiveFile = byteArrayAsArchiveFile(byteArray, archiveDir, enhanceDir)
123             val deCompressFileName = normalizedPathName(enhanceDir)
124             return archiveFile.deCompress(deCompressFileName)
125         }
126
127         /** copy the [filePart] zip file to [archiveDir] and then decompress to [enhanceDir] */
128         suspend fun copyFilePartToEnhanceDir(filePart: FilePart, archiveDir: String, enhanceDir: String): File {
129             val filePartFile = filePartAsArchiveFile(filePart, archiveDir, enhanceDir)
130             val deCompressFileName = normalizedPathName(enhanceDir)
131             return filePartFile.deCompress(deCompressFileName)
132         }
133
134         /** compress [enhanceDir] to [archiveDir] and return ByteArray */
135         suspend fun compressEnhanceDirAndReturnByteArray(enhanceDir: String, archiveDir: String,
136                                                          outputFileName: String = "enhanced-cba.zip"): ByteArray {
137             val compressedFile = normalizedFile(archiveDir, outputFileName)
138             BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
139             return compressedFile.readBytes()
140         }
141
142         /** compress [enhanceDir] to [archiveDir] and return ResponseEntity */
143         suspend fun compressEnhanceDirAndReturnFilePart(enhanceDir: String, archiveDir: String,
144                                                         outputFileName: String = "enhanced-cba.zip")
145                 : ResponseEntity<Resource> {
146             val compressedFile = normalizedFile(archiveDir, outputFileName)
147             BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
148             return prepareResourceEntity(compressedFile)
149         }
150
151         /** convert [file] to ResourceEntity */
152         suspend fun prepareResourceEntity(file: File): ResponseEntity<Resource> {
153             return prepareResourceEntity(file.name, file.readBytes())
154         }
155         /** convert [byteArray] to ResourceEntity with [fileName]*/
156         fun prepareResourceEntity(fileName: String, byteArray: ByteArray): ResponseEntity<Resource> {
157             return ResponseEntity.ok()
158                     .contentType(MediaType.parseMediaType("text/plain"))
159                     .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"")
160                     .body(ByteArrayResource(byteArray))
161         }
162
163         suspend fun cleanEnhancer(archiveLocation: String, enhancementLocation: String) {
164             deleteNBDir(archiveLocation)
165             deleteNBDir(enhancementLocation)
166         }
167     }
168 }