Fix checkstyle FinalClass issue
[odlparent.git] / bundles4-test / src / main / java / org / opendaylight / odlparent / bundles4test / BundleDiagInfos.java
1 /*
2  * Copyright (c) 2016, 2017 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.odlparent.bundles4test;
9
10 import static org.apache.karaf.bundle.core.BundleState.Active;
11 import static org.apache.karaf.bundle.core.BundleState.Installed;
12
13 import com.google.common.base.Strings;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.ArrayList;
17 import java.util.EnumMap;
18 import java.util.List;
19 import java.util.Map;
20 import org.apache.karaf.bundle.core.BundleInfo;
21 import org.apache.karaf.bundle.core.BundleService;
22 import org.apache.karaf.bundle.core.BundleState;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleContext;
25
26 /**
27  * System readiness diagnostic summary information.
28  *
29  * @author Michael Vorburger.ch
30  */
31 public final class BundleDiagInfos {
32
33     private final List<String> okBundleStateInfoTexts;
34     private final List<String> nokBundleStateInfoTexts;
35     private final List<String> whitelistedBundleStateInfoTexts;
36     private final Map<BundleState, Integer> bundleStatesCounters;
37
38     private static final Map<String, BundleState> WHITELISTED_BUNDLES = ImmutableMap.of(
39             "slf4j.log4j12", Installed);
40
41     private BundleDiagInfos(List<String> okBundleStateInfoTexts, List<String> nokBundleStateInfoTexts,
42             List<String> whitelistedBundleStateInfoTexts, Map<BundleState, Integer> bundleStatesCounters) {
43         this.okBundleStateInfoTexts = ImmutableList.copyOf(okBundleStateInfoTexts);
44         this.nokBundleStateInfoTexts = ImmutableList.copyOf(nokBundleStateInfoTexts);
45         this.whitelistedBundleStateInfoTexts = ImmutableList.copyOf(whitelistedBundleStateInfoTexts);
46         this.bundleStatesCounters = ImmutableMap.copyOf(bundleStatesCounters);
47     }
48
49     public static BundleDiagInfos forContext(BundleContext bundleContext, BundleService bundleService) {
50         List<String> okBundleStateInfoTexts = new ArrayList<>();
51         List<String> nokBundleStateInfoTexts = new ArrayList<>();
52         List<String> whitelistedBundleStateInfoTexts = new ArrayList<>();
53         Map<BundleState, Integer> bundleStatesCounters = new EnumMap<>(BundleState.class);
54         for (BundleState bundleState : BundleState.values()) {
55             bundleStatesCounters.put(bundleState, 0);
56         }
57
58         for (Bundle bundle : bundleContext.getBundles()) {
59             String bundleSymbolicName = bundle.getSymbolicName();
60             BundleInfo karafBundleInfo = bundleService.getInfo(bundle);
61             BundleState karafBundleState = karafBundleInfo.getState();
62
63             String bundleStateDiagText = "OSGi state = " + bundleStatetoText(bundle.getState())
64                 + ", Karaf bundleState = " + karafBundleState;
65             String diagText = bundleService.getDiag(bundle);
66             if (!Strings.isNullOrEmpty(diagText)) {
67                 bundleStateDiagText += ", due to: " + diagText;
68             }
69
70             if (WHITELISTED_BUNDLES.get(bundleSymbolicName) != null) {
71                 if (WHITELISTED_BUNDLES.get(bundleSymbolicName).equals(karafBundleState)) {
72                     String msg = "WHITELISTED " + bundleSymbolicName + ": " + bundleStateDiagText;
73                     whitelistedBundleStateInfoTexts.add(msg);
74                     continue;
75                 }
76             }
77
78             bundleStatesCounters.compute(karafBundleState, (key, counter) -> counter + 1);
79
80             // BundleState comparison as in Karaf's "diag" command,
81             // see https://github.com/apache/karaf/blob/master/bundle/core/src/main/java/org/apache/karaf/bundle/command/Diag.java
82             // but we intentionally, got a little further than Karaf's "diag" command,
83             // and instead of only checking some states, we check what's really Active,
84             // but accept that some remain just Resolved:
85             if (karafBundleState != Active && !(karafBundleState == BundleState.Resolved)) {
86                 String msg = "NOK " + bundleSymbolicName + ": " + bundleStateDiagText;
87                 nokBundleStateInfoTexts.add(msg);
88             } else {
89                 String msg = "OK " + bundleSymbolicName + ": " + bundleStateDiagText;
90                 okBundleStateInfoTexts.add(msg);
91             }
92         }
93
94         return new BundleDiagInfos(okBundleStateInfoTexts, nokBundleStateInfoTexts,
95                 whitelistedBundleStateInfoTexts, bundleStatesCounters);
96     }
97
98     private static String bundleStatetoText(int state) {
99         switch (state) {
100             case Bundle.INSTALLED:
101                 return "Installed";
102             case Bundle.RESOLVED:
103                 return "Resolved";
104             case Bundle.STARTING:
105                 return "Starting";
106             case Bundle.ACTIVE:
107                 return "Active";
108             case Bundle.STOPPING:
109                 return "Stopping";
110             case Bundle.UNINSTALLED:
111                 return "Uninstalled";
112             default:
113                 return state + "???";
114         }
115     }
116
117     public SystemState getSystemState() {
118         if (bundleStatesCounters.get(BundleState.Failure) > 0) {
119             return SystemState.Failure;
120         } else if (bundleStatesCounters.get(BundleState.Stopping) > 0) {
121             return SystemState.Stopping;
122         } else if (bundleStatesCounters.get(BundleState.Installed) == 0
123                 // No, just Resolved is OK, so do not: && bundleStatesCounters.get(BundleState.Resolved) == 0
124                 && bundleStatesCounters.get(BundleState.Unknown) == 0
125                 && bundleStatesCounters.get(BundleState.GracePeriod) == 0
126                 && bundleStatesCounters.get(BundleState.Waiting) == 0
127                 && bundleStatesCounters.get(BundleState.Starting) == 0
128                 // BundleState.Active *should* be ~= total # of bundles (minus Resolved, and whitelisted installed)
129                 && bundleStatesCounters.get(BundleState.Stopping) == 0
130                 && bundleStatesCounters.get(BundleState.Failure) == 0) {
131             return SystemState.Active;
132         } else {
133             return SystemState.Booting;
134         }
135     }
136
137     public String getFullDiagnosticText() {
138         StringBuilder sb = new StringBuilder(getSummaryText());
139         int failureNumber = 1;
140         for (String nokBundleStateInfoText : getNokBundleStateInfoTexts()) {
141             sb.append('\n');
142             sb.append(failureNumber++);
143             sb.append(". ");
144             sb.append(nokBundleStateInfoText);
145         }
146         return sb.toString();
147     }
148
149     public String getSummaryText() {
150         return "diag: " + getSystemState() + " " + bundleStatesCounters.toString();
151     }
152
153     public List<String> getNokBundleStateInfoTexts() {
154         return ImmutableList.copyOf(nokBundleStateInfoTexts);
155     }
156
157     public List<String> getOkBundleStateInfoTexts() {
158         return ImmutableList.copyOf(okBundleStateInfoTexts);
159     }
160
161     public List<String> getWhitelistedBundleStateInfoTexts() {
162         return ImmutableList.copyOf(whitelistedBundleStateInfoTexts);
163     }
164
165     @Override
166     public String toString() {
167         return getFullDiagnosticText();
168     }
169 }