2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation;
23 import java.io.IOException;
24 import java.util.List;
26 import javax.inject.Provider;
27 import javax.transaction.Transactional;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
36 import org.apache.commons.lang3.StringUtils;
37 import org.apache.http.HttpStatus;
38 import org.onap.so.apihandler.common.ErrorNumbers;
39 import org.onap.so.apihandlerinfra.MsoException;
40 import org.onap.so.apihandlerinfra.exceptions.ApiException;
41 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
42 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
43 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
44 import org.onap.so.apihandlerinfra.tenantisolationbeans.Distribution;
45 import org.onap.so.apihandlerinfra.tenantisolationbeans.Status;
46 import org.onap.so.exceptions.ValidationException;
47 import org.onap.so.logger.MessageEnum;
48 import org.onap.so.logger.MsoLogger;
49 import org.onap.so.serviceinstancebeans.RequestError;
50 import org.onap.so.serviceinstancebeans.ServiceException;
51 import org.springframework.beans.factory.annotation.Autowired;
53 import com.fasterxml.jackson.annotation.JsonInclude.Include;
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 import io.swagger.annotations.Api;
58 import io.swagger.annotations.ApiOperation;
61 @Path("/onap/so/infra/modelDistributions")
62 @Api(value="/onap/so/infra/modelDistributions",description="API Requests for Model Distributions")
63 public class ModelDistributionRequest {
65 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH, ModelDistributionRequest.class);
67 private Provider<TenantIsolationRunnable> tenantIsolationRunnable;
70 @Path("/{version:[vV][1]}/distributions/{distributionId}")
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces(MediaType.APPLICATION_JSON)
73 @ApiOperation(value="Update model distribution status",response=Response.class)
75 public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version, @PathParam("distributionId") String distributionId) throws ApiException{
76 long startTime = System.currentTimeMillis ();
77 Distribution distributionRequest = null;
80 ObjectMapper mapper = new ObjectMapper();
81 distributionRequest = mapper.readValue(requestJSON, Distribution.class);
82 } catch(IOException e) {
83 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
86 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
87 .cause(e).errorInfo(errorLoggerInfo).build();
88 throw validateException;
93 parse(distributionRequest);
94 } catch(ValidationException e) {
96 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
99 ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
100 .cause(e).errorInfo(errorLoggerInfo).build();
101 throw validateException;
104 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
105 cor.setDistribution(distributionRequest);
106 cor.setDistributionId(distributionId);
108 TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
109 runnable.run(Action.distributionStatus, null, cor, null);
111 return Response.ok().build();
114 private void parse(Distribution distributionRequest) throws ValidationException {
115 if(distributionRequest.getStatus() == null) {
116 throw new ValidationException("status");
119 if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
120 throw new ValidationException("errorReason");
124 private Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, String text,
125 String messageId, List<String> variables) throws ApiException{
126 RequestError re = new RequestError();
127 ServiceException se = new ServiceException();
128 se.setMessageId(messageId);
130 if(variables != null){
131 if(variables != null){
132 for(String variable: variables){
133 se.getVariables().add(variable);
137 re.setServiceException(se);
139 String requestErrorStr = null;
141 ObjectMapper mapper = new ObjectMapper();
142 mapper.setSerializationInclusion(Include.NON_DEFAULT);
143 requestErrorStr = mapper.writeValueAsString(re);
144 }catch(JsonProcessingException e){
146 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR,MsoLogger.ErrorCode.DataError).build();
149 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
150 .cause(e).errorInfo(errorLoggerInfo).build();
151 throw validateException;
154 return Response.status (httpResponseCode).entity(requestErrorStr).build ();