Containerization feature of SO
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.tenantisolation;
22
23 import java.io.IOException;
24 import java.util.List;
25
26 import javax.inject.Provider;
27 import javax.transaction.Transactional;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35
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.MessageEnum;
48 import org.onap.so.logger.MsoLogger;
49 import org.onap.so.serviceinstancebeans.RequestError;
50 import org.onap.so.serviceinstancebeans.ServiceException;
51 import org.springframework.beans.factory.annotation.Autowired;
52
53 import com.fasterxml.jackson.annotation.JsonInclude.Include;
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56
57 import io.swagger.annotations.Api;
58 import io.swagger.annotations.ApiOperation;
59
60
61 @Path("/onap/so/infra/modelDistributions")
62 @Api(value="/onap/so/infra/modelDistributions",description="API Requests for Model Distributions")
63 public class ModelDistributionRequest {
64         
65         private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH, ModelDistributionRequest.class);
66         @Autowired
67         private Provider<TenantIsolationRunnable> tenantIsolationRunnable;
68         
69         @POST
70         @Path("/{version:[vV][1]}/distributions/{distributionId}")
71         @Consumes(MediaType.APPLICATION_JSON)
72         @Produces(MediaType.APPLICATION_JSON)
73         @ApiOperation(value="Update model distribution status",response=Response.class)
74         @Transactional
75         public Response updateModelDistributionStatus(String requestJSON, @PathParam("version") String version, @PathParam("distributionId") String distributionId) throws ApiException{
76                 long startTime = System.currentTimeMillis ();
77                 Distribution distributionRequest = null;
78                 
79                 try {
80                         ObjectMapper mapper = new ObjectMapper();
81                         distributionRequest = mapper.readValue(requestJSON, Distribution.class);
82                 } catch(IOException e) {
83                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
84
85
86                         ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
87                                         .cause(e).errorInfo(errorLoggerInfo).build();
88                         throw validateException;
89
90                 }
91
92                 try {
93                         parse(distributionRequest);
94                 } catch(ValidationException e) {
95
96                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
97
98
99                         ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
100                                         .cause(e).errorInfo(errorLoggerInfo).build();
101                         throw validateException;
102                 }
103                 
104                 CloudOrchestrationRequest cor = new CloudOrchestrationRequest();
105                 cor.setDistribution(distributionRequest);
106                 cor.setDistributionId(distributionId);
107                 
108                 TenantIsolationRunnable runnable = tenantIsolationRunnable.get();
109                 runnable.run(Action.distributionStatus, null, cor, null);
110                 
111                 return Response.ok().build();
112         }
113
114         private void parse(Distribution distributionRequest) throws ValidationException {
115                 if(distributionRequest.getStatus() == null) {
116                         throw new ValidationException("status");
117                 }
118                 
119                 if(StringUtils.isBlank(distributionRequest.getErrorReason()) && Status.DISTRIBUTION_COMPLETE_ERROR.equals(distributionRequest.getStatus())) {
120                         throw new ValidationException("errorReason");
121                 }
122         }
123         
124     private Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, String text,
125                                                                                 String messageId, List<String> variables) throws ApiException{
126         RequestError re = new RequestError();
127         ServiceException se = new ServiceException();
128         se.setMessageId(messageId);
129         se.setText(text);
130         if(variables != null){
131                 if(variables != null){
132                         for(String variable: variables){
133                                 se.getVariables().add(variable);
134                         }
135                 }
136         }
137         re.setServiceException(se);
138
139         String requestErrorStr = null;
140         try{
141                 ObjectMapper mapper = new ObjectMapper();
142                 mapper.setSerializationInclusion(Include.NON_DEFAULT);
143                 requestErrorStr = mapper.writeValueAsString(re);
144         }catch(JsonProcessingException e){
145
146                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR,MsoLogger.ErrorCode.DataError).build();
147
148
149                         ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed.  " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
150                                         .cause(e).errorInfo(errorLoggerInfo).build();
151                         throw validateException;
152         }
153
154         return Response.status (httpResponseCode).entity(requestErrorStr).build ();
155     }
156 }