3cfb7f28a54c33385f2fbf0c903b10560d8ac62d
[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
28 import java.net.InetAddress;
29 import java.net.NetworkInterface;
30 import java.net.UnknownHostException;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.Properties;
35 import java.util.concurrent.atomic.AtomicBoolean;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
42 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
43 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
44 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
46 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
47 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
48 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
49 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
50 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
51 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
59
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.ops4j.pax.exam.Configuration;
77 import org.ops4j.pax.exam.Option;
78 import org.ops4j.pax.exam.junit.PaxExam;
79 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
80 import org.ops4j.pax.exam.options.MavenUrlReference;
81 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
82 import org.ops4j.pax.exam.spi.reactors.PerClass;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85
86 /**
87  * Integration tests for netvirt
88  *
89  * @author Sam Hague (shague@redhat.com)
90  */
91 @RunWith(PaxExam.class)
92 @ExamReactorStrategy(PerClass.class)
93 public class NetvirtIT extends AbstractMdsalTestBase {
94     private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
95     private static final int OVSDB_UPDATE_TIMEOUT = 1000;
96     private static DataBroker dataBroker = null;
97     private static String addressStr;
98     private static String portStr;
99     private static String connectionType;
100     private static String controllerStr;
101     private static AtomicBoolean setup = new AtomicBoolean(false);
102     private static MdsalUtils mdsalUtils = null;
103     private static Southbound southbound = null;
104     private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
105
106     @Override
107     public String getModuleName() {
108         return "netvirt-providers-impl";
109     }
110
111     @Override
112     public String getInstanceName() {
113         return "netvirt-providers-default";
114     }
115
116     @Override
117     public MavenUrlReference getFeatureRepo() {
118         return maven()
119                 .groupId("org.opendaylight.ovsdb")
120                 .artifactId("features-ovsdb")
121                 .classifier("features")
122                 .type("xml")
123                 .versionAsInProject();
124     }
125
126     @Override
127     public String getFeatureName() {
128         return "odl-ovsdb-openstack";
129     }
130
131     @Configuration
132     @Override
133     public Option[] config() {
134         Option[] parentOptions = super.config();
135         Option[] propertiesOptions = getPropertiesOptions();
136         Option[] otherOptions = getOtherOptions();
137         Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
138         System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
139         System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
140         System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
141                 otherOptions.length);
142         return options;
143     }
144
145     private Option[] getOtherOptions() {
146         return new Option[] {
147                 wrappedBundle(
148                         mavenBundle("org.opendaylight.ovsdb", "utils.mdsal-openflow")
149                                 .version(asInProject())
150                                 .type("jar")),
151                 wrappedBundle(
152                         mavenBundle("org.opendaylight.ovsdb", "utils.config")
153                                 .version(asInProject())
154                                 .type("jar")),
155                 configureConsole().startLocalConsole(),
156                 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
157                 keepRuntimeFolder()
158         };
159     }
160
161     public Option[] getPropertiesOptions() {
162         return new Option[] {
163                 propagateSystemProperties(NetvirtITConstants.SERVER_IPADDRESS,
164                         NetvirtITConstants.SERVER_PORT, NetvirtITConstants.CONNECTION_TYPE,
165                         NetvirtITConstants.CONTROLLER_IPADDRESS),
166         };
167     }
168
169     @Override
170     public Option getLoggingOption() {
171         return composite(
172                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
173                         "log4j.logger.org.opendaylight.ovsdb",
174                         LogLevelOption.LogLevel.TRACE.name()),
175                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
176                         logConfiguration(NetvirtIT.class),
177                         LogLevelOption.LogLevel.INFO.name()),
178                 //editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
179                 //        "log4j.logger.org.opendaylight.ovsdb.lib",
180                 //        LogLevelOption.LogLevel.INFO.name()),
181                 super.getLoggingOption());
182     }
183
184     protected String usage() {
185         return "Integration Test needs a valid connection configuration as follows :\n"
186                 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
187                 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
188     }
189
190     private void getProperties() {
191         Properties props = System.getProperties();
192         addressStr = props.getProperty(NetvirtITConstants.SERVER_IPADDRESS);
193         portStr = props.getProperty(NetvirtITConstants.SERVER_PORT, NetvirtITConstants.DEFAULT_SERVER_PORT);
194         connectionType = props.getProperty(NetvirtITConstants.CONNECTION_TYPE, "active");
195         controllerStr = props.getProperty(NetvirtITConstants.CONTROLLER_IPADDRESS);
196         LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}, controller ip: {}",
197                 connectionType, addressStr, portStr, controllerStr);
198         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
199             if (addressStr == null) {
200                 fail(usage());
201             }
202         }
203     }
204
205     @Before
206     @Override
207     public void setup() throws InterruptedException {
208         if (setup.get()) {
209             LOG.info("Skipping setUp, already initialized");
210             return;
211         }
212
213         try {
214             super.setup();
215         } catch (Exception e) {
216             e.printStackTrace();
217         }
218
219         getProperties();
220
221         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
222             if (addressStr == null) {
223                 fail(usage());
224             }
225         }
226
227         dataBroker = getDatabroker(getProviderContext());
228         mdsalUtils = new MdsalUtils(dataBroker);
229         assertNotNull("mdsalUtils should not be null", mdsalUtils);
230         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
231         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
232         assertNotNull("southbound should not be null", southbound);
233         setup.set(true);
234     }
235
236     private BindingAwareBroker.ProviderContext getProviderContext() {
237         BindingAwareBroker.ProviderContext providerContext = null;
238         for (int i=0; i < 60; i++) {
239             providerContext = getSession();
240             if (providerContext != null) {
241                 break;
242             } else {
243                 try {
244                     Thread.sleep(1000);
245                 } catch (InterruptedException e) {
246                     e.printStackTrace();
247                 }
248             }
249         }
250         assertNotNull("providercontext should not be null", providerContext);
251         /* One more second to let the provider finish initialization */
252         try {
253             Thread.sleep(1000);
254         } catch (InterruptedException e) {
255             e.printStackTrace();
256         }
257         return providerContext;
258     }
259
260     private DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
261         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
262         assertNotNull("dataBroker should not be null", dataBroker);
263         return dataBroker;
264     }
265
266     private Boolean getNetvirtTopology() {
267         LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
268         Boolean found = false;
269         final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
270         InstanceIdentifier<Topology> path =
271                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
272         for (int i = 0; i < 60; i++) {
273             Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
274             if (topology != null) {
275                 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
276                 found = true;
277                 break;
278             } else {
279                 LOG.info("getNetvirtTopology: still looking ({})...", i);
280                 try {
281                     Thread.sleep(1000);
282                 } catch (InterruptedException e) {
283                     e.printStackTrace();
284                 }
285             }
286         }
287         return found;
288     }
289
290     /**
291      * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port
292      * 6640. This test will wait for incoming connections for {@link NetvirtITConstants#CONNECTION_INIT_TIMEOUT} ms.
293      *
294      * @throws InterruptedException
295      */
296     @Ignore
297     @Test
298     public void testPassiveNode() throws InterruptedException {
299         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_PASSIVE)) {
300             //Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
301             Thread.sleep(NetvirtITConstants.CONNECTION_INIT_TIMEOUT);
302         }
303     }
304
305     private ConnectionInfo getConnectionInfo(final String addressStr, final String portStr) {
306         InetAddress inetAddress = null;
307         try {
308             inetAddress = InetAddress.getByName(addressStr);
309         } catch (UnknownHostException e) {
310             fail("Could not allocate InetAddress: " + e);
311         }
312
313         IpAddress address = SouthboundMapper.createIpAddress(inetAddress);
314         PortNumber port = new PortNumber(Integer.parseInt(portStr));
315
316         LOG.info("connectionInfo: {}", new ConnectionInfoBuilder()
317                 .setRemoteIp(address)
318                 .setRemotePort(port)
319                 .build());
320         return new ConnectionInfoBuilder()
321                        .setRemoteIp(address)
322                        .setRemotePort(port)
323                        .build();
324     }
325
326     private String connectionInfoToString(final ConnectionInfo connectionInfo) {
327         return String.valueOf(connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
328     }
329
330     private boolean addOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
331         boolean result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION,
332                 SouthboundMapper.createInstanceIdentifier(connectionInfo),
333                 SouthboundMapper.createNode(connectionInfo));
334         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
335         return result;
336     }
337
338     private Node getOvsdbNode(final ConnectionInfo connectionInfo) {
339         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL,
340                 SouthboundMapper.createInstanceIdentifier(connectionInfo));
341     }
342
343     private boolean deleteOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
344         boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
345                 SouthboundMapper.createInstanceIdentifier(connectionInfo));
346         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
347         return result;
348     }
349
350     private Node connectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
351         Assert.assertTrue(addOvsdbNode(connectionInfo));
352         Node node = getOvsdbNode(connectionInfo);
353         Assert.assertNotNull("Should find OVSDB node after connect", node);
354         LOG.info("Connected to {}", connectionInfoToString(connectionInfo));
355         return node;
356     }
357
358     private boolean disconnectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
359         Assert.assertTrue(deleteOvsdbNode(connectionInfo));
360         Node node = getOvsdbNode(connectionInfo);
361         Assert.assertNull("Should not find OVSDB node after disconnect", node);
362         //Assume.assumeNotNull(node); // Using assumeNotNull because there is no assumeNull
363         LOG.info("Disconnected from {}", connectionInfoToString(connectionInfo));
364         return true;
365     }
366
367     private String getControllerIPAddress() {
368         String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
369         if (addressString != null) {
370             try {
371                 if (InetAddress.getByName(addressString) != null) {
372                     return addressString;
373                 }
374             } catch (UnknownHostException e) {
375                 LOG.error("Host {} is invalid", addressString);
376             }
377         }
378
379         addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
380         if (addressString != null) {
381             try {
382                 if (InetAddress.getByName(addressString) != null) {
383                     return addressString;
384                 }
385             } catch (UnknownHostException e) {
386                 LOG.error("Host {} is invalid", addressString);
387             }
388         }
389
390         return null;
391     }
392
393     private short getControllerOFPort() {
394         short openFlowPort = Constants.OPENFLOW_PORT;
395         String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
396         if (portString != null) {
397             try {
398                 openFlowPort = Short.parseShort(portString);
399             } catch (NumberFormatException e) {
400                 LOG.warn("Invalid port:{}, use default({})", portString,
401                         openFlowPort);
402             }
403         }
404         return openFlowPort;
405     }
406
407     private List<String> getControllersFromOvsdbNode(Node node) {
408         List<String> controllersStr = new ArrayList<>();
409
410         String controllerIpStr = getControllerIPAddress();
411         if (controllerIpStr != null) {
412             // If codepath makes it here, the ip address to be used was explicitly provided.
413             // Being so, also fetch openflowPort provided via ConfigProperties.
414             controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
415                     + ":" + controllerIpStr + ":" + getControllerOFPort());
416         } else {
417             // Check if ovsdb node has manager entries
418             OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
419             if (ovsdbNodeAugmentation != null) {
420                 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
421                 if (managerEntries != null && !managerEntries.isEmpty()) {
422                     for (ManagerEntry managerEntry : managerEntries) {
423                         if (managerEntry == null || managerEntry.getTarget() == null) {
424                             continue;
425                         }
426                         String[] tokens = managerEntry.getTarget().getValue().split(":");
427                         if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
428                             controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
429                                     + ":" + tokens[1] + ":" + getControllerOFPort());
430                         } else if (tokens[0].equalsIgnoreCase("ptcp")) {
431                             ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
432                             if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
433                                 controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
434                                 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
435                                         + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
436                             } else {
437                                 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
438                             }
439                         } else {
440                             LOG.trace("Skipping manager entry {} for node {}",
441                                     managerEntry.getTarget(), node.getNodeId().getValue());
442                         }
443                     }
444                 } else {
445                     LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
446                 }
447             }
448         }
449
450         if (controllersStr.isEmpty()) {
451             // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
452             LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
453             controllerIpStr = getLocalControllerHostIpAddress();
454             if (controllerIpStr != null) {
455                 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
456                         + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
457             }
458         }
459
460         if (controllersStr.isEmpty()) {
461             LOG.warn("Failed to determine OpenFlow controller ip address");
462         } else if (LOG.isDebugEnabled()) {
463             controllerIpStr = "";
464             for (String currControllerIpStr : controllersStr) {
465                 controllerIpStr += " " + currControllerIpStr;
466             }
467             LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
468         }
469
470         return controllersStr;
471     }
472
473     private String getLocalControllerHostIpAddress() {
474         String ipaddress = null;
475         try{
476             for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
477                 NetworkInterface iface = ifaces.nextElement();
478
479                 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
480                     InetAddress inetAddr = inetAddrs.nextElement();
481                     if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
482                         ipaddress = inetAddr.getHostAddress();
483                         break;
484                     }
485                 }
486             }
487         }catch (Exception e){
488             LOG.warn("Exception while fetching local host ip address ", e);
489         }
490         return ipaddress;
491     }
492
493     private String getControllerTarget(Node ovsdbNode) {
494         return getControllersFromOvsdbNode(ovsdbNode).get(0);
495     }
496
497     @Test
498     public void testAddDeleteOvsdbNode() throws InterruptedException {
499         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
500         connectOvsdbNode(connectionInfo);
501         ControllerEntry controllerEntry;
502         for (int i = 0; i < 10; i++) {
503             Node ovsdbNode = getOvsdbNode(connectionInfo);
504             Assert.assertNotNull("ovsdb node not found", ovsdbNode);
505             String controllerTarget = getControllerTarget(ovsdbNode);
506             Assert.assertNotNull("Failed to get controller target", controllerTarget);
507             OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
508             Assert.assertNotNull(bridge);
509             Assert.assertNotNull(bridge.getControllerEntry());
510             controllerEntry = bridge.getControllerEntry().iterator().next();
511             Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
512             if (controllerEntry.isIsConnected()) {
513                 Assert.assertTrue(controllerEntry.isIsConnected());
514                 break;
515             }
516             Thread.sleep(1000);
517         }
518
519         Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
520         Thread.sleep(1000);
521         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
522     }
523
524     @Ignore
525     @Test
526     public void testOpenVSwitchOtherConfig() throws InterruptedException {
527         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
528         Node ovsdbNode = connectOvsdbNode(connectionInfo);
529         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
530         Assert.assertNotNull(ovsdbNodeAugmentation);
531         List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
532         if (otherConfigsList != null) {
533             for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
534                 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
535                     LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
536                     break;
537                 } else {
538                     LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
539                 }
540             }
541         } else {
542             LOG.info("other_config is not present");
543         }
544         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
545         //Assume.assumeTrue(disconnectOvsdbNode(connectionInfo));
546     }
547
548     private OvsdbTerminationPointAugmentationBuilder createGenericOvsdbTerminationPointAugmentationBuilder() {
549         OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder =
550                 new OvsdbTerminationPointAugmentationBuilder();
551         ovsdbTerminationPointAugmentationBuilder.setInterfaceType(
552                 new InterfaceTypeEntryBuilder()
553                         .setInterfaceType(
554                                 SouthboundMapper.createInterfaceType("internal"))
555                         .build().getInterfaceType());
556         return ovsdbTerminationPointAugmentationBuilder;
557     }
558
559     private boolean addTerminationPoint(final NodeId bridgeNodeId, final String portName,
560             final OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder)
561         throws InterruptedException {
562
563         InstanceIdentifier<Node> portIid = SouthboundMapper.createInstanceIdentifier(bridgeNodeId);
564         NodeBuilder portNodeBuilder = new NodeBuilder();
565         NodeId portNodeId = SouthboundMapper.createManagedNodeId(portIid);
566         portNodeBuilder.setNodeId(portNodeId);
567         TerminationPointBuilder entry = new TerminationPointBuilder();
568         entry.setKey(new TerminationPointKey(new TpId(portName)));
569         entry.addAugmentation(
570                 OvsdbTerminationPointAugmentation.class,
571                 ovsdbTerminationPointAugmentationBuilder.build());
572         portNodeBuilder.setTerminationPoint(Lists.newArrayList(entry.build()));
573         boolean result = mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION,
574                 portIid, portNodeBuilder.build());
575         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
576         return result;
577     }
578
579     /**
580      * Extract the <code>store</code> type data store contents for the particular bridge identified by
581      * <code>bridgeName</code>.
582      *
583      * @param connectionInfo The connection information.
584      * @param bridgeName The bridge name.
585      * @param store defined by the <code>LogicalDatastoreType</code> enumeration
586      * @return <code>store</code> type data store contents
587      */
588     private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName,
589             LogicalDatastoreType store) {
590         Node bridgeNode = getBridgeNode(connectionInfo, bridgeName, store);
591         Assert.assertNotNull(bridgeNode);
592         OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
593         Assert.assertNotNull(ovsdbBridgeAugmentation);
594         return ovsdbBridgeAugmentation;
595     }
596
597     /**
598      * extract the <code>LogicalDataStoreType.OPERATIONAL</code> type data store contents for the particular bridge
599      * identified by <code>bridgeName</code>
600      *
601      * @param connectionInfo The connection information.
602      * @param bridgeName The bridge name.
603      * @see <code>NetvirtIT.getBridge(ConnectionInfo, String, LogicalDatastoreType)</code>
604      * @return <code>LogicalDatastoreType.OPERATIONAL</code> type data store contents
605      */
606     private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName) {
607         return getBridge(connectionInfo, bridgeName, LogicalDatastoreType.OPERATIONAL);
608     }
609
610     /**
611      * Extract the node contents from <code>store</code> type data store for the
612      * bridge identified by <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 Node getBridgeNode(ConnectionInfo connectionInfo, String bridgeName, LogicalDatastoreType store) {
620         InstanceIdentifier<Node> bridgeIid =
621                 SouthboundMapper.createInstanceIdentifier(connectionInfo,
622                     new OvsdbBridgeName(bridgeName));
623         return mdsalUtils.read(store, bridgeIid);
624     }
625
626     private boolean deleteBridge(final ConnectionInfo connectionInfo, final String bridgeName)
627         throws InterruptedException {
628
629         boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
630                 SouthboundMapper.createInstanceIdentifier(connectionInfo,
631                         new OvsdbBridgeName(bridgeName)));
632         Thread.sleep(OVSDB_UPDATE_TIMEOUT);
633         return result;
634     }
635
636     private InstanceIdentifier<Node> getTpIid(ConnectionInfo connectionInfo, OvsdbBridgeAugmentation bridge) {
637         return SouthboundMapper.createInstanceIdentifier(connectionInfo,
638             bridge.getBridgeName());
639     }
640
641     private void netVirtAddPort(ConnectionInfo connectionInfo) throws InterruptedException {
642         OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
643         Assert.assertNotNull(bridge);
644         NodeId nodeId = SouthboundMapper.createManagedNodeId(SouthboundMapper.createInstanceIdentifier(
645                 connectionInfo, bridge.getBridgeName()));
646         OvsdbTerminationPointAugmentationBuilder ovsdbTerminationBuilder =
647                 createGenericOvsdbTerminationPointAugmentationBuilder();
648         String portName = NetvirtITConstants.PORT_NAME;
649         ovsdbTerminationBuilder.setName(portName);
650         Assert.assertTrue(addTerminationPoint(nodeId, portName, ovsdbTerminationBuilder));
651         InstanceIdentifier<Node> terminationPointIid = getTpIid(connectionInfo, bridge);
652         Node terminationPointNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, terminationPointIid);
653         Assert.assertNotNull(terminationPointNode);
654     }
655
656     /**
657      * Test for basic southbound events to netvirt.
658      * <pre>The test will:
659      * - connect to an OVSDB node and verify it is added to operational
660      * - then verify that br-int was created on the node and stored in operational
661      * - a port is then added to the bridge to verify that it is ignored by netvirt
662      * - remove the bridge
663      * - remove the node and verify it is not in operational
664      * </pre>
665      * @throws InterruptedException
666      */
667     @Ignore
668     @Test
669     public void testNetVirt() throws InterruptedException {
670         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
671         Node ovsdbNode = connectOvsdbNode(connectionInfo);
672
673         Thread.sleep(30000);
674         // Verify the pipeline flows were installed
675         PipelineOrchestrator pipelineOrchestrator =
676                 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
677         assertNotNull("Could not find PipelineOrchestrator Service", pipelineOrchestrator);
678         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
679         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
680         LOG.info("testNetVirt: bridgeNode: {}", bridgeNode);
681         long datapathId = southbound.getDataPathId(bridgeNode);
682         assertNotEquals("datapathId was not found", datapathId, 0);
683         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
684                 FlowUtils.createNodeBuilder(datapathId);
685
686         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
687         List<Service> staticPipelineFound = Lists.newArrayList();
688         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
689             if (staticPipeline.contains(service)) {
690                 staticPipelineFound.add(service);
691             }
692             FlowBuilder flowBuilder = FlowUtils.getPipelineFlow(service.getTable(), (short)0);
693             Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
694             assertNotNull("Could not find flow in config", flow);
695             flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
696             assertNotNull("Could not find flow in operational", flow);
697         }
698         assertEquals("did not find all expected flows in static pipeline",
699                 staticPipeline.size(), staticPipelineFound.size());
700
701         netVirtAddPort(connectionInfo);
702         Thread.sleep(10000);
703         Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
704         Thread.sleep(10000);
705         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
706     }
707
708     @Ignore
709     @Test
710     public void testNetVirt2() throws InterruptedException {
711         Thread.sleep(60000);
712     }
713
714     @Ignore
715     @Test
716     public void testReadOvsdbTopologyNodes() throws InterruptedException {
717         Thread.sleep(10000);
718         List<Node> ovsdbNodes = southbound.readOvsdbTopologyNodes();
719         for (Node node : ovsdbNodes) {
720             LOG.info(">>>>> node: {}", node);
721         }
722     }
723
724     private Flow getFlow (
725             FlowBuilder flowBuilder,
726             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
727             LogicalDatastoreType store)
728             throws InterruptedException {
729
730         Flow flow = null;
731         for (int i = 0; i < 10; i++) {
732             flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
733             if (flow != null) {
734                 LOG.info("getFlow: flow({}): {}", store, flow);
735                 break;
736             }
737             Thread.sleep(1000);
738         }
739         return flow;
740     }
741 }