Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-util / src / test / java / org / opendaylight / controller / config / util / JolokiaHelper.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 package org.opendaylight.controller.config.util;
9
10 import java.io.IOException;
11
12 import org.jolokia.jvmagent.JolokiaServer;
13 import org.jolokia.jvmagent.JvmAgentConfig;
14
15 public class JolokiaHelper {
16     private static JolokiaServer jolokiaServer;
17
18     /**
19      * Bind to port 17777. By convention, ports above 10000 are used for testing
20      * and < 10000 for production
21      *
22      * @return url that can be passed to new J4pClient(url)
23      */
24     public static String startTestingJolokia() {
25         return startJolokia("localhost", 17777);
26     }
27
28     /**
29      * @return url that can be passed to new J4pClient(url)
30      * @throws IOException
31      */
32     public static String startJolokia(String host, int port) {
33         String agentArgs = "host=" + host + ",port=" + port;
34         JvmAgentConfig config = new JvmAgentConfig(agentArgs);
35         Exception lastException = null;
36         for (int i = 0; i < 10; i++) {
37             try {
38                 jolokiaServer = new JolokiaServer(config, false);
39                 jolokiaServer.start();
40                 return "http://" + host + ":" + port + "/jolokia/";
41             } catch (Exception e) {
42                 lastException = e;
43                 try {
44                     Thread.sleep(100);
45                 } catch (InterruptedException ie) {
46                     ie.printStackTrace();
47                 }
48             }
49         }
50         throw new RuntimeException(lastException);
51     }
52
53     public static void stopJolokia() {
54         jolokiaServer.stop();
55     }
56 }