80fdda7b6b9236bf521687f976962ec3a681ddd8
[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.junit.Assume.assumeTrue;
16 import static org.ops4j.pax.exam.CoreOptions.composite;
17 import static org.ops4j.pax.exam.CoreOptions.maven;
18 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
19 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperties;
20 import static org.ops4j.pax.exam.CoreOptions.vmOption;
21 import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
22 import static org.ops4j.pax.exam.MavenUtils.asInProject;
23 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
24 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
25 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
26
27 import com.google.common.collect.Lists;
28 import com.google.common.collect.Maps;
29
30 import java.net.InetAddress;
31 import java.net.NetworkInterface;
32 import java.net.UnknownHostException;
33 import java.util.ArrayList;
34 import java.util.Enumeration;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.concurrent.atomic.AtomicBoolean;
39
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
46 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
47 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
48 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
49 import org.opendaylight.neutron.spi.INeutronPortCRUD;
50 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
51 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
52 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
53 import org.opendaylight.neutron.spi.NeutronPort;
54 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
55 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
56 import org.opendaylight.neutron.spi.NeutronSecurityRule;
57 import org.opendaylight.neutron.spi.NeutronNetwork;
58 import org.opendaylight.neutron.spi.NeutronSubnet;
59 import org.opendaylight.ovsdb.lib.notation.Version;
60 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
61 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
62 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
63 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
64 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
65 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
66 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
67 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
68 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
69 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
70 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
71 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
73 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
74 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
97 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
98 import org.ops4j.pax.exam.Configuration;
99 import org.ops4j.pax.exam.Option;
100 import org.ops4j.pax.exam.junit.PaxExam;
101 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
102 import org.ops4j.pax.exam.options.MavenUrlReference;
103 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
104 import org.ops4j.pax.exam.spi.reactors.PerClass;
105 import org.slf4j.Logger;
106 import org.slf4j.LoggerFactory;
107
108 /**
109  * Integration tests for netvirt
110  *
111  * @author Sam Hague (shague@redhat.com)
112  */
113 @RunWith(PaxExam.class)
114 @ExamReactorStrategy(PerClass.class)
115 public class NetvirtIT extends AbstractMdsalTestBase {
116     private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
117     private static final int OVSDB_UPDATE_TIMEOUT = 1000;
118     private static DataBroker dataBroker = null;
119     private static String addressStr;
120     private static String portStr;
121     private static String connectionType;
122     private static String controllerStr;
123     private static AtomicBoolean setup = new AtomicBoolean(false);
124     private static MdsalUtils mdsalUtils = null;
125     private static Southbound southbound = null;
126     private static SouthboundUtils southboundUtils;
127     private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
128     private static final String SDPLNAME = "sg1";
129     private static final String NETWORK_ID = "521e29d6-67b8-4b3c-8633-027d21195111";
130     private static final String TENANT_ID = "521e29d6-67b8-4b3c-8633-027d21195100";
131     private static final String SUBNET_ID = "521e29d6-67b8-4b3c-8633-027d21195112";
132     private static final String PORT1_ID = "521e29d6-67b8-4b3c-8633-027d21195113";
133     private static final String DHCPPORT_ID ="521e29d6-67b8-4b3c-8633-027d21195115";
134
135     @Override
136     public String getModuleName() {
137         return "netvirt-providers-impl";
138     }
139
140     @Override
141     public String getInstanceName() {
142         return "netvirt-providers-default";
143     }
144
145     @Override
146     public MavenUrlReference getFeatureRepo() {
147         return maven()
148                 .groupId("org.opendaylight.ovsdb")
149                 .artifactId("features-ovsdb")
150                 .classifier("features")
151                 .type("xml")
152                 .versionAsInProject();
153     }
154
155     @Override
156     public String getFeatureName() {
157         return "odl-ovsdb-openstack";
158     }
159
160     @Configuration
161     @Override
162     public Option[] config() {
163         Option[] parentOptions = super.config();
164         Option[] propertiesOptions = getPropertiesOptions();
165         Option[] otherOptions = getOtherOptions();
166         Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
167         System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
168         System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
169         System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
170                 otherOptions.length);
171         return options;
172     }
173
174     private Option[] getOtherOptions() {
175         return new Option[] {
176                 wrappedBundle(
177                         mavenBundle("org.opendaylight.ovsdb", "utils.mdsal-openflow")
178                                 .version(asInProject())
179                                 .type("jar")),
180                 wrappedBundle(
181                         mavenBundle("org.opendaylight.ovsdb", "utils.config")
182                                 .version(asInProject())
183                                 .type("jar")),
184                 configureConsole().startLocalConsole(),
185                 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
186                 keepRuntimeFolder()
187         };
188     }
189
190     public Option[] getPropertiesOptions() {
191         return new Option[] {
192                 propagateSystemProperties(NetvirtITConstants.SERVER_IPADDRESS,
193                         NetvirtITConstants.SERVER_PORT, NetvirtITConstants.CONNECTION_TYPE,
194                         NetvirtITConstants.CONTROLLER_IPADDRESS,
195                         NetvirtITConstants.USERSPACE_ENABLED)
196         };
197     }
198
199     @Override
200     public Option getLoggingOption() {
201         return composite(
202                 //editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
203                 //        "log4j.logger.org.opendaylight.controller",
204                 //        LogLevelOption.LogLevel.TRACE.name()),
205                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
206                         "log4j.logger.org.opendaylight.ovsdb",
207                         LogLevelOption.LogLevel.TRACE.name()),
208                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
209                         logConfiguration(NetvirtIT.class),
210                         LogLevelOption.LogLevel.INFO.name()),
211                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
212                         "log4j.logger.org.opendaylight.ovsdb.lib",
213                         LogLevelOption.LogLevel.INFO.name()),
214                 super.getLoggingOption());
215     }
216
217     protected String usage() {
218         return "Integration Test needs a valid connection configuration as follows :\n"
219                 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
220                 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
221     }
222
223     private void getProperties() {
224         Properties props = System.getProperties();
225         addressStr = props.getProperty(NetvirtITConstants.SERVER_IPADDRESS);
226         portStr = props.getProperty(NetvirtITConstants.SERVER_PORT, NetvirtITConstants.DEFAULT_SERVER_PORT);
227         connectionType = props.getProperty(NetvirtITConstants.CONNECTION_TYPE, "active");
228         controllerStr = props.getProperty(NetvirtITConstants.CONTROLLER_IPADDRESS, "0.0.0.0");
229         String userSpaceEnabled = props.getProperty(NetvirtITConstants.USERSPACE_ENABLED, "no");
230         LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}, controller ip: {}, " +
231                 "userspace.enabled: {}",
232                 connectionType, addressStr, portStr, controllerStr, userSpaceEnabled);
233         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
234             if (addressStr == null) {
235                 fail(usage());
236             }
237         }
238     }
239
240     @Before
241     @Override
242     public void setup() throws InterruptedException {
243         if (setup.get()) {
244             LOG.info("Skipping setUp, already initialized");
245             return;
246         }
247
248         try {
249             super.setup();
250         } catch (Exception e) {
251             e.printStackTrace();
252         }
253
254         getProperties();
255
256         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
257             if (addressStr == null) {
258                 fail(usage());
259             }
260         }
261
262         dataBroker = getDatabroker(getProviderContext());
263         mdsalUtils = new MdsalUtils(dataBroker);
264         assertNotNull("mdsalUtils should not be null", mdsalUtils);
265         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
266         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
267         assertNotNull("southbound should not be null", southbound);
268         southboundUtils = new SouthboundUtils(mdsalUtils);
269         setup.set(true);
270     }
271
272     private BindingAwareBroker.ProviderContext getProviderContext() {
273         BindingAwareBroker.ProviderContext providerContext = null;
274         for (int i=0; i < 60; i++) {
275             providerContext = getSession();
276             if (providerContext != null) {
277                 break;
278             } else {
279                 try {
280                     Thread.sleep(1000);
281                 } catch (InterruptedException e) {
282                     e.printStackTrace();
283                 }
284             }
285         }
286         assertNotNull("providercontext should not be null", providerContext);
287         /* One more second to let the provider finish initialization */
288         try {
289             Thread.sleep(1000);
290         } catch (InterruptedException e) {
291             e.printStackTrace();
292         }
293         return providerContext;
294     }
295
296     private DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
297         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
298         assertNotNull("dataBroker should not be null", dataBroker);
299         return dataBroker;
300     }
301
302     private Boolean getNetvirtTopology() {
303         LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
304         Boolean found = false;
305         final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
306         InstanceIdentifier<Topology> path =
307                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
308         for (int i = 0; i < 60; i++) {
309             Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
310             if (topology != null) {
311                 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
312                 found = true;
313                 break;
314             } else {
315                 LOG.info("getNetvirtTopology: still looking ({})...", i);
316                 try {
317                     Thread.sleep(1000);
318                 } catch (InterruptedException e) {
319                     e.printStackTrace();
320                 }
321             }
322         }
323         return found;
324     }
325
326     /**
327      * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port
328      * 6640. This test will wait for incoming connections for {@link NetvirtITConstants#CONNECTION_INIT_TIMEOUT} ms.
329      *
330      * @throws InterruptedException
331      */
332     @Ignore
333     @Test
334     public void testPassiveNode() throws InterruptedException {
335         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_PASSIVE)) {
336             //Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
337             Thread.sleep(NetvirtITConstants.CONNECTION_INIT_TIMEOUT);
338         }
339     }
340
341     private ConnectionInfo getConnectionInfo(final String addressStr, final String portStr) {
342         InetAddress inetAddress = null;
343         try {
344             inetAddress = InetAddress.getByName(addressStr);
345         } catch (UnknownHostException e) {
346             fail("Could not allocate InetAddress: " + e);
347         }
348
349         IpAddress address = SouthboundMapper.createIpAddress(inetAddress);
350         PortNumber port = new PortNumber(Integer.parseInt(portStr));
351
352         LOG.info("connectionInfo: {}", new ConnectionInfoBuilder()
353                 .setRemoteIp(address)
354                 .setRemotePort(port)
355                 .build());
356         return new ConnectionInfoBuilder()
357                        .setRemoteIp(address)
358                        .setRemotePort(port)
359                        .build();
360     }
361
362     private String connectionInfoToString(final ConnectionInfo connectionInfo) {
363         return String.valueOf(connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
364     }
365
366     private boolean addOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
367         boolean result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION,
368                 SouthboundUtils.createInstanceIdentifier(connectionInfo),
369                 SouthboundUtils.createNode(connectionInfo));
370         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
371         return result;
372     }
373
374     private Node getOvsdbNode(final ConnectionInfo connectionInfo) {
375         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL,
376                 SouthboundUtils.createInstanceIdentifier(connectionInfo));
377     }
378
379     private boolean deleteOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
380         boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
381                 SouthboundUtils.createInstanceIdentifier(connectionInfo));
382         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
383         return result;
384     }
385
386     private Node connectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
387         Assert.assertTrue(addOvsdbNode(connectionInfo));
388         Node node = getOvsdbNode(connectionInfo);
389         Assert.assertNotNull("Should find OVSDB node after connect", node);
390         LOG.info("Connected to {}", connectionInfoToString(connectionInfo));
391         return node;
392     }
393
394     private boolean disconnectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
395         Assert.assertTrue(deleteOvsdbNode(connectionInfo));
396         Node node = getOvsdbNode(connectionInfo);
397         Assert.assertNull("Should not find OVSDB node after disconnect", node);
398         //Assume.assumeNotNull(node); // Using assumeNotNull because there is no assumeNull
399         LOG.info("Disconnected from {}", connectionInfoToString(connectionInfo));
400         return true;
401     }
402
403     private String getControllerIPAddress() {
404         String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
405         if (addressString != null) {
406             try {
407                 if (InetAddress.getByName(addressString) != null) {
408                     return addressString;
409                 }
410             } catch (UnknownHostException e) {
411                 LOG.error("Host {} is invalid", addressString);
412             }
413         }
414
415         addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
416         if (addressString != null) {
417             try {
418                 if (InetAddress.getByName(addressString) != null) {
419                     return addressString;
420                 }
421             } catch (UnknownHostException e) {
422                 LOG.error("Host {} is invalid", addressString);
423             }
424         }
425
426         return null;
427     }
428
429     private short getControllerOFPort() {
430         short openFlowPort = Constants.OPENFLOW_PORT;
431         String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
432         if (portString != null) {
433             try {
434                 openFlowPort = Short.parseShort(portString);
435             } catch (NumberFormatException e) {
436                 LOG.warn("Invalid port:{}, use default({})", portString,
437                         openFlowPort);
438             }
439         }
440         return openFlowPort;
441     }
442
443     private List<String> getControllersFromOvsdbNode(Node node) {
444         List<String> controllersStr = new ArrayList<>();
445
446         String controllerIpStr = getControllerIPAddress();
447         if (controllerIpStr != null) {
448             // If codepath makes it here, the ip address to be used was explicitly provided.
449             // Being so, also fetch openflowPort provided via ConfigProperties.
450             controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
451                     + ":" + controllerIpStr + ":" + getControllerOFPort());
452         } else {
453             // Check if ovsdb node has manager entries
454             OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
455             if (ovsdbNodeAugmentation != null) {
456                 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
457                 if (managerEntries != null && !managerEntries.isEmpty()) {
458                     for (ManagerEntry managerEntry : managerEntries) {
459                         if (managerEntry == null || managerEntry.getTarget() == null) {
460                             continue;
461                         }
462                         String[] tokens = managerEntry.getTarget().getValue().split(":");
463                         if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
464                             controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
465                                     + ":" + tokens[1] + ":" + getControllerOFPort());
466                         } else if (tokens[0].equalsIgnoreCase("ptcp")) {
467                             ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
468                             if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
469                                 controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
470                                 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
471                                         + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
472                             } else {
473                                 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
474                             }
475                         } else {
476                             LOG.trace("Skipping manager entry {} for node {}",
477                                     managerEntry.getTarget(), node.getNodeId().getValue());
478                         }
479                     }
480                 } else {
481                     LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
482                 }
483             }
484         }
485
486         if (controllersStr.isEmpty()) {
487             // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
488             LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
489             controllerIpStr = getLocalControllerHostIpAddress();
490             if (controllerIpStr != null) {
491                 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
492                         + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
493             }
494         }
495
496         if (controllersStr.isEmpty()) {
497             LOG.warn("Failed to determine OpenFlow controller ip address");
498         } else if (LOG.isDebugEnabled()) {
499             controllerIpStr = "";
500             for (String currControllerIpStr : controllersStr) {
501                 controllerIpStr += " " + currControllerIpStr;
502             }
503             LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
504         }
505
506         return controllersStr;
507     }
508
509     private String getLocalControllerHostIpAddress() {
510         String ipaddress = null;
511         try{
512             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
513                 NetworkInterface iface = ifaces.nextElement();
514
515                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
516                     InetAddress inetAddr = inetAddrs.nextElement();
517                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
518                         ipaddress = inetAddr.getHostAddress();
519                         break;
520                     }
521                 }
522             }
523         }catch (Exception e){
524             LOG.warn("Exception while fetching local host ip address ", e);
525         }
526         return ipaddress;
527     }
528
529     private String getControllerTarget(Node ovsdbNode) {
530         return getControllersFromOvsdbNode(ovsdbNode).get(0);
531     }
532
533     @Test
534     public void testAddDeleteOvsdbNode() throws InterruptedException {
535         LOG.info("testAddDeleteOvsdbNode enter 3");
536         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
537         connectOvsdbNode(connectionInfo);
538         ControllerEntry controllerEntry;
539         for (int i = 0; i < 10; i++) {
540             LOG.info("testAddDeleteOvsdbNode ({}): looking for controller", i);
541             Node ovsdbNode = getOvsdbNode(connectionInfo);
542             Assert.assertNotNull("ovsdb node not found", ovsdbNode);
543             String controllerTarget = getControllerTarget(ovsdbNode);
544             Assert.assertNotNull("Failed to get controller target", controllerTarget);
545             OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
546             Assert.assertNotNull(bridge);
547             Assert.assertNotNull(bridge.getControllerEntry());
548             controllerEntry = bridge.getControllerEntry().iterator().next();
549             Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
550             if (controllerEntry.isIsConnected()) {
551                 Assert.assertTrue(controllerEntry.isIsConnected());
552                 break;
553             }
554             Thread.sleep(1000);
555         }
556
557         Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
558         Thread.sleep(1000);
559         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
560     }
561
562     @Ignore
563     @Test
564     public void testOpenVSwitchOtherConfig() throws InterruptedException {
565         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
566         Node ovsdbNode = connectOvsdbNode(connectionInfo);
567         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
568         Assert.assertNotNull(ovsdbNodeAugmentation);
569         List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
570         if (otherConfigsList != null) {
571             for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
572                 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
573                     LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
574                     break;
575                 } else {
576                     LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
577                 }
578             }
579         } else {
580             LOG.info("other_config is not present");
581         }
582         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
583     }
584
585     private OvsdbTerminationPointAugmentationBuilder createGenericOvsdbTerminationPointAugmentationBuilder() {
586         OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder =
587                 new OvsdbTerminationPointAugmentationBuilder();
588         ovsdbTerminationPointAugmentationBuilder.setInterfaceType(
589                 new InterfaceTypeEntryBuilder()
590                         .setInterfaceType(
591                                 SouthboundMapper.createInterfaceType("internal"))
592                         .build().getInterfaceType());
593         return ovsdbTerminationPointAugmentationBuilder;
594     }
595
596     private boolean addTerminationPoint(final NodeId bridgeNodeId, final String portName,
597             final OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder)
598         throws InterruptedException {
599
600         InstanceIdentifier<Node> portIid = SouthboundMapper.createInstanceIdentifier(bridgeNodeId);
601         NodeBuilder portNodeBuilder = new NodeBuilder();
602         NodeId portNodeId = SouthboundMapper.createManagedNodeId(portIid);
603         portNodeBuilder.setNodeId(portNodeId);
604         TerminationPointBuilder entry = new TerminationPointBuilder();
605         entry.setKey(new TerminationPointKey(new TpId(portName)));
606         entry.addAugmentation(
607                 OvsdbTerminationPointAugmentation.class,
608                 ovsdbTerminationPointAugmentationBuilder.build());
609         portNodeBuilder.setTerminationPoint(Lists.newArrayList(entry.build()));
610         boolean result = mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION,
611                 portIid, portNodeBuilder.build());
612         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
613         return result;
614     }
615
616     /**
617      * Extract the <code>store</code> type data store contents for the particular bridge identified by
618      * <code>bridgeName</code>.
619      *
620      * @param connectionInfo The connection information.
621      * @param bridgeName The bridge name.
622      * @param store defined by the <code>LogicalDatastoreType</code> enumeration
623      * @return <code>store</code> type data store contents
624      */
625     private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName,
626             LogicalDatastoreType store) {
627         Node bridgeNode = getBridgeNode(connectionInfo, bridgeName, store);
628         Assert.assertNotNull(bridgeNode);
629         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
630         Assert.assertNotNull(ovsdbBridgeAugmentation);
631         return ovsdbBridgeAugmentation;
632     }
633
634     /**
635      * extract the <code>LogicalDataStoreType.OPERATIONAL</code> type data store contents for the particular bridge
636      * identified by <code>bridgeName</code>
637      *
638      * @param connectionInfo The connection information.
639      * @param bridgeName The bridge name.
640      * @see <code>NetvirtIT.getBridge(ConnectionInfo, String, LogicalDatastoreType)</code>
641      * @return <code>LogicalDatastoreType.OPERATIONAL</code> type data store contents
642      */
643     private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName) {
644         return getBridge(connectionInfo, bridgeName, LogicalDatastoreType.OPERATIONAL);
645     }
646
647     /**
648      * Extract the node contents from <code>store</code> type data store for the
649      * bridge identified by <code>bridgeName</code>
650      *
651      * @param connectionInfo The connection information.
652      * @param bridgeName The bridge name.
653      * @param store defined by the <code>LogicalDatastoreType</code> enumeration
654      * @return <code>store</code> type data store contents
655      */
656     private Node getBridgeNode(ConnectionInfo connectionInfo, String bridgeName, LogicalDatastoreType store) {
657         InstanceIdentifier<Node> bridgeIid =
658                 SouthboundUtils.createInstanceIdentifier(connectionInfo,
659                     new OvsdbBridgeName(bridgeName));
660         return mdsalUtils.read(store, bridgeIid);
661     }
662
663     private boolean deleteBridge(final ConnectionInfo connectionInfo, final String bridgeName)
664         throws InterruptedException {
665
666         boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
667                 SouthboundUtils.createInstanceIdentifier(connectionInfo,
668                         new OvsdbBridgeName(bridgeName)));
669         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
670         return result;
671     }
672
673     private InstanceIdentifier<Node> getTpIid(ConnectionInfo connectionInfo, OvsdbBridgeAugmentation bridge) {
674         return SouthboundUtils.createInstanceIdentifier(connectionInfo,
675             bridge.getBridgeName());
676     }
677
678     private void netVirtAddPort(ConnectionInfo connectionInfo) throws InterruptedException {
679         OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
680         Assert.assertNotNull(bridge);
681         NodeId nodeId = SouthboundMapper.createManagedNodeId(SouthboundUtils.createInstanceIdentifier(
682                 connectionInfo, bridge.getBridgeName()));
683         OvsdbTerminationPointAugmentationBuilder ovsdbTerminationBuilder =
684                 createGenericOvsdbTerminationPointAugmentationBuilder();
685         String portName = NetvirtITConstants.PORT_NAME;
686         ovsdbTerminationBuilder.setName(portName);
687         Assert.assertTrue(addTerminationPoint(nodeId, portName, ovsdbTerminationBuilder));
688         InstanceIdentifier<Node> terminationPointIid = getTpIid(connectionInfo, bridge);
689         Node terminationPointNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, terminationPointIid);
690         Assert.assertNotNull(terminationPointNode);
691     }
692
693     /**
694      * Test for basic southbound events to netvirt.
695      * <pre>The test will:
696      * - connect to an OVSDB node and verify it is added to operational
697      * - then verify that br-int was created on the node and stored in operational
698      * - a port is then added to the bridge to verify that it is ignored by netvirt
699      * - remove the bridge
700      * - remove the node and verify it is not in operational
701      * </pre>
702      * @throws InterruptedException
703      */
704     @Test
705     public void testNetVirt() throws InterruptedException {
706         LOG.info("testNetVirt: starting test 2");
707         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
708         Node ovsdbNode = connectOvsdbNode(connectionInfo);
709         LOG.info("testNetVirt: should be connected");
710
711         Thread.sleep(10000);
712         LOG.info("testNetVirt: should really be connected after sleep");
713         // Verify the pipeline flows were installed
714         PipelineOrchestrator pipelineOrchestrator =
715                 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
716         assertNotNull("Could not find PipelineOrchestrator Service", pipelineOrchestrator);
717         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
718         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
719         LOG.info("testNetVirt: bridgeNode: {}", bridgeNode);
720         long datapathId = southbound.getDataPathId(bridgeNode);
721         assertNotEquals("datapathId was not found", datapathId, 0);
722
723         //TODO add check for controller connection
724         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
725                 FlowUtils.createNodeBuilder(datapathId);
726
727         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
728         List<Service> staticPipelineFound = Lists.newArrayList();
729         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
730             if (staticPipeline.contains(service)) {
731                 staticPipelineFound.add(service);
732             }
733             FlowBuilder flowBuilder = FlowUtils.getPipelineFlow(service.getTable(), (short)0);
734             Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
735             assertNotNull("Could not find flow in config", flow);
736             Thread.sleep(1000);
737             flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
738             assertNotNull("Could not find flow in operational", flow);
739         }
740         assertEquals("did not find all expected flows in static pipeline",
741                 staticPipeline.size(), staticPipelineFound.size());
742
743         netVirtAddPort(connectionInfo);
744         Thread.sleep(10000);
745         Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
746         Thread.sleep(10000);
747         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
748     }
749
750     @Ignore
751     @Test
752     public void testNetVirt2() throws InterruptedException {
753         Thread.sleep(60000);
754     }
755
756     @Ignore
757     @Test
758     public void testReadOvsdbTopologyNodes() throws InterruptedException {
759         Thread.sleep(10000);
760         List<Node> ovsdbNodes = southbound.readOvsdbTopologyNodes();
761         for (Node node : ovsdbNodes) {
762             LOG.info(">>>>> node: {}", node);
763         }
764     }
765
766     @Test
767     public void testNetVirtFixedSG() throws InterruptedException {
768         final Version minSGOvsVersion = Version.fromString("1.10.2");
769
770         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
771         assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
772         Node ovsdbNode = connectOvsdbNode(connectionInfo);
773         assertNotNull("node is not connected", ovsdbNode);
774
775         // Verify the minimum version required for this test
776         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
777         Assert.assertNotNull(ovsdbNodeAugmentation);
778         assertNotNull(ovsdbNodeAugmentation.getOvsVersion());
779         String ovsVersion = ovsdbNodeAugmentation.getOvsVersion();
780         Version version = Version.fromString(ovsVersion);
781         if (version.compareTo(minSGOvsVersion) < 0) {
782             LOG.warn("{} minimum version is required", minSGOvsVersion);
783             Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
784             Thread.sleep(10000);
785             Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
786             return;
787         }
788
789         Thread.sleep(10000);
790         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
791         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
792         long datapathId = southbound.getDataPathId(bridgeNode);
793         assertNotEquals("datapathId was not found", datapathId, 0);
794
795         NeutronNetwork nn = createNeutronNetwork(NETWORK_ID, TENANT_ID,NetworkHandler.NETWORK_TYPE_VXLAN, "100");
796         NeutronSubnet ns = createNeutronSubnet(SUBNET_ID, TENANT_ID, NETWORK_ID, "10.0.0.0/24");
797         NeutronPort nport = createNeutronPort(NETWORK_ID, SUBNET_ID, PORT1_ID, "compute", "10.0.0.10", "f6:00:00:0f:00:01");
798         NeutronPort dhcp = createNeutronPort(NETWORK_ID, SUBNET_ID, DHCPPORT_ID, "dhcp", "10.0.0.1", "f6:00:00:0f:00:02");
799
800         Thread.sleep(10000);
801         Map<String, String> externalIds = Maps.newHashMap();
802         externalIds.put("attached-mac", "f6:00:00:0f:00:01");
803         externalIds.put("iface-id", PORT1_ID);
804         southboundUtils.addTerminationPoint(bridgeNode, SDPLNAME, "internal", null, externalIds, 3L);
805         southboundUtils.addTerminationPoint(bridgeNode, "vm1", "internal", null, null, 0L);
806         southboundUtils.addTerminationPoint(bridgeNode, "vm2", "internal", null, null, 0L);
807         Map<String, String> options = Maps.newHashMap();
808         options.put("key", "flow");
809         options.put("remote_ip", "192.168.120.32");
810         southboundUtils.addTerminationPoint(bridgeNode, "vx", "vxlan", options, null, 4L);
811         Thread.sleep(1000);
812
813         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
814                 FlowUtils.createNodeBuilder(datapathId);
815         MatchBuilder matchBuilder1 = new MatchBuilder();
816         matchBuilder1 = MatchUtils.createDhcpMatch(matchBuilder1, 68, 67);
817         String flowId1 = "Egress_DHCP_Client"  + "_Permit_";
818         FlowBuilder flowBuilder1 = initFlowBuilder(matchBuilder1, flowId1, (short)40);
819         Flow flow1 = getFlow(flowBuilder1, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
820         assertNotNull("EgressSG : Could not find flow in configuration ", flow1);
821         flow1 = getFlow(flowBuilder1, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
822         assertNotNull("EgressSG Operational : Could not find flow in config", flow1);
823
824         testDefaultsSG(nport, datapathId, nn);
825         Thread.sleep(10000);
826         Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
827         Thread.sleep(10000);
828         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
829     }
830
831     private void testDefaultsSG(NeutronPort nport, long datapathId, NeutronNetwork nn)
832             throws InterruptedException {
833         INeutronSecurityGroupCRUD ineutronSecurityGroupCRUD =
834                 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
835         assertNotNull("Could not find ineutronSecurityGroupCRUD Service", ineutronSecurityGroupCRUD);
836         INeutronSecurityRuleCRUD ineutronSecurityRuleCRUD =
837                 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
838         assertNotNull("Could not find ineutronSecurityRuleCRUD Service", ineutronSecurityRuleCRUD);
839
840         NeutronSecurityGroup neutronSG = new NeutronSecurityGroup();
841         neutronSG.setSecurityGroupDescription("testig defaultSG-IT");
842         neutronSG.setSecurityGroupName("DefaultSG");
843         neutronSG.setSecurityGroupUUID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
844         neutronSG.setTenantID(TENANT_ID);
845
846         List<NeutronSecurityRule> nsrs = new ArrayList<>();
847         NeutronSecurityRule nsrIN = new NeutronSecurityRule();
848         nsrIN.setSecurityRemoteGroupID(null);
849         nsrIN.setSecurityRuleDirection("ingress");
850         nsrIN.setSecurityRuleEthertype("IPv4");
851         nsrIN.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
852         nsrIN.setSecurityRuleProtocol("TCP");
853         nsrIN.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
854         nsrIN.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e6");
855         nsrIN.setTenantID(TENANT_ID);
856
857         NeutronSecurityRule nsrEG = new NeutronSecurityRule();
858         nsrEG.setSecurityRemoteGroupID(null);
859         nsrEG.setSecurityRuleDirection("egress");
860         nsrEG.setSecurityRuleEthertype("IPv4");
861         nsrEG.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
862         nsrEG.setSecurityRuleProtocol("TCP");
863         nsrEG.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
864         nsrEG.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e1");
865         nsrEG.setTenantID(TENANT_ID);
866
867         nsrs.add(nsrIN);
868         nsrs.add(nsrEG);
869
870         neutronSG.setSecurityRules(nsrs);
871         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrIN);
872         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrEG);
873         ineutronSecurityGroupCRUD.add(neutronSG);
874
875         List<NeutronSecurityGroup> sgs = new ArrayList<>();
876         sgs.add(neutronSG);
877         nport.setSecurityGroups(sgs);
878
879         INeutronPortCRUD iNeutronPortCRUD =
880                 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
881         iNeutronPortCRUD.update(PORT1_ID, nport);
882
883         Thread.sleep(10000);
884         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilderEg =
885                 FlowUtils.createNodeBuilder(datapathId);
886         MatchBuilder matchBuilderEg = new MatchBuilder();
887         matchBuilderEg = MatchUtils.createEtherMatchWithType(matchBuilderEg, null, nport.getMacAddress());
888         String flowIdEg = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
889         FlowBuilder flowBuilderEg = initFlowBuilder(matchBuilderEg, flowIdEg, (short)40);
890         Flow flowEg = getFlow(flowBuilderEg, nodeBuilderEg, LogicalDatastoreType.CONFIGURATION);
891         assertNotNull("EgressSG : Could not find flow in configuration ", flowEg);
892         flowEg = getFlow(flowBuilderEg, nodeBuilderEg, LogicalDatastoreType.OPERATIONAL);
893         assertNotNull("EgressSG Operational : Could not find flow in config", flowEg);
894
895         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilderIng =
896                 FlowUtils.createNodeBuilder(datapathId);
897         MatchBuilder matchBuilderIng = new MatchBuilder();
898         matchBuilderIng = MatchUtils.createEtherMatchWithType(matchBuilderIng,null, nport.getMacAddress());
899         String flowIdIng = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
900         FlowBuilder flowBuilderIng = initFlowBuilder(matchBuilderIng, flowIdIng, (short)90);
901         Flow flowIng = getFlow(flowBuilderIng, nodeBuilderIng, LogicalDatastoreType.CONFIGURATION);
902         assertNotNull("IngressSG : Could not find flow in configuration ", flowIng);
903         flowIng = getFlow(flowBuilderIng, nodeBuilderIng, LogicalDatastoreType.OPERATIONAL);
904         assertNotNull("IngressSG Operational : Could not find flow in config", flowIng);
905     }
906
907     private NeutronPort createNeutronPort(String networkId, String subnetId,
908              String id, String owner, String ipaddr, String mac) {
909         INeutronPortCRUD iNeutronPortCRUD =
910                 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
911         NeutronPort np = new NeutronPort();
912         np.initDefaults();
913         np.setID(id);
914         np.setDeviceOwner(owner);
915         np.setMacAddress(mac);
916         np.setNetworkUUID(networkId);
917         List<org.opendaylight.neutron.spi.Neutron_IPs> srcAddressList =
918                 new ArrayList<>();
919         org.opendaylight.neutron.spi.Neutron_IPs nip = new org.opendaylight.neutron.spi.Neutron_IPs();
920         nip.setIpAddress(ipaddr);
921         nip.setSubnetUUID(subnetId);
922         srcAddressList.add(nip);
923         np.setFixedIPs(srcAddressList);
924         List<NeutronSecurityGroup> nsgs = new ArrayList<>();
925         np.setSecurityGroups(nsgs);
926         iNeutronPortCRUD.add(np);
927         return np;
928     }
929
930     private NeutronSubnet createNeutronSubnet(String subnetId, String tenantId,
931               String networkId, String cidr) {
932         INeutronSubnetCRUD iNeutronSubnetCRUD =
933                 (INeutronSubnetCRUD) ServiceHelper.getGlobalInstance(INeutronSubnetCRUD.class, this);
934         NeutronSubnet ns = new NeutronSubnet();
935         ns.setID(subnetId);
936         ns.setCidr(cidr);
937         ns.initDefaults();
938         ns.setNetworkUUID(networkId);
939         ns.setTenantID(tenantId);
940         iNeutronSubnetCRUD.add(ns);
941         return ns;
942     }
943
944     private NeutronNetwork createNeutronNetwork(String uuid, String tenantID, String networkTypeVxlan, String segId) {
945         INeutronNetworkCRUD iNeutronNetworkCRUD =
946                 (INeutronNetworkCRUD) ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
947         NeutronNetwork nn = new NeutronNetwork();
948         nn.setID(uuid);
949         nn.initDefaults();
950         nn.setTenantID(tenantID);
951         nn.setProviderNetworkType(networkTypeVxlan);
952         nn.setProviderSegmentationID(segId);
953         iNeutronNetworkCRUD.addNetwork(nn);
954         return nn;
955     }
956
957     private FlowBuilder initFlowBuilder(MatchBuilder matchBuilder, String flowId, short tableId) {
958         FlowBuilder flowBuilder = new FlowBuilder();
959         flowBuilder.setMatch(matchBuilder.build());
960         flowBuilder.setId(new FlowId(flowId));
961         flowBuilder.setFlowName(flowId);
962         FlowKey key = new FlowKey(new FlowId(flowId));
963         flowBuilder.setStrict(true);
964         flowBuilder.setTableId(tableId);
965         flowBuilder.setKey(key);
966         return flowBuilder;
967     }
968
969     private Flow getFlow (
970             FlowBuilder flowBuilder,
971             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
972             LogicalDatastoreType store)
973             throws InterruptedException {
974
975         Flow flow = null;
976         for (int i = 0; i < 10; i++) {
977             LOG.info("getFlow {}-{}: looking for flowBuilder: {}, nodeBuilder: {}",
978                     i, store, flowBuilder.build(), nodeBuilder.build());
979             flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
980             if (flow != null) {
981                 LOG.info("getFlow: found flow({}): {}", store, flow);
982                 break;
983             }
984             Thread.sleep(1000);
985         }
986         return flow;
987     }
988 }