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