d92277269a01f2f8fc3eac7df8af6d78f212bf22
[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
16
17     private String stateToString(int state) {
18         switch (state) {
19         case Bundle.ACTIVE:
20             return "ACTIVE";
21         case Bundle.INSTALLED:
22             return "INSTALLED";
23         case Bundle.RESOLVED:
24             return "RESOLVED";
25         case Bundle.UNINSTALLED:
26             return "UNINSTALLED";
27         case Bundle.STARTING:
28             return "STARTING";
29         default:
30             return "Not CONVERTED: state value is " + state;
31         }
32     }
33
34     public void start(final BundleContext bundleContext) throws Exception {
35         Timer monitorTimer = new Timer("monitor timer", true);
36         monitorTimer.schedule(new TimerTask() {
37             @Override
38             public void run() {
39                 int countup = 0;
40                 boolean failed = false;
41                 boolean resolved = false;
42                 while (!resolved) {
43                     resolved = true;
44                     failed = false;
45                     for(Bundle bundle : bundleContext.getBundles()){
46                         /*
47                          * A bundle should be ACTIVE, unless it a fragment, in which case it should be RESOLVED
48                          */
49                         int state = bundle.getState();
50                         if ((bundle.adapt(BundleRevision.class).getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
51                             //fragment
52                             if (state != Bundle.RESOLVED) {
53                                 System.out.println("------ Failed to activate/resolve fragment = " + bundle.getSymbolicName() + " state = " + stateToString(bundle.getState()));
54                                 failed = true;
55                                 if (state == Bundle.STARTING)
56                                     resolved = false;
57                             }
58                         } else {
59                             if(state != Bundle.ACTIVE) {
60                                 System.out.println("------ Failed to activate/resolve bundle = " + bundle.getSymbolicName() + " state = " + stateToString(bundle.getState()));
61                                 failed = true;
62                                 if (state == Bundle.STARTING)
63                                     resolved = false;
64                             }
65                         }
66                     }
67                     if (!resolved) {
68                         countup++;
69                         if (countup < 60) {
70                             System.out.println("all bundles haven't finished starting, will repeat");
71                             try {
72                                 Thread.sleep(SUBSEQUENT_DELAY);
73                             } catch (Exception e) {
74                                 ;
75                             }
76                         } else
77                             resolved = true;
78                     }
79                 }
80
81                 if(failed){
82                     System.out.flush();
83                     System.out.println("exiting with 1 as failed");
84                     System.out.close();
85                     Runtime.getRuntime().exit(1);
86                 } else {
87                     System.out.flush();
88                     System.out.println("exiting with 0 as succeeded");
89                     System.out.close();
90                     Runtime.getRuntime().exit(0);
91                 }
92             }
93         }, INITIAL_DELAY);
94     }
95
96     public void stop(BundleContext bundleContext) throws Exception {
97
98     }
99 }