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