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.openecomp.mso.apihandlerinfra.tenantisolation;
23 import java.util.List;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import javax.xml.bind.ValidationException;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.http.HttpStatus;
35 import org.openecomp.mso.apihandler.common.ErrorNumbers;
36 import org.openecomp.mso.apihandlerinfra.Constants;
37 import org.openecomp.mso.apihandlerinfra.MsoException;
38 import org.openecomp.mso.apihandlerinfra.tenantisolationbeans.Action;
39 import org.openecomp.mso.apihandlerinfra.tenantisolationbeans.Distribution;
40 import org.openecomp.mso.apihandlerinfra.tenantisolationbeans.Status;
41 import org.openecomp.mso.logger.MessageEnum;
42 import org.openecomp.mso.logger.MsoLogger;
43 import org.openecomp.mso.serviceinstancebeans.RequestError;
44 import org.openecomp.mso.serviceinstancebeans.ServiceException;
46 import com.fasterxml.jackson.annotation.JsonInclude.Include;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.wordnik.swagger.annotations.Api;
49 import com.wordnik.swagger.annotations.ApiOperation;
50 import com.wordnik.swagger.jaxrs.PATCH;
52 @Path("/modelDistributions")
53 @Api(value="/modelDistributions",description="API Requests for Model Distributions")
54 public class ModelDistributionRequest {
56 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
57 private TenantIsolationRunnable tenantIsolation = null;
60 @Path("/{version:[vV][1]}/distributions/{distributionId}")
61 @Consumes(MediaType.APPLICATION_JSON)
62 @Produces(MediaType.APPLICATION_JSON)
63 @ApiOperation(value="Update model distribution status",response=Response.class)
64 public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version, @PathParam("distributionId") String distributionId) {
65 long startTime = System.currentTimeMillis ();
66 Distribution distributionRequest = null;
69 ObjectMapper mapper = new ObjectMapper();
70 distributionRequest = mapper.readValue(requestJSON, Distribution.class);
71 } catch(Exception e) {
72 msoLogger.debug ("Mapping of request to JSON object failed : ", e);
73 Response response = buildServiceErrorResponse(HttpStatus.SC_BAD_REQUEST,
74 MsoException.ServiceException,
75 "Mapping of request to JSON object failed. " + e.getMessage(),
76 ErrorNumbers.SVC_BAD_PARAMETER, null);
77 msoLogger.error (MessageEnum.APIH_REQUEST_VALIDATION_ERROR, Constants.MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.SchemaError, requestJSON, e);
78 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, "Mapping of request to JSON object failed");
79 msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
84 parse(distributionRequest);
85 } catch(Exception e) {
86 msoLogger.debug ("Validation failed: ", e);
87 msoLogger.error (MessageEnum.APIH_REQUEST_VALIDATION_ERROR, Constants.MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.SchemaError, requestJSON, e);
88 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.SchemaError, "Validation of the input request failed");
89 Response response = buildServiceErrorResponse(HttpStatus.SC_BAD_REQUEST,
90 MsoException.ServiceException,
91 "Error parsing request. " + e.getMessage(),
92 ErrorNumbers.SVC_BAD_PARAMETER, null);
93 msoLogger.debug ("End of the transaction, the final response is: " + (String) response.getEntity ());
97 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
98 cor.setDistribution(distributionRequest);
99 cor.setDistributionId(distributionId);
101 TenantIsolationRunnable runnable = getThread();
102 runnable.setAction(Action.distributionStatus);
103 runnable.setCor(cor);
104 runnable.setOperationalEnvType(null);
105 runnable.setRequestId(null);
107 Thread thread = new Thread(runnable);
110 return Response.ok().build();
113 private void parse(Distribution distributionRequest) throws ValidationException {
114 if(distributionRequest.getStatus() == null) {
115 throw new ValidationException("status");
118 if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
119 throw new ValidationException("errorReason");
123 private Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, String text,
124 String messageId, List<String> variables) {
125 RequestError re = new RequestError();
126 ServiceException se = new ServiceException();
127 se.setMessageId(messageId);
129 if(variables != null){
130 if(variables != null){
131 for(String variable: variables){
132 se.getVariables().add(variable);
136 re.setServiceException(se);
138 String requestErrorStr = null;
140 ObjectMapper mapper = new ObjectMapper();
141 mapper.setSerializationInclusion(Include.NON_DEFAULT);
142 requestErrorStr = mapper.writeValueAsString(re);
144 msoLogger.error (MessageEnum.APIH_VALIDATION_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in buildServiceErrorResponse writing exceptionType to string ", e);
147 return Response.status (httpResponseCode).entity(requestErrorStr).build ();
150 public TenantIsolationRunnable getThread() {
151 if(tenantIsolation == null) {
152 tenantIsolation = new TenantIsolationRunnable();
154 return tenantIsolation;
157 public void setThread(TenantIsolationRunnable thread) {
158 this.tenantIsolation = thread;