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.util.List;
25 import java.util.concurrent.ConcurrentMap;
27 import javax.inject.Inject;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.opendaylight.controller.sal.core.Node;
33 import org.opendaylight.controller.sal.utils.ServiceHelper;
34 import org.opendaylight.controller.sal.utils.Status;
35 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
36 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
37 import org.opendaylight.ovsdb.lib.OvsdbClient;
38 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
39 import org.opendaylight.ovsdb.lib.notation.Row;
40 import org.opendaylight.ovsdb.plugin.Connection;
41 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
42 import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
43 import org.opendaylight.ovsdb.plugin.StatusWithUuid;
44 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
45 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
46 import org.ops4j.pax.exam.Configuration;
47 import org.ops4j.pax.exam.Option;
48 import org.ops4j.pax.exam.junit.PaxExam;
49 import org.ops4j.pax.exam.util.PathUtils;
50 import org.osgi.framework.Bundle;
51 import org.osgi.framework.BundleContext;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.common.collect.ImmutableMap;
56 import com.google.common.collect.Sets;
58 @RunWith(PaxExam.class)
59 public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
60 private Logger log = LoggerFactory.getLogger(OvsdbPluginIT.class);
62 private BundleContext bc;
63 private OvsdbConfigService ovsdbConfigService = null;
64 private Node node = null;
65 private OvsdbClient client = null;
67 // Configure the OSGi container
69 public Option[] config() {
72 systemProperty("logback.configurationFile").value(
73 "file:" + PathUtils.getBaseDir()
74 + "/src/test/resources/logback.xml"
76 // To start OSGi console for inspection remotely
77 systemProperty("osgi.console").value("2401"),
79 propagateSystemProperty("ovsdbserver.ipaddress"),
80 propagateSystemProperty("ovsdbserver.port"),
82 ConfigurationBundles.controllerBundles(),
83 ConfigurationBundles.ovsdbLibraryBundles(),
84 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
85 mavenBundle("org.opendaylight.ovsdb", "ovsdb_plugin").versionAsInProject(),
90 private String stateToString(int state) {
94 case Bundle.INSTALLED:
98 case Bundle.UNINSTALLED:
101 return "Not CONVERTED";
106 public void areWeReady() throws InterruptedException {
108 boolean debugit = false;
109 Bundle b[] = bc.getBundles();
110 for (Bundle element : b) {
111 int state = element.getState();
112 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
113 log.info("Bundle:" + element.getSymbolicName() + " state:"
114 + stateToString(state));
119 log.debug("Do some debugging because some bundle is unresolved");
122 assertFalse(debugit);
124 node = getPluginTestConnection();
125 } catch (Exception e) {
126 fail("Exception : "+e.getMessage());
128 this.ovsdbConfigService = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class, this);
132 public void apiTests() throws Exception {
134 IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
136 // Check for the ovsdb Connection as seen by the Plugin layer
137 assertNotNull(connectionService.getNodes());
138 assertTrue(connectionService.getNodes().size() > 0);
139 Node node = connectionService.getNodes().get(0);
140 Connection connection = connectionService.getConnection(node);
141 OvsdbConnectionInfo connectionInfo = connection.getClient().getConnectionInfo();
142 String identifier = IDENTIFIER;
143 if (connectionInfo.getType().equals(OvsdbConnectionInfo.ConnectionType.PASSIVE)) {
144 identifier = connectionInfo.getRemoteAddress().getHostAddress()+":"+connectionInfo.getRemotePort();
146 assertEquals(Node.fromString("OVS|"+identifier), connectionService.getNodes().get(0));
147 System.out.println("Nodes = "+ connectionService.getNodes());
150 * 1. Print Cache and Assert to make sure the bridge is not created yet.
151 * 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
152 * 3. Assert to make sure the bridge is created with a valid Uuid.
153 * 4. Delete the bridge & Assert to make sure the return status is success.
154 * 5. Assert to make sure the bridge is deleted
157 this.endToEndApiTest(connection, getOpenVSwitchTableUUID(connection));
160 * Repeat all of the above tests without the parent_uuid
163 this.endToEndApiTest(connection, null);
166 public void endToEndApiTest(Connection connection, String parentUuid) throws Exception {
167 // 1. Print Cache and Assert to make sure the bridge is not created yet.
170 // 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
171 StatusWithUuid status = insertBridge(connection, parentUuid);
172 assertTrue(status.isSuccess());
174 Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
176 // 3. Assert to make sure the bridge is created with a valid Uuid.
178 Bridge bridge = connection.getClient().getTypedRowWrapper(Bridge.class, null);
179 Row bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
180 assertNotNull(bridgeRow);
181 bridge = connection.getClient().getTypedRowWrapper(Bridge.class, bridgeRow);
182 assertEquals(bridge.getUuid(), status.getUuid());
184 // 4. Delete the bridge & Assert to make sure the return status is success.
185 Status delStatus = ovsdbConfigService.deleteRow(node, bridge.getSchema().getName(), status.getUuid().toString());
186 assertTrue(delStatus.isSuccess());
187 Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
189 // 5. Assert to make sure the bridge is deleted
190 bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
191 assertNull(bridgeRow);
194 public StatusWithUuid insertBridge(Connection connection, String parentUuid) throws Exception {
195 Bridge bridge = connection.getClient().createTypedRowWrapper(Bridge.class);
196 bridge.setName("br_test1");
197 bridge.setStatus(ImmutableMap.of("key", "value"));
198 bridge.setFloodVlans(Sets.newHashSet(34L));
199 return ovsdbConfigService.insertRow(node, bridge.getSchema().getName(), parentUuid, bridge.getRow());
202 public String getOpenVSwitchTableUUID(Connection connection) throws Exception {
203 OpenVSwitch openVSwitch = connection.getClient().getTypedRowWrapper(OpenVSwitch.class, null);
204 ConcurrentMap<String, Row> row = ovsdbConfigService.getRows(node, openVSwitch.getSchema().getName());
205 if (row == null || row.size() == 0) return null;
206 return (String)row.keySet().toArray()[0];
209 public void printCache() throws Exception {
210 List<String> tables = ovsdbConfigService.getTables(node);
211 System.out.println("Tables = "+tables);
212 assertNotNull(tables);
213 for (String table : tables) {
214 System.out.println("Table "+table);
215 ConcurrentMap<String,Row> row = ovsdbConfigService.getRows(node, table);
216 System.out.println(row);