aboutsummaryrefslogtreecommitdiffstats
path: root/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/MdcTransaction.java
blob: e3b2f8a8a4e78cdbf477a08713d42f9e05b97749 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*-
 * ============LICENSE_START=======================================================
 * policy-utils
 * ================================================================================
 * Copyright (C) 2018-2019 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.
 */

package org.onap.policy.drools.utils.logging;

import java.time.Instant;
import org.onap.policy.common.utils.network.NetworkUtil;

/**
 * MDC Transaction Utility Class.
 *
 * <p>There is an implicit 2-level tree of Transactions in ONAP: transactions and subtransactions.
 *
 * <p>1. The top level transaction relates to the overarching transaction id (ie. RequestId) and should
 * be made available to subtransactions for reuse in the ThreadLocal MDC structure.
 *
 * <p>This is the data to be inherited and common to all subtransactions (not a common case but could
 * be modified by subtransactions):
 *
 * <p>Request ID Virtual Server Name Partner Name Server Server IP Address Server FQDN
 *
 * <p>2. The second level at the leaves is formed by subtransactions and the key identifier is the
 * invocation id.
 *
 * <p>Begin Timestamp End Timestamp Elapsed Time Service Instance ID Service Name Status Code Response
 * Code Response Description Instance UUID Severity Target Entity Target Service Name Server Server
 * IP Address Server FQDN Client IP Address Process Key Remote Host Alert Severity Target Virtual
 * Entity
 *
 *
 * <p>The naming convention for the fields must match the naming given at
 *
 * <p>https://wiki.onap.org/pages/viewpage.action?pageId=20087036
 */
public interface MdcTransaction {
    /*
     * The fields must match the naming given at
     * https://wiki.onap.org/pages/viewpage.action?pageId=20087036
     */

    /**
     * End to end transaction ID. Subtransactions will inherit this value from the transaction.
     */
    String REQUEST_ID = "RequestID";

    /**
     * Invocation ID, ie. SubTransaction ID.
     */
    String INVOCATION_ID = "InvocationID";

    /**
     * Service Name. Both transactions and subtransactions will have its own copy.
     */
    String SERVICE_NAME = "ServiceName";

    /**
     * Partner Name Subtransactions will inherit this value from the transaction.
     */
    String PARTNER_NAME = "PartnerName";

    /**
     * Start Timestamp. Both transactions and subtransactions will have its own copy.
     */
    String BEGIN_TIMESTAMP = "BeginTimestamp";

    /**
     * End Timestamp. Both transactions and subtransactions will have its own copy.
     */
    String END_TIMESTAMP = "EndTimestamp";

    /**
     * Elapsed Time. Both transactions and subtransactions will have its own copy.
     */
    String ELAPSED_TIME = "ElapsedTime";

    /**
     * Elapsed Time. Both transactions and subtransactions will have its own copy.
     */
    String SERVICE_INSTANCE_ID = "ServiceInstanceID";

    /**
     * Virtual Server Name. Subtransactions will inherit this value from the transaction.
     */
    String VIRTUAL_SERVER_NAME = "VirtualServerName";

    /**
     * Status Code Both transactions and subtransactions will have its own copy.
     */
    String STATUS_CODE = "StatusCode";

    /**
     * Response Code Both transactions and subtransactions will have its own copy.
     */
    String RESPONSE_CODE = "ResponseCode";

    /**
     * Response Description Both transactions and subtransactions will have its own copy.
     */
    String RESPONSE_DESCRIPTION = "ResponseDescription";

    /**
     * Instance UUID Both transactions and subtransactions will have its own copy.
     */
    String INSTANCE_UUID = "InstanceUUID";

    /**
     * Severity Both transactions and subtransactions will have its own copy.
     */
    String SEVERITY = "Severity";

    /**
     * Target Entity Both transactions and subtransactions will have its own copy.
     */
    String TARGET_ENTITY = "TargetEntity";

    /**
     * Target Service Name Both transactions and subtransactions will have its own copy.
     */
    String TARGET_SERVICE_NAME = "TargetServiceName";

    /**
     * Server Subtransactions inherit this value. if (this.getSources().size() == 1)
     * this.getSources().get(0).getTopic();
     */
    String SERVER = "Server";

    /**
     * Server IP Address Subtransactions inherit this value.
     */
    String SERVER_IP_ADDRESS = "ServerIpAddress";

    /**
     * Server FQDN Subtransactions inherit this value.
     */
    String SERVER_FQDN = "ServerFQDN";

    /**
     * Client IP Address Both transactions and subtransactions will have its own copy.
     */
    String CLIENT_IP_ADDRESS = "ClientIPAddress";

    /**
     * Process Key Both transactions and subtransactions will have its own copy.
     */
    String PROCESS_KEY = "ProcessKey";

    /**
     * Remote Host Both transactions and subtransactions will have its own copy.
     */
    String REMOTE_HOST = "RemoteHost";

    /**
     * Alert Severity Both transactions and subtransactions will have its own copy.
     */
    String ALERT_SEVERITY = "AlertSeverity";

    /**
     * Target Virtual Entity Both transactions and subtransactions will have its own copy.
     */
    String TARGET_VIRTUAL_ENTITY = "TargetVirtualEntity";

    /**
     * Default Service Name.
     */
    String DEFAULT_SERVICE_NAME = "PDP-D";

    /**
     * Default Host Name.
     */
    String DEFAULT_HOSTNAME = NetworkUtil.getHostname();

    /**
     * Default Host IP.
     */
    String DEFAULT_HOSTIP = NetworkUtil.getHostIp();

    /**
     * Status Code Complete.
     */
    String STATUS_CODE_COMPLETE = "COMPLETE";

