2 * Copyright (c) 2015 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.openstack.netvirt.it;
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;
26 import com.google.common.collect.Lists;
27 import com.google.common.collect.Maps;
29 import java.net.InetAddress;
30 import java.net.NetworkInterface;
31 import java.net.UnknownHostException;
32 import java.util.ArrayList;
33 import java.util.Enumeration;
34 import java.util.List;
36 import java.util.Properties;
37 import java.util.concurrent.atomic.AtomicBoolean;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
45 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
46 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
47 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
48 import org.opendaylight.neutron.spi.INeutronPortCRUD;
49 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
50 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
51 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
52 import org.opendaylight.neutron.spi.NeutronPort;
53 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
54 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
55 import org.opendaylight.neutron.spi.NeutronSecurityRule;
56 import org.opendaylight.neutron.spi.NeutronNetwork;
57 import org.opendaylight.neutron.spi.NeutronSubnet;
58 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
59 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
60 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
61 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
62 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
63 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
64 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
65 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
66 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
92 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
93 import org.ops4j.pax.exam.Configuration;
94 import org.ops4j.pax.exam.Option;
95 import org.ops4j.pax.exam.junit.PaxExam;
96 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
97 import org.ops4j.pax.exam.options.MavenUrlReference;
98 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
99 import org.ops4j.pax.exam.spi.reactors.PerClass;
100 import org.slf4j.Logger;
101 import org.slf4j.LoggerFactory;
104 * Integration tests for netvirt
106 * @author Sam Hague (shague@redhat.com)
108 @RunWith(PaxExam.class)
109 @ExamReactorStrategy(PerClass.class)
110 public class NetvirtIT extends AbstractMdsalTestBase {
111 private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
112 private static final int OVSDB_UPDATE_TIMEOUT = 1000;
113 private static DataBroker dataBroker = null;
114 private static String addressStr;
115 private static String portStr;
116 private static String connectionType;
117 private static String controllerStr;
118 private static AtomicBoolean setup = new AtomicBoolean(false);
119 private static MdsalUtils mdsalUtils = null;
120 private static Southbound southbound = null;
121 private static SouthboundUtils southboundUtils;
122 private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
123 private static final String SDPLNAME = "sg1";
124 private static final String NETWORK_ID = "521e29d6-67b8-4b3c-8633-027d21195111";
125 private static final String TENANT_ID = "521e29d6-67b8-4b3c-8633-027d21195100";
126 private static final String SUBNET_ID = "521e29d6-67b8-4b3c-8633-027d21195112";
127 private static final String PORT1_ID = "521e29d6-67b8-4b3c-8633-027d21195113";
128 private static final String DHCPPORT_ID ="521e29d6-67b8-4b3c-8633-027d21195115";
131 public String getModuleName() {
132 return "netvirt-providers-impl";
136 public String getInstanceName() {
137 return "netvirt-providers-default";
141 public MavenUrlReference getFeatureRepo() {
143 .groupId("org.opendaylight.ovsdb")
144 .artifactId("features-ovsdb")
145 .classifier("features")
147 .versionAsInProject();
151 public String getFeatureName() {
152 return "odl-ovsdb-openstack";
157 public Option[] config() {
158 Option[] parentOptions = super.config();
159 Option[] propertiesOptions = getPropertiesOptions();
160 Option[] otherOptions = getOtherOptions();
161 Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
162 System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
163 System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
164 System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
165 otherOptions.length);
169 private Option[] getOtherOptions() {
170 return new Option[] {
172 mavenBundle("org.opendaylight.ovsdb", "utils.mdsal-openflow")
173 .version(asInProject())
176 mavenBundle("org.opendaylight.ovsdb", "utils.config")
177 .version(asInProject())
179 configureConsole().startLocalConsole(),
180 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
185 public Option[] getPropertiesOptions() {
186 return new Option[] {
187 propagateSystemProperties(NetvirtITConstants.SERVER_IPADDRESS,
188 NetvirtITConstants.SERVER_PORT, NetvirtITConstants.CONNECTION_TYPE,
189 NetvirtITConstants.CONTROLLER_IPADDRESS,
190 NetvirtITConstants.USERSPACE_ENABLED)
195 public Option getLoggingOption() {
197 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
198 "log4j.logger.org.opendaylight.ovsdb",
199 LogLevelOption.LogLevel.TRACE.name()),
200 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
201 logConfiguration(NetvirtIT.class),
202 LogLevelOption.LogLevel.INFO.name()),
203 //editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
204 // "log4j.logger.org.opendaylight.ovsdb.lib",
205 // LogLevelOption.LogLevel.INFO.name()),
206 super.getLoggingOption());
209 protected String usage() {
210 return "Integration Test needs a valid connection configuration as follows :\n"
211 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
212 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
215 private void getProperties() {
216 Properties props = System.getProperties();
217 addressStr = props.getProperty(NetvirtITConstants.SERVER_IPADDRESS);
218 portStr = props.getProperty(NetvirtITConstants.SERVER_PORT, NetvirtITConstants.DEFAULT_SERVER_PORT);
219 connectionType = props.getProperty(NetvirtITConstants.CONNECTION_TYPE, "active");
220 controllerStr = props.getProperty(NetvirtITConstants.CONTROLLER_IPADDRESS, "0.0.0.0");
221 String userSpaceEnabled = props.getProperty(NetvirtITConstants.USERSPACE_ENABLED, "no");
222 LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}, controller ip: {}, " +
223 "userspace.enabled: {}",
224 connectionType, addressStr, portStr, controllerStr, userSpaceEnabled);
225 if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
226 if (addressStr == null) {
234 public void setup() throws InterruptedException {
236 LOG.info("Skipping setUp, already initialized");
242 } catch (Exception e) {
248 if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
249 if (addressStr == null) {
254 dataBroker = getDatabroker(getProviderContext());
255 mdsalUtils = new MdsalUtils(dataBroker);
256 assertNotNull("mdsalUtils should not be null", mdsalUtils);
257 assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
258 southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
259 assertNotNull("southbound should not be null", southbound);
260 southboundUtils = new SouthboundUtils(mdsalUtils);
264 private BindingAwareBroker.ProviderContext getProviderContext() {
265 BindingAwareBroker.ProviderContext providerContext = null;
266 for (int i=0; i < 60; i++) {
267 providerContext = getSession();
268 if (providerContext != null) {
273 } catch (InterruptedException e) {
278 assertNotNull("providercontext should not be null", providerContext);
279 /* One more second to let the provider finish initialization */
282 } catch (InterruptedException e) {
285 return providerContext;
288 private DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
289 DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
290 assertNotNull("dataBroker should not be null", dataBroker);
294 private Boolean getNetvirtTopology() {
295 LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
296 Boolean found = false;
297 final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
298 InstanceIdentifier<Topology> path =
299 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
300 for (int i = 0; i < 60; i++) {
301 Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
302 if (topology != null) {
303 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
307 LOG.info("getNetvirtTopology: still looking ({})...", i);
310 } catch (InterruptedException e) {
319 * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port
320 * 6640. This test will wait for incoming connections for {@link NetvirtITConstants#CONNECTION_INIT_TIMEOUT} ms.
322 * @throws InterruptedException
326 public void testPassiveNode() throws InterruptedException {
327 if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_PASSIVE)) {
328 //Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
329 Thread.sleep(NetvirtITConstants.CONNECTION_INIT_TIMEOUT);
333 private ConnectionInfo getConnectionInfo(final String addressStr, final String portStr) {
334 InetAddress inetAddress = null;
336 inetAddress = InetAddress.getByName(addressStr);
337 } catch (UnknownHostException e) {
338 fail("Could not allocate InetAddress: " + e);
341 IpAddress address = SouthboundMapper.createIpAddress(inetAddress);
342 PortNumber port = new PortNumber(Integer.parseInt(portStr));
344 LOG.info("connectionInfo: {}", new ConnectionInfoBuilder()
345 .setRemoteIp(address)
348 return new ConnectionInfoBuilder()
349 .setRemoteIp(address)
354 private String connectionInfoToString(final ConnectionInfo connectionInfo) {
355 return String.valueOf(connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
358 private boolean addOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
359 boolean result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION,
360 SouthboundMapper.createInstanceIdentifier(connectionInfo),
361 SouthboundMapper.createNode(connectionInfo));
362 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
366 private Node getOvsdbNode(final ConnectionInfo connectionInfo) {
367 return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL,
368 SouthboundMapper.createInstanceIdentifier(connectionInfo));
371 private boolean deleteOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
372 boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
373 SouthboundMapper.createInstanceIdentifier(connectionInfo));
374 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
378 private Node connectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
379 Assert.assertTrue(addOvsdbNode(connectionInfo));
380 Node node = getOvsdbNode(connectionInfo);
381 Assert.assertNotNull("Should find OVSDB node after connect", node);
382 LOG.info("Connected to {}", connectionInfoToString(connectionInfo));
386 private boolean disconnectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
387 Assert.assertTrue(deleteOvsdbNode(connectionInfo));
388 Node node = getOvsdbNode(connectionInfo);
389 Assert.assertNull("Should not find OVSDB node after disconnect", node);
390 //Assume.assumeNotNull(node); // Using assumeNotNull because there is no assumeNull
391 LOG.info("Disconnected from {}", connectionInfoToString(connectionInfo));
395 private String getControllerIPAddress() {
396 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
397 if (addressString != null) {
399 if (InetAddress.getByName(addressString) != null) {
400 return addressString;
402 } catch (UnknownHostException e) {
403 LOG.error("Host {} is invalid", addressString);
407 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
408 if (addressString != null) {
410 if (InetAddress.getByName(addressString) != null) {
411 return addressString;
413 } catch (UnknownHostException e) {
414 LOG.error("Host {} is invalid", addressString);
421 private short getControllerOFPort() {
422 short openFlowPort = Constants.OPENFLOW_PORT;
423 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
424 if (portString != null) {
426 openFlowPort = Short.parseShort(portString);
427 } catch (NumberFormatException e) {
428 LOG.warn("Invalid port:{}, use default({})", portString,
435 private List<String> getControllersFromOvsdbNode(Node node) {
436 List<String> controllersStr = new ArrayList<>();
438 String controllerIpStr = getControllerIPAddress();
439 if (controllerIpStr != null) {
440 // If codepath makes it here, the ip address to be used was explicitly provided.
441 // Being so, also fetch openflowPort provided via ConfigProperties.
442 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
443 + ":" + controllerIpStr + ":" + getControllerOFPort());
445 // Check if ovsdb node has manager entries
446 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
447 if (ovsdbNodeAugmentation != null) {
448 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
449 if (managerEntries != null && !managerEntries.isEmpty()) {
450 for (ManagerEntry managerEntry : managerEntries) {
451 if (managerEntry == null || managerEntry.getTarget() == null) {
454 String[] tokens = managerEntry.getTarget().getValue().split(":");
455 if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
456 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
457 + ":" + tokens[1] + ":" + getControllerOFPort());
458 } else if (tokens[0].equalsIgnoreCase("ptcp")) {
459 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
460 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
461 controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
462 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
463 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
465 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
468 LOG.trace("Skipping manager entry {} for node {}",
469 managerEntry.getTarget(), node.getNodeId().getValue());
473 LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
478 if (controllersStr.isEmpty()) {
479 // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
480 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
481 controllerIpStr = getLocalControllerHostIpAddress();
482 if (controllerIpStr != null) {
483 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
484 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
488 if (controllersStr.isEmpty()) {
489 LOG.warn("Failed to determine OpenFlow controller ip address");
490 } else if (LOG.isDebugEnabled()) {
491 controllerIpStr = "";
492 for (String currControllerIpStr : controllersStr) {
493 controllerIpStr += " " + currControllerIpStr;
495 LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
498 return controllersStr;
501 private String getLocalControllerHostIpAddress() {
502 String ipaddress = null;
504 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
505 NetworkInterface iface = ifaces.nextElement();
507 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
508 InetAddress inetAddr = inetAddrs.nextElement();
509 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
510 ipaddress = inetAddr.getHostAddress();
515 }catch (Exception e){
516 LOG.warn("Exception while fetching local host ip address ", e);
521 private String getControllerTarget(Node ovsdbNode) {
522 return getControllersFromOvsdbNode(ovsdbNode).get(0);
526 public void testAddDeleteOvsdbNode() throws InterruptedException {
527 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
528 connectOvsdbNode(connectionInfo);
529 ControllerEntry controllerEntry;
530 for (int i = 0; i < 10; i++) {
531 Node ovsdbNode = getOvsdbNode(connectionInfo);
532 Assert.assertNotNull("ovsdb node not found", ovsdbNode);
533 String controllerTarget = getControllerTarget(ovsdbNode);
534 Assert.assertNotNull("Failed to get controller target", controllerTarget);
535 OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
536 Assert.assertNotNull(bridge);
537 Assert.assertNotNull(bridge.getControllerEntry());
538 controllerEntry = bridge.getControllerEntry().iterator().next();
539 Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
540 if (controllerEntry.isIsConnected()) {
541 Assert.assertTrue(controllerEntry.isIsConnected());
547 Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
549 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
554 public void testOpenVSwitchOtherConfig() throws InterruptedException {
555 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
556 Node ovsdbNode = connectOvsdbNode(connectionInfo);
557 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
558 Assert.assertNotNull(ovsdbNodeAugmentation);
559 List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
560 if (otherConfigsList != null) {
561 for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
562 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
563 LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
566 LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
570 LOG.info("other_config is not present");
572 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
573 //Assume.assumeTrue(disconnectOvsdbNode(connectionInfo));
576 private OvsdbTerminationPointAugmentationBuilder createGenericOvsdbTerminationPointAugmentationBuilder() {
577 OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder =
578 new OvsdbTerminationPointAugmentationBuilder();
579 ovsdbTerminationPointAugmentationBuilder.setInterfaceType(
580 new InterfaceTypeEntryBuilder()
582 SouthboundMapper.createInterfaceType("internal"))
583 .build().getInterfaceType());
584 return ovsdbTerminationPointAugmentationBuilder;
587 private boolean addTerminationPoint(final NodeId bridgeNodeId, final String portName,
588 final OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder)
589 throws InterruptedException {
591 InstanceIdentifier<Node> portIid = SouthboundMapper.createInstanceIdentifier(bridgeNodeId);
592 NodeBuilder portNodeBuilder = new NodeBuilder();
593 NodeId portNodeId = SouthboundMapper.createManagedNodeId(portIid);
594 portNodeBuilder.setNodeId(portNodeId);
595 TerminationPointBuilder entry = new TerminationPointBuilder();
596 entry.setKey(new TerminationPointKey(new TpId(portName)));
597 entry.addAugmentation(
598 OvsdbTerminationPointAugmentation.class,
599 ovsdbTerminationPointAugmentationBuilder.build());
600 portNodeBuilder.setTerminationPoint(Lists.newArrayList(entry.build()));
601 boolean result = mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION,
602 portIid, portNodeBuilder.build());
603 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
608 * Extract the <code>store</code> type data store contents for the particular bridge identified by
609 * <code>bridgeName</code>.
611 * @param connectionInfo The connection information.
612 * @param bridgeName The bridge name.
613 * @param store defined by the <code>LogicalDatastoreType</code> enumeration
614 * @return <code>store</code> type data store contents
616 private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName,
617 LogicalDatastoreType store) {
618 Node bridgeNode = getBridgeNode(connectionInfo, bridgeName, store);
619 Assert.assertNotNull(bridgeNode);
620 OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
621 Assert.assertNotNull(ovsdbBridgeAugmentation);
622 return ovsdbBridgeAugmentation;
626 * extract the <code>LogicalDataStoreType.OPERATIONAL</code> type data store contents for the particular bridge
627 * identified by <code>bridgeName</code>
629 * @param connectionInfo The connection information.
630 * @param bridgeName The bridge name.
631 * @see <code>NetvirtIT.getBridge(ConnectionInfo, String, LogicalDatastoreType)</code>
632 * @return <code>LogicalDatastoreType.OPERATIONAL</code> type data store contents
634 private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName) {
635 return getBridge(connectionInfo, bridgeName, LogicalDatastoreType.OPERATIONAL);
639 * Extract the node contents from <code>store</code> type data store for the
640 * bridge identified by <code>bridgeName</code>
642 * @param connectionInfo The connection information.
643 * @param bridgeName The bridge name.
644 * @param store defined by the <code>LogicalDatastoreType</code> enumeration
645 * @return <code>store</code> type data store contents
647 private Node getBridgeNode(ConnectionInfo connectionInfo, String bridgeName, LogicalDatastoreType store) {
648 InstanceIdentifier<Node> bridgeIid =
649 SouthboundMapper.createInstanceIdentifier(connectionInfo,
650 new OvsdbBridgeName(bridgeName));
651 return mdsalUtils.read(store, bridgeIid);
654 private boolean deleteBridge(final ConnectionInfo connectionInfo, final String bridgeName)
655 throws InterruptedException {
657 boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
658 SouthboundMapper.createInstanceIdentifier(connectionInfo,
659 new OvsdbBridgeName(bridgeName)));
660 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
664 private InstanceIdentifier<Node> getTpIid(ConnectionInfo connectionInfo, OvsdbBridgeAugmentation bridge) {
665 return SouthboundMapper.createInstanceIdentifier(connectionInfo,
666 bridge.getBridgeName());
669 private void netVirtAddPort(ConnectionInfo connectionInfo) throws InterruptedException {
670 OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
671 Assert.assertNotNull(bridge);
672 NodeId nodeId = SouthboundMapper.createManagedNodeId(SouthboundMapper.createInstanceIdentifier(
673 connectionInfo, bridge.getBridgeName()));
674 OvsdbTerminationPointAugmentationBuilder ovsdbTerminationBuilder =
675 createGenericOvsdbTerminationPointAugmentationBuilder();
676 String portName = NetvirtITConstants.PORT_NAME;
677 ovsdbTerminationBuilder.setName(portName);
678 Assert.assertTrue(addTerminationPoint(nodeId, portName, ovsdbTerminationBuilder));
679 InstanceIdentifier<Node> terminationPointIid = getTpIid(connectionInfo, bridge);
680 Node terminationPointNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, terminationPointIid);
681 Assert.assertNotNull(terminationPointNode);
685 * Test for basic southbound events to netvirt.
686 * <pre>The test will:
687 * - connect to an OVSDB node and verify it is added to operational
688 * - then verify that br-int was created on the node and stored in operational
689 * - a port is then added to the bridge to verify that it is ignored by netvirt
690 * - remove the bridge
691 * - remove the node and verify it is not in operational
693 * @throws InterruptedException
696 public void testNetVirt() throws InterruptedException {
697 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
698 Node ovsdbNode = connectOvsdbNode(connectionInfo);
701 // Verify the pipeline flows were installed
702 PipelineOrchestrator pipelineOrchestrator =
703 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
704 assertNotNull("Could not find PipelineOrchestrator Service", pipelineOrchestrator);
705 Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
706 assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
707 LOG.info("testNetVirt: bridgeNode: {}", bridgeNode);
708 long datapathId = southbound.getDataPathId(bridgeNode);
709 assertNotEquals("datapathId was not found", datapathId, 0);
710 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
711 FlowUtils.createNodeBuilder(datapathId);
713 List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
714 List<Service> staticPipelineFound = Lists.newArrayList();
715 for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
716 if (staticPipeline.contains(service)) {
717 staticPipelineFound.add(service);
719 FlowBuilder flowBuilder = FlowUtils.getPipelineFlow(service.getTable(), (short)0);
720 Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
721 assertNotNull("Could not find flow in config", flow);
722 flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
723 assertNotNull("Could not find flow in operational", flow);
725 assertEquals("did not find all expected flows in static pipeline",
726 staticPipeline.size(), staticPipelineFound.size());
728 netVirtAddPort(connectionInfo);
730 Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
732 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
737 public void testNetVirt2() throws InterruptedException {
743 public void testReadOvsdbTopologyNodes() throws InterruptedException {
745 List<Node> ovsdbNodes = southbound.readOvsdbTopologyNodes();
746 for (Node node : ovsdbNodes) {
747 LOG.info(">>>>> node: {}", node);
752 public void testNetVirtFixedSG() throws InterruptedException {
753 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
754 assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
755 Node ovsdbNode = connectOvsdbNode(connectionInfo);
756 assertNotNull("node is not connected", ovsdbNode);
759 Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
760 assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
761 long datapathId = southbound.getDataPathId(bridgeNode);
762 assertNotEquals("datapathId was not found", datapathId, 0);
764 NeutronNetwork nn = createNeutronNetwork(NETWORK_ID, TENANT_ID,NetworkHandler.NETWORK_TYPE_VXLAN, "100");
765 NeutronSubnet ns = createNeutronSubnet(SUBNET_ID, TENANT_ID, NETWORK_ID, "10.0.0.0/24");
766 NeutronPort nport = createNeutronPort(NETWORK_ID, SUBNET_ID, PORT1_ID, "compute", "10.0.0.10", "f6:00:00:0f:00:01");
767 NeutronPort dhcp = createNeutronPort(NETWORK_ID, SUBNET_ID, DHCPPORT_ID, "dhcp", "10.0.0.1", "f6:00:00:0f:00:02");
770 Map<String, String> externalIds = Maps.newHashMap();
771 externalIds.put("attached-mac", "f6:00:00:0f:00:01");
772 externalIds.put("iface-id", PORT1_ID);
773 southboundUtils.addTerminationPoint(bridgeNode, null, SDPLNAME, "internal", null, externalIds, new Long(3));
774 southboundUtils.addTerminationPoint(bridgeNode, null, "vm1", "internal", null, null, 0L);
775 southboundUtils.addTerminationPoint(bridgeNode, null, "vm2", "internal", null, null, 0L);
776 Map<String, String> options = Maps.newHashMap();
777 options.put("key", "flow");
778 options.put("remote_ip", "192.168.120.32");
779 southboundUtils.addTerminationPoint(bridgeNode, null, "vx", "vxlan", options, null, new Long(4));
782 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
783 FlowUtils.createNodeBuilder(datapathId);
784 MatchBuilder matchBuilder1 = new MatchBuilder();
785 matchBuilder1 = MatchUtils.createDhcpMatch(matchBuilder1, 68, 67);
786 String flowId1 = "Egress_DHCP_Client" + "_Permit_";
787 FlowBuilder flowBuilder1 = initFlowBuilder(matchBuilder1, flowId1, (short)40);
788 Flow flow1 = getFlow(flowBuilder1, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
789 assertNotNull("EgressSG : Could not find flow in configuration ", flow1);
790 flow1 = getFlow(flowBuilder1, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
791 assertNotNull("EgressSG Operational : Could not find flow in config", flow1);
793 testDefaultsSG(nport, datapathId, nn);
795 Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
797 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
800 private void testDefaultsSG(NeutronPort nport, long datapathId, NeutronNetwork nn)
801 throws InterruptedException {
802 INeutronSecurityGroupCRUD ineutronSecurityGroupCRUD =
803 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
804 assertNotNull("Could not find ineutronSecurityGroupCRUD Service", ineutronSecurityGroupCRUD);
805 INeutronSecurityRuleCRUD ineutronSecurityRuleCRUD =
806 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
807 assertNotNull("Could not find ineutronSecurityRuleCRUD Service", ineutronSecurityRuleCRUD);
809 NeutronSecurityGroup neutronSG = new NeutronSecurityGroup();
810 neutronSG.setSecurityGroupDescription("testig defaultSG-IT");
811 neutronSG.setSecurityGroupName("DefaultSG");
812 neutronSG.setSecurityGroupUUID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
813 neutronSG.setTenantID(TENANT_ID);
815 List<NeutronSecurityRule> nsrs = new ArrayList<NeutronSecurityRule>();
816 NeutronSecurityRule nsrIN = new NeutronSecurityRule();
817 nsrIN.setSecurityRemoteGroupID(null);
818 nsrIN.setSecurityRuleDirection("ingress");
819 nsrIN.setSecurityRuleEthertype("IPv4");
820 nsrIN.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
821 nsrIN.setSecurityRuleProtocol("TCP");
822 nsrIN.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
823 nsrIN.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e6");
824 nsrIN.setTenantID(TENANT_ID);
826 NeutronSecurityRule nsrEG = new NeutronSecurityRule();
827 nsrEG.setSecurityRemoteGroupID(null);
828 nsrEG.setSecurityRuleDirection("egress");
829 nsrEG.setSecurityRuleEthertype("IPv4");
830 nsrEG.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
831 nsrEG.setSecurityRuleProtocol("TCP");
832 nsrEG.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
833 nsrEG.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e1");
834 nsrEG.setTenantID(TENANT_ID);
839 neutronSG.setSecurityRules(nsrs);
840 ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrIN);
841 ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrEG);
842 ineutronSecurityGroupCRUD.add(neutronSG);
844 List<NeutronSecurityGroup> sgs = new ArrayList<NeutronSecurityGroup>();
846 nport.setSecurityGroups(sgs);
848 INeutronPortCRUD iNeutronPortCRUD =
849 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
850 iNeutronPortCRUD.update(PORT1_ID, nport);
853 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilderEg =
854 FlowUtils.createNodeBuilder(datapathId);
855 MatchBuilder matchBuilderEg = new MatchBuilder();
856 matchBuilderEg = MatchUtils.createEtherMatchWithType(matchBuilderEg, null, nport.getMacAddress());
857 String flowIdEg = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
858 FlowBuilder flowBuilderEg = initFlowBuilder(matchBuilderEg, flowIdEg, (short)40);
859 Flow flowEg = getFlow(flowBuilderEg, nodeBuilderEg, LogicalDatastoreType.CONFIGURATION);
860 assertNotNull("EgressSG : Could not find flow in configuration ", flowEg);
861 flowEg = getFlow(flowBuilderEg, nodeBuilderEg, LogicalDatastoreType.OPERATIONAL);
862 assertNotNull("EgressSG Operational : Could not find flow in config", flowEg);
864 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilderIng =
865 FlowUtils.createNodeBuilder(datapathId);
866 MatchBuilder matchBuilderIng = new MatchBuilder();
867 matchBuilderIng = MatchUtils.createEtherMatchWithType(matchBuilderIng,null, nport.getMacAddress());
868 String flowIdIng = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
869 FlowBuilder flowBuilderIng = initFlowBuilder(matchBuilderIng, flowIdIng, (short)90);
870 Flow flowIng = getFlow(flowBuilderIng, nodeBuilderIng, LogicalDatastoreType.CONFIGURATION);
871 assertNotNull("IngressSG : Could not find flow in configuration ", flowIng);
872 flowEg = getFlow(flowBuilderIng, nodeBuilderIng, LogicalDatastoreType.OPERATIONAL);
873 assertNotNull("IngressSG Operational : Could not find flow in config", flowIng);
877 private NeutronPort createNeutronPort(String networkId, String subnetId,
878 String id, String owner, String ipaddr, String mac) {
879 INeutronPortCRUD iNeutronPortCRUD =
880 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
881 NeutronPort np = new NeutronPort();
884 np.setDeviceOwner(owner);
885 np.setMacAddress(mac);
886 np.setNetworkUUID(networkId);
887 List<org.opendaylight.neutron.spi.Neutron_IPs> srcAddressList =
888 new ArrayList<org.opendaylight.neutron.spi.Neutron_IPs>();
889 org.opendaylight.neutron.spi.Neutron_IPs nip = new org.opendaylight.neutron.spi.Neutron_IPs();
890 nip.setIpAddress(ipaddr);
891 nip.setSubnetUUID(subnetId);
892 srcAddressList.add(nip);
893 np.setFixedIPs(srcAddressList);
894 List<NeutronSecurityGroup> nsgs = new ArrayList<NeutronSecurityGroup>();
895 np.setSecurityGroups(nsgs);
896 iNeutronPortCRUD.add(np);
900 private NeutronSubnet createNeutronSubnet(String subnetId, String tenantId,
901 String networkId, String cidr) {
902 INeutronSubnetCRUD iNeutronSubnetCRUD =
903 (INeutronSubnetCRUD) ServiceHelper.getGlobalInstance(INeutronSubnetCRUD.class, this);
904 NeutronSubnet ns = new NeutronSubnet();
908 ns.setNetworkUUID(networkId);
909 ns.setTenantID(tenantId);
910 iNeutronSubnetCRUD.add(ns);
914 private NeutronNetwork createNeutronNetwork(String uuid, String tenantID, String networkTypeVxlan, String segId) {
915 INeutronNetworkCRUD iNeutronNetworkCRUD =
916 (INeutronNetworkCRUD) ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
917 NeutronNetwork nn = new NeutronNetwork();
920 nn.setTenantID(tenantID);
921 nn.setProviderNetworkType(networkTypeVxlan);
922 nn.setProviderSegmentationID(segId);
923 iNeutronNetworkCRUD.addNetwork(nn);
927 private FlowBuilder initFlowBuilder(MatchBuilder matchBuilder, String flowId, short tableId) {
928 FlowBuilder flowBuilder = new FlowBuilder();
929 flowBuilder.setMatch(matchBuilder.build());
930 flowBuilder.setId(new FlowId(flowId));
931 flowBuilder.setFlowName(flowId);
932 FlowKey key = new FlowKey(new FlowId(flowId));
933 flowBuilder.setStrict(true);
934 flowBuilder.setTableId(tableId);
935 flowBuilder.setKey(key);
939 private Flow getFlow (
940 FlowBuilder flowBuilder,
941 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
942 LogicalDatastoreType store)
943 throws InterruptedException {
946 for (int i = 0; i < 10; i++) {
947 flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
949 LOG.info("getFlow: flow({}): {}", store, flow);