aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java
blob: 67bea00c897cb244ca36611e97e42f3ea5c8fe6a (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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.onap.sdc.workflow.api.types.CollectionResponse;
import org.onap.sdc.workflow.api.types.VersionStateDto;
import org.onap.sdc.workflow.api.types.VersionStatesFormatter;
import org.onap.sdc.workflow.api.types.dto.ArtifactDeliveriesRequestDto;
import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
import org.onap.sdc.workflow.services.WorkflowVersionManager;
import org.onap.sdc.workflow.services.annotations.UserId;
import org.onap.sdc.workflow.services.types.WorkflowVersion;
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.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.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 org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

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

    private final WorkflowVersionManager workflowVersionManager;
    private final ArtifactAssociationService associationHandler;

    @Autowired
    public WorkflowVersionController(@Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager,
            @Qualifier("ArtifactAssociationHandler") ArtifactAssociationService artifactAssociationHandler) {
        this.workflowVersionManager = workflowVersionManager;
        this.associationHandler = artifactAssociationHandler;
    }

    @GetMapping
    @ApiOperation("List workflow versions")
    @ApiImplicitParam(name = "state", dataType = "string", paramType = "query", allowableValues = "DRAFT,CERTIFIED",
            value = "Filter by state")
    public CollectionResponse<WorkflowVersion> list(@PathVariable("workflowId") String workflowId,
            @ApiIgnore VersionStatesFormatter stateFilter, @UserId String user) {
        return new CollectionResponse<>(workflowVersionManager.list(workflowId, stateFilter.getVersionStates()));
    }

    @PostMapping
    @ApiOperation("Create workflow version")
    public ResponseEntity<WorkflowVersion> create(@RequestBody @Valid WorkflowVersion version,
            @PathVariable("workflowId") String workflowId,
            @RequestParam(value = "baseVersionId", required = false) String baseVersionId, @UserId 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, @UserId String user) {
        return workflowVersionManager.get(workflowId, versionId);
    }

    @PutMapping("/{versionId}")
    @ApiOperation("Update workflow version")
    public void update(@RequestBody @Valid WorkflowVersion version, @PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @UserId 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, @UserId 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,
            @UserId String user) {
        workflowVersionManager.updateState(workflowId, versionId, state.getName());
        return new VersionStateDto(state.getName());
    }

    @PostMapping("/{versionId}/artifact-deliveries")
    @ApiOperation("upload of artifact to VF operation workflow")
    public ResponseEntity<String> artifactDeliveries(@RequestBody ArtifactDeliveriesRequestDto deliveriesRequestDto,
            @PathVariable("workflowId") String workflowId, @PathVariable("versionId") String versionId,
            @UserId String user) {
        return associationHandler
                       .execute(user, deliveriesRequestDto, workflowVersionManager.getArtifact(workflowId, versionId));
    }

    @PutMapping("/{versionId}/artifact")
    @ApiOperation("Create/update artifact of a version")
    public void uploadArtifact(@RequestBody MultipartFile fileToUpload, @PathVariable("workflowId") String workflowId,
            @PathVariable("versionId") String versionId, @UserId 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, @UserId 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, @UserId String user) {
        workflowVersionManager.deleteArtifact(workflowId, versionId);
    }
}