    /**
     * Status Code Error.
     */
    String STATUS_CODE_FAILURE = "ERROR";

    /**
     * reset subtransaction data.
     */
    MdcTransaction resetSubTransaction();

    /**
     * resets transaction data.
     */
    MdcTransaction resetTransaction();

    /**
     * flush to MDC structure.
     */
    MdcTransaction flush();

    /**
     * convenience method to log a metric. Alternatively caller could call flush() and the logging
     * statement directly for further granularity.
     */
    MdcTransaction metric();

    /**
     * convenience method to log a transaction record. Alternatively caller could call flush() and
     * the logging statement directly for further granularity.
     */
    MdcTransaction transaction();

    /**
     * get invocation id.
     */
    MdcTransaction setInvocationId(String invocationId);

    /**
     * set start time.
     */
    MdcTransaction setStartTime(Instant startTime);

    /**
     * set service name.
     */
    MdcTransaction setServiceName(String serviceName);

    /**
     * set status code.
     */
    MdcTransaction setStatusCode(String statusCode);

    /**
     * set status code.
     */
    MdcTransaction setStatusCode(boolean success);

    /**
     * sets response code.
     */
    MdcTransaction setResponseCode(String responseCode);

    /**
     * sets response description.
     */
    MdcTransaction setResponseDescription(String responseDescription);

    /**
     * sets instance uuid.
     */
    MdcTransaction setInstanceUuid(String instanceUuid);

    /**
     * set severity.
     */
    MdcTransaction setSeverity(String severity);

    /**
     * set target entity.
     */
    MdcTransaction setTargetEntity(String targetEntity);

    /**
     * set target service name.
     */
    MdcTransaction setTargetServiceName(String targetServiceName);

    /**
     * set target virtual entity.
     */
    MdcTransaction setTargetVirtualEntity(String targetVirtualEntity);

    /**
     * set request id.
     */
    MdcTransaction setRequestId(String requestId);

    /**
     * set partner.
     */
    MdcTransaction setPartner(String partner);

    /**
     * set server.
     */
    MdcTransaction setServer(String server);

    /**
     * set server ip address.
     */
    MdcTransaction setServerIpAddress(String serverIpAddress);

    /**
     * set server fqdn.
     */
    MdcTransaction setServerFqdn(String serverFqdn);

    /**
     * set virtual server.
     */
    MdcTransaction setVirtualServerName(String virtualServerName);

    /**
     * sets end time.
     */
    MdcTransaction setEndTime(Instant endTime);

    /**
     * sets elapsed time.
     */
    MdcTransaction setElapsedTime(Long elapsedTime);

    /**
     * sets service instance id.
     */
    MdcTransaction setServiceInstanceId(String serviceInstanceId);

    /**
     * sets process key.
     */
    MdcTransaction setProcessKey(String processKey);

    /**
     * sets alert severity.
     */
    MdcTransaction setAlertSeverity(String alertSeverity);

    /**
     * sets client ip address.
     */
    MdcTransaction setClientIpAddress(String clientIpAddress);

    /**
     * sets remote host.
     */
    MdcTransaction setRemoteHost(String remoteHost);

    /**
     * get start time.
     */
    Instant getStartTime();

    /**
     * get server.
     */
    String getServer();

    /**
     * get end time.
     */
    Instant getEndTime();

    /**
     * get elapsed time.
     */
    Long getElapsedTime();

    /**
     * get remote host.
     */
    String getRemoteHost();

    /**
     * get client ip address.
     */
    String getClientIpAddress();

    /**
     * get alert severity.
     */
    String getAlertSeverity();

    /**
     * get process key.
     */
    String getProcessKey();

    /**
     * get service instance id.
     */
    String getServiceInstanceId();

    /**
     * get invocation id.
     */
    String getInvocationId();

    /**
     * get service name.
     */
    String getServiceName();

    /**
     * get status code.
     */
    String getStatusCode();

    /**
     * get response description.
     */
    String getResponseDescription();

    /**
     * get instance uuid.
     */
    String getInstanceUuid();

    /**
     * get severity.
     */
    String getSeverity();

    /**
     * get target entity.
     */
    String getTargetEntity();

    /**
     * get service name.
     */
    String getTargetServiceName();

    /**
     * get target virtual entity.
     */
    String getTargetVirtualEntity();

    /**
     * get response code.
     */
    String getResponseCode();

    /**
     * get request id.
     */
    String getRequestId();

    /**
     * get partner.
     */
    String getPartner();

    /**
     * get server fqdn.
     */
    String getServerFqdn();

    /**
     * get virtual server name.
     */
    String getVirtualServerName();

    /**
     * get server ip.
     */
    String getServerIpAddress();

    /**
     * generate timestamp used for logging.
     */
    String timestamp(Instant time);

    /**
     * create new MDC Transaction.
     *
     * @param requestId transaction Id
     * @param partner requesting partner
     *
     * @return MDC Transaction
     */
    static MdcTransaction newTransaction(String requestId, String partner) {
        return new MdcTransactionImpl(requestId, partner);
    }

    /**
     * create new MDC Transaction.
     */
    static MdcTransaction newTransaction() {
        return new MdcTransactionImpl();
    }

    /**
     * create new subtransaction.
     *
     * @param invocationId sub-transaction od
     * @return MDC Transaction
     */
    static MdcTransaction newSubTransaction(String invocationId) {
        return new MdcTransactionImpl(invocationId);
    }

    /**
     * create transaction from an existing one.
     *
     * @param transaction transaction
     * @return MDC Transaction
     */
    static MdcTransaction fromTransaction(MdcTransaction transaction) {
        return new MdcTransactionImpl(transaction);
    }

}