diff options
Diffstat (limited to 'workflow-designer-be/src/main/java/org/onap')
17 files changed, 292 insertions, 251 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java index 2173c05a..bb4320cc 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java @@ -1,33 +1,66 @@ package org.onap.sdc.workflow.api; +import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.onap.sdc.workflow.api.types.CollectionWrapper; import org.onap.sdc.workflow.persistence.types.Workflow; +import org.onap.sdc.workflow.services.WorkflowManager; +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.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.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; @RequestMapping("/workflows") @Api("Workflows") -public interface WorkflowController { +@RestController("workflowController") +public class WorkflowController { + + private final WorkflowManager workflowManager; + + @Autowired + public WorkflowController(@Qualifier("workflowManager") WorkflowManager workflowManager) { + this.workflowManager = workflowManager; + } @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("List workflows") - CollectionWrapper<Workflow> list(String user); + public CollectionWrapper<Workflow> list(@RequestHeader(USER_ID_HEADER_PARAM) String user) { + return new CollectionWrapper<>(workflowManager.list()); + } @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("Create workflow") - ResponseEntity<Workflow> create(Workflow workflow, String user); + public ResponseEntity<Workflow> create(@RequestBody Workflow workflow, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + return new ResponseEntity<>(workflowManager.create(workflow), HttpStatus.CREATED); + } @GetMapping(path = "/{workflowId}") @ApiOperation("Get workflow") - Workflow get(String workflowId, String user); + public Workflow get(@PathVariable("workflowId") String workflowId, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + Workflow workflow = new Workflow(); + workflow.setId(workflowId); + return workflowManager.get(workflow); + } @PutMapping(path = "/{workflowId}", consumes = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("Update workflow") - Workflow update(Workflow workflow, String workflowId, String user); + public Workflow update(@RequestBody Workflow workflow, @PathVariable("workflowId") String workflowId, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + workflow.setId(workflowId); + workflowManager.update(workflow); + return workflow; + } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java index cb1fb1f3..6214f1bb 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java @@ -1,51 +1,117 @@ package org.onap.sdc.workflow.api; +import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.onap.sdc.workflow.api.types.CollectionWrapper; import org.onap.sdc.workflow.api.types.VersionRequestDto; +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.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.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RequestMapping("/workflows/{workflowId}/versions") @Api("Workflow versions") -public interface WorkflowVersionController { +@RestController("workflowsVersionController") +public class WorkflowVersionController { + private final WorkflowVersionManager workflowVersionManager; + + @Autowired + public WorkflowVersionController( + @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) { + this.workflowVersionManager = workflowVersionManager; + } @GetMapping @ApiOperation("List workflow versions") - CollectionWrapper<WorkflowVersion> list(String workflowId,String user); + public CollectionWrapper<WorkflowVersion> list(@PathVariable("workflowId") String workflowId, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + return new CollectionWrapper<>(workflowVersionManager.list(workflowId)); + } @PostMapping @ApiOperation("Create workflow version") - ResponseEntity<WorkflowVersion> create(String workflowId, VersionRequestDto versionRequest, String user); + public ResponseEntity<WorkflowVersion> create(@RequestBody VersionRequestDto versionRequest, + @PathVariable("workflowId") String workflowId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + + WorkflowVersion createdVersion = workflowVersionManager.create(workflowId, versionRequest); + return new ResponseEntity<>(createdVersion, HttpStatus.CREATED); + } @GetMapping("/{versionId}") @ApiOperation("Get workflow version") - WorkflowVersion get(String workflowId,String versionId, String user); + public WorkflowVersion get(@PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + return workflowVersionManager.get(workflowId, versionId); + } @PutMapping("/{versionId}") @ApiOperation("Update workflow version") - void update(WorkflowVersion version,String workflowId, String versionId, String user); + public void update(@RequestBody WorkflowVersion version, @PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) 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_PARAM) 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_PARAM) String user) { + workflowVersionManager.updateState(workflowId, versionId, state.getName()); + return new VersionStateDto(state.getName()); + } @PutMapping("/{versionId}/artifact") @ApiOperation("Create/update artifact of a version") - void createArtifact(MultipartFile fileToUpload, String workflowId, String versionId, String user); + public void uploadArtifact(@RequestBody MultipartFile fileToUpload, @PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + workflowVersionManager.uploadArtifact(workflowId, versionId, fileToUpload); + } @GetMapping("/{versionId}/artifact") @ApiOperation("Download workflow version artifact") - ResponseEntity<Resource> getArtifact(String workflowId, String versionId, String user); + public ResponseEntity<Resource> getArtifact(@PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) 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") - void deleteArtifact(String workflowId, String versionId, String user); + public void deleteArtifact(@PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + workflowVersionManager.deleteArtifact(workflowId, versionId); + } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java index 0b5278ac..68fd41a5 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java @@ -7,8 +7,9 @@ import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY; import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException; import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException; -import org.onap.sdc.workflow.services.exceptions.CreateVersionException; +import org.onap.sdc.workflow.services.exceptions.VersionCreationException; import org.onap.sdc.workflow.services.exceptions.VersionModificationException; +import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -31,16 +32,17 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce return new ResponseEntity<>(exception.getMessage(), NOT_FOUND); } - @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class}) + @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class, + VersionStateModificationException.class}) public final ResponseEntity<String> handleInvalidArtifactException( Exception exception) { return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY); } - @ExceptionHandler(CreateVersionException.class) + @ExceptionHandler(VersionCreationException.class) public final ResponseEntity<String> handleVersioningErrorException( - CreateVersionException exception) { + VersionCreationException exception) { return new ResponseEntity<>(exception.getMessage(), FORBIDDEN); } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowControllerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowControllerImpl.java deleted file mode 100644 index 0d9ee4be..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowControllerImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.onap.sdc.workflow.api.impl; - -import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; - -import org.onap.sdc.workflow.api.WorkflowController; -import org.onap.sdc.workflow.api.types.CollectionWrapper; -import org.onap.sdc.workflow.persistence.types.Workflow; -import org.onap.sdc.workflow.services.WorkflowManager; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RestController; - -@RestController("workflowController") -public class WorkflowControllerImpl implements WorkflowController { - - private final WorkflowManager workflowManager; - - @Autowired - public WorkflowControllerImpl(@Qualifier("workflowManager") WorkflowManager workflowManager) { - this.workflowManager = workflowManager; - } - - @Override - public CollectionWrapper<Workflow> list(@RequestHeader(USER_ID_HEADER_PARAM) String user) { - return new CollectionWrapper<>(workflowManager.list()); - } - - @Override - public ResponseEntity<Workflow> create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - return new ResponseEntity<>(workflowManager.create(workflow), HttpStatus.CREATED); - } - - @Override - public Workflow get(@PathVariable("workflowId") String workflowId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - Workflow workflow = new Workflow(); - workflow.setId(workflowId); - return workflowManager.get(workflow); - } - - @Override - public Workflow update(@RequestBody Workflow workflow, @PathVariable("workflowId") String workflowId, - @RequestHeader(USER_ID_HEADER_PARAM) String user) { - workflow.setId(workflowId); - workflowManager.update(workflow); - return workflow; - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java deleted file mode 100644 index 0570ac85..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.onap.sdc.workflow.api.impl; - -import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; - -import org.onap.sdc.workflow.api.WorkflowVersionController; -import org.onap.sdc.workflow.api.types.CollectionWrapper; -import org.onap.sdc.workflow.api.types.VersionRequestDto; -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.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; - -@RestController("workflowsVersionController") -public class WorkflowVersionControllerImpl implements WorkflowVersionController { - - private final WorkflowVersionManager workflowVersionManager; - - @Autowired - public WorkflowVersionControllerImpl(@Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) { - this.workflowVersionManager = workflowVersionManager; - } - - @Override - public CollectionWrapper<WorkflowVersion> list(@PathVariable("workflowId") String workflowId, - @RequestHeader(USER_ID_HEADER_PARAM) String user) { - return new CollectionWrapper<>(workflowVersionManager.list(workflowId)); - } - - @Override - public ResponseEntity<WorkflowVersion> create(@PathVariable("workflowId") String workflowId,@RequestBody VersionRequestDto versionRequest, - @RequestHeader(USER_ID_HEADER_PARAM) String user) { - - WorkflowVersion createdVersion = workflowVersionManager.create(workflowId, versionRequest); - return new ResponseEntity<>(createdVersion, HttpStatus.CREATED); - } - - @Override - public WorkflowVersion get(@PathVariable("workflowId") String workflowId,@PathVariable("versionId") String versionId, - @RequestHeader(USER_ID_HEADER_PARAM) String user) { - return workflowVersionManager.get(workflowId,versionId); - } - - @Override - public void update(@RequestBody WorkflowVersion version,@PathVariable("workflowId") String workflowId, - @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - - version.setId(versionId); - workflowVersionManager.update(workflowId,version); - } - - @Override - public void createArtifact(@RequestBody MultipartFile fileToUpload, @PathVariable("workflowId") String workflowId, - @PathVariable("versionId") String versionId,@RequestHeader(USER_ID_HEADER_PARAM) String user) { - - workflowVersionManager.uploadArtifact(workflowId,new WorkflowVersion(versionId),fileToUpload); - - } - - @Override - public ResponseEntity<Resource> getArtifact(@PathVariable("workflowId") String workflowId, - @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - WorkflowVersion requestedVersion = new WorkflowVersion(versionId); - ArtifactEntity artifact = workflowVersionManager.getArtifact(workflowId, requestedVersion); - - return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + artifact.getFileName()) - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .body(new InputStreamResource(artifact.getArtifactData())); - } - - @Override - public void deleteArtifact(@PathVariable("workflowId") String workflowId, @PathVariable("versionId") String versionId, - @RequestHeader(USER_ID_HEADER_PARAM) String user) { - - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionStateDto.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionStateDto.java new file mode 100644 index 00000000..5963a2bf --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionStateDto.java @@ -0,0 +1,20 @@ +package org.onap.sdc.workflow.api.types; + +import java.util.List; +import lombok.Data; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; + +@Data +public class VersionStateDto { + + private WorkflowVersionState name; + private List<WorkflowVersionState> nextStates; + + public VersionStateDto() { + } + + public VersionStateDto(WorkflowVersionState state) { + name = state; + nextStates = state.getNextStates(); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java index ce90e3fd..db0999b6 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java @@ -13,12 +13,12 @@ public class WorkflowVersion { private String baseId; private Date creationTime; private Date modificationTime; - private WorkflowVersionStatus status; + private WorkflowVersionState state; public WorkflowVersion(String id) { this.id = id; - this.status = WorkflowVersionStatus.DRAFT; + this.state = WorkflowVersionState.DRAFT; } public WorkflowVersion() { diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersionState.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersionState.java new file mode 100644 index 00000000..9414fa17 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersionState.java @@ -0,0 +1,20 @@ +package org.onap.sdc.workflow.persistence.types; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public enum WorkflowVersionState { + + CERTIFIED, DRAFT(CERTIFIED); + + private final List<WorkflowVersionState> nextStates; + + WorkflowVersionState(WorkflowVersionState... nextStates) { + this.nextStates = Collections.unmodifiableList(Arrays.asList(nextStates)); + } + + public List<WorkflowVersionState> getNextStates() { + return nextStates; + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersionStatus.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersionStatus.java deleted file mode 100644 index 62f5847c..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersionStatus.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.onap.sdc.workflow.persistence.types; - -public enum WorkflowVersionStatus { - - DRAFT, CERTIFIED; -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java index ec9db1f7..30234526 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java @@ -2,8 +2,9 @@ package org.onap.sdc.workflow.services; import java.util.Collection; import org.onap.sdc.workflow.api.types.VersionRequestDto; -import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; import org.springframework.web.multipart.MultipartFile; @@ -11,16 +12,19 @@ public interface WorkflowVersionManager { Collection<WorkflowVersion> list(String workflowId); - WorkflowVersion get(String workflowId,String versionId); + WorkflowVersion create(String workflowId, VersionRequestDto versionRequest); + + void update(String id, WorkflowVersion version); - WorkflowVersion create(String workflowId, VersionRequestDto versionRequest); + WorkflowVersion get(String workflowId, String versionId); - void update(String id,WorkflowVersion version); + WorkflowVersionState getState(String workflowId, String versionId); - void uploadArtifact(String workflowId,WorkflowVersion version, MultipartFile artifact); + void updateState(String workflowId, String versionId, WorkflowVersionState state); - ArtifactEntity getArtifact(String workflowId,WorkflowVersion version); + ArtifactEntity getArtifact(String workflowId, String versionId); - void deleteArtifact(String workflowId,WorkflowVersion version); + void deleteArtifact(String workflowId, String versionId); + void uploadArtifact(String workflowId, String versionId, MultipartFile artifact); } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java deleted file mode 100644 index cadabb4c..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.onap.sdc.workflow.services.exceptions; - -public class CreateVersionException extends RuntimeException { - - public CreateVersionException(String workflowId, String version) { - super(String.format("Error creating new version for workflow with id %s. There is already a draft version %s", - workflowId, version)); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionCreationException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionCreationException.java new file mode 100644 index 00000000..af01c68f --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionCreationException.java @@ -0,0 +1,15 @@ +package org.onap.sdc.workflow.services.exceptions; + +public class VersionCreationException extends RuntimeException { + + private static final String MSG = "Error creating a new version for workflow with id %s"; + private static final String MSG_WITH_BASE_ID = MSG + " based on version %s"; + + public VersionCreationException(String workflowId, String baseVersionId) { + super(String.format(MSG_WITH_BASE_ID, workflowId, baseVersionId)); + } + + public VersionCreationException(String workflowId) { + super(String.format(MSG, workflowId)); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java index 35680475..64ab6103 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java @@ -2,8 +2,9 @@ package org.onap.sdc.workflow.services.exceptions; public class VersionModificationException extends RuntimeException { - public VersionModificationException(String workflowId, String version) { - super(String.format("Error while trying to modify version for workflow id %s. " - + "Version %s is CERTIFIED and can not be edited", workflowId, version)); + public VersionModificationException(String workflowId, String versionId) { + super(String.format( + "Error while trying to modify version %s of workflow %s: Version is CERTIFIED and can not be edited", + versionId, workflowId)); } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionStateModificationException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionStateModificationException.java new file mode 100644 index 00000000..6c4606db --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionStateModificationException.java @@ -0,0 +1,12 @@ +package org.onap.sdc.workflow.services.exceptions; + +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; + +public class VersionStateModificationException extends RuntimeException { + + public VersionStateModificationException(String workflowId, String versionId, WorkflowVersionState sourceState, + WorkflowVersionState targetState) { + super(String.format("Workflow %s, version %s: state can not be changed from %s to %s", workflowId, versionId, + sourceState.name(), targetState.name())); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java index e934efd3..d559f025 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java @@ -6,20 +6,21 @@ import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; import org.onap.sdc.workflow.api.types.VersionRequestDto; import org.onap.sdc.workflow.persistence.ArtifactRepository; import org.onap.sdc.workflow.persistence.types.ArtifactEntity; -import org.onap.sdc.workflow.persistence.types.WorkflowVersionStatus; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; import org.onap.sdc.workflow.services.WorkflowVersionManager; -import org.onap.sdc.workflow.services.exceptions.CreateVersionException; import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException; +import org.onap.sdc.workflow.services.exceptions.VersionCreationException; import org.onap.sdc.workflow.services.exceptions.VersionModificationException; +import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException; import org.onap.sdc.workflow.services.impl.mappers.VersionMapper; +import org.onap.sdc.workflow.services.impl.mappers.VersionStateMapper; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.types.VersionCreationMethod; @@ -31,54 +32,41 @@ import org.springframework.web.multipart.MultipartFile; @Service("workflowVersionManager") public class WorkflowVersionManagerImpl implements WorkflowVersionManager { + private static final String VERSION_NOT_EXIST_MSG = "version with id '%s' does not exist for workflow with id '%s'"; private final VersioningManager versioningManager; private final ArtifactRepository artifactRepository; - private VersionMapper versionMapper; + private final VersionMapper versionMapper; + private final VersionStateMapper versionStateMapper; @Autowired public WorkflowVersionManagerImpl(VersioningManager versioningManager, ArtifactRepository artifactRepository, - VersionMapper versionMapper) { + VersionMapper versionMapper, VersionStateMapper versionStateMapper) { this.versioningManager = versioningManager; this.artifactRepository = artifactRepository; this.versionMapper = versionMapper; + this.versionStateMapper = versionStateMapper; } @Override public Collection<WorkflowVersion> list(String workflowId) { - - return versioningManager.list(workflowId).stream() - .map(version -> versionMapper.versionToWorkflowVersion(version)) + return versioningManager.list(workflowId).stream().map(versionMapper::versionToWorkflowVersion) .collect(Collectors.toList()); } @Override public WorkflowVersion get(String workflowId, String versionId) { - - Version version = new Version(versionId); - try { - return this.versionMapper.versionToWorkflowVersion(versioningManager.get(workflowId, version)); - } catch (Exception e) { - throw new EntityNotFoundException( - String.format("version with id '%s' does not exist for workflow with id %s", version.getId(), - workflowId)); - } + return versionMapper.versionToWorkflowVersion(getVersion(workflowId, versionId)); } @Override public WorkflowVersion create(String workflowId, VersionRequestDto versionRequest) { - List<Version> versions = versioningManager.list(workflowId); if (versionRequest.getBaseVersionId() != null) { - Version baseVersion = findVersion(versions, versionRequest.getBaseVersionId()).orElseThrow( - () -> new EntityNotFoundException( - String.format("version with id '%s' does not exist for workflow with id %s", - versionRequest.getBaseVersionId(), workflowId))); - - if(!Certified.equals(baseVersion.getStatus())){ - throw new CreateVersionException(workflowId, versionRequest.getBaseVersionId()); - } + validateVersionExistAndCertified(workflowId, versions, versionRequest.getBaseVersionId()); + } else if (!versions.isEmpty()) { + throw new VersionCreationException(workflowId); } Version version = new Version(); @@ -86,40 +74,63 @@ public class WorkflowVersionManagerImpl implements WorkflowVersionManager { version.setBaseId(versionRequest.getBaseVersionId()); Version createdVersion = versioningManager.create(workflowId, version, VersionCreationMethod.major); - if (versions.isEmpty()){ + if (versions.isEmpty()) { // only for first version artifactRepository.createStructure(workflowId, createdVersion.getId()); + versioningManager.publish(workflowId, createdVersion, "Add workflow structure"); } - versioningManager.publish(workflowId, createdVersion, "Initial version"); - return versionMapper.versionToWorkflowVersion(createdVersion); } - private Optional<Version> findVersion(List<Version> versions, String baseVersionId) { - return versions.stream().filter(version -> baseVersionId.equals(version.getId())).findFirst(); + @Override + public void update(String workflowId, WorkflowVersion workflowVersion) { + Version retrievedVersion = getVersion(workflowId, workflowVersion.getId()); + if (WorkflowVersionState.CERTIFIED + .equals(versionStateMapper.versionStatusToWorkflowVersionState(retrievedVersion.getStatus()))) { + throw new VersionModificationException(workflowId, workflowVersion.getId()); + } + + Version version = versionMapper.workflowVersionToVersion(workflowVersion); + version.setName(retrievedVersion.getName()); + version.setStatus(retrievedVersion.getStatus()); + + versioningManager.updateVersion(workflowId, version); + versioningManager.publish(workflowId, version, "Update version"); } @Override - public void update(String id, WorkflowVersion version) { - - Version versionToUpdate = mapVersionInfo(id, version); - versioningManager.updateVersion(id, versionToUpdate); - versioningManager.publish(id, versionToUpdate, "Update version"); + public WorkflowVersionState getState(String workflowId, String versionId) { + return versionStateMapper.versionStatusToWorkflowVersionState(getVersion(workflowId, versionId).getStatus()); } + @Override + public void updateState(String workflowId, String versionId, WorkflowVersionState state) { + Version retrievedVersion = getVersion(workflowId, versionId); + WorkflowVersionState retrievedState = + versionStateMapper.versionStatusToWorkflowVersionState(retrievedVersion.getStatus()); + if (WorkflowVersionState.CERTIFIED.equals(retrievedState) || retrievedState.equals(state)) { + throw new VersionStateModificationException(workflowId, versionId, retrievedState, state); + } + + retrievedVersion.setStatus(versionStateMapper.workflowVersionStateToVersionStatus(state)); + versioningManager.updateVersion(workflowId, retrievedVersion); + versioningManager.publish(workflowId, retrievedVersion, + String.format("Update version state from %s to %s", retrievedState.name(), state.name())); + } @Override - public void uploadArtifact(String workflowId, WorkflowVersion version, MultipartFile artifact) { - WorkflowVersion retrievedVersion = get(workflowId, version.getId()); - if (WorkflowVersionStatus.CERTIFIED.equals(retrievedVersion.getStatus())) { - throw new VersionModificationException(workflowId, version.getId()); + public void uploadArtifact(String workflowId, String versionId, MultipartFile artifact) { + Version retrievedVersion = getVersion(workflowId, versionId); + if (WorkflowVersionState.CERTIFIED + .equals(versionStateMapper.versionStatusToWorkflowVersionState(retrievedVersion.getStatus()))) { + throw new VersionModificationException(workflowId, versionId); } try (InputStream artifactData = artifact.getInputStream()) { ArtifactEntity artifactEntity = new ArtifactEntity(StringUtils.cleanPath(artifact.getOriginalFilename()), artifactData); - artifactRepository.update(workflowId, version.getId(), artifactEntity); - versioningManager.publish(workflowId, versionMapper.workflowVersionToVersion(version), "Update Artifact"); + artifactRepository.update(workflowId, versionId, artifactEntity); + versioningManager.publish(workflowId, new Version(versionId), "Update Artifact"); } catch (IOException e) { throw new InvalidArtifactException(e.getMessage()); @@ -127,38 +138,49 @@ public class WorkflowVersionManagerImpl implements WorkflowVersionManager { } @Override - public ArtifactEntity getArtifact(String workflowId, WorkflowVersion version) { - WorkflowVersion retrievedVersion = get(workflowId, version.getId()); - Optional<ArtifactEntity> artifactOptional = artifactRepository.get(workflowId, retrievedVersion.getId()); + public ArtifactEntity getArtifact(String workflowId, String versionId) { + getVersion(workflowId, versionId); + Optional<ArtifactEntity> artifactOptional = artifactRepository.get(workflowId, versionId); if (!artifactOptional.isPresent()) { throw new EntityNotFoundException( - String.format("Artifact for workflow id %S version id %S was not found", workflowId, - version.getId())); + String.format("Artifact for workflow id %S version id %S was not found", workflowId, versionId)); } return artifactOptional.get(); } @Override - public void deleteArtifact(String workflowId, WorkflowVersion version) { - WorkflowVersion retrievedVersion = get(workflowId, version.getId()); - if (WorkflowVersionStatus.CERTIFIED.equals(retrievedVersion.getStatus())) { - throw new VersionModificationException(workflowId, version.getId()); + public void deleteArtifact(String workflowId, String versionId) { + WorkflowVersion retrievedVersion = get(workflowId, versionId); + if (WorkflowVersionState.CERTIFIED.equals(retrievedVersion.getState())) { + throw new VersionModificationException(workflowId, versionId); } - artifactRepository.delete(workflowId, version.getId()); + artifactRepository.delete(workflowId, versionId); + versioningManager.publish(workflowId, new Version(versionId), "Delete Artifact"); } - private boolean isFirstVersionCreation(String id, Version createdVersion) { - return Objects.isNull(createdVersion.getBaseId()) && versioningManager.list(id).size() == 1; - } + private void validateVersionExistAndCertified(String workflowId, List<Version> versions, String versionId) { + Version baseVersion = findVersion(versions, versionId).orElseThrow( + () -> new EntityNotFoundException(String.format(VERSION_NOT_EXIST_MSG, versionId, workflowId))); - protected Version mapVersionInfo(String id, WorkflowVersion workflowVersion) { - WorkflowVersion retrievedVersion = get(id, workflowVersion.getId()); - Version version = new Version(workflowVersion.getId()); - version.setBaseId(retrievedVersion.getBaseId()); - version.setName(retrievedVersion.getName()); - return version; + if (!Certified.equals(baseVersion.getStatus())) { + throw new VersionCreationException(workflowId, versionId); + } } + private Version getVersion(String workflowId, String versionId) { + try { + Version version = versioningManager.get(workflowId, new Version(versionId)); + if (version == null) { + throw new EntityNotFoundException(String.format(VERSION_NOT_EXIST_MSG, versionId, workflowId)); + } + return version; + } catch (Exception e) { + throw new EntityNotFoundException(String.format(VERSION_NOT_EXIST_MSG, versionId, workflowId)); + } + } + private static Optional<Version> findVersion(List<Version> versions, String versionId) { + return versions.stream().filter(version -> versionId.equals(version.getId())).findFirst(); + } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapper.java index 6e0c3d6d..bcf89661 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapper.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionMapper.java @@ -2,13 +2,15 @@ package org.onap.sdc.workflow.services.impl.mappers; import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.Mapper; +import org.mapstruct.Mapping; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.openecomp.sdc.versioning.dao.types.Version; -@Mapper(componentModel = "spring", uses = VersionStatusMapper.class) +@Mapper(componentModel = "spring", uses = VersionStateMapper.class) public interface VersionMapper { + @Mapping(source = "status", target = "state") WorkflowVersion versionToWorkflowVersion(Version version); @InheritInverseConfiguration diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionStatusMapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionStateMapper.java index 1cdfd69e..0c2f5391 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionStatusMapper.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/mappers/VersionStateMapper.java @@ -4,19 +4,19 @@ import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.Mapper; import org.mapstruct.ValueMapping; import org.mapstruct.ValueMappings; -import org.onap.sdc.workflow.persistence.types.WorkflowVersionStatus; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; import org.openecomp.sdc.versioning.dao.types.VersionStatus; @Mapper(componentModel = "spring") -public interface VersionStatusMapper { +public interface VersionStateMapper { @ValueMappings({@ValueMapping(source = "Certified", target = "CERTIFIED"), @ValueMapping(source = "Draft", target = "DRAFT"), @ValueMapping(source = "<ANY_REMAINING>", target = "DRAFT")}) - WorkflowVersionStatus versionStatusToWorkflowVersionStatus(VersionStatus status); + WorkflowVersionState versionStatusToWorkflowVersionState(VersionStatus status); @InheritInverseConfiguration - VersionStatus workflowVersionStatusToVersionStatus(WorkflowVersionStatus status); + VersionStatus workflowVersionStateToVersionStatus(WorkflowVersionState status); } |