Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / main / java / org / onap / aai / babel / request / RequestHeaders.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.babel.request;
22
23 import java.util.Optional;
24 import javax.ws.rs.core.HttpHeaders;
25
26 /** Bean to represent the ECOMP request/transaction IDs required for EELF logging. */
27 public class RequestHeaders {
28
29     // ECOMP request ID a.k.a. transaction ID or correlation ID
30     public static final String HEADER_REQUEST_ID = "X-ECOMP-RequestID";
31     public static final String HEADER_SERVICE_INSTANCE_ID = "X-ECOMP-ServiceInstanceID";
32     // This value should match with org.openecomp.restclient.client.Headers.TRANSACTION_ID
33     private static final String HEADER_X_TRANSACTION_ID = "X-TransactionId";
34
35     private String requestId;
36     private String instanceId;
37     private String transactionId;
38
39     public RequestHeaders(HttpHeaders headers) {
40         requestId = headers.getHeaderString(RequestHeaders.HEADER_REQUEST_ID);
41         instanceId = headers.getHeaderString(RequestHeaders.HEADER_SERVICE_INSTANCE_ID);
42         transactionId = headers.getHeaderString(RequestHeaders.HEADER_X_TRANSACTION_ID);
43     }
44
45     public String getRequestId() {
46         return requestId;
47     }
48
49     public String getInstanceId() {
50         return instanceId;
51     }
52
53     public String getTransactionId() {
54         return transactionId;
55     }
56
57     /**
58      * Get the global request ID from the HTTP headers. The value will be taken from the header "X-ECOMP-RequestID" if
59      * this is set, or else the value of "X-TransactionId" (which may be null).
60      *
61      * <p>
62      * If the correlation ID contains the symbol : then this character and any trailing characters are removed. This
63      * allows for an incrementing numeric sequence where there are multiple HTTP requests for a single transaction.
64      *
65      * @return the normalsed UUID used for correlating transactions across components, or else null (if no ID is set)
66      */
67     public String getCorrelationId() {
68         // If the request ID is missing, use the transaction ID (if present)
69         String uuid = Optional.ofNullable(getRequestId()).orElse(getTransactionId());
70
71         // Normalize the correlation ID by removing any suffix
72         if (uuid != null && uuid.contains(":")) {
73             uuid = uuid.split(":")[0];
74         }
75
76         return uuid;
77     }
78
79     @Override
80     public String toString() {
81         return "RequestHeaders [requestId=" + requestId + ", instanceId=" + instanceId + ", transactionId="
82                 + transactionId + "]";
83     }
84 }