Add bundles-diag
[odlparent.git] / bundles-test-lib / src / main / java / org / opendaylight / odlparent / bundlestest / lib / BundleDiagInfosImpl.java
index 6e0d8800df9f2b7f13f034edbc4dfe1e6447d336..67d057fbca4140ef082f924c0bc6790234ec7aca 100644 (file)
@@ -7,21 +7,13 @@
  */
 package org.opendaylight.odlparent.bundlestest.lib;
 
-import static org.apache.karaf.bundle.core.BundleState.Active;
-import static org.apache.karaf.bundle.core.BundleState.Installed;
-import static org.apache.karaf.bundle.core.BundleState.Waiting;
-
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import org.apache.karaf.bundle.core.BundleInfo;
-import org.apache.karaf.bundle.core.BundleService;
-import org.apache.karaf.bundle.core.BundleState;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
+import org.opendaylight.odlparent.bundles.diag.ContainerState;
+import org.opendaylight.odlparent.bundles.diag.Diag;
 
 /**
  * {@link BundleDiagInfos} implementation.
@@ -33,16 +25,16 @@ final class BundleDiagInfosImpl implements BundleDiagInfos {
     @java.io.Serial
     private static final long serialVersionUID = 1L;
 
-    private static final Map<String, BundleState> WHITELISTED_BUNDLES = Map.of(
-        "slf4j.log4j12", Installed,
+    private static final Map<String, ContainerState> WHITELISTED_BUNDLES = Map.of(
+        "slf4j.log4j12", ContainerState.INSTALLED,
         // ODLPARENT-144
-        "org.apache.karaf.scr.management", Waiting);
+        "org.apache.karaf.scr.management", ContainerState.WAITING);
 
     private final List<String> okBundleStateInfoTexts;
     private final List<String> nokBundleStateInfoTexts;
     private final List<String> whitelistedBundleStateInfoTexts;
-    private final Map<BundleState, Integer> bundleStatesCounters;
-    private final Map<BundleSymbolicNameWithVersion, BundleState> bundlesStateMap;
+    private final Map<ContainerState, Integer> bundleStatesCounters;
+    private final Map<BundleSymbolicNameWithVersion, ContainerState> bundlesStateMap;
 
     /**
      * Create an instance. The collections provided as arguments will be kept as-is; it’s up to the caller
@@ -54,9 +46,9 @@ final class BundleDiagInfosImpl implements BundleDiagInfos {
      * @param bundleStatesCounters bundle state counters.
      * @param bundlesStateMap bundle state map (state of each bundle).
      */
