Merge "k8s entrypoint wgets from git not jira now"
[logging-analytics.git] / reference / logging-slf4j-demo / src / test / java / org / onap / logging / ref / slf4j / analysis / CallGraphAnalyzer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.logging
4  * ================================================================================
5  * Copyright © 2018 Amdocs
6  * All rights reserved.
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
22 package org.onap.logging.ref.slf4j.analysis;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.lang3.StringUtils;
28
29 /**
30  * Crude analyzer for log messages, to build a simple
31  * representation of the call graph.
32  */
33 public class CallGraphAnalyzer {
34
35     /** Messages of interest. */
36     private List<LogEntry> mEntries = new ArrayList<>();
37
38     /**
39      * Capture entry if it's interesting.
40      * @param entry candidate.
41      * @return this.
42      */
43     public CallGraphAnalyzer add(final LogEntry entry) {
44
45         if (entry.getLogger().contains("ONAPLogAdapterTest")) {
46             return this;
47         }
48
49         if (StringUtils.isNotBlank(entry.getMarkers())) {
50             this.mEntries.add(entry);
51         }
52
53         return this;
54     }
55
56     /**
57      * Get all captured entries, for diagnostics only.
58      * @return entries.
59      */
60     public List<LogEntry> getEntries() {
61         return this.mEntries;
62     }
63
64     /**
65      * Find the entry point into the call graph through the various components.
66      * @return entry point or (failure) null.
67      */
68     public LogEntry findEntryPoint() {
69         for (final LogEntry e : this.mEntries) {
70             if (e.getLogger().endsWith("ComponentAlpha")) {
71                 if ("ENTRY".equals(e.getMarkers())) {
72                     if (StringUtils.isBlank(e.getPartnerName())) {
73                         return e;
74                     }
75                 }
76             }
77         }
78         return null;
79     }
80
81     /**
82      * Find entries for where a component invokes others.
83      * @param parent parent ENTRY (not actually the entry where it's doing the invoking).
84      * @return components invoked by this one.
85      */
86     public List<LogEntry> findInvokes(final LogEntry parent) {
87         final List<LogEntry> invokes = new ArrayList<>();
88         for (final LogEntry e : this.mEntries) {
89             if (StringUtils.equals(parent.getInvocationID(), e.getInvocationID())) {
90                 final String invokingID = e.getInvokingID();
91                 if (StringUtils.isNotBlank(invokingID)) {
92                     invokes.add(e);
93                 }
94             }
95         }
96         return invokes;
97     }
98
99     /**
100      * Find a specific invocation.
101      * @param invoke invocation record.
102      * @return invocation ENTRY, or (failure) null if not found.
103      */
104     public LogEntry findInvocation(final LogEntry invoke) {
105         for (final LogEntry e : this.mEntries) {
106             if ("ENTRY".equals(e.getMarkers())) {
107                 if (StringUtils.equals(invoke.getInvokingID(), e.getInvocationID())) {
108                     return e;
109                 }
110             }
111         }
112         return null;
113     }
114 }