2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.service.handler
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants
22 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode
24 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch
28 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository
30 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository
31 import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils
32 import org.springframework.core.io.ByteArrayResource
33 import org.springframework.core.io.Resource
34 import org.springframework.http.HttpHeaders
35 import org.springframework.http.MediaType
36 import org.springframework.http.ResponseEntity
37 import org.springframework.http.codec.multipart.FilePart
38 import org.springframework.stereotype.Service
39 import org.springframework.transaction.annotation.Transactional
40 import reactor.core.publisher.Mono
41 import java.io.IOException
44 * BlueprintModelHandler Purpose: Handler service to handle the request from BlurPrintModelRest
46 * @author Brinda Santh
51 open class BluePrintModelHandler(private val bluePrintCatalogService: BluePrintCatalogService, private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
52 private val blueprintModelSearchRepository: ControllerBlueprintModelSearchRepository,
53 private val blueprintModelRepository: ControllerBlueprintModelRepository,
54 private val blueprintModelContentRepository: ControllerBlueprintModelContentRepository) {
57 * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
59 * @return List<BlueprintModelSearch> list of the controller blueprint archives
60 </BlueprintModelSearch> */
61 open fun allBlueprintModel(): List<BlueprintModelSearch> {
62 return blueprintModelSearchRepository.findAll()
66 * This is a saveBlueprintModel method
68 * @param filePart filePart
69 * @return Mono<BlueprintModelSearch>
70 * @throws BluePrintException BluePrintException
71 </BlueprintModelSearch> */
72 @Throws(BluePrintException::class)
73 open fun saveBlueprintModel(filePart: FilePart): Mono<BlueprintModelSearch> {
75 val cbaLocation = BluePrintFileUtils.getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath)
76 return BluePrintEnhancerUtils.saveCBAFile(filePart, cbaLocation).map { fileName ->
77 var blueprintId: String? = null
79 blueprintId = bluePrintCatalogService.saveToDatabase(cbaLocation.resolve(fileName).toFile(), false)
80 } catch (e: BluePrintException) {
81 // FIXME handle expection
83 blueprintModelSearchRepository.findById(blueprintId!!).get()
85 } catch (e: IOException) {
86 throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value,
87 String.format("I/O Error while uploading the CBA file: %s", e.message), e)
93 * This is a publishBlueprintModel method to change the status published to YES
96 * @return BlueprintModelSearch
97 * @throws BluePrintException BluePrintException
99 @Throws(BluePrintException::class)
100 open fun publishBlueprintModel(id: String): BlueprintModelSearch {
101 val blueprintModelSearch: BlueprintModelSearch
102 val dbBlueprintModel = blueprintModelSearchRepository.findById(id)
103 if (dbBlueprintModel.isPresent) {
104 blueprintModelSearch = dbBlueprintModel.get()
106 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
107 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
109 blueprintModelSearch.published = ApplicationConstants.ACTIVE_Y
110 return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch)
114 * This is a searchBlueprintModels method
117 * @return List<BlueprintModelSearch>
118 </BlueprintModelSearch> */
119 open fun searchBlueprintModels(tags: String): List<BlueprintModelSearch> {
120 return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags)
124 * This is a getBlueprintModelSearchByNameAndVersion method
127 * @param version version
128 * @return BlueprintModelSearch
129 * @throws BluePrintException BluePrintException
131 @Throws(BluePrintException::class)
132 open fun getBlueprintModelSearchByNameAndVersion(name: String, version: String): BlueprintModelSearch {
133 val blueprintModelSearch: BlueprintModelSearch
134 val dbBlueprintModel = blueprintModelSearchRepository
135 .findByArtifactNameAndArtifactVersion(name, version)
136 if (dbBlueprintModel.isPresent) {
137 blueprintModelSearch = dbBlueprintModel.get()
139 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
140 String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version))
142 return blueprintModelSearch
146 * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
149 * @param version version
150 * @return ResponseEntity<Resource>
151 * @throws BluePrintException BluePrintException
153 @Throws(BluePrintException::class)
154 open fun downloadBlueprintModelFileByNameAndVersion(name: String,
155 version: String): ResponseEntity<Resource> {
156 val blueprintModel: BlueprintModel
158 blueprintModel = getBlueprintModelByNameAndVersion(name, version)
159 } catch (e: BluePrintException) {
160 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e)
163 val fileName = blueprintModel.id + ".zip"
164 val file = blueprintModel.blueprintModelContent.content
165 return prepareResourceEntity(fileName, file)
169 * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource
171 * @return ResponseEntity<Resource>
172 * @throws BluePrintException BluePrintException
174 @Throws(BluePrintException::class)
175 open fun downloadBlueprintModelFile(id: String): ResponseEntity<Resource> {
176 val blueprintModel: BlueprintModel
178 blueprintModel = getBlueprintModel(id)
179 } catch (e: BluePrintException) {
180 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e)
183 val fileName = blueprintModel.id + ".zip"
184 val file = blueprintModel.blueprintModelContent.content
185 return prepareResourceEntity(fileName, file)
189 * @return ResponseEntity<Resource>
191 private fun prepareResourceEntity(fileName: String, file: ByteArray): ResponseEntity<Resource> {
192 return ResponseEntity.ok()
193 .contentType(MediaType.parseMediaType("text/plain"))
194 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$fileName\"")
195 .body(ByteArrayResource(file))
199 * This is a getBlueprintModel method
202 * @return BlueprintModel
203 * @throws BluePrintException BluePrintException
205 @Throws(BluePrintException::class)
206 open fun getBlueprintModel(id: String): BlueprintModel {
207 val blueprintModel: BlueprintModel
208 val dbBlueprintModel = blueprintModelRepository.findById(id)
209 if (dbBlueprintModel.isPresent) {
210 blueprintModel = dbBlueprintModel.get()
212 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
213 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
215 return blueprintModel
219 * This is a getBlueprintModelByNameAndVersion method
222 * @param version version
223 * @return BlueprintModel
224 * @throws BluePrintException BluePrintException
226 @Throws(BluePrintException::class)
227 open fun getBlueprintModelByNameAndVersion(name: String, version: String): BlueprintModel {
228 val blueprintModel = blueprintModelRepository
229 .findByArtifactNameAndArtifactVersion(name, version)
230 if (blueprintModel != null) {
231 return blueprintModel
233 val msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version)
234 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
239 * This is a getBlueprintModelSearch method
242 * @return BlueprintModelSearch
243 * @throws BluePrintException BluePrintException
245 @Throws(BluePrintException::class)
246 open fun getBlueprintModelSearch(id: String): BlueprintModelSearch {
247 val blueprintModelSearch: BlueprintModelSearch
248 val dbBlueprintModel = blueprintModelSearchRepository.findById(id)
249 if (dbBlueprintModel.isPresent) {
250 blueprintModelSearch = dbBlueprintModel.get()
252 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
253 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
256 return blueprintModelSearch
260 * This is a deleteBlueprintModel method
263 * @throws BluePrintException BluePrintException
266 @Throws(BluePrintException::class)
267 open fun deleteBlueprintModel(id: String) {
268 val dbBlueprintModel = blueprintModelRepository.findById(id)
269 if (dbBlueprintModel.isPresent) {
270 blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get())
271 blueprintModelRepository.delete(dbBlueprintModel.get())
273 val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
274 throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
280 private const val BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo"
281 private const val BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" + " and version(%s) from repo"