2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
4 * Modifications Copyright © 2019 IBM.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils
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
43 import java.nio.file.Paths
45 class BluePrintEnhancerUtils {
47 val log = logger(BluePrintEnhancerUtils)
49 fun populateDataTypes(
50 bluePrintContext: BluePrintContext,
51 bluePrintRepoService: BluePrintRepoService,
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)
61 fun populateRelationshipType(
62 bluePrintContext: BluePrintContext,
63 bluePrintRepoService: BluePrintRepoService,
64 relationshipName: String
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
75 bluePrintContext: BluePrintContext,
76 bluePrintRepoService: BluePrintRepoService,
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)
87 fun populateArtifactType(
88 bluePrintContext: BluePrintContext,
89 bluePrintRepoService: BluePrintRepoService,
90 artifactTypeName: String
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)
100 suspend fun byteArrayAsFile(byteArray: ByteArray, targetFile: File): File {
102 targetFile.parentFile.reCreateNBDirs()
103 targetFile.writeBytes(byteArray).apply {
104 log.info("CBA file(${targetFile.absolutePath} written successfully")
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)
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)
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)
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)
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)
149 /** compress [enhanceDir] to [archiveDir] and return ByteArray */
150 suspend fun compressEnhanceDirAndReturnByteArray(
153 outputFileName: String = "enhanced-cba.zip"
155 val compressedFile = normalizedFile(archiveDir, outputFileName)
156 BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
157 return compressedFile.readBytes()
160 /** compress [enhanceDir] to [archiveDir] and return ResponseEntity */
161 suspend fun compressEnhanceDirAndReturnFilePart(
164 outputFileName: String = "enhanced-cba.zip"
166 ResponseEntity<Resource> {
167 val compressedFile = normalizedFile(archiveDir, outputFileName)
168 BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
169 return prepareResourceEntity(compressedFile)
172 /** convert [file] to ResourceEntity */
173 suspend fun prepareResourceEntity(file: File): ResponseEntity<Resource> {
174 return prepareResourceEntity(file.name, file.readBytes())
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))
185 suspend fun cleanEnhancer(archiveLocation: String, enhancementLocation: String) {
186 deleteNBDir(archiveLocation)
187 deleteNBDir(enhancementLocation)