Search in sources :

Example 11 with PropertyInstance

use of org.opentosca.toscana.core.transformation.properties.PropertyInstance 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)

Aggregations

PropertyInstance (org.opentosca.toscana.core.transformation.properties.PropertyInstance)11 Transformation (org.opentosca.toscana.core.transformation.Transformation)9 HashSet (java.util.HashSet)6 InputProperty (org.opentosca.toscana.core.transformation.properties.InputProperty)5 Csar (org.opentosca.toscana.core.csar.Csar)2 NoSuchPropertyException (org.opentosca.toscana.core.transformation.properties.NoSuchPropertyException)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 IllegalTransformationStateException (org.opentosca.toscana.api.exceptions.IllegalTransformationStateException)1 GetInputsResponse (org.opentosca.toscana.api.model.GetInputsResponse)1 InputWrap (org.opentosca.toscana.api.model.InputWrap)1 InputsResponse (org.opentosca.toscana.api.model.InputsResponse)1 BaseUnitTest (org.opentosca.toscana.core.BaseUnitTest)1 CsarImpl (org.opentosca.toscana.core.csar.CsarImpl)1 TransformationContext (org.opentosca.toscana.core.transformation.TransformationContext)1 TransformationImpl (org.opentosca.toscana.core.transformation.TransformationImpl)1 TransformationState (org.opentosca.toscana.core.transformation.TransformationState)1