ad29c5f21e234cb825ea093bb3ecf4fc4c575d26
[ovsdb.git] / library / it / src / test / java / org / opendaylight / ovsdb / lib / it / LibraryIntegrationTestUtils.java
1 /*
2  * Copyright © 2015 Red Hat, 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.ovsdb.lib.it;
10
11 import java.io.IOException;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Objects;
15 import java.util.Properties;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.opendaylight.ovsdb.lib.OvsdbClient;
24 import org.opendaylight.ovsdb.lib.OvsdbConnection;
25 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
26 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Utilities for OVSDB integration tests.
32  */
33 public final class LibraryIntegrationTestUtils {
34     private static final Logger LOG = LoggerFactory.getLogger(LibraryIntegrationTestUtils.class);
35     public static final String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
36     public static final String SERVER_PORT = "ovsdbserver.port";
37     public static final String CONNECTION_TYPE = "ovsdbserver.connection";
38     public static final String OPEN_VSWITCH = "Open_vSwitch";
39     public static final String HARDWARE_VTEP = "hardware_vtep";
40     private static final String CONNECTION_TYPE_ACTIVE = "active";
41     private static final String CONNECTION_TYPE_PASSIVE = "passive";
42     private static final String DEFAULT_SERVER_PORT = "6640";
43
44     /**
45      * Prevent instantiation of a utility class.
46      */
47     private LibraryIntegrationTestUtils() {
48         // Nothing to do
49     }
50
51     public static OvsdbClient getTestConnection(Object provider) throws IOException,
52             InterruptedException, ExecutionException, TimeoutException {
53         Properties props = System.getProperties();
54         String addressStr = props.getProperty(SERVER_IPADDRESS);
55         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
56         String connectionType = props.getProperty(CONNECTION_TYPE, "active");
57
58         // If the connection type is active, controller connects to the ovsdb-server
59         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
60             if (addressStr == null) {
61                 throw new IllegalArgumentException(usage());
62             }
63
64             InetAddress address;
65             try {
66                 address = InetAddress.getByName(addressStr);
67             } catch (UnknownHostException e) {
68                 LOG.warn("Unable to resolve {}", addressStr, e);
69                 return null;
70             }
71
72             Integer port;
73             try {
74                 port = Integer.parseInt(portStr);
75             } catch (NumberFormatException e) {
76                 LOG.warn("Invalid port number: {}", portStr, e);
77                 return null;
78             }
79
80             OvsdbConnection connection =
81                     (OvsdbConnection) ServiceHelper.getGlobalInstance(OvsdbConnection.class, provider);
82             return connection.connect(address, port);
83         } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
84             ExecutorService executor = Executors.newFixedThreadPool(1);
85             Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
86             return passiveConnection.get(60, TimeUnit.SECONDS);
87         }
88         throw new IllegalArgumentException("Connection parameter (" + CONNECTION_TYPE
89                 + ") must be either active or passive");
90     }
91
92     private static String usage() {
93         return "Integration Test needs a valid connection configuration as follows :\n"
94                 + "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x "
95                 + " -Dovsdbserver.port=yyyy verify\n"
96                 + "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
97     }
98
99     private static class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
100         OvsdbClient client = null;
101
102         @Override
103         public OvsdbClient call() throws Exception {
104             OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
105             connection.registerConnectionListener(this);
106             while (client == null) {
107                 Thread.sleep(500);
108             }
109             return client;
110         }
111
112         @Override
113         public void connected(OvsdbClient newClient) {
114             this.client = newClient;
115         }
116
117         @Override
118         public void disconnected(OvsdbClient newClient) {
119             if (!Objects.equals(this.client.getConnectionInfo(), newClient.getConnectionInfo())) {
120                 throw new IllegalStateException("disconnected unexpected client");
121             }
122             this.client = null;
123         }
124     }
125 }