Merge "Improve logging of persister and netconf client."
[controller.git] / opendaylight / commons / controller-maven-plugin / src / main / java / org / opendaylight / controller / maven / plugin / StartControllerMojo.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.maven.plugin;
10
11 import java.net.MalformedURLException;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.maven.plugin.MojoExecutionException;
16 import org.apache.maven.plugin.MojoFailureException;
17 import org.apache.maven.plugins.annotations.LifecyclePhase;
18 import org.apache.maven.plugins.annotations.Mojo;
19 import org.apache.maven.plugins.annotations.Parameter;
20
21
22 /**
23  * Starts the controller
24  */
25 @Mojo( name = "run", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST )
26 public class StartControllerMojo extends AbstractControllerMojo {
27     public static final String REDIRECT_LOG = "controller.out";
28
29     /**
30      * The timeout value for starting the controller. Defaults to 60 secs
31      */
32     @Parameter(defaultValue = "60")
33     public int timeoutSecs = 60;
34
35     /**
36      * The startArgs for starting the controller
37      */
38     @Parameter(required = false)
39     protected List<String> startArgs = new ArrayList<String>();
40
41     /**
42      * The time to wait after successfully connecting to the controller and
43      * before returning from execution.
44      */
45     @Parameter(required = false)
46     protected int warmupTimeSecs = 10;
47
48
49     @Override
50     public void start() throws MojoExecutionException, MojoFailureException {
51         killControllers();
52         // if we can still connect to a controller, bail out
53         if (canConnect()) {
54             getLog().error("A controller is already running. Shutdown and retry.");
55             throw new MojoFailureException("Controller is already running.");
56         }
57         startArgs.add("-D" + CTRL_PROP);
58         Process process = invokeScript(startArgs, REDIRECT_LOG);
59         getLog().info("Controller starting... (waiting for open ports)");
60         try {
61             waitForListening(process);
62             getLog().info("Controller port open. Waiting for warmup: "
63                     + warmupTimeSecs);
64             Thread.sleep(warmupTimeSecs*1000);
65         } catch (Exception e) {
66             throw new MojoExecutionException(e.getMessage());
67         }
68         getLog().info("Controller started successfully.");
69     }
70
71     protected boolean waitForListening(Process process)
72             throws MalformedURLException, InterruptedException, MojoExecutionException
73     {
74         long timeElapsedMillis = 0L;
75         long sleepTimeMillis = 2000L; // 2 secs
76         long timeoutMillis = timeoutSecs * 1000;
77
78         while (timeElapsedMillis < timeoutMillis) {
79             long timeRemaining = timeoutMillis - timeElapsedMillis;
80             sleepTimeMillis *= 2;
81             long toSleep = (sleepTimeMillis > timeRemaining)
82                     ? timeRemaining : sleepTimeMillis;
83             Thread.sleep(toSleep);
84             timeElapsedMillis += toSleep;
85             if (canConnect()) {
86                 return true;
87             }
88             if (!isControllerRunning()) {
89                 throw new MojoExecutionException("Process seems to have exited prematurely.");
90             }
91         }
92         return false;
93     }
94 }