Merge "BUG-5006: rework SouthboundProviderTest, clean up"
[netvirt.git] / openstack / net-virt-it / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / it / NetvirtIT.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.ovsdb.openstack.netvirt.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.ops4j.pax.exam.CoreOptions.composite;
16 import static org.ops4j.pax.exam.CoreOptions.maven;
17 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
18 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperties;
19 import static org.ops4j.pax.exam.CoreOptions.vmOption;
20 import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
21 import static org.ops4j.pax.exam.MavenUtils.asInProject;
22 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
23 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
24 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
25
26 import com.google.common.collect.Lists;
27 import com.google.common.collect.Maps;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33 import java.util.concurrent.atomic.AtomicBoolean;
34
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
43 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
44 import org.opendaylight.neutron.spi.INeutronPortCRUD;
45 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
46 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
47 import org.opendaylight.neutron.spi.NeutronPort;
48 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
49 import org.opendaylight.neutron.spi.NeutronSecurityRule;
50 import org.opendaylight.neutron.spi.NeutronNetwork;
51 import org.opendaylight.neutron.spi.NeutronSubnet;
52 import org.opendaylight.ovsdb.lib.notation.Version;
53 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
54 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
55 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
56 import org.opendaylight.ovsdb.openstack.netvirt.providers.NetvirtProvidersProvider;
57 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
58 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
59 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
60 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
61 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
62 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.ops4j.pax.exam.Configuration;
77 import org.ops4j.pax.exam.Option;
78 import org.ops4j.pax.exam.junit.PaxExam;
79 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
80 import org.ops4j.pax.exam.options.MavenUrlReference;
81 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
82 import org.ops4j.pax.exam.spi.reactors.PerClass;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85
86 /**
87  * Integration tests for netvirt
88  *
89  * @author Sam Hague (shague@redhat.com)
90  */
91 @RunWith(PaxExam.class)
92 @ExamReactorStrategy(PerClass.class)
93 public class NetvirtIT extends AbstractMdsalTestBase {
94     private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
95     private static DataBroker dataBroker = null;
96     private static String addressStr;
97     private static String portStr;
98     private static String connectionType;
99     private static String controllerStr;
100     private static AtomicBoolean setup = new AtomicBoolean(false);
101     private static MdsalUtils mdsalUtils = null;
102     private static Southbound southbound = null;
103     private static PipelineOrchestrator pipelineOrchestrator = null;
104     private static SouthboundUtils southboundUtils;
105     private static NeutronUtils neutronUtils = new NeutronUtils();
106     private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
107
108     @Override
109     public String getModuleName() {
110         return "netvirt-providers-impl";
111     }
112
113     @Override
114     public String getInstanceName() {
115         return "netvirt-providers-default";
116     }
117
118     @Override
119     public MavenUrlReference getFeatureRepo() {
120         return maven()
121                 .groupId("org.opendaylight.ovsdb")
122                 .artifactId("features-ovsdb")
123                 .classifier("features")
124                 .type("xml")
125                 .versionAsInProject();
126     }
127
128     @Override
129     public String getFeatureName() {
130         return "odl-ovsdb-openstack";
131     }
132
133     @Configuration
134     @Override
135     public Option[] config() {
136         Option[] parentOptions = super.config();
137         Option[] propertiesOptions = getPropertiesOptions();
138         Option[] otherOptions = getOtherOptions();
139         Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
140         System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
141         System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
142         System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
143                 otherOptions.length);
144         return options;
145     }
146
147     private Option[] getOtherOptions() {
148         return new Option[] {
149                 wrappedBundle(
150                         mavenBundle("org.opendaylight.ovsdb", "utils.mdsal-openflow")
151                                 .version(asInProject())
152                                 .type("jar")),
153                 wrappedBundle(
154                         mavenBundle("org.opendaylight.ovsdb", "utils.config")
155                                 .version(asInProject())
156                                 .type("jar")),
157                 configureConsole().startLocalConsole(),
158                 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
159                 keepRuntimeFolder()
160         };
161     }
162
163     public Option[] getPropertiesOptions() {
164         return new Option[] {
165                 propagateSystemProperties(NetvirtITConstants.SERVER_IPADDRESS,
166                         NetvirtITConstants.SERVER_PORT, NetvirtITConstants.CONNECTION_TYPE,
167                         NetvirtITConstants.CONTROLLER_IPADDRESS,
168                         NetvirtITConstants.USERSPACE_ENABLED)
169         };
170     }
171
172     @Override
173     public Option getLoggingOption() {
174         return composite(
175                 //editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
176                 //        "log4j.logger.org.opendaylight.controller",
177                 //        LogLevelOption.LogLevel.TRACE.name()),
178                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
179                         "log4j.logger.org.opendaylight.ovsdb",
180                         LogLevelOption.LogLevel.TRACE.name()),
181                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
182                         logConfiguration(NetvirtIT.class),
183                         LogLevelOption.LogLevel.INFO.name()),
184                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
185                         "log4j.logger.org.opendaylight.ovsdb.lib",
186                         LogLevelOption.LogLevel.INFO.name()),
187                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
188                         "log4j.logger.org.opendaylight.openflowjava",
189                         LogLevelOption.LogLevel.INFO.name()),
190                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
191                         "log4j.logger.org.opendaylight.openflowplugin",
192                         LogLevelOption.LogLevel.INFO.name()),
193                 super.getLoggingOption());
194     }
195
196     protected String usage() {
197         return "Integration Test needs a valid connection configuration as follows :\n"
198                 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
199                 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
200     }
201
202     private void getProperties() {
203         Properties props = System.getProperties();
204         addressStr = props.getProperty(NetvirtITConstants.SERVER_IPADDRESS);
205         portStr = props.getProperty(NetvirtITConstants.SERVER_PORT, NetvirtITConstants.DEFAULT_SERVER_PORT);
206         connectionType = props.getProperty(NetvirtITConstants.CONNECTION_TYPE, "active");
207         controllerStr = props.getProperty(NetvirtITConstants.CONTROLLER_IPADDRESS, "0.0.0.0");
208         String userSpaceEnabled = props.getProperty(NetvirtITConstants.USERSPACE_ENABLED, "no");
209         LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}, controller ip: {}, " +
210                 "userspace.enabled: {}",
211                 connectionType, addressStr, portStr, controllerStr, userSpaceEnabled);
212         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
213             if (addressStr == null) {
214                 fail(usage());
215             }
216         }
217     }
218
219     @Before
220     @Override
221     public void setup() throws InterruptedException {
222         if (setup.get()) {
223             LOG.info("Skipping setUp, already initialized");
224             return;
225         }
226
227         try {
228             super.setup();
229         } catch (Exception e) {
230             LOG.warn("Failed to setup test", e);
231             fail("Failed to setup test: " + e);
232         }
233
234         getProperties();
235
236         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
237             if (addressStr == null) {
238                 fail(usage());
239             }
240         }
241
242         dataBroker = getDatabroker(getProviderContext());
243         mdsalUtils = new MdsalUtils(dataBroker);
244         assertNotNull("mdsalUtils should not be null", mdsalUtils);
245         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
246         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
247         assertNotNull("southbound should not be null", southbound);
248         southboundUtils = new SouthboundUtils(mdsalUtils);
249         pipelineOrchestrator =
250                 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
251         assertNotNull("pipelineOrchestrator should not be null", pipelineOrchestrator);
252         setup.set(true);
253     }
254
255     private BindingAwareBroker.ProviderContext getProviderContext() {
256         BindingAwareBroker.ProviderContext providerContext = null;
257         for (int i=0; i < 60; i++) {
258             providerContext = getSession();
259             if (providerContext != null) {
260                 break;
261             } else {
262                 try {
263                     Thread.sleep(1000);
264                 } catch (InterruptedException e) {
265                     LOG.warn("Interrupted while waiting for provider context", e);
266                 }
267             }
268         }
269         assertNotNull("providercontext should not be null", providerContext);
270         /* One more second to let the provider finish initialization */
271         try {
272             Thread.sleep(1000);
273         } catch (InterruptedException e) {
274             LOG.warn("Interrupted while waiting for other provider", e);
275         }
276         return providerContext;
277     }
278
279     private DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
280         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
281         assertNotNull("dataBroker should not be null", dataBroker);
282         return dataBroker;
283     }
284
285     private Boolean getNetvirtTopology() {
286         LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
287         Boolean found = false;
288         final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
289         InstanceIdentifier<Topology> path =
290                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
291         for (int i = 0; i < 60; i++) {
292             Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
293             if (topology != null) {
294                 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
295                 found = true;
296                 break;
297             } else {
298                 LOG.info("getNetvirtTopology: still looking ({})...", i);
299                 try {
300                     Thread.sleep(1000);
301                 } catch (InterruptedException e) {
302                     LOG.warn("Interrupted while waiting for {}", NETVIRT_TOPOLOGY_ID, e);
303                 }
304             }
305         }
306         return found;
307     }
308
309     /**
310      * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port
311      * 6640. This test will wait for incoming connections for {@link NetvirtITConstants#CONNECTION_INIT_TIMEOUT} ms.
312      *
313      * @throws InterruptedException
314      */
315     @Ignore
316     @Test
317     public void testPassiveNode() throws InterruptedException {
318         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_PASSIVE)) {
319             //Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
320             Thread.sleep(NetvirtITConstants.CONNECTION_INIT_TIMEOUT);
321         }
322     }
323
324     private Node connectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
325         LOG.info("connectOvsdbNode enter");
326         Assert.assertTrue(southboundUtils.addOvsdbNode(connectionInfo));
327         Node node = southboundUtils.getOvsdbNode(connectionInfo);
328         Assert.assertNotNull("Should find OVSDB node after connect", node);
329         LOG.info("Connected to {}", SouthboundUtils.connectionInfoToString(connectionInfo));
330         return node;
331     }
332
333     private boolean disconnectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
334         LOG.info("disconnectOvsdbNode enter");
335         Assert.assertTrue(southboundUtils.deleteOvsdbNode(connectionInfo));
336         Node node = southboundUtils.getOvsdbNode(connectionInfo);
337         Assert.assertNull("Should not find OVSDB node after disconnect", node);
338         LOG.info("Disconnected from {}", SouthboundUtils.connectionInfoToString(connectionInfo));
339         return true;
340     }
341
342     // This is an extra test for local testing and testNetVirt covers this is more detail
343     @Ignore
344     @Test
345     public void testAddDeleteOvsdbNode() throws InterruptedException {
346         LOG.info("testAddDeleteOvsdbNode enter");
347         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
348         Node ovsdbNode = connectOvsdbNode(connectionInfo);
349         assertNotNull("connection failed", ovsdbNode);
350         LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
351
352         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
353                 + " is not connected", isControllerConnected(connectionInfo));
354
355         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
356         Thread.sleep(1000);
357         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
358         LOG.info("testAddDeleteOvsdbNode exit");
359     }
360
361     // TODO add tests for when L3 is enabled and check for br-ex
362
363     // This is an extra test for local testing and testNetVirt covers this is more detail
364     @Ignore
365     @Test
366     public void testAddDeleteOvsdbNodeWithTableOffset() throws InterruptedException {
367         LOG.info("testAddDeleteOvsdbNodeWithTableOffset enter");
368         NetvirtProvidersProvider.setTableOffset((short)1);
369         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
370         Node ovsdbNode = connectOvsdbNode(connectionInfo);
371         assertNotNull("connection failed", ovsdbNode);
372         LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
373
374         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
375                 + " is not connected", isControllerConnected(connectionInfo));
376
377         // Verify the pipeline flows were installed
378         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
379         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
380         long datapathId = southbound.getDataPathId(bridgeNode);
381         String datapathIdString = southbound.getDatapathId(bridgeNode);
382         LOG.info("testNetVirt: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
383         assertNotEquals("datapathId was not found", datapathId, 0);
384
385         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
386         List<Service> staticPipelineFound = Lists.newArrayList();
387         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
388             if (staticPipeline.contains(service)) {
389                 staticPipelineFound.add(service);
390             }
391             String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
392             verifyFlow(datapathId, flowId, service);
393         }
394         assertEquals("did not find all expected flows in static pipeline",
395                 staticPipeline.size(), staticPipelineFound.size());
396
397         String flowId = "TableOffset_" + pipelineOrchestrator.getTable(Service.CLASSIFIER);
398         verifyFlow(datapathId, flowId, Service.CLASSIFIER.getTable());
399
400         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
401         Thread.sleep(1000);
402         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
403         LOG.info("testAddDeleteOvsdbNodeWithTableOffset exit");
404     }
405
406     private boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
407         LOG.info("isControllerConnected enter");
408         Boolean connected = false;
409         ControllerEntry controllerEntry;
410         Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
411         assertNotNull("ovsdb node not found", ovsdbNode);
412
413         BridgeConfigurationManager bridgeConfigurationManager =
414                 (BridgeConfigurationManager) ServiceHelper.getGlobalInstance(BridgeConfigurationManager.class, this);
415         assertNotNull("Could not find BridgeConfigurationManager Service", bridgeConfigurationManager);
416         String controllerTarget = bridgeConfigurationManager.getControllersFromOvsdbNode(ovsdbNode).get(0);
417         Assert.assertNotNull("Failed to get controller target", controllerTarget);
418
419         for (int i = 0; i < 10; i++) {
420             LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
421             OvsdbBridgeAugmentation bridge =
422                     southboundUtils.getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
423             Assert.assertNotNull(bridge);
424             Assert.assertNotNull(bridge.getControllerEntry());
425             controllerEntry = bridge.getControllerEntry().iterator().next();
426             Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
427             if (controllerEntry.isIsConnected()) {
428                 Assert.assertTrue("Controller is not connected", controllerEntry.isIsConnected());
429                 connected = true;
430                 break;
431             }
432             Thread.sleep(1000);
433         }
434         LOG.info("isControllerConnected exit: {} - {}", connected, controllerTarget);
435         return connected;
436     }
437
438     @Ignore
439     @Test
440     public void testOpenVSwitchOtherConfig() throws InterruptedException {
441         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
442         Node ovsdbNode = connectOvsdbNode(connectionInfo);
443         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
444         Assert.assertNotNull(ovsdbNodeAugmentation);
445         List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
446         if (otherConfigsList != null) {
447             for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
448                 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
449                     LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
450                     break;
451                 } else {
452                     LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
453                 }
454             }
455         } else {
456             LOG.info("other_config is not present");
457         }
458         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
459     }
460
461     /**
462      * Test for basic southbound events to netvirt.
463      * <pre>The test will:
464      * - connect to an OVSDB node and verify it is added to operational
465      * - then verify that br-int was created on the node and stored in operational
466      * - a port is then added to the bridge to verify that it is ignored by netvirt
467      * - remove the bridge
468      * - remove the node and verify it is not in operational
469      * </pre>
470      * @throws InterruptedException
471      */
472     @Test
473     public void testNetVirt() throws InterruptedException {
474         LOG.info("testNetVirt: starting test");
475         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
476         Node ovsdbNode = connectOvsdbNode(connectionInfo);
477         assertNotNull("connection failed", ovsdbNode);
478         LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
479
480         //TODO use controller value rather that ovsdb connectionInfo or change log
481         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
482                 + " is not connected", isControllerConnected(connectionInfo));
483
484         // Verify the pipeline flows were installed
485         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
486         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
487         long datapathId = southbound.getDataPathId(bridgeNode);
488         String datapathIdString = southbound.getDatapathId(bridgeNode);
489         LOG.info("testNetVirt: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
490         assertNotEquals("datapathId was not found", datapathId, 0);
491
492         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
493         List<Service> staticPipelineFound = Lists.newArrayList();
494         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
495             if (staticPipeline.contains(service)) {
496                 staticPipelineFound.add(service);
497             }
498             String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
499             verifyFlow(datapathId, flowId, service);
500         }
501         assertEquals("did not find all expected flows in static pipeline",
502                 staticPipeline.size(), staticPipelineFound.size());
503
504         southboundUtils.addTerminationPoint(bridgeNode, NetvirtITConstants.PORT_NAME, "internal", null, null, 0L);
505         Thread.sleep(1000);
506         OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
507                 southbound.getTerminationPointOfBridge(bridgeNode, NetvirtITConstants.PORT_NAME);
508         Assert.assertNotNull("Did not find " + NetvirtITConstants.PORT_NAME, ovsdbTerminationPointAugmentation);
509         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
510         Thread.sleep(1000);
511         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
512     }
513
514     @Test
515     public void testNetVirtFixedSG() throws InterruptedException {
516         final Version minSGOvsVersion = Version.fromString("1.10.2");
517         final String portName = "sg1";
518         final String networkId = "521e29d6-67b8-4b3c-8633-027d21195111";
519         final String tenantId = "521e29d6-67b8-4b3c-8633-027d21195100";
520         final String subnetId = "521e29d6-67b8-4b3c-8633-027d21195112";
521         final String portId = "521e29d6-67b8-4b3c-8633-027d21195113";
522         final String dhcpPortId ="521e29d6-67b8-4b3c-8633-027d21195115";
523
524         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
525         Node ovsdbNode = connectOvsdbNode(connectionInfo);
526         assertNotNull("connection failed", ovsdbNode);
527         LOG.info("testNetVirtFixedSG: should be connected: {}", ovsdbNode.getNodeId());
528
529         // Verify the minimum version required for this test
530         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
531         Assert.assertNotNull(ovsdbNodeAugmentation);
532         assertNotNull(ovsdbNodeAugmentation.getOvsVersion());
533         String ovsVersion = ovsdbNodeAugmentation.getOvsVersion();
534         Version version = Version.fromString(ovsVersion);
535         if (version.compareTo(minSGOvsVersion) < 0) {
536             LOG.warn("{} minimum version is required", minSGOvsVersion);
537             Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo,
538                     NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
539             Thread.sleep(1000);
540             Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
541             return;
542         }
543
544         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
545                 + " is not connected", isControllerConnected(connectionInfo));
546
547         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
548         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
549         long datapathId = southbound.getDataPathId(bridgeNode);
550         String datapathIdString = southbound.getDatapathId(bridgeNode);
551         LOG.info("testNetVirtFixedSG: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
552         assertNotEquals("datapathId was not found", datapathId, 0);
553
554         NeutronNetwork nn = neutronUtils.createNeutronNetwork(networkId, tenantId,
555                 NetworkHandler.NETWORK_TYPE_VXLAN, "100");
556         NeutronSubnet ns = neutronUtils.createNeutronSubnet(subnetId, tenantId, networkId, "10.0.0.0/24");
557         NeutronPort nport = neutronUtils.createNeutronPort(networkId, subnetId, portId,
558                 "compute", "10.0.0.10", "f6:00:00:0f:00:01");
559         NeutronPort dhcp = neutronUtils.createNeutronPort(networkId, subnetId, dhcpPortId,
560                 "dhcp", "10.0.0.1", "f6:00:00:0f:00:02");
561
562         Thread.sleep(1000);
563         Map<String, String> externalIds = Maps.newHashMap();
564         externalIds.put("attached-mac", "f6:00:00:0f:00:01");
565         externalIds.put("iface-id", portId);
566         southboundUtils.addTerminationPoint(bridgeNode, portName, "internal", null, externalIds, 3L);
567         southboundUtils.addTerminationPoint(bridgeNode, "vm1", "internal", null, null, 0L);
568         southboundUtils.addTerminationPoint(bridgeNode, "vm2", "internal", null, null, 0L);
569         Map<String, String> options = Maps.newHashMap();
570         options.put("key", "flow");
571         options.put("remote_ip", "192.168.120.32");
572         southboundUtils.addTerminationPoint(bridgeNode, "vx", "vxlan", options, null, 4L);
573         Thread.sleep(1000);
574
575         String flowId = "Egress_DHCP_Client"  + "_Permit_";
576         verifyFlow(datapathId, flowId, Service.EGRESS_ACL);
577
578         testDefaultSG(nport, datapathId, nn, tenantId, portId);
579         Thread.sleep(1000);
580         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
581         Thread.sleep(1000);
582         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
583     }
584
585     private void testDefaultSG(NeutronPort nport, long datapathId, NeutronNetwork nn, String tenantId, String portId)
586             throws InterruptedException {
587         INeutronSecurityGroupCRUD ineutronSecurityGroupCRUD =
588                 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
589         assertNotNull("Could not find ineutronSecurityGroupCRUD Service", ineutronSecurityGroupCRUD);
590         INeutronSecurityRuleCRUD ineutronSecurityRuleCRUD =
591                 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
592         assertNotNull("Could not find ineutronSecurityRuleCRUD Service", ineutronSecurityRuleCRUD);
593
594         NeutronSecurityGroup neutronSG = new NeutronSecurityGroup();
595         neutronSG.setSecurityGroupDescription("testig defaultSG-IT");
596         neutronSG.setSecurityGroupName("DefaultSG");
597         neutronSG.setSecurityGroupUUID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
598         neutronSG.setTenantID(tenantId);
599
600         List<NeutronSecurityRule> nsrs = new ArrayList<>();
601         NeutronSecurityRule nsrIN = new NeutronSecurityRule();
602         nsrIN.setSecurityRemoteGroupID(null);
603         nsrIN.setSecurityRuleDirection("ingress");
604         nsrIN.setSecurityRuleEthertype("IPv4");
605         nsrIN.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
606         nsrIN.setSecurityRuleProtocol("TCP");
607         nsrIN.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
608         nsrIN.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e6");
609         nsrIN.setTenantID(tenantId);
610
611         NeutronSecurityRule nsrEG = new NeutronSecurityRule();
612         nsrEG.setSecurityRemoteGroupID(null);
613         nsrEG.setSecurityRuleDirection("egress");
614         nsrEG.setSecurityRuleEthertype("IPv4");
615         nsrEG.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
616         nsrEG.setSecurityRuleProtocol("TCP");
617         nsrEG.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
618         nsrEG.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e1");
619         nsrEG.setTenantID(tenantId);
620
621         nsrs.add(nsrIN);
622         nsrs.add(nsrEG);
623
624         neutronSG.setSecurityRules(nsrs);
625         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrIN);
626         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrEG);
627         ineutronSecurityGroupCRUD.add(neutronSG);
628
629         List<NeutronSecurityGroup> sgs = new ArrayList<>();
630         sgs.add(neutronSG);
631         nport.setSecurityGroups(sgs);
632
633         INeutronPortCRUD iNeutronPortCRUD =
634                 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
635         iNeutronPortCRUD.update(portId, nport);
636
637         LOG.info("Neutron ports have been added");
638         Thread.sleep(10000);
639         String flowId = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
640         verifyFlow(datapathId, flowId, Service.EGRESS_ACL);
641
642         flowId = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
643         verifyFlow(datapathId, flowId, Service.INGRESS_ACL);
644     }
645
646     private Flow getFlow (
647             FlowBuilder flowBuilder,
648             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
649             LogicalDatastoreType store) throws InterruptedException {
650
651         Flow flow = null;
652         for (int i = 0; i < 10; i++) {
653             LOG.info("getFlow try {} from {}: looking for flow: {}, node: {}",
654                     i, store, flowBuilder.build(), nodeBuilder.build());
655             flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
656             if (flow != null) {
657                 LOG.info("getFlow try {} from {}: found flow: {}", i, store, flow);
658                 break;
659             }
660             Thread.sleep(1000);
661         }
662         return flow;
663     }
664
665     private void verifyFlow(long datapathId, String flowId, short table) throws InterruptedException {
666         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
667                 FlowUtils.createNodeBuilder(datapathId);
668         FlowBuilder flowBuilder =
669                 FlowUtils.initFlowBuilder(new FlowBuilder(), flowId, table);
670         Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
671         assertNotNull("Could not find flow in config: " + flowBuilder.build() + "--" + nodeBuilder.build(), flow);
672         flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
673         assertNotNull("Could not find flow in operational: " + flowBuilder.build() + "--" + nodeBuilder.build(),
674                 flow);
675     }
676
677     private void verifyFlow(long datapathId, String flowId, Service service) throws InterruptedException {
678         verifyFlow(datapathId, flowId, pipelineOrchestrator.getTable(service));
679     }
680 }