8eb223907b761e92efcd8b9833ac78d9188112bd
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / api / WorkflowController.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.workflow.api;
18
19 import static org.onap.sdc.workflow.api.RestParams.LIMIT;
20 import static org.onap.sdc.workflow.api.RestParams.OFFSET;
21 import static org.onap.sdc.workflow.api.RestParams.SORT;
22 import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_LIMIT;
23 import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_OFFSET;
24
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiImplicitParam;
27 import io.swagger.annotations.ApiImplicitParams;
28 import io.swagger.annotations.ApiOperation;
29 import io.swagger.annotations.ApiParam;
30 import javax.validation.Valid;
31 import org.onap.sdc.workflow.api.types.Paging;
32 import org.onap.sdc.workflow.api.types.Sorting;
33 import org.onap.sdc.workflow.api.types.VersionStatesFormatter;
34 import org.onap.sdc.workflow.api.types.WorkflowStatusDto;
35 import org.onap.sdc.workflow.services.WorkflowManager;
36 import org.onap.sdc.workflow.services.WorkflowVersionManager;
37 import org.onap.sdc.workflow.services.annotations.UserId;
38 import org.onap.sdc.workflow.services.types.Page;
39 import org.onap.sdc.workflow.services.types.PagingRequest;
40 import org.onap.sdc.workflow.services.types.RequestSpec;
41 import org.onap.sdc.workflow.services.types.SortingRequest;
42 import org.onap.sdc.workflow.services.types.Workflow;
43 import org.onap.sdc.workflow.services.types.WorkflowStatus;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Qualifier;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.validation.annotation.Validated;
50 import org.springframework.web.bind.annotation.GetMapping;
51 import org.springframework.web.bind.annotation.PathVariable;
52 import org.springframework.web.bind.annotation.PostMapping;
53 import org.springframework.web.bind.annotation.PutMapping;
54 import org.springframework.web.bind.annotation.RequestBody;
55 import org.springframework.web.bind.annotation.RequestMapping;
56 import org.springframework.web.bind.annotation.RequestParam;
57 import org.springframework.web.bind.annotation.RestController;
58 import springfox.documentation.annotations.ApiIgnore;
59
60 @RequestMapping("/wf/workflows")
61 @Api("Workflows")
62 @RestController("workflowController")
63 public class WorkflowController {
64
65     private final WorkflowManager workflowManager;
66     private final WorkflowVersionManager workflowVersionManager;
67
68     @Autowired
69     public WorkflowController(@Qualifier("workflowManager") WorkflowManager workflowManager,
70             @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) {
71         this.workflowManager = workflowManager;
72         this.workflowVersionManager = workflowVersionManager;
73     }
74
75     @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
76     @ApiOperation("List workflows")
77     @ApiImplicitParams({@ApiImplicitParam(name = "versionState", dataType = "string", paramType = "query",
78             allowableValues = "DRAFT,CERTIFIED", value = "Filter by version state"),
79             @ApiImplicitParam(name = OFFSET, dataType = "string", paramType = "query", defaultValue = "0",
80                     value = "Index of the starting item"),
81             @ApiImplicitParam(name = LIMIT, dataType = "string", paramType = "query", defaultValue = "200",
82                     value = "Number of returned items"),
83             @ApiImplicitParam(name = SORT, dataType = "string", paramType = "query", defaultValue = "name:asc",
84                     value = "Sorting criteria in the format: property:(asc|desc). Default sort order is ascending.",
85                     allowableValues = "name:asc,name:desc"),
86             @ApiImplicitParam(name = "searchNameFilter", dataType = "string", paramType = "query",
87                     value = "Filter by workflow name"),
88             @ApiImplicitParam(name = "archiving", dataType = "string", paramType = "query",
89                     allowableValues = "ACTIVE,ARCHIVED", value = "Filter by workflow status")})
90     public Page<Workflow> list(@ApiIgnore String archiving, @ApiIgnore String searchNameFilter,
91             @ApiIgnore VersionStatesFormatter versionStateFilter, @ApiIgnore Paging paging,
92             @ApiIgnore Sorting sorting, @UserId String user) {
93         return workflowManager.list(archiving, searchNameFilter, versionStateFilter.getVersionStates(), initRequestSpec(paging, sorting));
94     }
95
96     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
97     @ApiOperation("Create workflow")
98     public ResponseEntity<Workflow> create(@Validated @RequestBody Workflow workflow, @UserId String user) {
99         return new ResponseEntity<>(workflowManager.create(workflow), HttpStatus.CREATED);
100     }
101
102     @GetMapping(path = "/{workflowId}")
103     @ApiOperation("Get workflow")
104     public Workflow get(@PathVariable("workflowId") String workflowId,
105             @ApiParam(value = "Expand workflow data", allowableValues = "versions")
106             @RequestParam(value = "expand", required = false) String expand, @UserId String user) {
107         Workflow workflow = new Workflow();
108         workflow.setId(workflowId);
109         Workflow retrievedWorkflow = workflowManager.get(workflow);
110         if ("versions".equals(expand)) {
111             retrievedWorkflow.setVersions(workflowVersionManager.list(workflowId, null));
112         }
113         return retrievedWorkflow;
114     }
115
116     @PutMapping(path = "/{workflowId}", consumes = MediaType.APPLICATION_JSON_VALUE)
117     @ApiOperation("Update workflow")
118     public Workflow update(@RequestBody Workflow workflow, @PathVariable("workflowId") String workflowId,
119             @UserId String user) {
120         workflow.setId(workflowId);
121         workflowManager.update(workflow);
122         return workflow;
123     }
124
125     @PostMapping(path = "/{workflowId}/archiving", consumes = MediaType.APPLICATION_JSON_VALUE)
126     @ApiOperation("Update workflow status")
127     public ResponseEntity updateStatus(@RequestBody @Valid WorkflowStatusDto request, @PathVariable("workflowId") String workflowId,
128             @UserId String user) {
129         workflowManager.updateStatus(workflowId,WorkflowStatus.valueOf(request.getStatus()));
130         return new ResponseEntity(HttpStatus.OK);
131     }
132
133     private RequestSpec initRequestSpec(Paging paging, Sorting sorting) {
134         return new RequestSpec(new PagingRequest(paging.getOffset() == null ? DEFAULT_OFFSET : paging.getOffset(),
135                 paging.getLimit() == null ? DEFAULT_LIMIT : paging.getLimit()),
136                 SortingRequest.builder().sorts(sorting.getSorts()).build());
137     }
138 }