diff options
Diffstat (limited to 'mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/ModelDistributionRequest.java')
-rw-r--r-- | mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/ModelDistributionRequest.java | 188 |
1 files changed, 97 insertions, 91 deletions
diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/ModelDistributionRequest.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/ModelDistributionRequest.java index 4be1ec9187..b2933df030 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/ModelDistributionRequest.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/ModelDistributionRequest.java @@ -24,7 +24,6 @@ package org.onap.so.apihandlerinfra.tenantisolation; import java.io.IOException; import java.util.List; - import javax.inject.Provider; import javax.transaction.Transactional; import javax.ws.rs.Consumes; @@ -34,7 +33,6 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; - import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.onap.so.apihandler.common.ErrorNumbers; @@ -54,110 +52,118 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; - import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; - import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @Component @Path("/onap/so/infra/modelDistributions") -@Api(value="/onap/so/infra/modelDistributions",description="API Requests for Model Distributions") +@Api(value = "/onap/so/infra/modelDistributions", description = "API Requests for Model Distributions") public class ModelDistributionRequest { - - private static Logger logger = LoggerFactory.getLogger(ModelDistributionRequest.class); - @Autowired - private Provider<TenantIsolationRunnable> tenantIsolationRunnable; - - @PATCH - @Path("/{version:[vV][1]}/distributions/{distributionId}") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value="Update model distribution status",response=Response.class) - @Transactional - public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version, @PathParam("distributionId") String distributionId) throws ApiException{ - long startTime = System.currentTimeMillis (); - Distribution distributionRequest = null; - - try { - ObjectMapper mapper = new ObjectMapper(); - distributionRequest = mapper.readValue(requestJSON, Distribution.class); - } catch(IOException e) { - ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, - ErrorCode.SchemaError).build(); - - - ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER) - .cause(e).errorInfo(errorLoggerInfo).build(); - throw validateException; - - } - - try { - parse(distributionRequest); - } catch(ValidationException e) { - - ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, - ErrorCode.SchemaError).build(); - - - ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER) - .cause(e).errorInfo(errorLoggerInfo).build(); - throw validateException; - } - - CloudOrchestrationRequest cor = new CloudOrchestrationRequest(); - cor.setDistribution(distributionRequest); - cor.setDistributionId(distributionId); - - TenantIsolationRunnable runnable = tenantIsolationRunnable.get(); - runnable.run(Action.distributionStatus, null, cor, null); - - return Response.ok().build(); - } - - private void parse(Distribution distributionRequest) throws ValidationException { - if(distributionRequest.getStatus() == null) { - throw new ValidationException("status"); - } - - if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) { - throw new ValidationException("errorReason"); - } - } - - private Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, String text, - String messageId, List<String> variables) throws ApiException{ - RequestError re = new RequestError(); - ServiceException se = new ServiceException(); - se.setMessageId(messageId); - se.setText(text); - if(variables != null){ - if(variables != null){ - for(String variable: variables){ - se.getVariables().add(variable); - } - } - } - re.setServiceException(se); + + private static Logger logger = LoggerFactory.getLogger(ModelDistributionRequest.class); + @Autowired + private Provider<TenantIsolationRunnable> tenantIsolationRunnable; + + @PATCH + @Path("/{version:[vV][1]}/distributions/{distributionId}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Update model distribution status", response = Response.class) + @Transactional + public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version, + @PathParam("distributionId") String distributionId) throws ApiException { + long startTime = System.currentTimeMillis(); + Distribution distributionRequest = null; + + try { + ObjectMapper mapper = new ObjectMapper(); + distributionRequest = mapper.readValue(requestJSON, Distribution.class); + } catch (IOException e) { + ErrorLoggerInfo errorLoggerInfo = + new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError) + .build(); + + + ValidateException validateException = + new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), + HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e) + .errorInfo(errorLoggerInfo).build(); + throw validateException; + + } + + try { + parse(distributionRequest); + } catch (ValidationException e) { + + ErrorLoggerInfo errorLoggerInfo = + new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError) + .build(); + + + ValidateException validateException = + new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST, + ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build(); + throw validateException; + } + + CloudOrchestrationRequest cor = new CloudOrchestrationRequest(); + cor.setDistribution(distributionRequest); + cor.setDistributionId(distributionId); + + TenantIsolationRunnable runnable = tenantIsolationRunnable.get(); + runnable.run(Action.distributionStatus, null, cor, null); + + return Response.ok().build(); + } + + private void parse(Distribution distributionRequest) throws ValidationException { + if (distributionRequest.getStatus() == null) { + throw new ValidationException("status"); + } + + if (StringUtils.isBlank(distributionRequest.getErrorReason()) + && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) { + throw new ValidationException("errorReason"); + } + } + + private Response buildServiceErrorResponse(int httpResponseCode, MsoException exceptionType, String text, + String messageId, List<String> variables) throws ApiException { + RequestError re = new RequestError(); + ServiceException se = new ServiceException(); + se.setMessageId(messageId); + se.setText(text); + if (variables != null) { + if (variables != null) { + for (String variable : variables) { + se.getVariables().add(variable); + } + } + } + re.setServiceException(se); String requestErrorStr = null; - try{ - ObjectMapper mapper = new ObjectMapper(); - mapper.setSerializationInclusion(Include.NON_DEFAULT); - requestErrorStr = mapper.writeValueAsString(re); - }catch(JsonProcessingException e){ + try { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(Include.NON_DEFAULT); + requestErrorStr = mapper.writeValueAsString(re); + } catch (JsonProcessingException e) { - ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build(); + ErrorLoggerInfo errorLoggerInfo = + new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build(); - ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER) - .cause(e).errorInfo(errorLoggerInfo).build(); - throw validateException; + ValidateException validateException = + new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), + HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e) + .errorInfo(errorLoggerInfo).build(); + throw validateException; } - return Response.status (httpResponseCode).entity(requestErrorStr).build (); + return Response.status(httpResponseCode).entity(requestErrorStr).build(); } } |