-    private BundleDiagInfosImpl(List<String> okBundleStateInfoTexts, List<String> nokBundleStateInfoTexts,
-            List<String> whitelistedBundleStateInfoTexts, Map<BundleState, Integer> bundleStatesCounters,
-            Map<BundleSymbolicNameWithVersion, BundleState> bundlesStateMap) {
+    private BundleDiagInfosImpl(final List<String> okBundleStateInfoTexts, final List<String> nokBundleStateInfoTexts,
+            final List<String> whitelistedBundleStateInfoTexts, final Map<ContainerState, Integer> bundleStatesCounters,
+            final Map<BundleSymbolicNameWithVersion, ContainerState> bundlesStateMap) {
         this.okBundleStateInfoTexts = okBundleStateInfoTexts;
         this.nokBundleStateInfoTexts = nokBundleStateInfoTexts;
         this.whitelistedBundleStateInfoTexts = whitelistedBundleStateInfoTexts;
@@ -64,85 +56,64 @@ final class BundleDiagInfosImpl implements BundleDiagInfos {
         this.bundlesStateMap = bundlesStateMap;
     }
 
-    public static BundleDiagInfos forContext(BundleContext bundleContext, BundleService bundleService) {
-        List<String> okBundleStateInfoTexts = new ArrayList<>();
-        List<String> nokBundleStateInfoTexts = new ArrayList<>();
-        List<String> whitelistedBundleStateInfoTexts = new ArrayList<>();
-        Map<BundleSymbolicNameWithVersion, BundleState> bundlesStateMap = new HashMap<>();
-        Map<BundleState, Integer> bundleStatesCounters = new EnumMap<>(BundleState.class);
-        for (BundleState bundleState : BundleState.values()) {
-            bundleStatesCounters.put(bundleState, 0);
-        }
+    static BundleDiagInfosImpl ofDiag(final Diag diag) {
+        final var okBundleStateInfoTexts = new ArrayList<String>();
+        final var nokBundleStateInfoTexts = new ArrayList<String>();
+        final var whitelistedBundleStateInfoTexts = new ArrayList<String>();
+        final var bundlesStateMap = new HashMap<BundleSymbolicNameWithVersion, ContainerState>();
 
-        for (Bundle bundle : bundleContext.getBundles()) {
-            String bundleSymbolicName = bundle.getSymbolicName();
-            BundleSymbolicNameWithVersion bundleSymbolicNameWithVersion
-                = new BundleSymbolicNameWithVersion(bundleSymbolicName, bundle.getVersion().toString());
+        for (var bundle : diag.bundles()) {
+            final var bundleSymbolicName = bundle.symbolicName();
+            final var bundleSymbolicNameWithVersion = new BundleSymbolicNameWithVersion(bundleSymbolicName,
+                bundle.version());
 
-            BundleInfo karafBundleInfo = bundleService.getInfo(bundle);
-            String diagText = bundleService.getDiag(bundle);
-            BundleState karafBundleState = karafBundleInfo.getState();
-            bundlesStateMap.put(bundleSymbolicNameWithVersion, karafBundleState);
+            final var serviceState = bundle.serviceState();
+            final var diagText = serviceState.diag();
+            final var karafBundleState = serviceState.containerState();
+            bundlesStateMap.put(bundleSymbolicNameWithVersion, serviceState.containerState());
 
-            String bundleStateDiagText = "OSGi state = " + bundleStateToText(bundle.getState())
-                + ", Karaf bundleState = " + karafBundleState
+            final var bundleStateDiagText = "OSGi state = " + bundle.frameworkState().symbolicName()
+                + ", Karaf bundleState = " + karafBundleState.name()
                 + (diagText.isEmpty() ? "" : ", due to: " + diagText);
 
-            if (bundleSymbolicName != null && WHITELISTED_BUNDLES.get(bundleSymbolicName) == karafBundleState) {
+            if (bundleSymbolicName != null && karafBundleState.equals(WHITELISTED_BUNDLES.get(bundleSymbolicName))) {
                 whitelistedBundleStateInfoTexts.add(
                     "WHITELISTED " + bundleSymbolicNameWithVersion + ": " + bundleStateDiagText);
                 continue;
             }
 
-            bundleStatesCounters.compute(karafBundleState, (key, counter) -> counter + 1);
-
             // BundleState comparison as in Karaf's "diag" command,
             // see https://github.com/apache/karaf/blob/master/bundle/core/src/main/java/org/apache/karaf/bundle/command/Diag.java
             // but we intentionally, got a little further than Karaf's "diag" command,
             // and instead of only checking some states, we check what's really Active,
             // but accept that some remain just Resolved:
-            if (karafBundleState != Active && karafBundleState != BundleState.Resolved) {
-                String msg = "NOK " + bundleSymbolicNameWithVersion + ": " + bundleStateDiagText;
-                nokBundleStateInfoTexts.add(msg);
+            if (karafBundleState != ContainerState.ACTIVE && karafBundleState != ContainerState.RESOLVED) {
+                nokBundleStateInfoTexts.add("NOK " + bundleSymbolicNameWithVersion + ": " + bundleStateDiagText);
             } else {
-                String msg = "OK " + bundleSymbolicNameWithVersion + ": " + bundleStateDiagText;
-                okBundleStateInfoTexts.add(msg);
+                okBundleStateInfoTexts.add("OK " + bundleSymbolicNameWithVersion + ": " + bundleStateDiagText);
             }
         }
 
-        return new BundleDiagInfosImpl(Collections.unmodifiableList(okBundleStateInfoTexts),
-                Collections.unmodifiableList(nokBundleStateInfoTexts),
-                Collections.unmodifiableList(whitelistedBundleStateInfoTexts),
-                Collections.unmodifiableMap(bundleStatesCounters), Collections.unmodifiableMap(bundlesStateMap));
-    }
-
-    private static String bundleStateToText(int state) {
-        return switch (state) {
-            case Bundle.INSTALLED -> "Installed";
-            case Bundle.RESOLVED -> "Resolved";
-            case Bundle.STARTING -> "Starting";
-            case Bundle.ACTIVE -> "Active";
-            case Bundle.STOPPING -> "Stopping";
-            case Bundle.UNINSTALLED -> "Uninstalled";
-            default -> state + "???";
-        };
+        return new BundleDiagInfosImpl(List.copyOf(okBundleStateInfoTexts), List.copyOf(nokBundleStateInfoTexts),
+            List.copyOf(whitelistedBundleStateInfoTexts), Collections.unmodifiableMap(diag.containerStateFrequencies()),
+            Map.copyOf(bundlesStateMap));
     }
 
     @Override
     public SystemState getSystemState() {
-        if (bundleStatesCounters.get(BundleState.Failure) > 0) {
+        if (bundleStatesCounters.get(ContainerState.FAILURE) > 0) {
             return SystemState.Failure;
-        } else if (bundleStatesCounters.get(BundleState.Stopping) > 0) {
+        } else if (bundleStatesCounters.get(ContainerState.STOPPING) > 0) {
             return SystemState.Stopping;
-        } else if (bundleStatesCounters.get(BundleState.Installed) == 0
+        } else if (bundleStatesCounters.get(ContainerState.INSTALLED) == 0
                 // No, just Resolved is OK, so do not: && bundleStatesCounters.get(BundleState.Resolved) == 0
-                && bundleStatesCounters.get(BundleState.Unknown) == 0
-                && bundleStatesCounters.get(BundleState.GracePeriod) == 0
-                && bundleStatesCounters.get(BundleState.Waiting) == 0
-                && bundleStatesCounters.get(BundleState.Starting) == 0
+                && bundleStatesCounters.get(ContainerState.UNKNOWN) == 0
+                && bundleStatesCounters.get(ContainerState.GRACE_PERIOD) == 0
+                && bundleStatesCounters.get(ContainerState.WAITING) == 0
+                && bundleStatesCounters.get(ContainerState.STARTING) == 0
                 // BundleState.Active *should* be ~= total # of bundles (minus Resolved, and whitelisted installed)
-                && bundleStatesCounters.get(BundleState.Stopping) == 0
-                && bundleStatesCounters.get(BundleState.Failure) == 0) {
+                && bundleStatesCounters.get(ContainerState.STOPPING) == 0
+                && bundleStatesCounters.get(ContainerState.FAILURE) == 0) {
             return SystemState.Active;
         } else {
             return SystemState.Booting;
@@ -151,13 +122,10 @@ final class BundleDiagInfosImpl implements BundleDiagInfos {
 
     @Override
     public String getFullDiagnosticText() {
-        StringBuilder sb = new StringBuilder(getSummaryText());
+        final var sb = new StringBuilder(getSummaryText());
         int failureNumber = 1;
-        for (String nokBundleStateInfoText : getNokBundleStateInfoTexts()) {
-            sb.append('\n');
-            sb.append(failureNumber++);
-            sb.append(". ");
-            sb.append(nokBundleStateInfoText);
+        for (var nokBundleStateInfoText : getNokBundleStateInfoTexts()) {
+            sb.append('\n').append(failureNumber++).append(". ").append(nokBundleStateInfoText);
         }
         return sb.toString();
     }
@@ -168,7 +136,7 @@ final class BundleDiagInfosImpl implements BundleDiagInfos {
     }
 
     @Override
-    public Map<BundleSymbolicNameWithVersion, BundleState> getBundlesStateMap() {
+    public Map<BundleSymbolicNameWithVersion, ContainerState> getBundlesStateMap() {
         return bundlesStateMap;
     }
 
@@ -191,5 +159,4 @@ final class BundleDiagInfosImpl implements BundleDiagInfos {
     public String toString() {
         return getFullDiagnosticText();
     }
-
 }