2 * Copyright © 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
9 package org.opendaylight.ovsdb.openstack.netvirt.sfc;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.ops4j.pax.exam.CoreOptions.composite;
17 import static org.ops4j.pax.exam.CoreOptions.maven;
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.karaf.options.KarafDistributionOption.configureConsole;
21 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
22 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
35 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
36 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
37 import org.opendaylight.ovsdb.openstack.netvirt.sfc.openflow13.NshUtils;
38 import org.opendaylight.ovsdb.openstack.netvirt.sfc.openflow13.SfcClassifier;
39 import org.opendaylight.ovsdb.openstack.netvirt.sfc.utils.AclUtils;
40 import org.opendaylight.ovsdb.openstack.netvirt.sfc.utils.ClassifierUtils;
41 import org.opendaylight.ovsdb.openstack.netvirt.sfc.utils.SfcUtils;
42 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
43 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
44 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
45 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
46 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessLists;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessListsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.AclBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.AccessListEntriesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.AceBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.ActionsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.MatchesBuilder;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.Classifiers;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.ClassifiersBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.ClassifierBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.SffsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.sffs.SffBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.rev150105.Sfc;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.rev150105.SfcBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
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.network.topology.Topology;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
72 import org.opendaylight.yangtools.concepts.Builder;
73 import org.opendaylight.yangtools.yang.binding.DataObject;
74 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.ops4j.pax.exam.Configuration;
77 import org.ops4j.pax.exam.Option;
78 import org.ops4j.pax.exam.junit.PaxExam;
79 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
80 import org.ops4j.pax.exam.options.MavenUrlReference;
81 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
82 import org.ops4j.pax.exam.spi.reactors.PerClass;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
86 @RunWith(PaxExam.class)
87 @ExamReactorStrategy(PerClass.class)
88 public class NetvirtSfcIT extends AbstractMdsalTestBase {
89 private static final Logger LOG = LoggerFactory.getLogger(NetvirtSfcIT.class);
90 private static AclUtils aclUtils = new AclUtils();
91 private static ClassifierUtils classifierUtils = new ClassifierUtils();
92 private static SfcUtils sfcUtils = new SfcUtils();
93 private static MdsalUtils mdsalUtils;
94 private static AtomicBoolean setup = new AtomicBoolean(false);
95 private static SouthboundUtils southboundUtils;
96 private static String addressStr;
97 private static String portStr;
98 private static String connectionType;
99 private static Southbound southbound;
100 private static DataBroker dataBroker;
101 public static final String CONTROLLER_IPADDRESS = "ovsdb.controller.address";
102 public static final String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
103 public static final String SERVER_PORT = "ovsdbserver.port";
104 public static final String CONNECTION_TYPE = "ovsdbserver.connection";
105 public static final String CONNECTION_TYPE_ACTIVE = "active";
106 public static final String CONNECTION_TYPE_PASSIVE = "passive";
107 public static final String DEFAULT_SERVER_PORT = "6640";
108 public static final String INTEGRATION_BRIDGE_NAME = "br-int";
109 private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
112 public String getModuleName() {
113 return "netvirt-sfc";
117 public String getInstanceName() {
118 return "netvirt-sfc-default";
122 public MavenUrlReference getFeatureRepo() {
124 .groupId("org.opendaylight.ovsdb")
125 .artifactId("openstack.net-virt-sfc-features-test")
126 .classifier("features")
128 .versionAsInProject();
132 public String getFeatureName() {
133 return "odl-ovsdb-sfc-test";
138 public Option[] config() {
139 Option[] parentOptions = super.config();
140 Option[] propertiesOptions = getPropertiesOptions();
141 Option[] otherOptions = getOtherOptions();
142 Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
143 System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
144 System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
145 System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
146 otherOptions.length);
150 private Option[] getOtherOptions() {
151 return new Option[] {
152 configureConsole().startLocalConsole(),
153 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
158 public Option[] getPropertiesOptions() {
159 return new Option[] {
160 propagateSystemProperties(SERVER_IPADDRESS, SERVER_PORT, CONNECTION_TYPE, CONTROLLER_IPADDRESS),
165 public Option getLoggingOption() {
167 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
168 logConfiguration(NetvirtSfcIT.class),
169 LogLevel.INFO.name()),
170 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
171 "log4j.logger.org.opendaylight.ovsdb.openstack.netvirt.sfc",
172 LogLevel.TRACE.name()),
173 /*editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
174 "log4j.logger.org.opendaylight.ovsdb",
175 LogLevelOption.LogLevel.TRACE.name()),*/
176 super.getLoggingOption());
179 protected String usage() {
180 return "Integration Test needs a valid connection configuration as follows :\n"
181 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
182 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
185 private void getProperties() {
186 Properties props = System.getProperties();
187 addressStr = props.getProperty(SERVER_IPADDRESS);
188 portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
189 connectionType = props.getProperty(CONNECTION_TYPE, "active");
190 LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}",
191 connectionType, addressStr, portStr);
192 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
193 if (addressStr == null) {
201 public void setup() {
203 LOG.info("Skipping setUp, already initialized");
210 } catch (Exception e) {
216 dataBroker = getDatabroker(getProviderContext());
217 mdsalUtils = new MdsalUtils(dataBroker);
218 assertNotNull("mdsalUtils should not be null", mdsalUtils);
219 southboundUtils = new SouthboundUtils(mdsalUtils);
220 assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
221 southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
222 assertNotNull("southbound should not be null", southbound);
226 private ProviderContext getProviderContext() {
227 ProviderContext providerContext = null;
228 for (int i=0; i < 60; i++) {
229 providerContext = getSession();
230 if (providerContext != null) {
235 } catch (InterruptedException e) {
240 assertNotNull("providercontext should not be null", providerContext);
241 /* One more second to let the provider finish initialization */
244 } catch (InterruptedException e) {
247 return providerContext;
250 private DataBroker getDatabroker(ProviderContext providerContext) {
251 DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
252 assertNotNull("dataBroker should not be null", dataBroker);
256 private Boolean getNetvirtTopology() {
257 LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
258 Boolean found = false;
259 final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
260 InstanceIdentifier<Topology> path =
261 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
262 for (int i = 0; i < 60; i++) {
263 Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
264 if (topology != null) {
265 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
269 LOG.info("getNetvirtTopology: still looking ({})...", i);
272 } catch (InterruptedException e) {
281 public void testNetvirtSfcFeatureLoad() {
285 private AccessListsBuilder setAccessLists () {
286 MatchesBuilder matchesBuilder = aclUtils.createMatches(new MatchesBuilder(), 80);
287 ActionsBuilder actionsBuilder = aclUtils.createActions(new ActionsBuilder(), Boolean.TRUE);
288 AceBuilder accessListEntryBuilder = aclUtils.createAccessListEntryBuilder(
289 new AceBuilder(), "http", matchesBuilder, actionsBuilder);
290 AccessListEntriesBuilder accessListEntriesBuilder = aclUtils.createAccessListEntries(
291 new AccessListEntriesBuilder(), accessListEntryBuilder);
292 AclBuilder accessListBuilder = aclUtils.createAccessList(new AclBuilder(),
293 "http", accessListEntriesBuilder);
294 AccessListsBuilder accessListsBuilder = aclUtils.createAccessLists(new AccessListsBuilder(),
296 LOG.info("AccessLists: {}", accessListsBuilder.build());
297 return accessListsBuilder;
301 public void testAccessLists() {
302 testModel(setAccessLists(), AccessLists.class);
305 private ClassifiersBuilder setClassifiers() {
306 SffBuilder sffBuilder = classifierUtils.createSff(new SffBuilder(), "sffname");
307 SffsBuilder sffsBuilder = classifierUtils.createSffs(new SffsBuilder(), sffBuilder);
308 ClassifierBuilder classifierBuilder = classifierUtils.createClassifier(new ClassifierBuilder(),
309 "classifierName", "aclName", sffsBuilder);
310 ClassifiersBuilder classifiersBuilder = classifierUtils.createClassifiers(new ClassifiersBuilder(),
312 LOG.info("Classifiers: {}", classifiersBuilder.build());
313 return classifiersBuilder;
317 public void testClassifiers() {
318 testModel(setClassifiers(), Classifiers.class);
321 private SfcBuilder setSfc() {
322 return sfcUtils.createSfc(new SfcBuilder(), "sfc");
326 public void testSfc() {
327 testModel(setSfc(), Sfc.class);
330 private <T extends DataObject> void testModel(Builder<T> builder, Class<T> clazz) {
331 InstanceIdentifier<T> path = InstanceIdentifier.create(clazz);
332 assertTrue(mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, path, builder.build()));
333 T result = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, path);
334 assertNotNull(clazz.getSimpleName() + " should not be null", result);
335 assertTrue("Failed to remove " + clazz.getSimpleName(),
336 mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, path));
337 result = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, path);
338 assertNull(clazz.getSimpleName() + " should be null", result);
342 * Connect to an ovsdb node. Netvirt should add br-int, add the controller address
343 * and program the pipeline flows.
346 public void testDoIt() throws InterruptedException {
347 String bridgeName = INTEGRATION_BRIDGE_NAME;
348 ConnectionInfo connectionInfo = southboundUtils.getConnectionInfo(addressStr, portStr);
349 assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
350 Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
351 assertNotNull("node is not connected", ovsdbNode);
352 ControllerEntry controllerEntry;
353 // Loop 10s checking if the controller was added
354 for (int i = 0; i < 10; i++) {
355 ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
356 assertNotNull("ovsdb node not found", ovsdbNode);
357 String controllerTarget = "tcp:192.168.50.1:6653";//SouthboundUtil.getControllerTarget(ovsdbNode);
358 assertNotNull("Failed to get controller target", controllerTarget);
359 OvsdbBridgeAugmentation bridge = southboundUtils.getBridge(connectionInfo, bridgeName);
360 assertNotNull(bridge);
361 assertNotNull(bridge.getControllerEntry());
362 controllerEntry = bridge.getControllerEntry().iterator().next();
363 assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
364 if (controllerEntry.isIsConnected()) {
365 Assert.assertTrue(controllerEntry.isIsConnected());
371 /* TODO: add code to write to mdsal to exercise the sfc dataChangeListener */
372 /* allow some time to let the impl code do it's work to push flows */
373 /* or just comment out below lines and just manually verify on the bridges and reset them */
374 //Thread.sleep(10000);
376 assertTrue(southboundUtils.deleteBridge(connectionInfo, bridgeName));
378 assertTrue(southboundUtils.disconnectOvsdbNode(connectionInfo));
382 public void testDemo() throws InterruptedException {
383 for (DemoVm vm : demoVms){
384 ConnectionInfo connectionInfo = southboundUtils.getConnectionInfo(vm.ipAddr, vm.ipPort);
385 assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
386 Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
387 assertNotNull("node is not connected", ovsdbNode);
388 String controllerTarget = SouthboundUtil.getControllerTarget(ovsdbNode);
389 assertNotNull("Failed to get controller target", controllerTarget);
390 List<ControllerEntry> setControllerEntry = southboundUtils.createControllerEntry(controllerTarget);
391 Uri setUri = new Uri(controllerTarget);
392 assertTrue(southboundUtils.addBridge(connectionInfo, null, vm.name, null, true,
393 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get("secure"), true, null, null,
394 setControllerEntry, null));
396 for (int i = 0; i < 10; i++) {
397 OvsdbBridgeAugmentation bridge = southboundUtils.getBridge(connectionInfo, vm.name);
398 assertNotNull("bridge was not found: " + vm.name, bridge);
399 assertNotNull("ControllerEntry was not found: "
400 + southboundUtils.createControllerEntry(controllerTarget),
401 bridge.getControllerEntry());
402 List<ControllerEntry> getControllerEntries = bridge.getControllerEntry();
403 for (ControllerEntry entry : getControllerEntries) {
404 if (entry.isIsConnected()) {
405 assertTrue(entry.isIsConnected());
412 assertTrue(southboundUtils.deleteBridge(connectionInfo, vm.name));
414 assertTrue(southboundUtils.disconnectOvsdbNode(connectionInfo));
418 private class DemoVm {
423 DemoVm(String name, String ipAddr, String ipPort) {
425 this.ipAddr = ipAddr;
426 this.ipPort = ipPort;
430 private DemoVm[] demoVms = {
431 new DemoVm("sw1", "192.168.50.70", "6640"),
432 //new DemoVm("sw2", "192.168.50.71", "6640"),
436 public void testDoIt2() throws InterruptedException {
437 String bridgeName = "sw1";
438 ConnectionInfo connectionInfo = southboundUtils.getConnectionInfo(addressStr, portStr);
439 assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
440 Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
441 assertNotNull("node is not connected", ovsdbNode);
442 String controllerTarget = "tcp:192.168.50.1:6653";
443 List<ControllerEntry> setControllerEntry = southboundUtils.createControllerEntry(controllerTarget);
444 Assert.assertTrue(southboundUtils.addBridge(connectionInfo, null, bridgeName, null, true,
445 SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get("secure"), true, null, null,
446 setControllerEntry, null, "00:00:00:00:00:00:00:01"));
447 // Loop 10s checking if the controller was added
448 for (int i = 0; i < 10; i++) {
449 ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
450 assertNotNull("ovsdb node not found", ovsdbNode);
451 assertNotNull("Failed to get controller target", controllerTarget);
452 OvsdbBridgeAugmentation bridge = southboundUtils.getBridge(connectionInfo, bridgeName);
453 assertNotNull(bridge);
454 assertNotNull(bridge.getControllerEntry());
455 ControllerEntry controllerEntry = bridge.getControllerEntry().iterator().next();
456 assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
457 if (controllerEntry.isIsConnected()) {
458 Assert.assertTrue(controllerEntry.isIsConnected());
464 SfcClassifier sfcClassifier = new SfcClassifier(dataBroker, southbound, mdsalUtils);
465 Node bridgeNode = southbound.getBridgeNode(ovsdbNode, bridgeName);
466 assertNotNull("bridge " + bridgeName + " was not found", bridgeNode);
467 long datapathId = southbound.getDataPathId(bridgeNode);
468 //sfcClassifier.programSfcClassiferFlows();
469 sfcClassifier.programLocalInPort(datapathId, "4096", (long)1, (short)0, (short)50, true);
471 NshUtils nshUtils = new NshUtils(new Ipv4Address("192.168.50.71"), new PortNumber(6633),
472 (long)10, (short)255, (long)4096, (long)4096);
473 MatchesBuilder matchesBuilder = aclUtils.createMatches(new MatchesBuilder(), 80);
474 sfcClassifier.programSfcClassiferFlows(datapathId, (short)50, "test", matchesBuilder.build(),
475 nshUtils, (long)2, true);
479 //} catch (IOException e) {
480 // e.printStackTrace();
484 assertTrue(southboundUtils.deleteBridge(connectionInfo, bridgeName));
486 assertTrue(southboundUtils.deleteBridge(connectionInfo, INTEGRATION_BRIDGE_NAME));
488 assertTrue(southboundUtils.disconnectOvsdbNode(connectionInfo));