Merge "Bug 1885: VM ingress rule is not installed for vlan networking"
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / OvsdbIntegrationTestBase.java
1 /*
2  * Copyright (c) 2014 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  * Authors : Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.integrationtest;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import java.io.IOException;
18 import java.net.InetAddress;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Properties;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30
31 import org.opendaylight.controller.sal.connection.ConnectionConstants;
32 import org.opendaylight.controller.sal.core.Node;
33 import org.opendaylight.controller.sal.utils.ServiceHelper;
34 import org.opendaylight.ovsdb.lib.OvsdbClient;
35 import org.opendaylight.ovsdb.lib.OvsdbConnection;
36 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
37 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
38
39 public abstract class OvsdbIntegrationTestBase {
40     protected final static String IDENTIFIER = "TEST";
41     protected final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
42     protected final static String SERVER_PORT = "ovsdbserver.port";
43     protected final static String CONNECTION_TYPE = "ovsdbserver.connection";
44     protected final static String CONNECTION_TYPE_ACTIVE = "active";
45     protected final static String CONNECTION_TYPE_PASSIVE = "passive";
46     protected final static int CONNECTION_INIT_TIMEOUT = 10000;
47
48     protected final static String DEFAULT_SERVER_PORT = "6640";
49
50     /**
51      * Represents the Open Vswitch Schema
52      */
53     public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
54     public final static String HARDWARE_VTEP = "hardware_vtep";
55
56     public Properties loadProperties() {
57         Properties props = new Properties(System.getProperties());
58         return props;
59     }
60
61     public Node getPluginTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
62         Properties props = loadProperties();
63         String addressStr = props.getProperty(SERVER_IPADDRESS);
64         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
65         String connectionType = props.getProperty(CONNECTION_TYPE, "active");
66
67         OvsdbConnectionService
68                 connection = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
69         // If the connection type is active, controller connects to the ovsdb-server
70         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
71             if (addressStr == null) {
72                 fail(usage());
73             }
74
75             Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
76             params.put(ConnectionConstants.ADDRESS, addressStr);
77             params.put(ConnectionConstants.PORT, portStr);
78             return connection.connect(IDENTIFIER, params);
79         }  else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
80             // Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
81             Thread.sleep(CONNECTION_INIT_TIMEOUT);
82             List<Node> nodes = connection.getNodes();
83             assertNotNull(nodes);
84             assertTrue(nodes.size() > 0);
85             return nodes.get(0);
86         }
87         fail("Connection parameter ("+CONNECTION_TYPE+") must be active or passive");
88         return null;
89     }
90
91     public OvsdbClient getTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
92         Properties props = loadProperties();
93         String addressStr = props.getProperty(SERVER_IPADDRESS);
94         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
95         String connectionType = props.getProperty(CONNECTION_TYPE, "active");
96
97         // If the connection type is active, controller connects to the ovsdb-server
98         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
99             if (addressStr == null) {
100                 fail(usage());
101             }
102
103             InetAddress address;
104             try {
105                 address = InetAddress.getByName(addressStr);
106             } catch (Exception e) {
107                 System.out.println("Unable to resolve " + addressStr);
108                 e.printStackTrace();
109                 return null;
110             }
111
112             Integer port;
113             try {
114                 port = Integer.parseInt(portStr);
115             } catch (NumberFormatException e) {
116                 System.out.println("Invalid port number : " + portStr);
117                 e.printStackTrace();
118                 return null;
119             }
120
121             OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
122             return connection.connect(address, port);
123         } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
124             ExecutorService executor = Executors.newFixedThreadPool(1);
125             Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
126             return passiveConnection.get(60, TimeUnit.SECONDS);
127         }
128         fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
129         return null;
130     }
131
132     protected String usage() {
133         return "Integration Test needs a valid connection configuration as follows :\n" +
134                "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"+
135                "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
136     }
137
138     public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
139         OvsdbClient client = null;
140         @Override
141         public OvsdbClient call() throws Exception {
142             OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
143             connection.registerConnectionListener(this);
144             while (client == null) {
145                 Thread.sleep(500);
146             }
147             return client;
148         }
149
150         @Override
151         public void connected(OvsdbClient client) {
152             this.client = client;
153         }
154
155         @Override
156         public void disconnected(OvsdbClient client) {
157             assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());
158             this.client = null;
159         }
160     }
161 }