Search in sources :

Example 1 with IllegalTransformationStateException

use of org.opentosca.toscana.api.exceptions.IllegalTransformationStateException in project TOSCAna by StuPro-TOSCAna.

the class TransformationController method getOutputs.

/**
 *     Returns the outputs of a Transformation.
 *     <p>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a List of the outputs. (Empty if no deployment has been executed)</td>
 *     </tr>
 *     <tr>
 *     <td>400</td>
 *     <td>application/json</td>
 *     <td>Returned if the transformation is not in a valid state (has to be in DONE or ERROR) to set inputs</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns a error message if the csar is not found or if the csar does not have a transformation for the given
 *     name (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@ApiOperation(value = "Retrieve the outputs and their values", tags = { "transformations" }, notes = "This operation returns the outputs of a deployment. Retrieval of the outputs is not possible " + "if the transformation (including deployment) is not done yet")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully", response = GetOutputsResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier or the CSAR does not have " + "a transformation for the specified platform", response = RestErrorResponse.class), @ApiResponse(code = 400, message = "The state of the transformation is invalid (not ERROR or DONE)", response = RestErrorResponse.class) })
@RequestMapping(path = "/{platform}/outputs", method = { RequestMethod.GET }, produces = "application/hal+json")
public ResponseEntity<GetOutputsResponse> getOutputs(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "csarId") String csarId, @ApiParam(value = "The identifier for the platform", required = true, example = "kubernetes") @PathVariable(name = "platform") String platformId) {
    Csar csar = findByCsarId(csarId);
    Transformation transformation = findTransformationByPlatform(csar, platformId);
    if (transformation.getState() != TransformationState.DONE && transformation.getState() != TransformationState.ERROR) {
        throw new IllegalTransformationStateException("The Transformation has not finished yet!");
    }
    List<OutputProperty> outputs = transformation.getOutputs();
    List<OutputWrap> wrappedOutputs = outputs.stream().map(OutputWrap::new).collect(Collectors.toList());
    return ResponseEntity.ok(new GetOutputsResponse(csarId, platformId, wrappedOutputs));
}
Also used : IllegalTransformationStateException(org.opentosca.toscana.api.exceptions.IllegalTransformationStateException) Csar(org.opentosca.toscana.core.csar.Csar) Transformation(org.opentosca.toscana.core.transformation.Transformation) GetOutputsResponse(org.opentosca.toscana.api.model.GetOutputsResponse) OutputProperty(org.opentosca.toscana.core.transformation.properties.OutputProperty) OutputWrap(org.opentosca.toscana.api.model.OutputWrap) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with IllegalTransformationStateException

use of org.opentosca.toscana.api.exceptions.IllegalTransformationStateException in project TOSCAna by StuPro-TOSCAna.

the class TransformationController method getTransformationArtifact.

/**
 *     Returns the URL to download the target Artifact (Wrapped in json)<p>
 *     <p>
 *     Accessed with http call <code>GET /csars/{csar}/transformations/{platform}/artifacts</code>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a Json object containing the download url for the artifact</td>
 *     </tr>
 *     <tr>
 *     <td>400</td>
 *     <td>application/json</td>
 *     <td>Returned if the transformation is not in a valid state (has to be in DONE or ERROR) to set inputs</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns a error message if the csar is not found or if the csar does not have a transformation for the given
 *     name (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@RequestMapping(path = "/{platform}/artifact", method = RequestMethod.GET, produces = "application/octet-stream")
@ApiOperation(value = "Download the target artifact archive", tags = { "transformations" }, notes = "Once the transformation is done (in the state DONE) or it has encountered a error (state ERROR). " + "It is possible to download a archive (ZIP format) of all the files generated while the transformation was " + "running.")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully"), @ApiResponse(code = 400, message = "There is nothing to download yet because the execution of the transformation has not yet started " + "or is not finished (With or without errors)", response = RestErrorResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier or the CSAR does not have " + "a Transformation for the specified platform.", response = RestErrorResponse.class) })
public ResponseEntity<Void> getTransformationArtifact(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "csarId") String csarName, @ApiParam(value = "The identifier for the platform", required = true, example = "kubernetes") @PathVariable(name = "platform") String platform, HttpServletResponse response) throws IOException {
    Csar csar = csarService.getCsar(csarName).orElseThrow(CsarNotFoundException::new);
    Transformation transformation = csar.getTransformation(platform).orElseThrow(TransformationNotFoundException::new);
    TargetArtifact artifact = transformation.getTargetArtifact().orElseThrow(() -> new IllegalTransformationStateException(format("Artifact for csar '{}' and platform '{}' not found", csarName, platform)));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + artifact.name + "\"");
    response.setHeader("Content-Type", "application/octet-stream");
    response.setHeader("Content-Length", artifact.getFileSize() + "");
    InputStream in = artifact.readAccess();
    OutputStream out = response.getOutputStream();
    IOUtils.copy(in, out);
    in.close();
    out.close();
    return ResponseEntity.ok().build();
}
Also used : TargetArtifact(org.opentosca.toscana.core.transformation.artifacts.TargetArtifact) IllegalTransformationStateException(org.opentosca.toscana.api.exceptions.IllegalTransformationStateException) Csar(org.opentosca.toscana.core.csar.Csar) Transformation(org.opentosca.toscana.core.transformation.Transformation) TransformationNotFoundException(org.opentosca.toscana.api.exceptions.TransformationNotFoundException) CsarNotFoundException(org.opentosca.toscana.api.exceptions.CsarNotFoundException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with IllegalTransformationStateException

use of org.opentosca.toscana.api.exceptions.IllegalTransformationStateException in project TOSCAna by StuPro-TOSCAna.

the class TransformationController method setInputs.

/**
 *     This mapping is used to post the inputs to the server.
 *     <p>
 *     Accessed with http call <code>PUT or POST /csars/{csar}/transformations/{platform}/inputs</code>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a empty body if all required inputs have been set</td>
 *     </tr>
 *     <tr>
 *     <td>400</td>
 *     <td>application/json</td>
 *     <td>Returned if the transformation is not in a valid state (has to be in INPUT_REQUIRED or READY)</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns an error message if the csar is not found or if the csar does not have a transformation for the given
 *     platformId (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@RequestMapping(path = "/{platform}/inputs", method = { RequestMethod.POST, RequestMethod.PUT }, produces = "application/json")
@ApiOperation(value = "Set the value of inputs", tags = { "transformations" }, notes = "With this method it is possible to set the value of an input or multiple inputs at once. The values " + "of inputs can be set as long as they are in the READY or INPUT_REQUIRED state. The transformation changes its state " + "to ready once all required inputs have a valid value assigned to them.")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully", response = Void.class), @ApiResponse(code = 400, message = "Inputs cannot get set once the transformation has been started.", response = RestErrorResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier or the CSAR does not have " + "a transformation for the specified platform.", response = RestErrorResponse.class), @ApiResponse(code = 406, message = "At least one of the inputs could not get set because either the key does not exist or the " + "syntax validation of the value has failed.", response = InputsResponse.class) })
public ResponseEntity<InputsResponse> setInputs(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "csarId") String csarId, @ApiParam(value = "The identifier for the platform", required = true, example = "kubernetes") @PathVariable(name = "platform") String platformId, @RequestBody InputsResponse propertiesRequest) {
    Csar csar = findByCsarId(csarId);
    Transformation transformation = findTransformationByPlatform(csar, platformId);
    List<TransformationState> validStates = Arrays.asList(INPUT_REQUIRED, READY);
    if (!Arrays.asList(INPUT_REQUIRED, READY).contains(transformation.getState())) {
        throw new IllegalTransformationStateException(String.format("The transformation is not in one of the states '%s'", validStates));
    }
    PropertyInstance inputs = transformation.getInputs();
    Map<String, Boolean> successes = new HashMap<>();
    boolean somethingFailed = false;
    // Set the properties and check their validity
    for (InputWrap entry : propertiesRequest.getInputs()) {
        try {
            boolean success = inputs.set(entry.getKey(), entry.getValue());
            successes.put(entry.getKey(), success);
            if (!success) {
                somethingFailed = true;
            }
        } catch (NoSuchPropertyException e) {
            logger.error("Failed to set inputs for transformation '%s'", transformation, e);
            somethingFailed = true;
            successes.put(entry.getKey(), false);
        }
    }
    if (!somethingFailed) {
        return ResponseEntity.ok().build();
    } else {
        Set<String> requestedKeys = new HashSet<>(successes.keySet());
        List<InputWrap> propWrapList = toPropertyWrapList(inputs, requestedKeys);
        // The request contains invalid values
        if (requestedKeys.size() > propWrapList.size()) {
            Set<String> knownKeys = propWrapList.stream().map(InputWrap::getKey).collect(Collectors.toSet());
            // Remove all known (valid) keys
            requestedKeys.removeAll(knownKeys);
            requestedKeys.forEach(e -> {
                propWrapList.add(new InputWrap(e, PropertyType.INVALID_KEY, "Invalid Key", null, false, null, false));
            });
        }
        InputsResponse response = new InputsResponse(propWrapList);
        return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(response);
    }
}
Also used : IllegalTransformationStateException(org.opentosca.toscana.api.exceptions.IllegalTransformationStateException) Csar(org.opentosca.toscana.core.csar.Csar) Transformation(org.opentosca.toscana.core.transformation.Transformation) HashMap(java.util.HashMap) InputWrap(org.opentosca.toscana.api.model.InputWrap) PropertyInstance(org.opentosca.toscana.core.transformation.properties.PropertyInstance) TransformationState(org.opentosca.toscana.core.transformation.TransformationState) InputsResponse(org.opentosca.toscana.api.model.InputsResponse) GetInputsResponse(org.opentosca.toscana.api.model.GetInputsResponse) NoSuchPropertyException(org.opentosca.toscana.core.transformation.properties.NoSuchPropertyException) HashSet(java.util.HashSet) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with IllegalTransformationStateException

use of org.opentosca.toscana.api.exceptions.IllegalTransformationStateException in project TOSCAna by StuPro-TOSCAna.

the class TransformationController method startTransformation.

/**
 *     This Mapping starts a transformation
 *     <p>
 *     Accessed with http call <code>GET /csars/{csar}/transformations/{platform}/start</code>
 *     <table summary="">
 *     <tr>
 *     <td>HTTP-Code</td>
 *     <td>Mime-Type</td>
 *     <td>Description (Returned if)</td>
 *     </tr>
 *     <tr>
 *     <td>200</td>
 *     <td>application/hal+json</td>
 *     <td>Returns a empty body if the transformation has been started</td>
 *     </tr>
 *     <tr>
 *     <td>400</td>
 *     <td>application/json</td>
 *     <td>Returned if the transformation is not in a valid state (Required Properties missing, Already running/Done) to
 *     set inputs</td>
 *     </tr>
 *     <tr>
 *     <td>404</td>
 *     <td>application/json</td>
 *     <td>Returns a error message if the csar is not found or if the csar does not have a transformation for the given
 *     name
 *     (see returned error message for details)</td>
 *     </tr>
 *     </table>
 */
