Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / application / src / main / java / org / onap / ccsdk / cds / controllerblueprints / filters / ApplicationLoggingFilter.java
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *  Modifications Copyright © 2018 IBM.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.filters;
19
20 import com.google.common.base.Preconditions;
21 import org.apache.commons.lang3.StringUtils;
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.slf4j.MDC;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.context.annotation.Configuration;
28 import org.springframework.http.HttpHeaders;
29 import org.springframework.http.server.reactive.ServerHttpRequest;
30 import org.springframework.http.server.reactive.ServerHttpResponse;
31 import org.springframework.web.server.ServerWebExchange;
32 import org.springframework.web.server.WebFilter;
33 import org.springframework.web.server.WebFilterChain;
34 import reactor.core.publisher.Mono;
35
36 import java.time.ZoneOffset;
37 import java.time.ZonedDateTime;
38 import java.time.format.DateTimeFormatter;
39 import java.util.UUID;
40
41 /**
42  * ApplicationLoggingFilter
43  *
44  * @author Brinda Santh 8/14/2018
45  */
46 @Configuration
47 @SuppressWarnings("unused")
48 public class ApplicationLoggingFilter implements WebFilter {
49     private static Logger log = LoggerFactory.getLogger(ApplicationLoggingFilter.class);
50
51     @SuppressWarnings("unused")
52     @Value("${appVersion}")
53     private String appVersion;
54
55     @Override
56     public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
57         try {
58
59             ServerHttpRequest request = serverWebExchange.getRequest();
60             ServerHttpResponse response = serverWebExchange.getResponse();
61
62             String[] tokens = StringUtils.split(appVersion, '.');
63             Preconditions.checkNotNull(tokens, "failed to split application versions");
64             Preconditions.checkArgument(tokens.length == 3, "failed to tokenize application versions");
65             HttpHeaders header = response.getHeaders();
66
67             String requestID = defaultToUUID(request.getHeaders().getFirst("X-ONAP-RequestID"));
68             String invocationID = defaultToUUID(request.getHeaders().getFirst("X-ONAP-InvocationID"));
69             String partnerName = defaultToEmpty(request.getHeaders().getFirst("X-ONAP-PartnerName"));
70             MDC.put("InvokeTimestamp", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
71             MDC.put("RequestID", requestID);
72             MDC.put("InvocationID", invocationID);
73             MDC.put("PartnerName", partnerName);
74             MDC.put("ClientIPAddress", defaultToEmpty(request.getRemoteAddress().getAddress()));
75             MDC.put("ServerFQDN", defaultToEmpty(request.getRemoteAddress().getHostString()));
76
77             header.add(BluePrintConstants.RESPONSE_HEADER_TRANSACTION_ID, requestID);
78             header.add(BluePrintConstants.RESPONSE_HEADER_MINOR_VERSION, tokens[1]);
79             header.add(BluePrintConstants.RESPONSE_HEADER_PATCH_VERSION, tokens[2]);
80             header.add(BluePrintConstants.RESPONSE_HEADER_LATEST_VERSION, appVersion);
81         } catch (Exception e) {
82             e.printStackTrace();
83         }
84
85         return webFilterChain.filter(serverWebExchange);
86
87     }
88
89     private static String defaultToUUID(String in) {
90         return in == null ? UUID.randomUUID().toString() : in;
91     }
92
93     private static String defaultToEmpty(Object in) {
94         return in == null ? "" : in.toString();
95     }
96
97
98 }