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