907566c3223fce3c99e0a825ddd9fece54d7b32b
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.controllerblueprints.service.handler
19
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
42
43 /**
44  * BlueprintModelHandler Purpose: Handler service to handle the request from BlurPrintModelRest
45  *
46  * @author Brinda Santh
47  * @version 1.0
48  */
49
50 @Service
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) {
55
56     /**
57      * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
58      *
59      * @return List<BlueprintModelSearch> list of the controller blueprint archives
60     </BlueprintModelSearch> */
61     open fun allBlueprintModel(): List<BlueprintModelSearch> {
62         return blueprintModelSearchRepository.findAll()
63     }
64
65     /**
66      * This is a saveBlueprintModel method
67      *
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> {
74         try {
75             val cbaLocation = BluePrintFileUtils.getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath)
76             return BluePrintEnhancerUtils.saveCBAFile(filePart, cbaLocation).map { fileName ->
77                 var blueprintId: String? = null
78                 try {
79                     blueprintId = bluePrintCatalogService.saveToDatabase(cbaLocation.resolve(fileName).toFile(), false)
80                 } catch (e: BluePrintException) {
81                     // FIXME handle expection
82                 }
83                 blueprintModelSearchRepository.findById(blueprintId!!).get()
84             }
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)
88         }
89
90     }
91
92     /**
93      * This is a publishBlueprintModel method to change the status published to YES
94      *
95      * @param id id
96      * @return BlueprintModelSearch
97      * @throws BluePrintException BluePrintException
98      */
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()
105         } else {
106             val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
107             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
108         }
109         blueprintModelSearch.published = ApplicationConstants.ACTIVE_Y
110         return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch)
111     }
112
113     /**
114      * This is a searchBlueprintModels method
115      *
116      * @param tags tags
117      * @return List<BlueprintModelSearch>
118     </BlueprintModelSearch> */
119     open fun searchBlueprintModels(tags: String): List<BlueprintModelSearch> {
120         return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags)
121     }
122
123     /**
124      * This is a getBlueprintModelSearchByNameAndVersion method
125      *
126      * @param name name
127      * @param version version
128      * @return BlueprintModelSearch
129      * @throws BluePrintException BluePrintException
130      */
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()
138         } else {
139             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
140                     String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version))
141         }
142         return blueprintModelSearch
143     }
144
145     /**
146      * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
147      *
148      * @param name name
149      * @param version version
150      * @return ResponseEntity<Resource>
151      * @throws BluePrintException BluePrintException
152     </Resource> */
153     @Throws(BluePrintException::class)
154     open fun downloadBlueprintModelFileByNameAndVersion(name: String,
155                                                         version: String): ResponseEntity<Resource> {
156         val blueprintModel: BlueprintModel
157         try {
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)
161         }
162
163         val fileName = blueprintModel.id + ".zip"
164         val file = blueprintModel.blueprintModelContent.content
165         return prepareResourceEntity(fileName, file)
166     }
167
168     /**
169      * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource
170      *
171      * @return ResponseEntity<Resource>
172      * @throws BluePrintException BluePrintException
173     </Resource> */
174     @Throws(BluePrintException::class)
175     open fun downloadBlueprintModelFile(id: String): ResponseEntity<Resource> {
176         val blueprintModel: BlueprintModel
177         try {
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)
181         }
182
183         val fileName = blueprintModel.id + ".zip"
184         val file = blueprintModel.blueprintModelContent.content
185         return prepareResourceEntity(fileName, file)
186     }
187
188     /**
189      * @return ResponseEntity<Resource>
190     </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))
196     }
197
198     /**
199      * This is a getBlueprintModel method
200      *
201      * @param id id
202      * @return BlueprintModel
203      * @throws BluePrintException BluePrintException
204      */
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()
211         } else {
212             val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
213             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
214         }
215         return blueprintModel
216     }
217
218     /**
219      * This is a getBlueprintModelByNameAndVersion method
220      *
221      * @param name name
222      * @param version version
223      * @return BlueprintModel
224      * @throws BluePrintException BluePrintException
225      */
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
232         } else {
233             val msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version)
234             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
235         }
236     }
237
238     /**
239      * This is a getBlueprintModelSearch method
240      *
241      * @param id id
242      * @return BlueprintModelSearch
243      * @throws BluePrintException BluePrintException
244      */
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()
251         } else {
252             val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
253             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
254         }
255
256         return blueprintModelSearch
257     }
258
259     /**
260      * This is a deleteBlueprintModel method
261      *
262      * @param id id
263      * @throws BluePrintException BluePrintException
264      */
265     @Transactional
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())
272         } else {
273             val msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id)
274             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, msg)
275         }
276     }
277
278     companion object {
279
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"
282     }
283 }