Refactor OVSDB Neutron Code
[netvirt.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / neutron / NeutronIT.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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 : Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.integrationtest.neutron;
12
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.fail;
16 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
17 import static org.ops4j.pax.exam.CoreOptions.options;
18 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
19 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
20
21 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
22 import org.opendaylight.controller.sal.core.Node;
23 import org.opendaylight.controller.sal.utils.Status;
24 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
25 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
26 import org.opendaylight.ovsdb.lib.notation.Row;
27 import org.opendaylight.ovsdb.lib.notation.Version;
28 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
32 import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
33 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
34 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
35 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
36
37 import com.google.common.collect.ImmutableMap;
38 import org.apache.felix.dm.Component;
39 import org.apache.felix.dm.DependencyManager;
40 import org.junit.After;
41 import org.junit.Assert;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.ops4j.pax.exam.Configuration;
46 import org.ops4j.pax.exam.Option;
47 import org.ops4j.pax.exam.junit.PaxExam;
48 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
49 import org.ops4j.pax.exam.spi.reactors.PerClass;
50 import org.ops4j.pax.exam.util.PathUtils;
51 import org.osgi.framework.Bundle;
52 import org.osgi.framework.BundleContext;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import java.io.IOException;
57 import java.net.InetAddress;
58 import java.util.Map;
59 import java.util.Properties;
60 import java.util.concurrent.ExecutionException;
61 import java.util.concurrent.TimeoutException;
62
63 import javax.inject.Inject;
64
65
66 @RunWith(PaxExam.class)
67 @ExamReactorStrategy(PerClass.class)
68 public class NeutronIT extends OvsdbIntegrationTestBase {
69     private Logger log = LoggerFactory.getLogger(NeutronIT.class);
70     @Inject
71     private BundleContext bc;
72
73     @Inject
74     private OvsdbConfigService ovsdbConfigService;
75     private Node node = null;
76
77     Component of10Provider;
78     Component of13Provider;
79
80     @Inject
81     BridgeConfigurationManager bridgeConfigurationManager;
82     @Inject
83     ConfigurationService configurationService;
84
85     Boolean tearDownBridge = false;
86
87     // Configure the OSGi container
88     @Configuration
89     public Option[] config() {
90         return options(
91                 //
92                 systemProperty("logback.configurationFile").value(
93                         "file:" + PathUtils.getBaseDir()
94                         + "/src/test/resources/logback.xml"
95                 ),
96                 // To start OSGi console for inspection remotely
97                 systemProperty("osgi.console").value("2401"),
98
99                 propagateSystemProperty("ovsdbserver.ipaddress"),
100                 propagateSystemProperty("ovsdbserver.port"),
101
102                 ConfigurationBundles.controllerBundles(),
103                 ConfigurationBundles.ovsdbLibraryBundles(),
104                 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
105                 ConfigurationBundles.ovsdbNeutronBundles(),
106                 junitBundles()
107         );
108     }
109
110     private String stateToString(int state) {
111         switch (state) {
112             case Bundle.ACTIVE:
113                 return "ACTIVE";
114             case Bundle.INSTALLED:
115                 return "INSTALLED";
116             case Bundle.RESOLVED:
117                 return "RESOLVED";
118             case Bundle.UNINSTALLED:
119                 return "UNINSTALLED";
120             default:
121                 return "Not CONVERTED";
122         }
123     }
124
125     @Before
126     public void areWeReady() throws InterruptedException, ExecutionException, IOException, TimeoutException {
127         assertNotNull(bc);
128         boolean debugit = false;
129         Bundle b[] = bc.getBundles();
130         for (Bundle element : b) {
131             int state = element.getState();
132             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
133                 log.info("Bundle:" + element.getSymbolicName() + " state:"
134                          + stateToString(state));
135                 debugit = true;
136             }
137         }
138         if (debugit) {
139             log.debug("Do some debugging because some bundle is unresolved");
140         }
141
142         assertFalse(debugit);
143
144         if (node == null) {
145             try {
146                 node = getPluginTestConnection();
147             } catch (Exception e) {
148                 fail("Exception : " + e.getMessage());
149             }
150         }
151
152         //Register fake NetworkingProviders
153         Properties of10Properties = new Properties();
154         of10Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW10);
155
156         Properties of13Properties = new Properties();
157         of13Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW13);
158
159         DependencyManager dm = new DependencyManager(bc);
160
161         of10Provider = dm.createComponent();
162         of10Provider.setInterface(NetworkingProvider.class.getName(), of10Properties);
163         of10Provider.setImplementation(new FakeOF10Provider());
164
165         of13Provider = dm.createComponent();
166         of13Provider.setInterface(NetworkingProvider.class.getName(), of13Properties);
167         of13Provider.setImplementation(new FakeOF13Provider());
168
169         dm.add(of10Provider);
170         dm.add(of13Provider);
171     }
172
173     @Test
174     public void testPrepareNode() throws Exception {
175         Thread.sleep(5000);
176
177         // Create the integration bridge
178         bridgeConfigurationManager.prepareNode(node);
179
180         Map<String, Row>
181                 bridgeRows =
182                 ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Bridge.class));
183         Assert.assertEquals(1, bridgeRows.size());
184
185         Bridge bridgeRow = ovsdbConfigService.getTypedRow(node, Bridge.class, bridgeRows.values().iterator().next());
186         Assert.assertEquals(configurationService.getIntegrationBridgeName(), bridgeRow.getName());
187
188         String uuid = bridgeConfigurationManager.getBridgeUuid(node, configurationService.getIntegrationBridgeName());
189         Assert.assertEquals(uuid, bridgeRow.getUuid().toString());
190
191         tearDownBridge = true;
192     }
193
194     @Test
195     public void testGetTunnelEndpoint() throws Exception {
196         Thread.sleep(5000);
197
198         final String endpointAddress = "10.10.10.10";
199
200         Map<String, Row> ovsRows = ovsdbConfigService.getRows(node,
201                                                               ovsdbConfigService.getTableName(node, OpenVSwitch.class));
202         OpenVSwitch ovsRow = ovsdbConfigService.getTypedRow(node,
203                                                             OpenVSwitch.class,
204                                                             ovsRows.values().iterator().next());
205
206         Assert.assertEquals(null, configurationService.getTunnelEndPoint(node));
207
208         ovsRow.setOtherConfig(ImmutableMap.of(configurationService.getTunnelEndpointKey(), endpointAddress));
209         ovsdbConfigService.updateRow(node,
210                                      ovsdbConfigService.getTableName(node, OpenVSwitch.class),
211                                      null,
212                                      ovsRow.getUuid().toString(),
213                                      ovsRow.getRow());
214
215         Assert.assertEquals(InetAddress.getByName(endpointAddress), configurationService.getTunnelEndPoint(node));
216     }
217
218     @Test
219     public void testGetOpenflowVersion() throws Exception {
220         Thread.sleep(5000);
221
222         Version ovsVersion = this.getOvsVersion();
223
224         if (ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) < 0) {
225             Assert.assertEquals(Constants.OPENFLOW10, configurationService.getOpenflowVersion(node));
226         } else {
227             Assert.assertEquals(Constants.OPENFLOW13, configurationService.getOpenflowVersion(node));
228         }
229     }
230
231     @After
232     public void tearDown() throws InterruptedException {
233         Thread.sleep(5000);
234
235         if (tearDownBridge) {
236             String uuid = bridgeConfigurationManager.getBridgeUuid(node,
237                                                                    configurationService.getIntegrationBridgeName());
238             ovsdbConfigService.deleteRow(node, ovsdbConfigService.getTableName(node, Bridge.class), uuid);
239             tearDownBridge = false;
240         }
241
242         DependencyManager dm = new DependencyManager(bc);
243         dm.remove(of10Provider);
244         dm.remove(of13Provider);
245     }
246
247     private Version getOvsVersion(){
248         Map<String, Row> ovsRows = ovsdbConfigService.getRows(node,
249                                                               ovsdbConfigService.getTableName(node, OpenVSwitch.class));
250         OpenVSwitch ovsRow = ovsdbConfigService.getTypedRow(node,
251                                                             OpenVSwitch.class,
252                                                             ovsRows.values().iterator().next());
253         return Version.fromString(ovsRow.getOvsVersionColumn().getData().iterator().next());
254     }
255
256     private class FakeOF10Provider implements NetworkingProvider {
257
258         @Override
259         public boolean hasPerTenantTunneling() {
260             return true;
261         }
262
263         @Override
264         public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
265             return null;
266         }
267
268         @Override
269         public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
270             return null;
271         }
272
273         @Override
274         public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
275                                             boolean isLastInstanceOnNode) {
276             return null;
277         }
278
279         @Override
280         public void initializeFlowRules(Node node) {
281
282         }
283
284         @Override
285         public void initializeOFFlowRules(Node openflowNode) {
286
287         }
288     }
289
290     private class FakeOF13Provider implements NetworkingProvider {
291
292         @Override
293         public boolean hasPerTenantTunneling() {
294             return false;
295         }
296
297         @Override
298         public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
299             return null;
300         }
301
302         @Override
303         public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
304             return null;
305         }
306
307         @Override
308         public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
309                                             boolean isLastInstanceOnNode) {
310             return null;
311         }
312
313         @Override
314         public void initializeFlowRules(Node node) {
315
316         }
317
318         @Override
319         public void initializeOFFlowRules(Node openflowNode) {
320
321         }
322     }
323 }