diff options
Diffstat (limited to 'sdc-workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java')
-rw-r--r-- | sdc-workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/sdc-workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java b/sdc-workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java new file mode 100644 index 00000000..ff06f828 --- /dev/null +++ b/sdc-workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java @@ -0,0 +1,138 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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.sdc.workflow.api; + +import static org.onap.sdc.workflow.api.RestParams.LIMIT; +import static org.onap.sdc.workflow.api.RestParams.OFFSET; +import static org.onap.sdc.workflow.api.RestParams.SORT; +import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_LIMIT; +import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_OFFSET; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import javax.validation.Valid; +import org.onap.sdc.workflow.api.types.Paging; +import org.onap.sdc.workflow.api.types.Sorting; +import org.onap.sdc.workflow.api.types.VersionStatesFormatter; +import org.onap.sdc.workflow.api.types.WorkflowStatusDto; +import org.onap.sdc.workflow.services.WorkflowManager; +import org.onap.sdc.workflow.services.WorkflowVersionManager; +import org.onap.sdc.workflow.services.annotations.UserId; +import org.onap.sdc.workflow.services.types.ArchivingStatus; +import org.onap.sdc.workflow.services.types.Page; +import org.onap.sdc.workflow.services.types.PagingRequest; +import org.onap.sdc.workflow.services.types.RequestSpec; +import org.onap.sdc.workflow.services.types.SortingRequest; +import org.onap.sdc.workflow.services.types.Workflow; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +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.PutMapping; +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.RestController; +import springfox.documentation.annotations.ApiIgnore; + +@RequestMapping("/wf/workflows") +@Api("Workflows") +@RestController("workflowController") +public class WorkflowController { + + private final WorkflowManager workflowManager; + private final WorkflowVersionManager workflowVersionManager; + + @Autowired + public WorkflowController(@Qualifier("workflowManager") WorkflowManager workflowManager, + @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) { + this.workflowManager = workflowManager; + this.workflowVersionManager = workflowVersionManager; + } + + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation("List workflows") + @ApiImplicitParams({@ApiImplicitParam(name = "versionState", dataType = "string", paramType = "query", + allowableValues = "DRAFT,CERTIFIED", value = "Filter by version state"), + @ApiImplicitParam(name = OFFSET, dataType = "string", paramType = "query", defaultValue = "0", + value = "Index of the starting item"), + @ApiImplicitParam(name = LIMIT, dataType = "string", paramType = "query", defaultValue = "200", + value = "Number of returned items"), + @ApiImplicitParam(name = SORT, dataType = "string", paramType = "query", defaultValue = "name:asc", + value = "Sorting criteria in the format: property:(asc|desc). Default sort order is ascending.", + allowableValues = "name:asc,name:desc"), + @ApiImplicitParam(name = "searchNameFilter", dataType = "string", paramType = "query", + value = "Filter by workflow name"), + @ApiImplicitParam(name = "archiving", dataType = "string", paramType = "query", + allowableValues = "ACTIVE,ARCHIVED", value = "Filter by workflow status")}) + public Page<Workflow> list(@ApiIgnore String archiving, @ApiIgnore String searchNameFilter, + @ApiIgnore VersionStatesFormatter versionStateFilter, @ApiIgnore Paging paging, + @ApiIgnore Sorting sorting, @UserId String user) { + return workflowManager.list(archiving, searchNameFilter, versionStateFilter.getVersionStates(), initRequestSpec(paging, sorting)); + } + + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation("Create workflow") + public ResponseEntity<Workflow> create(@Validated @RequestBody Workflow workflow, @UserId String user) { + return new ResponseEntity<>(workflowManager.create(workflow), HttpStatus.CREATED); + } + + @GetMapping(path = "/{workflowId}") + @ApiOperation("Get workflow") + public Workflow get(@PathVariable("workflowId") String workflowId, + @ApiParam(value = "Expand workflow data", allowableValues = "versions") + @RequestParam(value = "expand", required = false) String expand, @UserId String user) { + Workflow workflow = new Workflow(); + workflow.setId(workflowId); + Workflow retrievedWorkflow = workflowManager.get(workflow); + if ("versions".equals(expand)) { + retrievedWorkflow.setVersions(workflowVersionManager.list(workflowId, null)); + } + return retrievedWorkflow; + } + + @PutMapping(path = "/{workflowId}", consumes = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation("Update workflow") + public Workflow update(@RequestBody Workflow workflow, @PathVariable("workflowId") String workflowId, + @UserId String user) { + workflow.setId(workflowId); + workflowManager.update(workflow); + return workflow; + } + + @PostMapping(path = "/{workflowId}/archiving", consumes = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation("Update workflow status") + public ResponseEntity updateStatus(@RequestBody @Valid WorkflowStatusDto request, @PathVariable("workflowId") String workflowId, + @UserId String user) { + workflowManager.updateStatus(workflowId, ArchivingStatus.valueOf(request.getStatus())); + return new ResponseEntity(HttpStatus.OK); + } + + private RequestSpec initRequestSpec(Paging paging, Sorting sorting) { + return new RequestSpec(new PagingRequest(paging.getOffset() == null ? DEFAULT_OFFSET : paging.getOffset(), + paging.getLimit() == null ? DEFAULT_LIMIT : paging.getLimit()), + SortingRequest.builder().sorts(sorting.getSorts()).build()); + } +} |