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.NetvirtProvidersProvider;
62 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
63 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
64 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
65 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
66 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
67 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
70 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
93 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
94 import org.ops4j.pax.exam.Configuration;
95 import org.ops4j.pax.exam.Option;
96 import org.ops4j.pax.exam.junit.PaxExam;
97 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
98 import org.ops4j.pax.exam.options.MavenUrlReference;
99 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
100 import org.ops4j.pax.exam.spi.reactors.PerClass;
101 import org.slf4j.Logger;
102 import org.slf4j.LoggerFactory;
105 * Integration tests for netvirt
107 * @author Sam Hague (shague@redhat.com)
109 @RunWith(PaxExam.class)
110 @ExamReactorStrategy(PerClass.class)
111 public class NetvirtIT extends AbstractMdsalTestBase {
112 private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
113 private static final int OVSDB_UPDATE_TIMEOUT = 1000;
114 private static DataBroker dataBroker = null;
115 private static String addressStr;
116 private static String portStr;
117 private static String connectionType;
118 private static String controllerStr;
119 private static AtomicBoolean setup = new AtomicBoolean(false);
120 private static MdsalUtils mdsalUtils = null;
121 private static Southbound southbound = null;
122 private static SouthboundUtils southboundUtils;
123 private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
124 private static final String SDPLNAME = "sg1";
125 private static final String NETWORK_ID = "521e29d6-67b8-4b3c-8633-027d21195111";
126 private static final String TENANT_ID = "521e29d6-67b8-4b3c-8633-027d21195100";
127 private static final String SUBNET_ID = "521e29d6-67b8-4b3c-8633-027d21195112";
128 private static final String PORT1_ID = "521e29d6-67b8-4b3c-8633-027d21195113";
129 private static final String DHCPPORT_ID ="521e29d6-67b8-4b3c-8633-027d21195115";
132 public String getModuleName() {
133 return "netvirt-providers-impl";
137 public String getInstanceName() {
138 return "netvirt-providers-default";
142 public MavenUrlReference getFeatureRepo() {
144 .groupId("org.opendaylight.ovsdb")
145 .artifactId("features-ovsdb")
146 .classifier("features")
148 .versionAsInProject();
152 public String getFeatureName() {
153 return "odl-ovsdb-openstack";
158 public Option[] config() {
159 Option[] parentOptions = super.config();
160 Option[] propertiesOptions = getPropertiesOptions();
161 Option[] otherOptions = getOtherOptions();
162 Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
163 System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
164 System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
165 System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
166 otherOptions.length);
170 private Option[] getOtherOptions() {
171 return new Option[] {
173 mavenBundle("org.opendaylight.ovsdb", "utils.mdsal-openflow")
174 .version(asInProject())
177 mavenBundle("org.opendaylight.ovsdb", "utils.config")
178 .version(asInProject())
180 configureConsole().startLocalConsole(),
181 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
186 public Option[] getPropertiesOptions() {
187 return new Option[] {
188 propagateSystemProperties(NetvirtITConstants.SERVER_IPADDRESS,
189 NetvirtITConstants.SERVER_PORT, NetvirtITConstants.CONNECTION_TYPE,
190 NetvirtITConstants.CONTROLLER_IPADDRESS,
191 NetvirtITConstants.USERSPACE_ENABLED)
196 public Option getLoggingOption() {
198 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
199 "log4j.logger.org.opendaylight.ovsdb",
200 LogLevelOption.LogLevel.TRACE.name()),
201 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
202 logConfiguration(NetvirtIT.class),
203 LogLevelOption.LogLevel.INFO.name()),
204 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
205 "log4j.logger.org.opendaylight.ovsdb.lib",
206 LogLevelOption.LogLevel.INFO.name()),
207 super.getLoggingOption());
210 protected String usage() {
211 return "Integration Test needs a valid connection configuration as follows :\n"
212 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
213 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
216 private void getProperties() {
217 Properties props = System.getProperties();
218 addressStr = props.getProperty(NetvirtITConstants.SERVER_IPADDRESS);
219 portStr = props.getProperty(NetvirtITConstants.SERVER_PORT, NetvirtITConstants.DEFAULT_SERVER_PORT);
220 connectionType = props.getProperty(NetvirtITConstants.CONNECTION_TYPE, "active");
221 controllerStr = props.getProperty(NetvirtITConstants.CONTROLLER_IPADDRESS, "0.0.0.0");
222 String userSpaceEnabled = props.getProperty(NetvirtITConstants.USERSPACE_ENABLED, "no");
223 LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}, controller ip: {}, " +
224 "userspace.enabled: {}",
225 connectionType, addressStr, portStr, controllerStr, userSpaceEnabled);
226 if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
227 if (addressStr == null) {
235 public void setup() throws InterruptedException {
237 LOG.info("Skipping setUp, already initialized");
243 } catch (Exception e) {
249 if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
250 if (addressStr == null) {
255 dataBroker = getDatabroker(getProviderContext());
256 mdsalUtils = new MdsalUtils(dataBroker);
257 assertNotNull("mdsalUtils should not be null", mdsalUtils);
258 assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
259 southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
260 assertNotNull("southbound should not be null", southbound);
261 southboundUtils = new SouthboundUtils(mdsalUtils);
265 private BindingAwareBroker.ProviderContext getProviderContext() {
266 BindingAwareBroker.ProviderContext providerContext = null;
267 for (int i=0; i < 60; i++) {
268 providerContext = getSession();
269 if (providerContext != null) {
274 } catch (InterruptedException e) {
279 assertNotNull("providercontext should not be null", providerContext);
280 /* One more second to let the provider finish initialization */
283 } catch (InterruptedException e) {
286 return providerContext;
289 private DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
290 DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
291 assertNotNull("dataBroker should not be null", dataBroker);
295 private Boolean getNetvirtTopology() {
296 LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
297 Boolean found = false;
298 final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
299 InstanceIdentifier<Topology> path =
300 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
301 for (int i = 0; i < 60; i++) {
302 Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
303 if (topology != null) {
304 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
308 LOG.info("getNetvirtTopology: still looking ({})...", i);
311 } catch (InterruptedException e) {
320 * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port
321 * 6640. This test will wait for incoming connections for {@link NetvirtITConstants#CONNECTION_INIT_TIMEOUT} ms.
323 * @throws InterruptedException
327 public void testPassiveNode() throws InterruptedException {
328 if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_PASSIVE)) {
329 //Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
330 Thread.sleep(NetvirtITConstants.CONNECTION_INIT_TIMEOUT);
334 private ConnectionInfo getConnectionInfo(final String addressStr, final String portStr) {
335 InetAddress inetAddress = null;
337 inetAddress = InetAddress.getByName(addressStr);
338 } catch (UnknownHostException e) {
339 fail("Could not allocate InetAddress: " + e);
342 IpAddress address = SouthboundMapper.createIpAddress(inetAddress);
343 PortNumber port = new PortNumber(Integer.parseInt(portStr));
345 LOG.info("connectionInfo: {}", new ConnectionInfoBuilder()
346 .setRemoteIp(address)
349 return new ConnectionInfoBuilder()
350 .setRemoteIp(address)
355 private String connectionInfoToString(final ConnectionInfo connectionInfo) {
356 return String.valueOf(connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
359 private boolean addOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
360 boolean result = mdsalUtils.put(LogicalDatastoreType.CONFIGURATION,
361 SouthboundMapper.createInstanceIdentifier(connectionInfo),
362 SouthboundMapper.createNode(connectionInfo));
363 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
367 private Node getOvsdbNode(final ConnectionInfo connectionInfo) {
368 return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL,
369 SouthboundMapper.createInstanceIdentifier(connectionInfo));
372 private boolean deleteOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
373 boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
374 SouthboundMapper.createInstanceIdentifier(connectionInfo));
375 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
379 private Node connectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
380 Assert.assertTrue(addOvsdbNode(connectionInfo));
381 Node node = getOvsdbNode(connectionInfo);
382 Assert.assertNotNull("Should find OVSDB node after connect", node);
383 LOG.info("Connected to {}", connectionInfoToString(connectionInfo));
387 private boolean disconnectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
388 Assert.assertTrue(deleteOvsdbNode(connectionInfo));
389 Node node = getOvsdbNode(connectionInfo);
390 Assert.assertNull("Should not find OVSDB node after disconnect", node);
391 //Assume.assumeNotNull(node); // Using assumeNotNull because there is no assumeNull
392 LOG.info("Disconnected from {}", connectionInfoToString(connectionInfo));
396 private String getControllerIPAddress() {
397 String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
398 if (addressString != null) {
400 if (InetAddress.getByName(addressString) != null) {
401 return addressString;
403 } catch (UnknownHostException e) {
404 LOG.error("Host {} is invalid", addressString);
408 addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
409 if (addressString != null) {
411 if (InetAddress.getByName(addressString) != null) {
412 return addressString;
414 } catch (UnknownHostException e) {
415 LOG.error("Host {} is invalid", addressString);
422 private short getControllerOFPort() {
423 short openFlowPort = Constants.OPENFLOW_PORT;
424 String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
425 if (portString != null) {
427 openFlowPort = Short.parseShort(portString);
428 } catch (NumberFormatException e) {
429 LOG.warn("Invalid port:{}, use default({})", portString,
436 private List<String> getControllersFromOvsdbNode(Node node) {
437 List<String> controllersStr = new ArrayList<>();
439 String controllerIpStr = getControllerIPAddress();
440 if (controllerIpStr != null) {
441 // If codepath makes it here, the ip address to be used was explicitly provided.
442 // Being so, also fetch openflowPort provided via ConfigProperties.
443 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
444 + ":" + controllerIpStr + ":" + getControllerOFPort());
446 // Check if ovsdb node has manager entries
447 OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
448 if (ovsdbNodeAugmentation != null) {
449 List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
450 if (managerEntries != null && !managerEntries.isEmpty()) {
451 for (ManagerEntry managerEntry : managerEntries) {
452 if (managerEntry == null || managerEntry.getTarget() == null) {
455 String[] tokens = managerEntry.getTarget().getValue().split(":");
456 if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
457 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
458 + ":" + tokens[1] + ":" + getControllerOFPort());
459 } else if (tokens[0].equalsIgnoreCase("ptcp")) {
460 ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
461 if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
462 controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
463 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
464 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
466 LOG.warn("Ovsdb Node does not contain connection info: {}", node);
469 LOG.trace("Skipping manager entry {} for node {}",
470 managerEntry.getTarget(), node.getNodeId().getValue());
474 LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
479 if (controllersStr.isEmpty()) {
480 // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
481 LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
482 controllerIpStr = getLocalControllerHostIpAddress();
483 if (controllerIpStr != null) {
484 controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
485 + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
489 if (controllersStr.isEmpty()) {
490 LOG.warn("Failed to determine OpenFlow controller ip address");
491 } else if (LOG.isDebugEnabled()) {
492 controllerIpStr = "";
493 for (String currControllerIpStr : controllersStr) {
494 controllerIpStr += " " + currControllerIpStr;
496 LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
499 return controllersStr;
502 private String getLocalControllerHostIpAddress() {
503 String ipaddress = null;
505 for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
506 NetworkInterface iface = ifaces.nextElement();
508 for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
509 InetAddress inetAddr = inetAddrs.nextElement();
510 if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
511 ipaddress = inetAddr.getHostAddress();
516 }catch (Exception e){
517 LOG.warn("Exception while fetching local host ip address ", e);
522 private String getControllerTarget(Node ovsdbNode) {
523 return getControllersFromOvsdbNode(ovsdbNode).get(0);
527 public void testAddDeleteOvsdbNode() throws InterruptedException {
528 LOG.info("testAddDeleteOvsdbNode enter 3");
529 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
530 connectOvsdbNode(connectionInfo);
531 ControllerEntry controllerEntry;
532 for (int i = 0; i < 10; i++) {
533 LOG.info("testAddDeleteOvsdbNode ({}): looking for controller", i);
534 Node ovsdbNode = getOvsdbNode(connectionInfo);
535 Assert.assertNotNull("ovsdb node not found", ovsdbNode);
536 String controllerTarget = getControllerTarget(ovsdbNode);
537 Assert.assertNotNull("Failed to get controller target", controllerTarget);
538 OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
539 Assert.assertNotNull(bridge);
540 Assert.assertNotNull(bridge.getControllerEntry());
541 controllerEntry = bridge.getControllerEntry().iterator().next();
542 Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
543 if (controllerEntry.isIsConnected()) {
544 Assert.assertTrue(controllerEntry.isIsConnected());
550 Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
552 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
557 public void testOpenVSwitchOtherConfig() throws InterruptedException {
558 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
559 Node ovsdbNode = connectOvsdbNode(connectionInfo);
560 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
561 Assert.assertNotNull(ovsdbNodeAugmentation);
562 List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
563 if (otherConfigsList != null) {
564 for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
565 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
566 LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
569 LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
573 LOG.info("other_config is not present");
575 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
576 //Assume.assumeTrue(disconnectOvsdbNode(connectionInfo));
579 private OvsdbTerminationPointAugmentationBuilder createGenericOvsdbTerminationPointAugmentationBuilder() {
580 OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder =
581 new OvsdbTerminationPointAugmentationBuilder();
582 ovsdbTerminationPointAugmentationBuilder.setInterfaceType(
583 new InterfaceTypeEntryBuilder()
585 SouthboundMapper.createInterfaceType("internal"))
586 .build().getInterfaceType());
587 return ovsdbTerminationPointAugmentationBuilder;
590 private boolean addTerminationPoint(final NodeId bridgeNodeId, final String portName,
591 final OvsdbTerminationPointAugmentationBuilder ovsdbTerminationPointAugmentationBuilder)
592 throws InterruptedException {
594 InstanceIdentifier<Node> portIid = SouthboundMapper.createInstanceIdentifier(bridgeNodeId);
595 NodeBuilder portNodeBuilder = new NodeBuilder();
596 NodeId portNodeId = SouthboundMapper.createManagedNodeId(portIid);
597 portNodeBuilder.setNodeId(portNodeId);
598 TerminationPointBuilder entry = new TerminationPointBuilder();
599 entry.setKey(new TerminationPointKey(new TpId(portName)));
600 entry.addAugmentation(
601 OvsdbTerminationPointAugmentation.class,
602 ovsdbTerminationPointAugmentationBuilder.build());
603 portNodeBuilder.setTerminationPoint(Lists.newArrayList(entry.build()));
604 boolean result = mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION,
605 portIid, portNodeBuilder.build());
606 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
611 * Extract the <code>store</code> type data store contents for the particular bridge identified by
612 * <code>bridgeName</code>.
614 * @param connectionInfo The connection information.
615 * @param bridgeName The bridge name.
616 * @param store defined by the <code>LogicalDatastoreType</code> enumeration
617 * @return <code>store</code> type data store contents
619 private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName,
620 LogicalDatastoreType store) {
621 Node bridgeNode = getBridgeNode(connectionInfo, bridgeName, store);
622 Assert.assertNotNull(bridgeNode);
623 OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
624 Assert.assertNotNull(ovsdbBridgeAugmentation);
625 return ovsdbBridgeAugmentation;
629 * extract the <code>LogicalDataStoreType.OPERATIONAL</code> type data store contents for the particular bridge
630 * identified by <code>bridgeName</code>
632 * @param connectionInfo The connection information.
633 * @param bridgeName The bridge name.
634 * @see <code>NetvirtIT.getBridge(ConnectionInfo, String, LogicalDatastoreType)</code>
635 * @return <code>LogicalDatastoreType.OPERATIONAL</code> type data store contents
637 private OvsdbBridgeAugmentation getBridge(ConnectionInfo connectionInfo, String bridgeName) {
638 return getBridge(connectionInfo, bridgeName, LogicalDatastoreType.OPERATIONAL);
642 * Extract the node contents from <code>store</code> type data store for the
643 * bridge identified by <code>bridgeName</code>
645 * @param connectionInfo The connection information.
646 * @param bridgeName The bridge name.
647 * @param store defined by the <code>LogicalDatastoreType</code> enumeration
648 * @return <code>store</code> type data store contents
650 private Node getBridgeNode(ConnectionInfo connectionInfo, String bridgeName, LogicalDatastoreType store) {
651 InstanceIdentifier<Node> bridgeIid =
652 SouthboundMapper.createInstanceIdentifier(connectionInfo,
653 new OvsdbBridgeName(bridgeName));
654 return mdsalUtils.read(store, bridgeIid);
657 private boolean deleteBridge(final ConnectionInfo connectionInfo, final String bridgeName)
658 throws InterruptedException {
660 boolean result = mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION,
661 SouthboundMapper.createInstanceIdentifier(connectionInfo,
662 new OvsdbBridgeName(bridgeName)));
663 Thread.sleep(OVSDB_UPDATE_TIMEOUT);
667 private InstanceIdentifier<Node> getTpIid(ConnectionInfo connectionInfo, OvsdbBridgeAugmentation bridge) {
668 return SouthboundMapper.createInstanceIdentifier(connectionInfo,
669 bridge.getBridgeName());
672 private void netVirtAddPort(ConnectionInfo connectionInfo) throws InterruptedException {
673 OvsdbBridgeAugmentation bridge = getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
674 Assert.assertNotNull(bridge);
675 NodeId nodeId = SouthboundMapper.createManagedNodeId(SouthboundMapper.createInstanceIdentifier(
676 connectionInfo, bridge.getBridgeName()));
677 OvsdbTerminationPointAugmentationBuilder ovsdbTerminationBuilder =
678 createGenericOvsdbTerminationPointAugmentationBuilder();
679 String portName = NetvirtITConstants.PORT_NAME;
680 ovsdbTerminationBuilder.setName(portName);
681 Assert.assertTrue(addTerminationPoint(nodeId, portName, ovsdbTerminationBuilder));
682 InstanceIdentifier<Node> terminationPointIid = getTpIid(connectionInfo, bridge);
683 Node terminationPointNode = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, terminationPointIid);
684 Assert.assertNotNull(terminationPointNode);
688 * Test for basic southbound events to netvirt.
689 * <pre>The test will:
690 * - connect to an OVSDB node and verify it is added to operational
691 * - then verify that br-int was created on the node and stored in operational
692 * - a port is then added to the bridge to verify that it is ignored by netvirt
693 * - remove the bridge
694 * - remove the node and verify it is not in operational
696 * @throws InterruptedException
699 public void testNetVirt() throws InterruptedException {
700 LOG.info("testNetVirt: starting test 2");
701 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
702 Node ovsdbNode = connectOvsdbNode(connectionInfo);
703 LOG.info("testNetVirt: should be connected");
706 LOG.info("testNetVirt: should really be connected after sleep");
707 // Verify the pipeline flows were installed
708 PipelineOrchestrator pipelineOrchestrator =
709 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
710 assertNotNull("Could not find PipelineOrchestrator Service", pipelineOrchestrator);
711 Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
712 assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
713 LOG.info("testNetVirt: bridgeNode: {}", bridgeNode);
714 long datapathId = southbound.getDataPathId(bridgeNode);
715 assertNotEquals("datapathId was not found", datapathId, 0);
717 //TODO add check for controller connection
718 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
719 FlowUtils.createNodeBuilder(datapathId);
721 List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
722 List<Service> staticPipelineFound = Lists.newArrayList();
723 for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
724 if (staticPipeline.contains(service)) {
725 staticPipelineFound.add(service);
727 FlowBuilder flowBuilder = FlowUtils.getPipelineFlow(service.getTable(), (short)0);
728 Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
729 assertNotNull("Could not find flow in config", flow);
730 //Thread.sleep(1000);
731 //flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
732 //assertNotNull("Could not find flow in operational", flow);
734 assertEquals("did not find all expected flows in static pipeline",
735 staticPipeline.size(), staticPipelineFound.size());
737 netVirtAddPort(connectionInfo);
739 Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
741 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
746 public void testNetVirt2() throws InterruptedException {
752 public void testReadOvsdbTopologyNodes() throws InterruptedException {
754 List<Node> ovsdbNodes = southbound.readOvsdbTopologyNodes();
755 for (Node node : ovsdbNodes) {
756 LOG.info(">>>>> node: {}", node);
761 public void testNetVirtFixedSG() throws InterruptedException {
762 ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portStr);
763 assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
764 Node ovsdbNode = connectOvsdbNode(connectionInfo);
765 assertNotNull("node is not connected", ovsdbNode);
768 Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
769 assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
770 long datapathId = southbound.getDataPathId(bridgeNode);
771 assertNotEquals("datapathId was not found", datapathId, 0);
773 NeutronNetwork nn = createNeutronNetwork(NETWORK_ID, TENANT_ID,NetworkHandler.NETWORK_TYPE_VXLAN, "100");
774 NeutronSubnet ns = createNeutronSubnet(SUBNET_ID, TENANT_ID, NETWORK_ID, "10.0.0.0/24");
775 NeutronPort nport = createNeutronPort(NETWORK_ID, SUBNET_ID, PORT1_ID, "compute", "10.0.0.10", "f6:00:00:0f:00:01");
776 NeutronPort dhcp = createNeutronPort(NETWORK_ID, SUBNET_ID, DHCPPORT_ID, "dhcp", "10.0.0.1", "f6:00:00:0f:00:02");
779 Map<String, String> externalIds = Maps.newHashMap();
780 externalIds.put("attached-mac", "f6:00:00:0f:00:01");
781 externalIds.put("iface-id", PORT1_ID);
782 southboundUtils.addTerminationPoint(bridgeNode, null, SDPLNAME, "internal", null, externalIds, new Long(3));
783 southboundUtils.addTerminationPoint(bridgeNode, null, "vm1", "internal", null, null, 0L);
784 southboundUtils.addTerminationPoint(bridgeNode, null, "vm2", "internal", null, null, 0L);
785 Map<String, String> options = Maps.newHashMap();
786 options.put("key", "flow");
787 options.put("remote_ip", "192.168.120.32");
788 southboundUtils.addTerminationPoint(bridgeNode, null, "vx", "vxlan", options, null, new Long(4));
791 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
792 FlowUtils.createNodeBuilder(datapathId);
793 MatchBuilder matchBuilder1 = new MatchBuilder();
794 matchBuilder1 = MatchUtils.createDhcpMatch(matchBuilder1, 68, 67);
795 String flowId1 = "Egress_DHCP_Client" + "_Permit_";
796 FlowBuilder flowBuilder1 = initFlowBuilder(matchBuilder1, flowId1, (short)40);
797 Flow flow1 = getFlow(flowBuilder1, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
798 assertNotNull("EgressSG : Could not find flow in configuration ", flow1);
799 flow1 = getFlow(flowBuilder1, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
800 assertNotNull("EgressSG Operational : Could not find flow in config", flow1);
802 testDefaultsSG(nport, datapathId, nn);
804 Assert.assertTrue(deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
806 Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
809 private void testDefaultsSG(NeutronPort nport, long datapathId, NeutronNetwork nn)
810 throws InterruptedException {
811 INeutronSecurityGroupCRUD ineutronSecurityGroupCRUD =
812 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
813 assertNotNull("Could not find ineutronSecurityGroupCRUD Service", ineutronSecurityGroupCRUD);
814 INeutronSecurityRuleCRUD ineutronSecurityRuleCRUD =
815 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
816 assertNotNull("Could not find ineutronSecurityRuleCRUD Service", ineutronSecurityRuleCRUD);
818 NeutronSecurityGroup neutronSG = new NeutronSecurityGroup();
819 neutronSG.setSecurityGroupDescription("testig defaultSG-IT");
820 neutronSG.setSecurityGroupName("DefaultSG");
821 neutronSG.setSecurityGroupUUID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
822 neutronSG.setTenantID(TENANT_ID);
824 List<NeutronSecurityRule> nsrs = new ArrayList<NeutronSecurityRule>();
825 NeutronSecurityRule nsrIN = new NeutronSecurityRule();
826 nsrIN.setSecurityRemoteGroupID(null);
827 nsrIN.setSecurityRuleDirection("ingress");
828 nsrIN.setSecurityRuleEthertype("IPv4");
829 nsrIN.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
830 nsrIN.setSecurityRuleProtocol("TCP");
831 nsrIN.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
832 nsrIN.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e6");
833 nsrIN.setTenantID(TENANT_ID);
835 NeutronSecurityRule nsrEG = new NeutronSecurityRule();
836 nsrEG.setSecurityRemoteGroupID(null);
837 nsrEG.setSecurityRuleDirection("egress");
838 nsrEG.setSecurityRuleEthertype("IPv4");
839 nsrEG.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
840 nsrEG.setSecurityRuleProtocol("TCP");
841 nsrEG.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
842 nsrEG.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e1");
843 nsrEG.setTenantID(TENANT_ID);
848 neutronSG.setSecurityRules(nsrs);
849 ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrIN);
850 ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrEG);
851 ineutronSecurityGroupCRUD.add(neutronSG);
853 List<NeutronSecurityGroup> sgs = new ArrayList<NeutronSecurityGroup>();
855 nport.setSecurityGroups(sgs);
857 INeutronPortCRUD iNeutronPortCRUD =
858 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
859 iNeutronPortCRUD.update(PORT1_ID, nport);
862 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilderEg =
863 FlowUtils.createNodeBuilder(datapathId);
864 MatchBuilder matchBuilderEg = new MatchBuilder();
865 matchBuilderEg = MatchUtils.createEtherMatchWithType(matchBuilderEg, null, nport.getMacAddress());
866 String flowIdEg = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
867 FlowBuilder flowBuilderEg = initFlowBuilder(matchBuilderEg, flowIdEg, (short)40);
868 Flow flowEg = getFlow(flowBuilderEg, nodeBuilderEg, LogicalDatastoreType.CONFIGURATION);
869 assertNotNull("EgressSG : Could not find flow in configuration ", flowEg);
870 flowEg = getFlow(flowBuilderEg, nodeBuilderEg, LogicalDatastoreType.OPERATIONAL);
871 assertNotNull("EgressSG Operational : Could not find flow in config", flowEg);
873 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilderIng =
874 FlowUtils.createNodeBuilder(datapathId);
875 MatchBuilder matchBuilderIng = new MatchBuilder();
876 matchBuilderIng = MatchUtils.createEtherMatchWithType(matchBuilderIng,null, nport.getMacAddress());
877 String flowIdIng = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
878 FlowBuilder flowBuilderIng = initFlowBuilder(matchBuilderIng, flowIdIng, (short)90);
879 Flow flowIng = getFlow(flowBuilderIng, nodeBuilderIng, LogicalDatastoreType.CONFIGURATION);
880 assertNotNull("IngressSG : Could not find flow in configuration ", flowIng);
881 flowEg = getFlow(flowBuilderIng, nodeBuilderIng, LogicalDatastoreType.OPERATIONAL);
882 assertNotNull("IngressSG Operational : Could not find flow in config", flowIng);
886 private NeutronPort createNeutronPort(String networkId, String subnetId,
887 String id, String owner, String ipaddr, String mac) {
888 INeutronPortCRUD iNeutronPortCRUD =
889 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
890 NeutronPort np = new NeutronPort();
893 np.setDeviceOwner(owner);
894 np.setMacAddress(mac);
895 np.setNetworkUUID(networkId);
896 List<org.opendaylight.neutron.spi.Neutron_IPs> srcAddressList =
897 new ArrayList<org.opendaylight.neutron.spi.Neutron_IPs>();
898 org.opendaylight.neutron.spi.Neutron_IPs nip = new org.opendaylight.neutron.spi.Neutron_IPs();
899 nip.setIpAddress(ipaddr);
900 nip.setSubnetUUID(subnetId);
901 srcAddressList.add(nip);
902 np.setFixedIPs(srcAddressList);
903 List<NeutronSecurityGroup> nsgs = new ArrayList<NeutronSecurityGroup>();
904 np.setSecurityGroups(nsgs);
905 iNeutronPortCRUD.add(np);
909 private NeutronSubnet createNeutronSubnet(String subnetId, String tenantId,
910 String networkId, String cidr) {
911 INeutronSubnetCRUD iNeutronSubnetCRUD =
912 (INeutronSubnetCRUD) ServiceHelper.getGlobalInstance(INeutronSubnetCRUD.class, this);
913 NeutronSubnet ns = new NeutronSubnet();
917 ns.setNetworkUUID(networkId);
918 ns.setTenantID(tenantId);
919 iNeutronSubnetCRUD.add(ns);
923 private NeutronNetwork createNeutronNetwork(String uuid, String tenantID, String networkTypeVxlan, String segId) {
924 INeutronNetworkCRUD iNeutronNetworkCRUD =
925 (INeutronNetworkCRUD) ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
926 NeutronNetwork nn = new NeutronNetwork();
929 nn.setTenantID(tenantID);
930 nn.setProviderNetworkType(networkTypeVxlan);
931 nn.setProviderSegmentationID(segId);
932 iNeutronNetworkCRUD.addNetwork(nn);
936 private FlowBuilder initFlowBuilder(MatchBuilder matchBuilder, String flowId, short tableId) {
937 FlowBuilder flowBuilder = new FlowBuilder();
938 flowBuilder.setMatch(matchBuilder.build());
939 flowBuilder.setId(new FlowId(flowId));
940 flowBuilder.setFlowName(flowId);
941 FlowKey key = new FlowKey(new FlowId(flowId));
942 flowBuilder.setStrict(true);
943 flowBuilder.setTableId(tableId);
944 flowBuilder.setKey(key);
948 private Flow getFlow (
949 FlowBuilder flowBuilder,
950 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
951 LogicalDatastoreType store)
952 throws InterruptedException {
955 for (int i = 0; i < 10; i++) {
956 LOG.info("getFlow {}-{}: looking for flowBuilder: {}, nodeBuilder: {}",
957 i, store, flowBuilder.build(), nodeBuilder.build());
958 flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
960 LOG.info("getFlow: found flow({}): {}", store, flow);