b81ee58aa4760091262530c3db0979dcf953446a
[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.apihandler.filters;
21
22 import java.io.IOException;
23 import javax.annotation.Priority;
24 import javax.ws.rs.container.ContainerRequestContext;
25 import javax.ws.rs.container.ContainerRequestFilter;
26 import javax.ws.rs.core.UriInfo;
27 import javax.ws.rs.ext.Provider;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.onap.so.apihandler.common.ErrorNumbers;
30 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestIdException;
31 import org.onap.so.db.request.beans.InfraActiveRequests;
32 import org.onap.so.db.request.client.RequestsDbClient;
33 import org.onap.so.serviceinstancebeans.RequestError;
34 import org.onap.so.serviceinstancebeans.ServiceException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.slf4j.MDC;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40 import com.fasterxml.jackson.core.JsonProcessingException;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43 @Priority(2)
44 @Provider
45 @Component
46 public class RequestIdFilter implements ContainerRequestFilter {
47
48     private static Logger logger = LoggerFactory.getLogger(RequestIdFilter.class);
49
50     @Autowired
51     private RequestsDbClient infraActiveRequestsClient;
52
53     @Override
54     public void filter(ContainerRequestContext context) throws IOException {
55         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
56         UriInfo uriInfo = context.getUriInfo();
57         String requestURI = uriInfo.getPath();
58
59         if (!requestURI.contains("orchestrationRequests")) {
60             logger.info("Checking if requestId: {} already exists in requestDb InfraActiveRequests table", requestId);
61             InfraActiveRequests infraActiveRequests =
62                     infraActiveRequestsClient.getInfraActiveRequestbyRequestId(requestId);
63
64             if (infraActiveRequests != null) {
65                 logger.error(
66                         "RequestId: {} already exists in RequestDB InfraActiveRequests table, throwing DuplicateRequestIdException",
67                         requestId);
68                 throw new DuplicateRequestIdException(createRequestError(requestId, "InfraActiveRequests"));
69             }
70         }
71     }
72
73     protected String createRequestError(String requestId, String requestTable) {
74         ObjectMapper mapper = new ObjectMapper();
75         RequestError error = new RequestError();
76         ServiceException serviceException = new ServiceException();
77         serviceException.setMessageId(ErrorNumbers.SVC_BAD_PARAMETER);
78         serviceException
79                 .setText("RequestId: " + requestId + " already exists in the RequestDB " + requestTable + " table");
80         error.setServiceException(serviceException);
81         String errorMessage = null;
82
83         try {
84             errorMessage = mapper.writeValueAsString(error);
85         } catch (JsonProcessingException e) {
86             return "Unable to write requestError to String when requestId already exists in the RequestDb due to error: "
87                     + e.getMessage();
88         }
89         return errorMessage;
90     }
91 }