ef5abe9f743ffd0d8319d4cf78dc9c55830b70c2
[so.git] /
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         Distribution distributionRequest;
79
80         try {
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)
86                             .build();
87
88
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;
94
95         }
96
97         try {
98             parse(distributionRequest);
99         } catch (ValidationException e) {
100
101             ErrorLoggerInfo errorLoggerInfo =
102                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
103                             .build();
104
105
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;
110         }
111
112         CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
113         cor.setDistribution(distributionRequest);
114         cor.setDistributionId(distributionId);
115
116         TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
117         runnable.run(Action.distributionStatus, null, cor, null);
118
119         return Response.ok().build();
120     }
121
122     private void parse(Distribution distributionRequest) throws ValidationException {
123         if (distributionRequest.getStatus() == null) {
124             throw new ValidationException("status");
125         }
126
127         if (StringUtils.isBlank(distributionRequest.getErrorReason())
128                 && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
129             throw new ValidationException("errorReason");
130         }
131     }
132
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);
138         se.setText(text);
139         if (variables != null) {
140             for (String variable : variables) {
141                 se.getVariables().add(variable);
142             }
143         }
144         re.setServiceException(se);
145
146         String requestErrorStr;
147         try {
148             ObjectMapper mapper = new ObjectMapper();
149             mapper.setSerializationInclusion(Include.NON_DEFAULT);
150             requestErrorStr = mapper.writeValueAsString(re);
151         } catch (JsonProcessingException e) {
152
153             ErrorLoggerInfo errorLoggerInfo =
154                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build();
155
156
157             ValidateException validateException =
158                     new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(),
159                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
160                                     .errorInfo(errorLoggerInfo).build();
161             throw validateException;
162         }
163
164         return Response.status(httpResponseCode).entity(requestErrorStr).build();
165     }
166 }