2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.filters;
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;
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;
36 import java.time.ZoneOffset;
37 import java.time.ZonedDateTime;
38 import java.time.format.DateTimeFormatter;
39 import java.util.UUID;
42 * ApplicationLoggingFilter
44 * @author Brinda Santh 8/14/2018
47 @SuppressWarnings("unused")
48 public class ApplicationLoggingFilter implements WebFilter {
49 private static Logger log = LoggerFactory.getLogger(ApplicationLoggingFilter.class);
51 @SuppressWarnings("unused")
52 @Value("${appVersion}")
53 private String appVersion;
56 public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
59 ServerHttpRequest request = serverWebExchange.getRequest();
60 ServerHttpResponse response = serverWebExchange.getResponse();
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();
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()));
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) {
85 return webFilterChain.filter(serverWebExchange);
89 private static String defaultToUUID(String in) {
90 return in == null ? UUID.randomUUID().toString() : in;
93 private static String defaultToEmpty(Object in) {
94 return in == null ? "" : in.toString();