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.controllerblueprints.service.handler
21 import org.onap.ccsdk.cds.controllerblueprints.core.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintEnhancerService
26 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModel
27 import org.onap.ccsdk.cds.controllerblueprints.service.domain.BlueprintModelSearch
28 import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository
29 import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelRepository
30 import org.onap.ccsdk.cds.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository
31 import org.onap.ccsdk.cds.controllerblueprints.service.utils.BluePrintEnhancerUtils
32 import org.slf4j.LoggerFactory
33 import org.springframework.core.io.ByteArrayResource
34 import org.springframework.core.io.Resource
35 import org.springframework.http.HttpHeaders
36 import org.springframework.http.MediaType
37 import org.springframework.http.ResponseEntity
38 import org.springframework.http.codec.multipart.FilePart
39 import org.springframework.stereotype.Service
40 import org.springframework.transaction.annotation.Transactional
42 import java.io.IOException
46 * BlueprintModelHandler Purpose: Handler service to handle the request from BlurPrintModelRest
48 * @author Brinda Santh
53 open class BluePrintModelHandler(private val controllerBlueprintsCatalogService: BluePrintCatalogService,
54 private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
55 private val blueprintModelSearchRepository: ControllerBlueprintModelSearchRepository,
56 private val blueprintModelRepository: ControllerBlueprintModelRepository,
57 private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository,
58 private val bluePrintEnhancerService: BluePrintEnhancerService) {
60 private val log = LoggerFactory.getLogger(BluePrintModelHandler::class.java)!!
63 * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
65 * @return List<BlueprintModelSearch> list of the controller blueprint archives
66 </BlueprintModelSearch> */
67 open fun allBlueprintModel(): List<BlueprintModelSearch> {
68 return blueprintModelSearchRepository.findAll()
72 * This is a saveBlueprintModel method
74 * @param filePart filePart
75 * @return Mono<BlueprintModelSearch>
76 * @throws BluePrintException BluePrintException
77 </BlueprintModelSearch> */
78 @Throws(BluePrintException::class)
79 open suspend fun saveBlueprintModel(filePart: FilePart): BlueprintModelSearch {
80 val saveId = UUID.randomUUID().toString()
81 val blueprintArchive = normalizedPathName(bluePrintLoadConfiguration.blueprintArchivePath, saveId)
84 normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath, saveId).reCreateDirs()
85 val deCompressedFile = normalizedFile(blueprintArchive, "cba.zip")
86 // Copy the File Part to Local File
87 BluePrintEnhancerUtils.copyFromFilePart(filePart, deCompressedFile)
88 // Save the Copied file to Database
89 val blueprintId = controllerBlueprintsCatalogService.saveToDatabase(saveId, deCompressedFile, false)
90 // Check and Return the Saved File
91 val blueprintModelSearch = blueprintModelSearchRepository.findById(blueprintId).get()
92 log.info("Save($saveId) successful for blueprint(${blueprintModelSearch.artifactName}) " +
93 "version(${blueprintModelSearch.artifactVersion})")
94 return blueprintModelSearch
95 } catch (e: IOException) {
96 throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value,
97 "Error in Save CBA: ${e.message}", e)
99 deleteDir(blueprintArchive)
105 * This is a searchBlueprintModels method
108 * @return List<BlueprintModelSearch>
109 </BlueprintModelSearch> */
110 open fun searchBlueprintModels(tags: String): List<BlueprintModelSearch> {
111 return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags)
115 * This is a getBlueprintModelSearchByNameAndVersion method
118 * @param version version
119 * @return BlueprintModelSearch
120 * @throws BluePrintException BluePrintException
122 @Throws(BluePrintException::class)
123 open fun getBlueprintModelSearchByNameAndVersion(name: String, version: String): BlueprintModelSearch {
124 val blueprintModelSearch: BlueprintModelSearch
125 val dbBlueprintModel = blueprintModelSearchRepository
126 .findByArtifactNameAndArtifactVersion(name, version)
127 if (dbBlueprintModel.isPresent) {
128 blueprintModelSearch = dbBlueprintModel.get()
130 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
131 String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version))
133 return blueprintModelSearch
137 * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
140 * @param version version
141 * @return ResponseEntity<Resource>
142 * @throws BluePrintException BluePrintException
144 @Throws(BluePrintException::class)
145 open fun downloadBlueprintModelFileByNameAndVersion(name: String,
146 version: String): ResponseEntity<Resource> {
147 val blueprintModel: BlueprintModel
149 blueprintModel = getBlueprintModelByNameAndVersion(name, version)
150 } catch (e: BluePrintException) {
151 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e)
154 val fileName = blueprintModel.id + ".zip"
155 val file = blueprintModel.blueprintModelContent.content
156 return prepareResourceEntity(fileName, file)
160 * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource
162 * @return ResponseEntity<Resource>
163 * @throws BluePrintException BluePrintException
165 @Throws(BluePrintException::class)
166 open fun downloadBlueprintModelFile(id: String): ResponseEntity<Resource> {
167 val blueprintModel: BlueprintModel
169 blueprintModel = getBlueprintModel(id)
170 } catch (e: BluePrintException) {
171 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e)
174 val fileName = blueprintModel.id + ".zip"
175 val file = blueprintModel.blueprintModelContent.content
176 return prepareResourceEntity(fileName, file)
180 * @return ResponseEntity<Resource>
182 private fun prepareResourceEntity(fileName: String, file: ByteArray): ResponseEntity<Resource> {
183 return ResponseEntity.ok()
184 .contentType(MediaType.parseMediaType("text/plain"))
185 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"")
186 .body(ByteArrayResource(file))
190 * This is a getBlueprintModel method
193 * @return BlueprintModel
194 * @throws BluePrintException BluePrintException
196 @Throws(BluePrintException::class)
197 open fun getBlueprintModel(id: String): BlueprintModel {
198 val blueprintModel: BlueprintModel
199 val dbBlueprintModel = blueprintModelRepository.findById(id)
200 if (dbBlueprintModel.isPresent) {
201 blueprintModel = dbBlueprintModel.get()
203 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
204 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
206 return blueprintModel
210 * This is a getBlueprintModelByNameAndVersion method
213 * @param version version
214 * @return BlueprintModel
215 * @throws BluePrintException BluePrintException
217 @Throws(BluePrintException::class)
218 open fun getBlueprintModelByNameAndVersion(name: String, version: String): BlueprintModel {
219 val blueprintModel = blueprintModelRepository
220 .findByArtifactNameAndArtifactVersion(name, version)
221 if (blueprintModel != null) {
222 return blueprintModel
224 val msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version)
225 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
230 * This is a getBlueprintModelSearch method
233 * @return BlueprintModelSearch
234 * @throws BluePrintException BluePrintException
236 @Throws(BluePrintException::class)
237 open fun getBlueprintModelSearch(id: String): BlueprintModelSearch {
238 val blueprintModelSearch: BlueprintModelSearch
239 val dbBlueprintModel = blueprintModelSearchRepository.findById(id)
240 if (dbBlueprintModel.isPresent) {
241 blueprintModelSearch = dbBlueprintModel.get()
243 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
244 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
247 return blueprintModelSearch
251 * This is a deleteBlueprintModel method
254 * @throws BluePrintException BluePrintException
257 @Throws(BluePrintException::class)
258 open fun deleteBlueprintModel(id: String) {
259 val dbBlueprintModel = blueprintModelRepository.findById(id)
260 if (dbBlueprintModel.isPresent) {
261 blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get())
262 blueprintModelRepository.delete(dbBlueprintModel.get())
264 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
265 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
270 * This is a CBA enrichBlueprint method
271 * Save the Zip File in archive location and extract the cba content.
272 * Populate the Enhancement Location
273 * Enhance the CBA content
274 * Compress the Enhanced Content
275 * Return back the the compressed content back to the caller.
277 * @param filePart filePart
278 * @return ResponseEntity<Resource>
279 * @throws BluePrintException BluePrintException
281 @Throws(BluePrintException::class)
282 open suspend fun enrichBlueprint(filePart: FilePart): ResponseEntity<Resource> {
283 val enhanceId = UUID.randomUUID().toString()
284 val blueprintArchive = normalizedPathName(bluePrintLoadConfiguration.blueprintArchivePath, enhanceId)
285 val blueprintWorkingDir = normalizedPathName(bluePrintLoadConfiguration.blueprintWorkingPath, enhanceId)
287 BluePrintEnhancerUtils.decompressFilePart(filePart, blueprintArchive, blueprintWorkingDir)
289 // Enhance the Blue Prints
290 bluePrintEnhancerService.enhance(blueprintWorkingDir)
292 return BluePrintEnhancerUtils.compressToFilePart(blueprintWorkingDir, blueprintArchive)
294 } catch (e: IOException) {
295 throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value,
296 "Error in Enriching CBA: ${e.message}", e)
298 BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintWorkingDir)
303 * This is a publishBlueprintModel method to change the status published to YES
305 * @param filePart filePart
306 * @return BlueprintModelSearch
307 * @throws BluePrintException BluePrintException
309 @Throws(BluePrintException::class)
310 open suspend fun publishBlueprint(filePart: FilePart): BlueprintModelSearch {
311 val publishId = UUID.randomUUID().toString()
312 val blueprintArchive = bluePrintLoadConfiguration.blueprintArchivePath.plus(File.separator).plus(publishId)
313 val blueprintWorkingDir = bluePrintLoadConfiguration.blueprintWorkingPath.plus(File.separator).plus(publishId)
315 val compressedFilePart = BluePrintEnhancerUtils
316 .extractCompressFilePart(filePart, blueprintArchive, blueprintWorkingDir)
318 val blueprintId = controllerBlueprintsCatalogService.saveToDatabase(publishId, compressedFilePart, true)
320 return blueprintModelSearchRepository.findById(blueprintId).get()
322 } catch (e: Exception) {
323 throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value,
324 "Error in Publishing CBA: ${e.message}", e)
326 BluePrintEnhancerUtils.cleanEnhancer(blueprintArchive, blueprintWorkingDir)
332 private const val BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo"
333 private const val BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" + " and version(%s) from repo"