Fix incorrect OVSDB class names
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / plugin / OvsdbPluginIT.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.plugin;
11
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;
23
24 import java.util.List;
25 import java.util.concurrent.ConcurrentMap;
26
27 import javax.inject.Inject;
28
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;
54
55 import com.google.common.collect.ImmutableMap;
56 import com.google.common.collect.Sets;
57
58 @RunWith(PaxExam.class)
59 public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
60     private Logger log = LoggerFactory.getLogger(OvsdbPluginIT.class);
61     @Inject
62     private BundleContext bc;
63     private OvsdbConfigService ovsdbConfigService = null;
64     private Node node = null;
65     private OvsdbClient client = null;
66
67     // Configure the OSGi container
68     @Configuration
69     public Option[] config() {
70         return options(
71             //
72             systemProperty("logback.configurationFile").value(
73                     "file:" + PathUtils.getBaseDir()
74                     + "/src/test/resources/logback.xml"
75             ),
76             // To start OSGi console for inspection remotely
77             systemProperty("osgi.console").value("2401"),
78
79             propagateSystemProperty("ovsdbserver.ipaddress"),
80             propagateSystemProperty("ovsdbserver.port"),
81
82             ConfigurationBundles.controllerBundles(),
83             ConfigurationBundles.ovsdbLibraryBundles(),
84             ConfigurationBundles.ovsdbDefaultSchemaBundles(),
85             mavenBundle("org.opendaylight.ovsdb", "ovsdb_plugin").versionAsInProject(),
86             junitBundles()
87         );
88     }
89
90     private String stateToString(int state) {
91         switch (state) {
92         case Bundle.ACTIVE:
93             return "ACTIVE";
94         case Bundle.INSTALLED:
95             return "INSTALLED";
96         case Bundle.RESOLVED:
97             return "RESOLVED";
98         case Bundle.UNINSTALLED:
99             return "UNINSTALLED";
100         default:
101             return "Not CONVERTED";
102         }
103     }
104
105     @Before
106     public void areWeReady() throws InterruptedException {
107         assertNotNull(bc);
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));
115                 debugit = true;
116             }
117         }
118         if (debugit) {
119             log.debug("Do some debugging because some bundle is unresolved");
120         }
121
122         assertFalse(debugit);
123         try {
124             node = getPluginTestConnection();
125         } catch (Exception e) {
126             fail("Exception : "+e.getMessage());
127         }
128         this.ovsdbConfigService = (OvsdbConfigService)ServiceHelper.getGlobalInstance(OvsdbConfigService.class, this);
129     }
130
131     @Test
132     public void apiTests() throws Exception {
133         Thread.sleep(5000);
134         IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
135
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();
145         }
146         assertEquals(Node.fromString("OVS|"+identifier), connectionService.getNodes().get(0));
147         System.out.println("Nodes = "+ connectionService.getNodes());
148         /*
149          * Test sequence :
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
155          */
156
157         this.endToEndApiTest(connection, getOpenVSwitchTableUUID(connection));
158
159         /*
160          * Repeat all of the above tests without the parent_uuid
161          */
162
163         this.endToEndApiTest(connection, null);
164     }
165
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.
168         printCache();
169
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());
173
174         Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
175
176         // 3. Assert to make sure the bridge is created with a valid Uuid.
177         printCache();
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());
183
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.
188
189         // 5. Assert to make sure the bridge is deleted
190         bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
191         assertNull(bridgeRow);
192     }
193
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());
200     }
201
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];
207     }
208
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);
217         }
218     }
219
220 }