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 {
48 val log = logger(BlueprintEnhancerUtils)
50 fun populateDataTypes(
51 bluePrintContext: BlueprintContext,
52 bluePrintRepoService: BlueprintRepoService,
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)
62 fun populateRelationshipType(
63 bluePrintContext: BlueprintContext,
64 bluePrintRepoService: BlueprintRepoService,
65 relationshipName: String
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
76 bluePrintContext: BlueprintContext,
77 bluePrintRepoService: BlueprintRepoService,
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)
88 fun populateArtifactType(
89 bluePrintContext: BlueprintContext,
90 bluePrintRepoService: BlueprintRepoService,
91 artifactTypeName: String
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)
101 suspend fun byteArrayAsFile(byteArray: ByteArray, targetFile: File): File {
103 targetFile.parentFile.reCreateNBDirs()
104 targetFile.writeBytes(byteArray).apply {
105 log.info("CBA file(${targetFile.absolutePath} written successfully")
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)
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)
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)
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)
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)
150 /** compress [enhanceDir] to [archiveDir] and return ByteArray */
151 suspend fun compressEnhanceDirAndReturnByteArray(
154 outputFileName: String = "enhanced-cba.zip"
156 val compressedFile = normalizedFile(archiveDir, outputFileName)
157 BlueprintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
158 return compressedFile.readBytes()
161 /** compress [enhanceDir] to [archiveDir] and return ResponseEntity */
162 suspend fun compressEnhanceDirAndReturnFilePart(
165 outputFileName: String = "enhanced-cba.zip"
167 ResponseEntity<Resource> {
168 val compressedFile = normalizedFile(archiveDir, outputFileName)
169 BlueprintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile)
170 return prepareResourceEntity(compressedFile)
173 /** convert [file] to ResourceEntity */
174 suspend fun prepareResourceEntity(file: File): ResponseEntity<Resource> {
175 return prepareResourceEntity(file.name, file.readBytes())
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))
186 suspend fun cleanEnhancer(archiveLocation: String, enhancementLocation: String) {
187 deleteNBDir(archiveLocation)
188 deleteNBDir(enhancementLocation)