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;
28 import javax.inject.Provider;
29 import javax.transaction.Transactional;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.PATCH;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.http.HttpStatus;
40 import org.onap.so.apihandler.common.ErrorNumbers;
41 import org.onap.so.apihandlerinfra.MsoException;
42 import org.onap.so.apihandlerinfra.exceptions.ApiException;
43 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
44 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
45 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
46 import org.onap.so.apihandlerinfra.tenantisolationbeans.Distribution;
47 import org.onap.so.apihandlerinfra.tenantisolationbeans.Status;
48 import org.onap.so.exceptions.ValidationException;
49 import org.onap.so.logger.ErrorCode;
50 import org.onap.so.logger.MessageEnum;
51 import org.onap.so.serviceinstancebeans.RequestError;
52 import org.onap.so.serviceinstancebeans.ServiceException;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Component;
58 import com.fasterxml.jackson.annotation.JsonInclude.Include;
59 import com.fasterxml.jackson.core.JsonProcessingException;
60 import com.fasterxml.jackson.databind.ObjectMapper;
62 import io.swagger.annotations.Api;
63 import io.swagger.annotations.ApiOperation;
66 @Path("/onap/so/infra/modelDistributions")
67 @Api(value="/onap/so/infra/modelDistributions",description="API Requests for Model Distributions")
68 public class ModelDistributionRequest {
70 private static Logger logger = LoggerFactory.getLogger(ModelDistributionRequest.class);
72 private Provider<TenantIsolationRunnable> tenantIsolationRunnable;
75 @Path("/{version:[vV][1]}/distributions/{distributionId}")
76 @Consumes(MediaType.APPLICATION_JSON)
77 @Produces(MediaType.APPLICATION_JSON)
78 @ApiOperation(value="Update model distribution status",response=Response.class)
80 public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version, @PathParam("distributionId") String distributionId) throws ApiException{
81 long startTime = System.currentTimeMillis ();
82 Distribution distributionRequest = null;
85 ObjectMapper mapper = new ObjectMapper();
86 distributionRequest = mapper.readValue(requestJSON, Distribution.class);
87 } catch(IOException e) {
88 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,
89 ErrorCode.SchemaError).build();
92 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
93 .cause(e).errorInfo(errorLoggerInfo).build();
94 throw validateException;
99 parse(distributionRequest);
100 } catch(ValidationException e) {
102 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,
103 ErrorCode.SchemaError).build();
106 ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
107 .cause(e).errorInfo(errorLoggerInfo).build();
108 throw validateException;
111 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
112 cor.setDistribution(distributionRequest);
113 cor.setDistributionId(distributionId);
115 TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
116 runnable.run(Action.distributionStatus, null, cor, null);
118 return Response.ok().build();
121 private void parse(Distribution distributionRequest) throws ValidationException {
122 if(distributionRequest.getStatus() == null) {
123 throw new ValidationException("status");
126 if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
127 throw new ValidationException("errorReason");
131 private Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, String text,
132 String messageId, List<String> variables) throws ApiException{
133 RequestError re = new RequestError();
134 ServiceException se = new ServiceException();
135 se.setMessageId(messageId);
137 if(variables != null){
138 if(variables != null){
139 for(String variable: variables){
140 se.getVariables().add(variable);
144 re.setServiceException(se);
146 String requestErrorStr = null;
148 ObjectMapper mapper = new ObjectMapper();
149 mapper.setSerializationInclusion(Include.NON_DEFAULT);
150 requestErrorStr = mapper.writeValueAsString(re);
151 }catch(JsonProcessingException e){
153 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build();
156 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
157 .cause(e).errorInfo(errorLoggerInfo).build();
158 throw validateException;
161 return Response.status (httpResponseCode).entity(requestErrorStr).build ();