2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandlerinfra.tenantisolation;
25 import java.io.IOException;
26 import java.util.List;
27 import javax.inject.Provider;
28 import javax.transaction.Transactional;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.PATCH;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MediaType;
35 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.ErrorCode;
48 import org.onap.so.logger.MessageEnum;
49 import org.onap.so.serviceinstancebeans.RequestError;
50 import org.onap.so.serviceinstancebeans.ServiceException;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.stereotype.Component;
55 import com.fasterxml.jackson.annotation.JsonInclude.Include;
56 import com.fasterxml.jackson.core.JsonProcessingException;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58 import io.swagger.annotations.Api;
59 import io.swagger.annotations.ApiOperation;
62 @Path("/onap/so/infra/modelDistributions")
63 @Api(value = "/onap/so/infra/modelDistributions", description = "API Requests for Model Distributions")
64 public class ModelDistributionRequest {
66 private static Logger logger = LoggerFactory.getLogger(ModelDistributionRequest.class);
68 private Provider<TenantIsolationRunnable> tenantIsolationRunnable;
71 @Path("/{version:[vV][1]}/distributions/{distributionId}")
72 @Consumes(MediaType.APPLICATION_JSON)
73 @Produces(MediaType.APPLICATION_JSON)
74 @ApiOperation(value = "Update model distribution status", response = Response.class)
76 public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version,
77 @PathParam("distributionId") String distributionId) throws ApiException {
78 Distribution distributionRequest;
81 ObjectMapper mapper = new ObjectMapper();
82 distributionRequest = mapper.readValue(requestJSON, Distribution.class);
83 } catch (IOException e) {
84 ErrorLoggerInfo errorLoggerInfo =
85 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
89 ValidateException validateException =
90 new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(),
91 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
92 .errorInfo(errorLoggerInfo).build();
93 throw validateException;
98 parse(distributionRequest);
99 } catch (ValidationException e) {
101 ErrorLoggerInfo errorLoggerInfo =
102 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
106 ValidateException validateException =
107 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
108 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
109 throw validateException;
112 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
113 cor.setDistribution(distributionRequest);
114 cor.setDistributionId(distributionId);
116 TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
117 runnable.run(Action.distributionStatus, null, cor, null);
119 return Response.ok().build();
122 private void parse(Distribution distributionRequest) throws ValidationException {
123 if (distributionRequest.getStatus() == null) {
124 throw new ValidationException("status");
127 if (StringUtils.isBlank(distributionRequest.getErrorReason())
128 && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
129 throw new ValidationException("errorReason");
133 private Response buildServiceErrorResponse(int httpResponseCode, MsoException exceptionType, String text,
134 String messageId, List<String> variables) throws ApiException {
135 RequestError re = new RequestError();
136 ServiceException se = new ServiceException();
137 se.setMessageId(messageId);
139 if (variables != null) {
140 if (variables != null) {
141 for (String variable : variables) {
142 se.getVariables().add(variable);
146 re.setServiceException(se);
148 String requestErrorStr;
150 ObjectMapper mapper = new ObjectMapper();
151 mapper.setSerializationInclusion(Include.NON_DEFAULT);
152 requestErrorStr = mapper.writeValueAsString(re);
153 } catch (JsonProcessingException e) {
155 ErrorLoggerInfo errorLoggerInfo =
156 new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build();
159 ValidateException validateException =
160 new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(),
161 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
162 .errorInfo(errorLoggerInfo).build();
163 throw validateException;
166 return Response.status(httpResponseCode).entity(requestErrorStr).build();