aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/BlueprintModelController.kt
blob: f19824d4758e62189845fa55247dcbbd246536f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * Copyright © 2019 Bell Canada Intellectual Property.
 * Modifications Copyright © 2019 IBM.
 * Modifications Copyright © 2019 Orange.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.designer.api

import io.swagger.annotations.ApiOperation
import io.swagger.annotations.Api
import io.swagger.annotations.ApiParam
import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses
import org.jetbrains.annotations.NotNull
import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.domain.BlueprintModelSearch
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.BlueprintModelHandler
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.BlueprintSortByOption
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.mdcWebCoroutineScope
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
import org.springframework.core.io.Resource
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.http.codec.multipart.FilePart
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController

/**
 * BlueprintModelController Purpose: Handle controllerBlueprint API request
 *
 * @author Vinal Patel
 * @version 1.0
 */
@RestController
@RequestMapping("/api/v1/blueprint-model")
@Api(
    value = "Blueprint Model Catalog",
    description = "Manages all blueprint models which are available in CDS"
)
open class BlueprintModelController(private val bluePrintModelHandler: BlueprintModelHandler) {

    @PostMapping(
        path = arrayOf("/bootstrap"), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE),
        consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE)
    )
    @ApiOperation(
        value = "Bootstrap CDS",
        notes = "Loads all Model Types, Resource Dictionaries and Blueprint Models which are included in CDS by default. " +
            "Before starting to work with CDS, bootstrap should be called to load all the basic models that each orginization might support. " +
            "Parameter values can be set as `false`  to skip loading e.g. the Resource Dictionaries but this is not recommended."
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 500, message = "Internal Server Error")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun bootstrap(
        @ApiParam(required = true, value = "Specifies which elements to load")
        @RequestBody bootstrapRequest: BootstrapRequest
    ): Unit = mdcWebCoroutineScope {
        bluePrintModelHandler.bootstrapBlueprint(bootstrapRequest)
    }

    @PostMapping(produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    @ApiOperation(
        value = "Save a Blueprint Model",
        notes = "Saves a blueprint model by the given CBA zip file input. " +
            "There is no validation of the attached CBA happening when this API is called.",
        response = BlueprintModelSearch::class
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 500, message = "Internal Server Error")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun saveBlueprint(
        @ApiParam(name = "file", value = "CBA file to be uploaded (example: cba.zip)", required = true)
        @RequestPart("file") filePart: FilePart
    ): BlueprintModelSearch = mdcWebCoroutineScope {
        bluePrintModelHandler.saveBlueprintModel(filePart)
    }

    @GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "List all Blueprint Models",
        notes = "Lists all meta-data of blueprint models which are saved in CDS."
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 500, message = "Internal Server Error")
    )
    @ResponseBody
    @PreAuthorize("hasRole('USER')")
    fun allBlueprintModel(): List<BlueprintModelSearch> {
        return this.bluePrintModelHandler.allBlueprintModel()
    }

    @GetMapping("/paged", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Get Blueprints ordered",
        notes = "Lists all blueprint models which are saved in CDS in an ordered mode.",
        nickname = "BlueprintModelController_allBlueprintModelPaged_GET.org.onap.ccsdk.cds.blueprintsprocessor.designer.api"
    )
    @ResponseBody
    @PreAuthorize("hasRole('USER')")
    fun allBlueprintModel(
        @ApiParam(value = "Maximum number of returned blueprint models") @RequestParam(defaultValue = "20") limit: Int,
        @ApiParam(value = "Offset") @RequestParam(defaultValue = "0") offset: Int,
        @ApiParam(value = "Order of returned blueprint models") @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption,
        @ApiParam(value = "Ascend or descend ordering") @RequestParam(defaultValue = "ASC") sortType: String
    ): Page<BlueprintModelSearch> {
        val pageRequest = PageRequest.of(
            offset, limit,
            Sort.Direction.fromString(sortType), sort.columnName
        )
        return this.bluePrintModelHandler.allBlueprintModel(pageRequest)
    }

    @GetMapping("meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Search for Blueprints by a Keyword",
        notes = "Lists all blueprint models by a matching keyword in any of the meta-data of the blueprint models. " +
            "Blueprint models are just returned if a whole keyword is matching, not just parts of it. Not case-sensitive. " +
            "Used by CDS UI.",
        responseContainer = "List",
        response = BlueprintModelSearch::class
    )
    @ResponseBody
    @PreAuthorize("hasRole('USER')")
    suspend fun allBlueprintModelMetaData(
        @NotNull
        @ApiParam(value = "Keyword to search for in blueprint model meta-data", required = true, example = "pnf_netconf")
        @PathVariable(value = "keyword") keyWord: String
    ): List<BlueprintModelSearch> =
        mdcWebCoroutineScope {
            bluePrintModelHandler.searchBlueprintModelsByKeyWord(keyWord)
        }

    @GetMapping("/paged/meta-data/{keyword}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Search for Blueprints by a Keyword in an ordered mode",
        notes = "Lists all blueprint models by a matching keyword in any of the meta-data of the blueprint models in an ordered mode. " +
            "Blueprint models are just returned if a whole keyword is matching, not just parts of it. Not case-sensitive. " +
            "Used by CDS UI."
    )
    @ResponseBody
    @PreAuthorize("hasRole('USER')")
    fun allBlueprintModelMetaDataPaged(
        @ApiParam(value = "Keyword to search for in blueprint model meta-data", required = true, example = "pnf_netconf")
        @NotNull @PathVariable(value = "keyword") keyWord: String,
        @ApiParam(value = "Maximum number of returned blueprint models") @RequestParam(defaultValue = "20") limit: Int,
        @ApiParam(value = "Offset") @RequestParam(defaultValue = "0") offset: Int,
        @ApiParam(value = "Order of returned blueprint models") @RequestParam(defaultValue = "DATE") sort: BlueprintSortByOption,
        @ApiParam(value = "Ascend or descend ordering") @RequestParam(defaultValue = "ASC") sortType: String
    ): Page<BlueprintModelSearch> {
        val pageRequest = PageRequest.of(
            offset, limit,
            Sort.Direction.fromString(sortType), sort.columnName
        )
        return this.bluePrintModelHandler.searchBlueprintModelsByKeyWordPaged(keyWord, pageRequest)
    }

    @DeleteMapping("/{id}")
    @ApiOperation(
        value = "Delete a Blueprint Model by ID",
        notes = "Delete a blueprint model by its ID. ID is the internally created ID of blueprint, not the name of blueprint."
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 404, message = "RESOURCE_NOT_FOUND")
    )
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun deleteBlueprint(
        @ApiParam(value = "ID of the blueprint model to delete", required = true, example = "67ec1f96-ab55-4b81-aff9-23ee0ed1d7a4")
        @PathVariable(value = "id") id: String
    ) = mdcWebCoroutineScope {
        bluePrintModelHandler.deleteBlueprintModel(id)
    }

    @GetMapping("/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Get a Blueprint Model by Name and Version",
        notes = "Get Meta-Data of a Blueprint Model by its name and version.",
        response = BlueprintModelSearch::class
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 404, message = "Not Found")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun getBlueprintByNameAndVersion(
        @ApiParam(value = "Name of the blueprint model", required = true, example = "pnf_netconf") @PathVariable(value = "name") name: String,
        @ApiParam(value = "Version of the blueprint model", required = true, example = "1.0.0") @PathVariable(value = "version") version: String
    ): ResponseEntity<BlueprintModelSearch> = mdcWebCoroutineScope {
        val bluePrintModel: BlueprintModelSearch? =
            bluePrintModelHandler.getBlueprintModelSearchByNameAndVersion(name, version)
        if (bluePrintModel != null)
            ResponseEntity(bluePrintModel, HttpStatus.OK)
        else
            ResponseEntity(HttpStatus.NO_CONTENT)
    }

    @GetMapping("/download/by-name/{name}/version/{version}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Download a Blueprint Model",
        notes = "Gets the CBA of a blueprint model by its name and version. Response can be saved to a file to download the CBA."
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 404, message = "Not Found")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun downloadBlueprintByNameAndVersion(
        @ApiParam(value = "Name of the blueprint model", required = true, example = "pnf_netconf") @PathVariable(value = "name") name: String,
        @ApiParam(value = "Version of the blueprint model", required = true, example = "1.0.0") @PathVariable(value = "version") version: String
    ): ResponseEntity<Resource> = mdcWebCoroutineScope {
        bluePrintModelHandler.downloadBlueprintModelFileByNameAndVersion(name, version)
    }

    @GetMapping("/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Get a Blueprint Model by ID",
        notes = "Get meta-data of a blueprint model by its internally created ID.",
        response = BlueprintModelSearch::class
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 404, message = "Not Found")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun getBlueprintModel(
        @ApiParam(value = "ID of the blueprint model to search for", required = true, example = "67ec1f96-ab55-4b81-aff9-23ee0ed1d7a4")
        @PathVariable(value = "id") id: String
    ): BlueprintModelSearch = mdcWebCoroutineScope {
        bluePrintModelHandler.getBlueprintModelSearch(id)
    }

    @GetMapping("/download/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Download a Blueprint Model by ID",
        notes = "Gets the CBA of a blueprint model by its ID. Response can be saved to a file to download the CBA."
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 404, message = "Not Found")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun downloadBlueprint(
        @ApiParam(value = "ID of the blueprint model to download", required = true, example = "67ec1f96-ab55-4b81-aff9-23ee0ed1d7a4")
        @PathVariable(value = "id") id: String
    ): ResponseEntity<Resource> =
        mdcWebCoroutineScope {
            bluePrintModelHandler.downloadBlueprintModelFile(id)
        }

    @PostMapping(
        "/enrich", produces = [MediaType.APPLICATION_JSON_VALUE],
        consumes = [
            MediaType
                .MULTIPART_FORM_DATA_VALUE
        ]
    )
    @ApiOperation(
        value = "Enrich a Blueprint Model",
        notes = "Enriches the attached CBA and returns the enriched CBA zip file in the response. " +
            "The enrichment process will complete the package by providing all the definition of types used."
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun enrichBlueprint(
        @ApiParam(name = "file", value = "CBA zip file to be uploaded (example: cba_unenriched.zip)", required = true)
        @RequestPart("file") file: FilePart
    ): ResponseEntity<Resource> = mdcWebCoroutineScope {
        bluePrintModelHandler.enrichBlueprint(file)
    }

    @PostMapping(
        "/enrichandpublish", produces = [MediaType.APPLICATION_JSON_VALUE],
        consumes = [
            MediaType
                .MULTIPART_FORM_DATA_VALUE
        ]
    )
    @ApiOperation(
        value = "Enrich and publish a Blueprint Model",
        notes = "Enriches the attached CBA, validates it and saves it in CDS if validation was successful.",
        response = BlueprintModelSearch::class
    )
    @ApiResponses(
        ApiResponse(code = 200, message = "OK"),
        ApiResponse(code = 503, message = "Service Unavailable")
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun enrichAndPubishlueprint(
        @ApiParam(name = "file", value = "Unenriched CBA zip file to be uploaded (example: cba_unenriched.zip)", required = true)
        @RequestPart("file") file: FilePart
    ): BlueprintModelSearch = mdcWebCoroutineScope {
        bluePrintModelHandler.enrichAndPublishBlueprint(file)
    }

    @PostMapping("/publish", produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    @ApiOperation(
        value = "Publish a Blueprint Model",
        notes = "Validates the attached CBA file and saves it in CDS if validation was successful. CBA needs to be already enriched.",
        response = BlueprintModelSearch::class
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun publishBlueprint(
        @ApiParam(name = "file", value = "Enriched CBA zip file to be uploaded (example: cba_enriched.zip)", required = true)
        @RequestPart("file") file: FilePart
    ): BlueprintModelSearch = mdcWebCoroutineScope {
        bluePrintModelHandler.publishBlueprint(file)
    }

    @GetMapping("/search/{tags}", produces = [MediaType.APPLICATION_JSON_VALUE])
    @ApiOperation(
        value = "Search for a Blueprint by Tag",
        notes = "Searches for all blueprint models which contain the specified input parameter in their tags. " +
            "Blueprint models which contain just parts of the searched word in their tags are also returned.",
        responseContainer = "List",
        response = BlueprintModelSearch::class
    )
    @ResponseBody
    @PreAuthorize("hasRole('USER')")
    suspend fun searchBlueprintModels(
        @ApiParam(value = "Tag to search for", example = "test", required = true)
        @PathVariable(value = "tags") tags: String
    ): List<BlueprintModelSearch> =
        mdcWebCoroutineScope {
            bluePrintModelHandler.searchBlueprintModels(tags)
        }

    @DeleteMapping("/name/{name}/version/{version}")
    @ApiOperation(
        value = "Delete a Blueprint Model by Name",
        notes = "Deletes a blueprint model identified by its name and version from CDS.",
        // to avoid duplicate operation IDs
        nickname = "BlueprintModelController_deleteBlueprintByName_DELETE.org.onap.ccsdk.cds.blueprintsprocessor.designer.api",
        produces = MediaType.APPLICATION_JSON_VALUE
    )
    @PreAuthorize("hasRole('USER')")
    suspend fun deleteBlueprint(
        @ApiParam(value = "Name of the blueprint model", required = true, example = "pnf_netconf")
        @PathVariable(value = "name") name: String,
        @ApiParam(value = "Version of the blueprint model", required = true, example = "1.0.0")
        @PathVariable(value = "version") version: String
    ) = mdcWebCoroutineScope {
        bluePrintModelHandler.deleteBlueprintModel(name, version)
    }

    @PostMapping(
        path = arrayOf("/workflow-spec"),
        produces = arrayOf(
            MediaType
                .APPLICATION_JSON_VALUE
        ),
        consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE)
    )
    @ApiOperation(
        value = "Get Workflow Specification",
        notes = "Get the workflow of a blueprint identified by Blueprint and workflow name. " +
            "Inputs, outputs and data types of workflow is returned."
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun workflowSpec(
        @ApiParam(required = true, value = "Blueprint and workflow identification")
        @RequestBody workFlowSpecReq: WorkFlowSpecRequest
    ):
        ResponseEntity<String> = mdcWebCoroutineScope {
            var json = bluePrintModelHandler.prepareWorkFlowSpec(workFlowSpecReq)
                .asJsonString()
            ResponseEntity(json, HttpStatus.OK)
        }

    @GetMapping(
        path = arrayOf(
            "/workflows/blueprint-name/{name}/version/{version}"
        ),
        produces = arrayOf(MediaType.APPLICATION_JSON_VALUE)
    )
    @ApiOperation(
        value = "Get Workflows of a Blueprint",
        notes = "Get all available workflows of a Blueprint identified by its name and version."
    )
    @ResponseBody
    @Throws(BlueprintException::class)
    @PreAuthorize("hasRole('USER')")
    suspend fun getWorkflowList(
        @ApiParam(value = "Name of the blueprint model", example = "pnf_netconf", required = true)
        @PathVariable(value = "name") name: String,
        @ApiParam(value = "Version of the blueprint model", example = "1.0.0", required = true)
        @PathVariable(value = "version") version: String
    ): ResponseEntity<String> = mdcWebCoroutineScope {
        var json = bluePrintModelHandler.getWorkflowNames(name, version)
            .asJsonString()
        ResponseEntity(json, HttpStatus.OK)
    }
}