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