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