4be1ec9187d9d20365af6fd86b7d20ebecd1d923
[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
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;
37
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;
57
58 import com.fasterxml.jackson.annotation.JsonInclude.Include;
59 import com.fasterxml.jackson.core.JsonProcessingException;
60 import com.fasterxml.jackson.databind.ObjectMapper;
61
62 import io.swagger.annotations.Api;
63 import io.swagger.annotations.ApiOperation;
64
65 @Component
66 @Path("/onap/so/infra/modelDistributions")
67 @Api(value="/onap/so/infra/modelDistributions",description="API Requests for Model Distributions")
68 public class ModelDistributionRequest {
69         
70         private static Logger logger = LoggerFactory.getLogger(ModelDistributionRequest.class);
71         @Autowired
72         private Provider<TenantIsolationRunnable> tenantIsolationRunnable;
73         
74         @PATCH
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)
79         @Transactional
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;
83                 
84                 try {
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();
90
91
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;
95
96                 }
97
98                 try {
99                         parse(distributionRequest);
100                 } catch(ValidationException e) {
101
102                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,
103           ErrorCode.SchemaError).build();
104
105
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;
109                 }
110                 
111                 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
112                 cor.setDistribution(distributionRequest);
113                 cor.setDistributionId(distributionId);
114                 
115                 TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
116                 runnable.run(Action.distributionStatus, null, cor, null);
117                 
118                 return Response.ok().build();
119         }
120
121         private void parse(Distribution distributionRequest) throws ValidationException {
122                 if(distributionRequest.getStatus() == null) {
123                         throw new ValidationException("status");
124                 }
125                 
126                 if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
127                         throw new ValidationException("errorReason");
128                 }
129         }
130         
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);
136         se.setText(text);
137         if(variables != null){
138                 if(variables != null){
139                         for(String variable: variables){
140                                 se.getVariables().add(variable);
141                         }
142                 }
143         }
144         re.setServiceException(se);
145
146         String requestErrorStr = null;
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 = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.DataError).build();
154
155
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;
159         }
160
161         return Response.status (httpResponseCode).entity(requestErrorStr).build ();
162     }
163 }