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