@RequestMapping(path = "/{platform}/start", method = RequestMethod.POST, produces = "application/hal+json")
@ApiOperation(value = "Start a Transformation", tags = { "transformations" }, notes = "Starts a transformation that has been created and is ready to get started. To start a transformation, the " + "Transformation has to be in the state READY otherwise the transformation cannot start.")
@ApiResponses({ @ApiResponse(code = 200, message = "The operation was executed successfully"), @ApiResponse(code = 400, message = "The state of the transformation is illegal. This means that the transformation is not in the" + "READY state. Therefore starting it is not possible", response = RestErrorResponse.class), @ApiResponse(code = 404, message = "There is no CSAR for the given identifier or the CSAR does not have " + "a Transformation for the specified platform.", response = RestErrorResponse.class) })
public ResponseEntity startTransformation(@ApiParam(value = "The unique identifier for the CSAR", required = true, example = "test") @PathVariable(name = "csarId") String name, @ApiParam(value = "The identifier for the platform", required = true, example = "kubernetes") @PathVariable(name = "platform") String platform) {
    logger.info("Starting transformation for csar '{}' on '{}'", name, platform);
    Csar csar = findByCsarId(name);
    Transformation transformation = findTransformationByPlatform(csar, platform);
    if (transformationService.startTransformation(transformation)) {
        return ResponseEntity.ok().build();
    } else {
        throw new IllegalTransformationStateException("Transformation could not start because" + " its not in a valid state to start.");
    }
}
Also used : IllegalTransformationStateException(org.opentosca.toscana.api.exceptions.IllegalTransformationStateException) Csar(org.opentosca.toscana.core.csar.Csar) Transformation(org.opentosca.toscana.core.transformation.Transformation) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 IllegalTransformationStateException (org.opentosca.toscana.api.exceptions.IllegalTransformationStateException)4 Csar (org.opentosca.toscana.core.csar.Csar)4 Transformation (org.opentosca.toscana.core.transformation.Transformation)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 CsarNotFoundException (org.opentosca.toscana.api.exceptions.CsarNotFoundException)1 TransformationNotFoundException (org.opentosca.toscana.api.exceptions.TransformationNotFoundException)1 GetInputsResponse (org.opentosca.toscana.api.model.GetInputsResponse)1 GetOutputsResponse (org.opentosca.toscana.api.model.GetOutputsResponse)1 InputWrap (org.opentosca.toscana.api.model.InputWrap)1 InputsResponse (org.opentosca.toscana.api.model.InputsResponse)1 OutputWrap (org.opentosca.toscana.api.model.OutputWrap)1 TransformationState (org.opentosca.toscana.core.transformation.TransformationState)1 TargetArtifact (org.opentosca.toscana.core.transformation.artifacts.TargetArtifact)1 NoSuchPropertyException (org.opentosca.toscana.core.transformation.properties.NoSuchPropertyException)1