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