summaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/openecomp/aai/logging/LoggingContext.java
blob: 17d94e619e2d71953922d67deccbbce8f9cc5f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.aai.logging;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.MDC;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class LoggingContext {

	public enum StatusCode {
		COMPLETE,
		ERROR
	}

	private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LoggingContext.class);

	private static final String PREVIOUS_CONTEXT_KEY = "_PREVIOUS_CONTEXT";

	//ECOMP Specific Log Event Fields
	public static enum LoggingField {
		START_TIME("startTime"),
		REQUEST_ID("requestId"),
		SERVICE_INSTANCE_ID("serviceInstanceId"),
		SERVER_NAME("serverName"),
		SERVICE_NAME("serviceName"),
		PARTNER_NAME("partnerName"),
		STATUS_CODE("statusCode"),
		RESPONSE_CODE("responseCode"),
		RESPONSE_DESCRIPTION("responseDescription"),
		INSTANCE_UUID("instanceUUID"),
		SEVERITY("severity"),
		SERVER_IP_ADDRESS("serverIpAddress"),
		ELAPSED_TIME("elapsedTime"),
		SERVER("server"),
		CLIENT_IP_ADDRESS("clientIpAddress"),
		UNUSED("unused"),
		PROCESS_KEY("processKey"),
		CUSTOM_FIELD_1("customField1"),
		CUSTOM_FIELD_2("customField2"),
		CUSTOM_FIELD_3("customField3"),
		CUSTOM_FIELD_4("customField4"),
		
		//ECOMP Specific Metric Log Event Fields
		TARGET_ENTITY("targetEntity"),

		//A&AI Specific Log Event Fields
		COMPONENT("component"),
		STOP_WATCH_START("stopWatchStart");

		private final String text;

		private LoggingField(final String text) {
			this.text = text;
		}

		public String toString() {
			return text;
		}
	}


	public static void init() {
		LoggingContext.clear();
		LoggingContext.startTime();
		LoggingContext.server();
		LoggingContext.serverIpAddress();
	}

	private static void startTime() {
		MDC.put(LoggingField.START_TIME.toString(), LogFormatTools.getCurrentDateTime());
	}

	public static void requestId(UUID requestId) {
		MDC.put(LoggingField.REQUEST_ID.toString(), requestId.toString());
	}

	public static void requestId(String requestId) {
		try {
			MDC.put(LoggingField.REQUEST_ID.toString(), UUID.fromString(requestId).toString());
		} catch (IllegalArgumentException e) {
			final UUID generatedRequestUuid = UUID.randomUUID();
			MDC.put(LoggingField.REQUEST_ID.toString(), generatedRequestUuid.toString());
			LOGGER.warn("Unable to use UUID " + requestId + " (Not formatted properly). Using generated UUID=" + generatedRequestUuid);
		}
	}

	public static void serviceInstanceId(String serviceInstanceId) {
		MDC.put(LoggingField.SERVICE_INSTANCE_ID.toString(), serviceInstanceId);
	}

	public static void serverName(String serverName) {
		MDC.put(LoggingField.SERVER_NAME.toString(), serverName);
	}

	public static void serviceName(String serviceName) {
		MDC.put(LoggingField.SERVICE_NAME.toString(), serviceName);
	}

	public static void partnerName(String partnerName) {
		MDC.put(LoggingField.PARTNER_NAME.toString(), partnerName);
	}

	public static void statusCode(LoggingContext.StatusCode statusCode) {
		MDC.put(LoggingField.STATUS_CODE.toString(), statusCode.toString());
	}

	public static String responseCode() {
		return (String) MDC.get(LoggingField.RESPONSE_CODE.toString());
	}

	public static void responseCode(String responseCode) {
		MDC.put(LoggingField.RESPONSE_CODE.toString(), responseCode);
	}

	public static void responseDescription(String responseDescription) {
		MDC.put(LoggingField.RESPONSE_DESCRIPTION.toString(), responseDescription);
	}

	public static void instanceUuid(UUID instanceUuid) {
		MDC.put(LoggingField.INSTANCE_UUID.toString(), instanceUuid.toString());
	}

	public static void severity(int severity) {
		MDC.put(LoggingField.SEVERITY.toString(), String.valueOf(severity));
	}

	private static void serverIpAddress() {
		try {
			MDC.put(LoggingField.SERVER_IP_ADDRESS.toString(), InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e) {
			LOGGER.warn("Unable to resolve server IP address - will not be displayed in logged events");
		}
	}

	public static void elapsedTime(long elapsedTime, TimeUnit timeUnit) {
		MDC.put(LoggingField.ELAPSED_TIME.toString(), String.valueOf(TimeUnit.MILLISECONDS.convert(elapsedTime, timeUnit)));
	}

	private static void server() {
		try {
			MDC.put(LoggingField.SERVER.toString(),  InetAddress.getLocalHost().getCanonicalHostName());
		} catch (UnknownHostException e) {
			LOGGER.warn("Unable to resolve server IP address - hostname will not be displayed in logged events");
		}
	}

	public static void clientIpAddress(InetAddress clientIpAddress) {
		MDC.put(LoggingField.CLIENT_IP_ADDRESS.toString(), clientIpAddress.getHostAddress());
	}

	public static void clientIpAddress(String clientIpAddress) {
		try {
			MDC.put(LoggingField.CLIENT_IP_ADDRESS.toString(), InetAddress.getByName(clientIpAddress).getHostAddress());
		} catch (UnknownHostException e) {
			//Ignore, will not be thrown since InetAddress.getByName(String) only
			//checks the validity of the passed in string
		}
	}

	public static void unused(String unused) {
		LOGGER.warn("Using field '" + LoggingField.UNUSED + "' (seems like this should go unused...)");
		MDC.put(LoggingField.UNUSED.toString(), unused);
	}

	public static void processKey(String processKey) {
		MDC.put(LoggingField.PROCESS_KEY.toString(), processKey);
	}

	public static void customField1(String customField1) {
		MDC.put(LoggingField.CUSTOM_FIELD_1.toString(), customField1);
	}

	public static void customField2(String customField2) {
		MDC.put(LoggingField.CUSTOM_FIELD_2.toString(), customField2);
	}

	public static void customField3(String customField3) {
		MDC.put(LoggingField.CUSTOM_FIELD_3.toString(), customField3);
	}

	public static void customField4(String customField4) {
		MDC.put(LoggingField.CUSTOM_FIELD_4.toString(), customField4);
	}

	public static void component(String component) {
		MDC.put(LoggingField.COMPONENT.toString(), component);
	}

	public static void targetEntity(String targetEntity) {
		MDC.put(LoggingField.TARGET_ENTITY.toString(), targetEntity);
	}

	public static void stopWatchStart() {
		MDC.put(LoggingField.STOP_WATCH_START.toString(), String.valueOf(System.nanoTime()));
	}

	public static double stopWatchStop() {
		final long stopWatchEnd = System.nanoTime();
		final Long stopWatchStart = Long.valueOf(MDC.get(LoggingField.STOP_WATCH_START.toString()));

		if (stopWatchStart == null) throw new StopWatchNotStartedException();

		MDC.remove(LoggingField.STOP_WATCH_START.toString());

		final double elapsedTimeMillis = (stopWatchEnd - stopWatchStart) / 1000.0 / 1000.0;

		LoggingContext.elapsedTime((long) elapsedTimeMillis, TimeUnit.MILLISECONDS);

		return elapsedTimeMillis;
	}

	public static void put(String key, String value) {
		MDC.put(key, value);
	}

	public static void clear() {
		MDC.clear();
	}

	public static void remove(String key) {
		MDC.remove(key);
	}

	public static void save() {
		final JSONObject context = new JSONObject();

		for (LoggingField field : LoggingField.values()) {
			if (field == LoggingField.ELAPSED_TIME) continue;

			try {
				context.put(field.toString(), MDC.get(field.toString()));
			} catch (JSONException e) {
				//Ignore - only occurs when the key is null (which can't happen)
				//			or the value is invalid (everything is converted to a string
				//			before it get put() to the MDC)
			}
		}

		MDC.put("_PREVIOUS_CONTEXT", context.toString());
	}

	public static void restore() {
		
		final String rawPreviousContext = MDC.get(PREVIOUS_CONTEXT_KEY);
	
		if (rawPreviousContext == null) {
			throw new LoggingContextNotExistsException();
		}

		try {
			final JSONObject previousContext = new JSONObject(rawPreviousContext);

			@SuppressWarnings("unchecked")
			final Iterator<String> keys = previousContext.keys();
	
			while (keys.hasNext()) {
				final String key = keys.next();

				try {
					MDC.put(key, previousContext.getString(key));
				} catch (JSONException e) {
					//Ignore, only occurs when the key is null (cannot happen)
					//			or the value is invalid (they are all strings)
				}
			}
	
			MDC.remove(PREVIOUS_CONTEXT_KEY);
		} catch (JSONException e) {
			//Ignore, the previousContext is serialized from a JSONObject
		}
	}
}