Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / tenantisolation / ModelDistributionRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.apihandlerinfra.tenantisolation;
24
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;
60
61 @Component
62 @Path("/onap/so/infra/modelDistributions")
63 @Api(value = "/onap/so/infra/modelDistributions", description = "API Requests for Model Distributions")
64 public class ModelDistributionRequest {
65
66     private static Logger logger = LoggerFactory.getLogger(ModelDistributionRequest.class);
67     @Autowired
68     private Provider<TenantIsolationRunnable> tenantIsolationRunnable;
69
70     @PATCH
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)
75     @Transactional
76     public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version,
77             @PathParam("distributionId") String distributionId) throws ApiException {
78         long startTime = System.currentTimeMillis();
79         Distribution distributionRequest = null;
80
81         try {
82             ObjectMapper mapper = new ObjectMapper();
83             distributionRequest = mapper.readValue(requestJSON, Distribution.class);
84         } catch (IOException e) {
85             ErrorLoggerInfo errorLoggerInfo =
86                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
87                             .build();
88
89
90             ValidateException validateException =
91                     new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(),
92                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
93                                     .errorInfo(errorLoggerInfo).build();
94             throw validateException;
95
96         }
97
98         try {
99             parse(distributionRequest);
100         } catch (ValidationException e) {
101
102             ErrorLoggerInfo errorLoggerInfo =
103                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
104                             .build();
105
106
107             ValidateException validateException =
108                     new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
109                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
110             throw validateException;
111         }
112
113         CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
114         cor.setDistribution(distributionRequest);
115         cor.setDistributionId(distributionId);
116
117         TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
118         runnable.run(Action.distributionStatus, null, cor, null);
119
120         return Response.ok().build();
121     }
122
123     private void parse(Distribution distributionRequest) throws ValidationException {
124         if (distributionRequest.getStatus() == null) {
125             throw new ValidationException("status");
126         }
127
128         if (StringUtils.isBlank(distributionRequest.getErrorReason())
129                 && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
130             throw new ValidationException("errorReason");
131         }
132     }
133
134     private Response buildServiceErrorResponse(int httpResponseCode, MsoException exceptionType, String text,
135             String messageId, List<String> variables) throws ApiException {
136         RequestError re = new RequestError();
137         ServiceException se = new ServiceException();
138         se.setMessageId(messageId);
139         se.setText(text);
140         if (variables != null) {
141             if (variables != null) {
142                 for (String variable : variables) {
143                     se.getVariables().add(variable);
144                 }
145             }
146         }
147         re.setServiceException(se);
148
149         String requestErrorStr = null;
150         try {
151             ObjectMapper mapper = new ObjectMapper();
152             mapper.setSerializationInclusion(Include.NON_DEFAULT);
153             requestErrorStr = mapper.writeValueAsString(re);
154         } catch (JsonProcessingException e) {
155
156             ErrorLoggerInfo errorLoggerInfo =
157                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build();
158
159
160             ValidateException validateException =
161                     new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(),
162                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
163                                     .errorInfo(errorLoggerInfo).build();
164             throw validateException;
165         }
166
167         return Response.status(httpResponseCode).entity(requestErrorStr).build();
168     }
169 }