aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java
blob: ba15f9f777742a809bce0dc6f859ca811a056389 (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
/*
 * 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.RestUtils.formatVersionStates;
import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.onap.sdc.workflow.api.types.CollectionResponse;
import org.onap.sdc.workflow.api.types.VersionStateDto;
import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
import org.onap.sdc.workflow.persistence.types.WorkflowVersion;
import org.onap.sdc.workflow.services.WorkflowVersionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
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.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/workflows/{workflowId}/versions")
@Api("Workflow versions")
@RestController("workflowsVersionController")
public class WorkflowVersionController {

    private final WorkflowVersionManager workflowVersionManager;
    private Validator validator;

    @InitBinder("WorkflowVersion")
    private void initBinder(WebDataBinder binder) {
        binder.addValidators(validator);
    }

    @Autowired
    public WorkflowVersionController(@Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager,
            @Qualifier("workflowVersionValidator") Validator validator) {
        this.workflowVersionManager = workflowVersionManager;
        this.validator = validator;
    }

    @GetMapping
    @ApiOperation("List workflow versions")
    public CollectionResponse<WorkflowVersion> list(@PathVariable("workflowId") String workflowId,
            @ApiParam(value = "Filter by state", allowableValues = "DRAFT,CERTIFIED")
            @RequestParam(value = "state", required = false) String stateFilter,
            @RequestHeader(USER_ID_HEADER) String user) {
        return new CollectionResponse<>(workflowVersionManager.list(workflowId, formatVersionStates(stateFilter)));
    }

    @PostMapping
    @ApiOperation("Create workflow version")
    public ResponseEntity<WorkflowVersion> create(@RequestBody @Validated WorkflowVersion version,
            @PathVariable("workflowId") String workflowId,
            @RequestParam(value = "baseVersionId", required = false) String baseVersionId,
            @RequestHeader(USER_ID_HEADER) String user) {
        WorkflowVersion createdVersion = workflowVersionManager.create(workflowId, baseVersionId, version);
        return new ResponseEntity<>(createdVersion, HttpStatus.CREATED);
    }

    @GetMapping("/{versionId}")
    @ApiOperation("Get workflow version")
    public WorkflowVersion get(@PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) {
        return workflowVersionManager.get(workflowId, versionId);
    }

    @PutMapping("/{versionId}")
    @ApiOperation("Update workflow version")
    public void update(@RequestBody @Validated WorkflowVersion version, @PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) {
        version.setId(versionId);
        workflowVersionManager.update(workflowId, version);
    }

    @GetMapping("/{versionId}/state")
    @ApiOperation("Get workflow version state")
    public VersionStateDto getState(@PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) {
        return new VersionStateDto(workflowVersionManager.getState(workflowId, versionId));
    }

    @PostMapping("/{versionId}/state")
    @ApiOperation("Update workflow version state")
    public VersionStateDto updateState(@RequestBody VersionStateDto state,
            @PathVariable("workflowId") String workflowId, @PathVariable("versionId") String versionId,
            @RequestHeader(USER_ID_HEADER) String user) {
        workflowVersionManager.updateState(workflowId, versionId, state.getName());
        return new VersionStateDto(state.getName());
    }

    @PutMapping("/{versionId}/artifact")
    @ApiOperation("Create/update artifact of a version")
    public void uploadArtifact(@RequestBody MultipartFile fileToUpload, @PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) {
        workflowVersionManager.uploadArtifact(workflowId, versionId, fileToUpload);
    }

    @GetMapping("/{versionId}/artifact")
    @ApiOperation("Download workflow version artifact")
    public ResponseEntity<Resource> getArtifact(@PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) {
        ArtifactEntity artifact = workflowVersionManager.getArtifact(workflowId, versionId);

        return ResponseEntity.ok()
                             .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + artifact.getFileName())
                             .contentType(MediaType.APPLICATION_OCTET_STREAM)
                             .body(new InputStreamResource(artifact.getArtifactData()));
    }

    @DeleteMapping("/{versionId}/artifact")
    @ApiOperation("Delete workflow version artifact")
    public void deleteArtifact(@PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) {
        workflowVersionManager.deleteArtifact(workflowId, versionId);
    }
}