6132815cf653f134f8d4402269a6164a58aee7e8
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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 package org.onap.so.apihandlerinfra;
21
22 import java.io.IOException;
23 import java.sql.Timestamp;
24 import java.util.HashMap;
25 import javax.transaction.Transactional;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.container.ContainerRequestContext;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.apache.http.HttpStatus;
36 import org.onap.logging.ref.slf4j.ONAPLogConstants;
37 import org.onap.so.apihandler.common.ErrorNumbers;
38 import org.onap.so.apihandler.common.RequestClientParameter;
39 import org.onap.so.apihandlerinfra.exceptions.ApiException;
40 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
41 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
42 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
43 import org.onap.so.constants.Status;
44 import org.onap.so.db.request.beans.InfraActiveRequests;
45 import org.onap.so.db.request.client.RequestsDbClient;
46 import org.onap.logging.filter.base.ErrorCode;
47 import org.onap.so.logger.HttpHeadersConstants;
48 import org.onap.so.logger.LogConstants;
49 import org.onap.so.logger.MdcConstants;
50 import org.onap.so.logger.MessageEnum;
51 import org.onap.so.serviceinstancebeans.ModelInfo;
52 import org.onap.so.serviceinstancebeans.ModelType;
53 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.slf4j.MDC;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.stereotype.Component;
59 import org.springframework.web.client.HttpClientErrorException;
60 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
61 import io.swagger.v3.oas.annotations.Operation;
62 import io.swagger.v3.oas.annotations.info.Info;
63 import io.swagger.v3.oas.annotations.media.ArraySchema;
64 import io.swagger.v3.oas.annotations.media.Content;
65 import io.swagger.v3.oas.annotations.media.Schema;
66 import io.swagger.v3.oas.annotations.responses.ApiResponse;
67
68 @Path("onap/so/infra/orchestrationRequests")
69 @OpenAPIDefinition(info = @Info(title = "onap/so/infra/orchestrationRequests"))
70
71 @Component
72 public class ResumeOrchestrationRequest {
73     private static Logger logger = LoggerFactory.getLogger(ResumeOrchestrationRequest.class);
74     private static final String SAVE_TO_DB = "save instance to db";
75     private static String uriPrefix = "/orchestrationRequests/";
76
77     @Autowired
78     private RequestHandlerUtils requestHandlerUtils;
79
80     @Autowired
81     private ServiceInstances serviceInstances;
82
83     @Autowired
84     private RequestsDbClient requestsDbClient;
85
86     @Autowired
87     private MsoRequest msoRequest;
88
89     @POST
90     @Path("/{version:[vV][7]}/{requestId}/resume")
91     @Consumes(MediaType.APPLICATION_JSON)
92     @Produces(MediaType.APPLICATION_JSON)
93     @Operation(description = "Resume request for a given requestId", responses = @ApiResponse(
94             content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
95     @Transactional
96     public Response resumeOrchestrationRequest(@PathParam("requestId") String requestId,
97             @PathParam("version") String version, @Context ContainerRequestContext requestContext) throws ApiException {
98
99         Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
100         String currentRequestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
101         logger.info("Beginning resume operation for new request: {}", currentRequestId);
102         InfraActiveRequests infraActiveRequest = null;
103         String source = MDC.get(MdcConstants.ORIGINAL_PARTNER_NAME);
104         String requestorId = MDC.get(HttpHeadersConstants.REQUESTOR_ID);
105         requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
106         String requestUri = MDC.get(LogConstants.HTTP_URL);
107         version = version.substring(1);
108
109         try {
110             infraActiveRequest = requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
111         } catch (HttpClientErrorException e) {
112             logger.error("Error occurred while performing requestDb lookup by requestId: " + requestId, e);
113             ErrorLoggerInfo errorLoggerInfo =
114                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.AvailabilityError).build();
115             throw new ValidateException.Builder("Exception while performing requestDb lookup by requestId",
116                     HttpStatus.SC_NOT_FOUND, ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e)
117                             .errorInfo(errorLoggerInfo).build();
118         }
119
120         InfraActiveRequests currentActiveRequest = requestHandlerUtils.createNewRecordCopyFromInfraActiveRequest(
121                 infraActiveRequest, currentRequestId, startTimeStamp, source, requestUri, requestorId, requestId);
122
123         if (infraActiveRequest == null) {
124             logger.error("No infraActiveRequest record found for requestId: {} in requesteDb lookup", requestId);
125             ErrorLoggerInfo errorLoggerInfo =
126                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.BusinessProcessError)
127                             .build();
128             ValidateException validateException = new ValidateException.Builder(
129                     "Null response from requestDB when searching by requestId: " + requestId, HttpStatus.SC_NOT_FOUND,
130                     ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
131             requestHandlerUtils.updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
132             throw validateException;
133
134         }
135
136         return resumeRequest(infraActiveRequest, currentActiveRequest, version, requestUri);
137     }
138
139     protected Response resumeRequest(InfraActiveRequests infraActiveRequest, InfraActiveRequests currentActiveRequest,
140             String version, String requestUri) throws ApiException {
141         String requestBody = infraActiveRequest.getRequestBody();
142         Action action = Action.valueOf(infraActiveRequest.getRequestAction());
143         String requestId = currentActiveRequest.getRequestId();
144         String requestScope = infraActiveRequest.getRequestScope();
145         String instanceName = getInstanceName(infraActiveRequest, requestScope, currentActiveRequest);
146         HashMap<String, String> instanceIdMap = setInstanceIdMap(infraActiveRequest, requestScope);
147
148         checkForInProgressRequest(currentActiveRequest, instanceIdMap, requestScope, instanceName, action);
149
150         ServiceInstancesRequest sir = null;
151         sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestBody, action, requestId, requestUri);
152         Boolean aLaCarte = sir.getRequestDetails().getRequestParameters().getALaCarte();
153
154         String pnfCorrelationId = serviceInstances.getPnfCorrelationId(sir);
155         RecipeLookupResult recipeLookupResult = requestHandlerUtils.getServiceInstanceOrchestrationURI(sir, action,
156                 msoRequest.getAlacarteFlag(sir), currentActiveRequest);
157
158         requestDbSave(currentActiveRequest);
159
160         if (aLaCarte == null) {
161             aLaCarte = setALaCarteFlagIfNull(requestScope, action);
162         }
163
164         RequestClientParameter requestClientParameter = setRequestClientParameter(recipeLookupResult, version,
165                 infraActiveRequest, currentActiveRequest, pnfCorrelationId, aLaCarte, sir);
166
167         return requestHandlerUtils.postBPELRequest(currentActiveRequest, requestClientParameter,
168                 recipeLookupResult.getOrchestrationURI(), requestScope);
169     }
170
171     protected Boolean setALaCarteFlagIfNull(String requestScope, Action action) {
172         Boolean aLaCarteFlag;
173         if (!requestScope.equalsIgnoreCase(ModelType.service.name()) && action != Action.recreateInstance) {
174             aLaCarteFlag = true;
175         } else {
176             aLaCarteFlag = false;
177         }
178         return aLaCarteFlag;
179     }
180
181     protected HashMap<String, String> setInstanceIdMap(InfraActiveRequests infraActiveRequest, String requestScope) {
182         HashMap<String, String> instanceIdMap = new HashMap<>();
183         ModelType type;
184         try {
185             type = ModelType.valueOf(requestScope);
186             instanceIdMap.put(type.name() + "InstanceId", type.getId(infraActiveRequest));
187         } catch (IllegalArgumentException e) {
188             logger.error("requestScope \"{}\" does not match a ModelType enum.", requestScope);
189         }
190         return instanceIdMap;
191     }
192
193     protected String getInstanceName(InfraActiveRequests infraActiveRequest, String requestScope,
194             InfraActiveRequests currentActiveRequest) throws ValidateException, RequestDbFailureException {
195         ModelType type;
196         String instanceName = "";
197         try {
198             type = ModelType.valueOf(requestScope);
199             instanceName = type.getName(infraActiveRequest);
200         } catch (IllegalArgumentException e) {
201             logger.error("requestScope \"{}\" does not match a ModelType enum.", requestScope);
202             ValidateException validateException = new ValidateException.Builder(
203                     "requestScope: \"" + requestScope + "\" from request: " + infraActiveRequest.getRequestId()
204                             + " does not match a ModelType enum.",
205                     HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).build();
206             requestHandlerUtils.updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
207             throw validateException;
208         }
209         return instanceName;
210     }
211
212     protected void checkForInProgressRequest(InfraActiveRequests currentActiveRequest,
213             HashMap<String, String> instanceIdMap, String requestScope, String instanceName, Action action)
214             throws ApiException {
215         boolean inProgress = false;
216         InfraActiveRequests requestInProgress = requestHandlerUtils.duplicateCheck(action, instanceIdMap, instanceName,
217                 requestScope, currentActiveRequest);
218         if (requestInProgress != null) {
219             inProgress = requestHandlerUtils.camundaHistoryCheck(requestInProgress, currentActiveRequest);
220         }
221         if (inProgress) {
222             requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveRequest, action, instanceIdMap, instanceName,
223                     requestScope, requestInProgress);
224         }
225     }
226
227     protected void requestDbSave(InfraActiveRequests currentActiveRequest) throws RequestDbFailureException {
228         try {
229             requestsDbClient.save(currentActiveRequest);
230         } catch (Exception e) {
231             logger.error("Exception while saving request to requestDb", e);
232             ErrorLoggerInfo errorLoggerInfo =
233                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
234                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
235             throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
236                     ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
237         }
238     }
239
240     protected RequestClientParameter setRequestClientParameter(RecipeLookupResult recipeLookupResult, String version,
241             InfraActiveRequests infraActiveRequest, InfraActiveRequests currentActiveRequest, String pnfCorrelationId,
242             Boolean aLaCarte, ServiceInstancesRequest sir) throws ApiException {
243         RequestClientParameter requestClientParameter = null;
244         Action action = Action.valueOf(infraActiveRequest.getRequestAction());
245         ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
246
247         Boolean isBaseVfModule = false;
248         if (requestHandlerUtils.getModelType(action, modelInfo).equals(ModelType.vfModule)) {
249             isBaseVfModule = requestHandlerUtils.getIsBaseVfModule(modelInfo, action, infraActiveRequest.getVnfType(),
250                     msoRequest.getSDCServiceModelVersion(sir), currentActiveRequest);
251         }
252
253         try {
254             requestClientParameter = new RequestClientParameter.Builder()
255                     .setRequestId(currentActiveRequest.getRequestId()).setBaseVfModule(isBaseVfModule)
256                     .setRecipeTimeout(recipeLookupResult.getRecipeTimeout())
257                     .setRequestAction(infraActiveRequest.getRequestAction())
258                     .setServiceInstanceId(infraActiveRequest.getServiceInstanceId())
259                     .setPnfCorrelationId(pnfCorrelationId).setVnfId(infraActiveRequest.getVnfId())
260                     .setVfModuleId(infraActiveRequest.getVfModuleId())
261                     .setVolumeGroupId(infraActiveRequest.getVolumeGroupId())
262                     .setNetworkId(infraActiveRequest.getNetworkId()).setServiceType(infraActiveRequest.getServiceType())
263                     .setVnfType(infraActiveRequest.getVnfType())
264                     .setVfModuleType(msoRequest.getVfModuleType(sir, infraActiveRequest.getRequestScope()))
265                     .setNetworkType(infraActiveRequest.getNetworkType())
266                     .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(infraActiveRequest.getRequestBody(), sir,
267                             aLaCarte, action))
268                     .setApiVersion(version).setALaCarte(aLaCarte).setRequestUri(currentActiveRequest.getRequestUrl())
269                     .setInstanceGroupId(infraActiveRequest.getInstanceGroupId()).build();
270         } catch (IOException e) {
271             logger.error("IOException while generating requestClientParameter to send to BPMN", e);
272             ErrorLoggerInfo errorLoggerInfo =
273                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
274                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
275             throw new ValidateException.Builder(
276                     "IOException while generating requestClientParameter to send to BPMN: " + e.getMessage(),
277                     HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
278                             .build();
279         }
280         return requestClientParameter;
281     }
282 }