2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.so.apihandlerinfra;
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.db.request.beans.InfraActiveRequests;
44 import org.onap.so.db.request.client.RequestsDbClient;
45 import org.onap.so.logger.ErrorCode;
46 import org.onap.so.logger.HttpHeadersConstants;
47 import org.onap.so.logger.LogConstants;
48 import org.onap.so.logger.MdcConstants;
49 import org.onap.so.logger.MessageEnum;
50 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.stereotype.Component;
56 import org.springframework.web.client.HttpClientErrorException;
57 import io.swagger.annotations.Api;
58 import io.swagger.annotations.ApiOperation;
60 @Path("onap/so/infra/orchestrationRequests")
61 @Api(value = "onap/so/infra/orchestrationRequests")
63 public class ResumeOrchestrationRequest {
64 private static Logger logger = LoggerFactory.getLogger(ResumeOrchestrationRequest.class);
65 private static final String SAVE_TO_DB = "save instance to db";
66 private static String uriPrefix = "/orchestrationRequests/";
69 private RequestHandlerUtils requestHandlerUtils;
72 private ServiceInstances serviceInstances;
75 private RequestsDbClient requestsDbClient;
79 @Path("/{version:[vV][7]}/requests/{requestId}/resume")
80 @Consumes(MediaType.APPLICATION_JSON)
81 @Produces(MediaType.APPLICATION_JSON)
82 @ApiOperation(value = "Resume request for a given requestId", response = Response.class)
84 public Response resumeOrchestrationRequest(@PathParam("requestId") String requestId,
85 @PathParam("version") String version, @Context ContainerRequestContext requestContext) throws ApiException {
87 Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
88 String currentRequestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
89 logger.info("Beginning resume operation for new request: {}", currentRequestId);
90 InfraActiveRequests infraActiveRequest = null;
91 String source = MDC.get(MdcConstants.ORIGINAL_PARTNER_NAME);
92 String requestorId = MDC.get(HttpHeadersConstants.REQUESTOR_ID);
93 requestHandlerUtils.getRequestUri(requestContext, uriPrefix);
94 String requestUri = MDC.get(LogConstants.HTTP_URL);
95 version = version.substring(1);
98 infraActiveRequest = requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
99 } catch (HttpClientErrorException e) {
100 logger.error("Error occurred while performing requestDb lookup by requestId: " + requestId, e);
101 ErrorLoggerInfo errorLoggerInfo =
102 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.AvailabilityError).build();
103 throw new ValidateException.Builder("Exception while performing requestDb lookup by requestId",
104 HttpStatus.SC_NOT_FOUND, ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e)
105 .errorInfo(errorLoggerInfo).build();
108 InfraActiveRequests currentActiveRequest = requestHandlerUtils.createNewRecordCopyFromInfraActiveRequest(
109 infraActiveRequest, currentRequestId, startTimeStamp, source, requestUri, requestorId, requestId);
111 if (infraActiveRequest == null) {
112 logger.error("No infraActiveRequest record found for requestId: {} in requesteDb lookup", requestId);
113 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND,
114 ErrorCode.BusinessProcesssError).build();
115 ValidateException validateException = new ValidateException.Builder(
116 "Null response from requestDB when searching by requestId: " + requestId, HttpStatus.SC_NOT_FOUND,
117 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
118 requestHandlerUtils.updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
119 throw validateException;
123 return resumeRequest(infraActiveRequest, currentActiveRequest, version, requestUri);
126 protected Response resumeRequest(InfraActiveRequests infraActiveRequest, InfraActiveRequests currentActiveRequest,
127 String version, String requestUri) throws ApiException {
128 String requestBody = infraActiveRequest.getRequestBody();
129 Action action = Action.valueOf(infraActiveRequest.getRequestAction());
130 String requestId = currentActiveRequest.getRequestId();
131 String serviceInstanceName = infraActiveRequest.getServiceInstanceName();
132 String requestScope = infraActiveRequest.getRequestScope();
133 String serviceInstanceId = infraActiveRequest.getServiceInstanceId();
135 checkForInProgressRequest(currentActiveRequest, serviceInstanceId, requestScope, serviceInstanceName, action);
137 ServiceInstancesRequest sir = null;
138 sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestBody, action, requestId, requestUri);
139 Boolean aLaCarte = sir.getRequestDetails().getRequestParameters().getALaCarte();
140 if (aLaCarte == null) {
144 String pnfCorrelationId = serviceInstances.getPnfCorrelationId(sir);
145 RecipeLookupResult recipeLookupResult = serviceRecipeLookup(currentActiveRequest, sir, action, aLaCarte);
147 requestDbSave(currentActiveRequest);
149 RequestClientParameter requestClientParameter = setRequestClientParameter(recipeLookupResult, version,
150 infraActiveRequest, currentActiveRequest, pnfCorrelationId, aLaCarte, sir);
152 return requestHandlerUtils.postBPELRequest(currentActiveRequest, requestClientParameter,
153 recipeLookupResult.getOrchestrationURI(), requestScope);
156 protected void checkForInProgressRequest(InfraActiveRequests currentActiveRequest, String serviceInstanceId,
157 String requestScope, String serviceInstanceName, Action action) throws ApiException {
158 boolean inProgress = false;
159 HashMap<String, String> instanceIdMap = new HashMap<>();
160 instanceIdMap.put("serviceInstanceId", serviceInstanceId);
161 InfraActiveRequests requestInProgress = requestHandlerUtils.duplicateCheck(action, instanceIdMap,
162 serviceInstanceName, requestScope, currentActiveRequest);
163 if (requestInProgress != null) {
164 inProgress = requestHandlerUtils.camundaHistoryCheck(requestInProgress, currentActiveRequest);
167 requestHandlerUtils.buildErrorOnDuplicateRecord(currentActiveRequest, action, instanceIdMap,
168 serviceInstanceName, requestScope, requestInProgress);
172 protected RecipeLookupResult serviceRecipeLookup(InfraActiveRequests currentActiveRequest,
173 ServiceInstancesRequest sir, Action action, Boolean aLaCarte)
174 throws ValidateException, RequestDbFailureException {
175 RecipeLookupResult recipeLookupResult = null;
177 recipeLookupResult = serviceInstances.getServiceURI(sir, action, aLaCarte);
178 } catch (IOException e) {
179 logger.error("IOException while performing service recipe lookup", e);
180 ErrorLoggerInfo errorLoggerInfo =
181 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
182 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
183 ValidateException validateException =
184 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
185 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
186 requestHandlerUtils.updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
187 throw validateException;
189 return recipeLookupResult;
192 protected void requestDbSave(InfraActiveRequests currentActiveRequest) throws RequestDbFailureException {
194 requestsDbClient.save(currentActiveRequest);
195 } catch (Exception e) {
196 logger.error("Exception while saving request to requestDb", e);
197 ErrorLoggerInfo errorLoggerInfo =
198 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
199 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
200 throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
201 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
205 protected RequestClientParameter setRequestClientParameter(RecipeLookupResult recipeLookupResult, String version,
206 InfraActiveRequests infraActiveRequest, InfraActiveRequests currentActiveRequest, String pnfCorrelationId,
207 Boolean aLaCarte, ServiceInstancesRequest sir) throws ValidateException {
208 RequestClientParameter requestClientParameter = null;
210 requestClientParameter = new RequestClientParameter.Builder()
211 .setRequestId(currentActiveRequest.getRequestId())
212 .setRecipeTimeout(recipeLookupResult.getRecipeTimeout())
213 .setRequestAction(infraActiveRequest.getRequestAction())
214 .setServiceInstanceId(infraActiveRequest.getServiceInstanceId())
215 .setPnfCorrelationId(pnfCorrelationId).setVnfId(infraActiveRequest.getVnfId())
216 .setVfModuleId(infraActiveRequest.getVfModuleId())
217 .setVolumeGroupId(infraActiveRequest.getVolumeGroupId())
218 .setNetworkId(infraActiveRequest.getNetworkId()).setServiceType(infraActiveRequest.getServiceType())
219 .setVnfType(infraActiveRequest.getVnfType()).setNetworkType(infraActiveRequest.getNetworkType())
220 .setRequestDetails(requestHandlerUtils.mapJSONtoMSOStyle(infraActiveRequest.getRequestBody(), sir,
221 aLaCarte, Action.valueOf(infraActiveRequest.getRequestAction())))
222 .setApiVersion(version).setALaCarte(aLaCarte).setRequestUri(currentActiveRequest.getRequestUrl())
223 .setInstanceGroupId(infraActiveRequest.getInstanceGroupId()).build();
224 } catch (IOException e) {
225 logger.error("IOException while generating requestClientParameter to send to BPMN", e);
226 ErrorLoggerInfo errorLoggerInfo =
227 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
228 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
229 throw new ValidateException.Builder(
230 "IOException while generating requestClientParameter to send to BPMN: " + e.getMessage(),
231 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
234 return requestClientParameter;