Make the sanity test countup limit a constant and set it to 120
[controller.git] / opendaylight / distribution / sanitytest / src / main / java / org / opendaylight / controller / sanitytest / internal / Activator.java
1 package org.opendaylight.controller.sanitytest.internal;
2
3 import java.util.Timer;
4 import java.util.TimerTask;
5
6 import org.osgi.framework.Bundle;
7 import org.osgi.framework.BundleActivator;
8 import org.osgi.framework.BundleContext;
9 import org.osgi.framework.wiring.BundleRevision;
10
11 public class Activator implements BundleActivator {
12     //10 Second initial, 1 second subsequent
13     private static final int INITIAL_DELAY = 10000;
14     private static final int SUBSEQUENT_DELAY = 1000;
15     private static final int MAX_ATTEMPTS = 120;
16
17
18     private String stateToString(int state) {
19         switch (state) {
20         case Bundle.ACTIVE:
21             return "ACTIVE";
22         case Bundle.INSTALLED:
23             return "INSTALLED";
24         case Bundle.RESOLVED:
25             return "RESOLVED";
26         case Bundle.UNINSTALLED:
27             return "UNINSTALLED";
28         case Bundle.STARTING:
29             return "STARTING";
30         default:
31             return "Not CONVERTED: state value is " + state;
32         }
33     }
34
35     public void start(final BundleContext bundleContext) throws Exception {
36         Timer monitorTimer = new Timer("monitor timer", true);
37         monitorTimer.schedule(new TimerTask() {
38             @Override
39             public void run() {
40                 int countup = 0;
41                 boolean failed = false;
42                 boolean resolved = false;
43                 while (!resolved) {
44                     resolved = true;
45                     failed = false;
46                     for(Bundle bundle : bundleContext.getBundles()){
47                         /*
48                          * A bundle should be ACTIVE, unless it a fragment, in which case it should be RESOLVED
49                          */
50                         int state = bundle.getState();
51                         if ((bundle.adapt(BundleRevision.class).getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
52                             //fragment
53                             if (state != Bundle.RESOLVED) {
54                                 System.out.println("------ Failed to activate/resolve fragment = " + bundle.getSymbolicName() + " state = " + stateToString(bundle.getState()));
55                                 failed = true;
56                                 if (state == Bundle.STARTING)
57                                     resolved = false;
58                             }
59                         } else {
60                             if(state != Bundle.ACTIVE) {
61                                 System.out.println("------ Failed to activate/resolve bundle = " + bundle.getSymbolicName() + " state = " + stateToString(bundle.getState()));
62                                 failed = true;
63                                 if (state == Bundle.STARTING)
64                                     resolved = false;
65                             }
66                         }
67                     }
68                     if (!resolved) {
69                         countup++;
70                         if (countup < MAX_ATTEMPTS) {
71                             System.out.println("all bundles haven't finished starting, will repeat");
72                             try {
73                                 Thread.sleep(SUBSEQUENT_DELAY);
74                             } catch (Exception e) {
75                                 System.out.println("Thread.sleep interuptted.");
76                                 break;
77                             }
78                         } else
79                             resolved = true;
80                     }
81                 }
82
83                 if(failed){
84                     System.out.flush();
85                     System.out.println("exiting with 1 as failed");
86                     System.out.close();
87                     Runtime.getRuntime().exit(1);
88                 } else {
89                     System.out.flush();
90                     System.out.println("exiting with 0 as succeeded");
91                     System.out.close();
92                     Runtime.getRuntime().exit(0);
93                 }
94             }
95         }, INITIAL_DELAY);
96     }
97
98     public void stop(BundleContext bundleContext) throws Exception {
99
100     }
101 }