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