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