3ec3ade9590a612eaf7782bc061b5de455ed6ded
[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
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.MessageEnum;
50 import org.onap.so.logger.MsoLogger;
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,MsoLogger.ErrorCode.SchemaError).build();
89
90
91                         ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
92                                         .cause(e).errorInfo(errorLoggerInfo).build();
93                         throw validateException;
94
95                 }
96
97                 try {
98                         parse(distributionRequest);
99                 } catch(ValidationException e) {
100
101                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
102
103
104                         ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
105                                         .cause(e).errorInfo(errorLoggerInfo).build();
106                         throw validateException;
107                 }
108                 
109                 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
110                 cor.setDistribution(distributionRequest);
111                 cor.setDistributionId(distributionId);
112                 
113                 TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
114                 runnable.run(Action.distributionStatus, null, cor, null);
115                 
116                 return Response.ok().build();
117         }
118
119         private void parse(Distribution distributionRequest) throws ValidationException {
120                 if(distributionRequest.getStatus() == null) {
121                         throw new ValidationException("status");
122                 }
123                 
124                 if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
125                         throw new ValidationException("errorReason");
126                 }
127         }
128         
129     private Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, String text,
130                                                                                 String messageId, List<String> variables) throws ApiException{
131         RequestError re = new RequestError();
132         ServiceException se = new ServiceException();
133         se.setMessageId(messageId);
134         se.setText(text);
135         if(variables != null){
136                 if(variables != null){
137                         for(String variable: variables){
138                                 se.getVariables().add(variable);
139                         }
140                 }
141         }
142         re.setServiceException(se);
143
144         String requestErrorStr = null;
145         try{
146                 ObjectMapper mapper = new ObjectMapper();
147                 mapper.setSerializationInclusion(Include.NON_DEFAULT);
148                 requestErrorStr = mapper.writeValueAsString(re);
149         }catch(JsonProcessingException e){
150
151                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR,MsoLogger.ErrorCode.DataError).build();
152
153
154                         ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
155                                         .cause(e).errorInfo(errorLoggerInfo).build();
156                         throw validateException;
157         }
158
159         return Response.status (httpResponseCode).entity(requestErrorStr).build ();
160     }
161 }