2 * Copyright (c) 2014 Red Hat, Inc. and others. All rights reserved.
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
8 * Authors : Madhu Venugopal
10 package org.opendaylight.ovsdb.integrationtest.plugin;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
19 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
20 import static org.ops4j.pax.exam.CoreOptions.options;
21 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
22 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Properties;
29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeoutException;
33 import javax.inject.Inject;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.opendaylight.controller.sal.connection.ConnectionConstants;
39 import org.opendaylight.controller.sal.core.Node;
40 import org.opendaylight.controller.sal.utils.ServiceHelper;
41 import org.opendaylight.controller.sal.utils.Status;
42 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
43 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
44 import org.opendaylight.ovsdb.lib.OvsdbClient;
45 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
46 import org.opendaylight.ovsdb.lib.notation.Row;
47 import org.opendaylight.ovsdb.plugin.Connection;
48 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
49 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
50 import org.opendaylight.ovsdb.plugin.StatusWithUuid;
51 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
52 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
53 import org.ops4j.pax.exam.Configuration;
54 import org.ops4j.pax.exam.Option;
55 import org.ops4j.pax.exam.junit.PaxExam;
56 import org.ops4j.pax.exam.util.PathUtils;
57 import org.osgi.framework.Bundle;
58 import org.osgi.framework.BundleContext;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 import com.google.common.collect.ImmutableMap;
63 import com.google.common.collect.Sets;
65 @RunWith(PaxExam.class)
66 public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
67 private Logger log = LoggerFactory.getLogger(OvsdbPluginIT.class);
69 private BundleContext bc;
70 private OVSDBConfigService ovsdbConfigService = null;
71 private Node node = null;
72 private OvsdbClient client = null;
74 // Configure the OSGi container
76 public Option[] config() {
79 systemProperty("logback.configurationFile").value(
80 "file:" + PathUtils.getBaseDir()
81 + "/src/test/resources/logback.xml"
83 // To start OSGi console for inspection remotely
84 systemProperty("osgi.console").value("2401"),
86 propagateSystemProperty("ovsdbserver.ipaddress"),
87 propagateSystemProperty("ovsdbserver.port"),
89 ConfigurationBundles.controllerBundles(),
90 ConfigurationBundles.ovsdbLibraryBundles(),
91 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
92 mavenBundle("org.opendaylight.ovsdb", "ovsdb_plugin").versionAsInProject(),
97 private String stateToString(int state) {
101 case Bundle.INSTALLED:
103 case Bundle.RESOLVED:
105 case Bundle.UNINSTALLED:
106 return "UNINSTALLED";
108 return "Not CONVERTED";
112 public Node getPluginTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
113 Properties props = loadProperties();
114 String addressStr = props.getProperty(SERVER_IPADDRESS);
115 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
116 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
118 IConnectionServiceInternal connection = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
119 // If the connection type is active, controller connects to the ovsdb-server
120 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
121 if (addressStr == null) {
125 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
126 params.put(ConnectionConstants.ADDRESS, addressStr);
127 params.put(ConnectionConstants.PORT, portStr);
128 return connection.connect(IDENTIFIER, params);
129 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
130 // Wait for 10 seconds for the Passive connection to be initiated by the ovsdb-server.
132 List<Node> nodes = connection.getNodes();
133 assertNotNull(nodes);
134 assertTrue(nodes.size() > 0);
137 fail("Connection parameter ("+CONNECTION_TYPE+") must be active or passive");
142 public void areWeReady() throws InterruptedException {
144 boolean debugit = false;
145 Bundle b[] = bc.getBundles();
146 for (Bundle element : b) {
147 int state = element.getState();
148 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
149 log.info("Bundle:" + element.getSymbolicName() + " state:"
150 + stateToString(state));
155 log.debug("Do some debugging because some bundle is unresolved");
158 assertFalse(debugit);
160 node = getPluginTestConnection();
161 } catch (Exception e) {
162 fail("Exception : "+e.getMessage());
164 this.ovsdbConfigService = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
168 public void apiTests() throws Exception {
170 IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
172 // Check for the ovsdb Connection as seen by the Plugin layer
173 assertNotNull(connectionService.getNodes());
174 assertTrue(connectionService.getNodes().size() > 0);
175 Node node = connectionService.getNodes().get(0);
176 Connection connection = connectionService.getConnection(node);
177 OvsdbConnectionInfo connectionInfo = connection.getClient().getConnectionInfo();
178 String identifier = IDENTIFIER;
179 if (connectionInfo.getType().equals(OvsdbConnectionInfo.ConnectionType.PASSIVE)) {
180 identifier = connectionInfo.getRemoteAddress().getHostAddress()+":"+connectionInfo.getRemotePort();
182 assertEquals(Node.fromString("OVS|"+identifier), connectionService.getNodes().get(0));
183 System.out.println("Nodes = "+ connectionService.getNodes());
186 * 1. Print Cache and Assert to make sure the bridge is not created yet.
187 * 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
188 * 3. Assert to make sure the bridge is created with a valid Uuid.
189 * 4. Delete the bridge & Assert to make sure the return status is success.
190 * 5. Assert to make sure the bridge is deleted
193 this.endToEndApiTest(connection, getOpenVSwitchTableUUID(connection));
196 * Repeat all of the above tests without the parent_uuid
199 this.endToEndApiTest(connection, null);
202 public void endToEndApiTest(Connection connection, String parentUuid) throws Exception {
203 // 1. Print Cache and Assert to make sure the bridge is not created yet.
206 // 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
207 StatusWithUuid status = insertBridge(connection, parentUuid);
208 assertTrue(status.isSuccess());
210 Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
212 // 3. Assert to make sure the bridge is created with a valid Uuid.
214 Bridge bridge = connection.getClient().getTypedRowWrapper(Bridge.class, null);
215 Row bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
216 assertNotNull(bridgeRow);
217 bridge = connection.getClient().getTypedRowWrapper(Bridge.class, bridgeRow);
218 assertEquals(bridge.getUuid(), status.getUuid());
220 // 4. Delete the bridge & Assert to make sure the return status is success.
221 Status delStatus = ovsdbConfigService.deleteRow(node, bridge.getSchema().getName(), status.getUuid().toString());
222 assertTrue(delStatus.isSuccess());
223 Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
225 // 5. Assert to make sure the bridge is deleted
226 bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
227 assertNull(bridgeRow);
230 public StatusWithUuid insertBridge(Connection connection, String parentUuid) throws Exception {
231 Bridge bridge = connection.getClient().createTypedRowWrapper(Bridge.class);
232 bridge.setName("br_test1");
233 bridge.setStatus(ImmutableMap.of("key", "value"));
234 bridge.setFloodVlans(Sets.newHashSet(34));
235 return ovsdbConfigService.insertRow(node, bridge.getSchema().getName(), parentUuid, bridge.getRow());
238 public String getOpenVSwitchTableUUID(Connection connection) throws Exception {
239 OpenVSwitch openVSwitch = connection.getClient().getTypedRowWrapper(OpenVSwitch.class, null);
240 ConcurrentMap<String, Row> row = ovsdbConfigService.getRows(node, openVSwitch.getSchema().getName());
241 if (row == null || row.size() == 0) return null;
242 return (String)row.keySet().toArray()[0];
245 public void printCache() throws Exception {
246 List<String> tables = ovsdbConfigService.getTables(node);
247 System.out.println("Tables = "+tables);
248 assertNotNull(tables);
249 for (String table : tables) {
250 System.out.println("Table "+table);
251 ConcurrentMap<String,Row> row = ovsdbConfigService.getRows(node, table);
252 System.out.println(row);