bdbc82540c7a97aec48dbaa35c31b3c91ed2e8ab
[netvirt.git] / openstack / net-virt-sfc / it / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / NetvirtSfcIT.java
1 /*
2  * Copyright © 2015 Red Hat, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.sfc;
10
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.editConfigurationFilePut;
21 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
22
23 import java.util.List;
24 import java.util.Properties;
25 import java.util.concurrent.atomic.AtomicBoolean;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
34 import org.opendaylight.ovsdb.openstack.netvirt.sfc.utils.AclUtils;
35 import org.opendaylight.ovsdb.openstack.netvirt.sfc.utils.ClassifierUtils;
36 import org.opendaylight.ovsdb.openstack.netvirt.sfc.utils.SfcUtils;
37 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
38 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
39 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
40 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessLists;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessListsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.AclBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.AccessListEntriesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.AceBuilder;
46 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;
47 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;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.Classifiers;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.ClassifiersBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.ClassifierBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.SffsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.sffs.SffBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.rev150105.Sfc;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.rev150105.SfcBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
64 import org.opendaylight.yangtools.concepts.Builder;
65 import org.opendaylight.yangtools.yang.binding.DataObject;
66 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
67
68 import org.ops4j.pax.exam.Configuration;
69 import org.ops4j.pax.exam.Option;
70 import org.ops4j.pax.exam.junit.PaxExam;
71 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
72 import org.ops4j.pax.exam.options.MavenUrlReference;
73 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
74 import org.ops4j.pax.exam.spi.reactors.PerClass;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78 @RunWith(PaxExam.class)
79 @ExamReactorStrategy(PerClass.class)
80 public class NetvirtSfcIT extends AbstractMdsalTestBase {
81     private static final Logger LOG = LoggerFactory.getLogger(NetvirtSfcIT.class);
82     private static AclUtils aclUtils = new AclUtils();
83     private static ClassifierUtils classifierUtils = new ClassifierUtils();
84     private static SfcUtils sfcUtils = new SfcUtils();
85     private static MdsalUtils mdsalUtils;
86     private static AtomicBoolean setup = new AtomicBoolean(false);
87     private static SouthboundUtils southboundUtils;
88     private static String addressStr;
89     private static String portStr;
90     private static String connectionType;
91     public static final String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
92     public static final String SERVER_PORT = "ovsdbserver.port";
93     public static final String CONNECTION_TYPE = "ovsdbserver.connection";
94     public static final String CONNECTION_TYPE_ACTIVE = "active";
95     public static final String CONNECTION_TYPE_PASSIVE = "passive";
96     public static final String DEFAULT_SERVER_PORT = "6640";
97     public static final String INTEGRATION_BRIDGE_NAME = "br-int";
98     private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
99
100     @Override
101     public String getModuleName() {
102         return "netvirt-sfc";
103     }
104
105     @Override
106     public String getInstanceName() {
107         return "netvirt-sfc-default";
108     }
109
110     @Override
111     public MavenUrlReference getFeatureRepo() {
112         return maven()
113                 .groupId("org.opendaylight.ovsdb")
114                 .artifactId("openstack.net-virt-sfc-features-test")
115                 .classifier("features")
116                 .type("xml")
117                 .versionAsInProject();
118     }
119
120     @Override
121     public String getFeatureName() {
122         return "odl-ovsdb-sfc-test";
123     }
124
125     @Configuration
126     @Override
127     public Option[] config() {
128         Option[] parentOptions = super.config();
129         Option[] propertiesOptions = getPropertiesOptions();
130         Option[] otherOptions = getOtherOptions();
131         Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
132         System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
133         System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
134         System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
135                 otherOptions.length);
136         return options;
137     }
138
139     private Option[] getOtherOptions() {
140         return new Option[] {
141                 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
142                 keepRuntimeFolder()
143         };
144     }
145
146     public Option[] getPropertiesOptions() {
147         return new Option[] {
148                 propagateSystemProperties(SERVER_IPADDRESS, SERVER_PORT, CONNECTION_TYPE),
149         };
150     }
151
152     @Override
153     public Option getLoggingOption() {
154         return composite(
155                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
156                         logConfiguration(NetvirtSfcIT.class),
157                         LogLevel.INFO.name()),
158                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
159                         "log4j.logger.org.opendaylight.ovsdb.openstack.netvirt.sfc",
160                         LogLevel.TRACE.name()),
161                 /*editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
162                         "log4j.logger.org.opendaylight.ovsdb",
163                         LogLevelOption.LogLevel.TRACE.name()),*/
164                 super.getLoggingOption());
165     }
166
167     protected String usage() {
168         return "Integration Test needs a valid connection configuration as follows :\n"
169                 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
170                 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
171     }
172
173     private void getProperties() {
174         Properties props = System.getProperties();
175         addressStr = props.getProperty(SERVER_IPADDRESS);
176         portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
177         connectionType = props.getProperty(CONNECTION_TYPE, "active");
178         LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}",
179                 connectionType, addressStr, portStr);
180         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
181             if (addressStr == null) {
182                 fail(usage());
183             }
184         }
185     }
186
187     @Before
188     @Override
189     public void setup() {
190         if (setup.get()) {
191             LOG.info("Skipping setUp, already initialized");
192             return;
193         }
194
195         try {
196             Thread.sleep(1000);
197             super.setup();
198         } catch (Exception e) {
199             e.printStackTrace();
200         }
201
202         getProperties();
203
204         DataBroker dataBroker = getDatabroker(getProviderContext());
205         mdsalUtils = new MdsalUtils(dataBroker);
206         assertNotNull("mdsalUtils should not be null", mdsalUtils);
207         southboundUtils = new SouthboundUtils(mdsalUtils);
208         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
209         setup.set(true);
210     }
211
212     private ProviderContext getProviderContext() {
213         ProviderContext providerContext = null;
214         for (int i=0; i < 60; i++) {
215             providerContext = getSession();
216             if (providerContext != null) {
217                 break;
218             } else {
219                 try {
220                     Thread.sleep(1000);
221                 } catch (InterruptedException e) {
222                     e.printStackTrace();
223                 }
224             }
225         }
226         assertNotNull("providercontext should not be null", providerContext);
227         /* One more second to let the provider finish initialization */
228         try {
229             Thread.sleep(1000);
230         } catch (InterruptedException e) {
231             e.printStackTrace();
232         }
233         return providerContext;
234     }
235
236     private DataBroker getDatabroker(ProviderContext providerContext) {
237         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
238         assertNotNull("dataBroker should not be null", dataBroker);
239         return dataBroker;
240     }
241
242     private Boolean getNetvirtTopology() {
243         LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
244         Boolean found = false;
245         final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
246         InstanceIdentifier<Topology> path =
247                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
248         for (int i = 0; i < 60; i++) {
249             Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
250             if (topology != null) {
251                 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
252                 found = true;
253                 break;
254             } else {
255                 LOG.info("getNetvirtTopology: still looking ({})...", i);
256                 try {
257                     Thread.sleep(1000);
258                 } catch (InterruptedException e) {
259                     e.printStackTrace();
260                 }
261             }
262         }
263         return found;
264     }
265
266     @Test
267     public void testNetvirtSfcFeatureLoad() {
268         assertTrue(true);
269     }
270
271     private AccessListsBuilder setAccessLists () {
272         MatchesBuilder matchesBuilder = aclUtils.createMatches(new MatchesBuilder(), 80);
273         ActionsBuilder actionsBuilder = aclUtils.createActions(new ActionsBuilder(), Boolean.TRUE);
274         AceBuilder accessListEntryBuilder = aclUtils.createAccessListEntryBuilder(
275                 new AceBuilder(), "http", matchesBuilder, actionsBuilder);
276         AccessListEntriesBuilder accessListEntriesBuilder = aclUtils.createAccessListEntries(
277                 new AccessListEntriesBuilder(), accessListEntryBuilder);
278         AclBuilder accessListBuilder = aclUtils.createAccessList(new AclBuilder(),
279                 "http", accessListEntriesBuilder);
280         AccessListsBuilder accessListsBuilder = aclUtils.createAccessLists(new AccessListsBuilder(),
281                 accessListBuilder);
282         LOG.info("AccessLists: {}", accessListsBuilder.build());
283         return accessListsBuilder;
284     }
285
286     @Test
287     public void testAccessLists() {
288         testModel(setAccessLists(), AccessLists.class);
289     }
290
291     private ClassifiersBuilder setClassifiers() {
292         SffBuilder sffBuilder = classifierUtils.createSff(new SffBuilder(), "sffname");
293         SffsBuilder sffsBuilder = classifierUtils.createSffs(new SffsBuilder(), sffBuilder);
294         ClassifierBuilder classifierBuilder = classifierUtils.createClassifier(new ClassifierBuilder(),
295                 "classifierName", "aclName", sffsBuilder);
296         ClassifiersBuilder classifiersBuilder = classifierUtils.createClassifiers(new ClassifiersBuilder(),
297                 classifierBuilder);
298         LOG.info("Classifiers: {}", classifiersBuilder.build());
299         return classifiersBuilder;
300     }
301
302     @Test
303     public void testClassifiers() {
304         testModel(setClassifiers(), Classifiers.class);
305     }
306
307     private SfcBuilder setSfc() {
308         return sfcUtils.createSfc(new SfcBuilder(), "sfc");
309     }
310
311     @Test
312     public void testSfc() {
313         testModel(setSfc(), Sfc.class);
314     }
315
316     private <T extends DataObject> void testModel(Builder<T> builder, Class<T> clazz) {
317         InstanceIdentifier<T> path = InstanceIdentifier.create(clazz);
318         assertTrue(mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, path, builder.build()));
319         T result = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, path);
320         assertNotNull(clazz.getSimpleName() + " should not be null", result);
321         assertTrue("Failed to remove " + clazz.getSimpleName(),
322                 mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, path));
323         result = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, path);
324         assertNull(clazz.getSimpleName() + " should be null", result);
325     }
326
327     /*
328      * Connect to an ovsdb node. Netvirt should add br-int, add the controller address
329      * and program the pipeline flows.
330      */
331     @Test
332     public void testDoIt() throws InterruptedException {
333         ConnectionInfo connectionInfo = southboundUtils.getConnectionInfo(addressStr, portStr);
334         assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
335         assertNotNull("node is not connected", southboundUtils.getOvsdbNode(connectionInfo));
336         ControllerEntry controllerEntry;
337         // Loop 10s checking if the controller was added
338         for (int i = 0; i < 10; i++) {
339             Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
340             assertNotNull("ovsdb node not found", ovsdbNode);
341             String controllerTarget = SouthboundUtil.getControllerTarget(ovsdbNode);
342             assertNotNull("Failed to get controller target", controllerTarget);
343             OvsdbBridgeAugmentation bridge = southboundUtils.getBridge(connectionInfo, INTEGRATION_BRIDGE_NAME);
344             assertNotNull(bridge);
345             assertNotNull(bridge.getControllerEntry());
346             controllerEntry = bridge.getControllerEntry().iterator().next();
347             assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
348             if (controllerEntry.isIsConnected()) {
349                 Assert.assertTrue(controllerEntry.isIsConnected());
350                 break;
351             }
352             Thread.sleep(1000);
353         }
354
355         /* TODO: add code to write to mdsal to exercise the sfc dataChangeListener */
356         /* allow some time to let the impl code do it's work to push flows */
357         /* or just comment out below lines and just manually verify on the bridges and reset them */
358         //Thread.sleep(10000);
359
360         assertTrue(southboundUtils.deleteBridge(connectionInfo, INTEGRATION_BRIDGE_NAME));
361         Thread.sleep(1000);
362         assertTrue(southboundUtils.disconnectOvsdbNode(connectionInfo));
363     }
364
365     @Test
366     public void testDemo() throws InterruptedException {
367         for (DemoVm vm : demoVms){
368             ConnectionInfo connectionInfo = southboundUtils.getConnectionInfo(vm.ipAddr, vm.ipPort);
369             assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
370             Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
371             assertNotNull("node is not connected", ovsdbNode);
372             String controllerTarget = SouthboundUtil.getControllerTarget(ovsdbNode);
373             assertNotNull("Failed to get controller target", controllerTarget);
374             List<ControllerEntry> setControllerEntry = southboundUtils.createControllerEntry(controllerTarget);
375             Uri setUri = new Uri(controllerTarget);
376             assertTrue(southboundUtils.addBridge(connectionInfo, null, vm.name, null, true,
377                     SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get("secure"), true, null, null,
378                     setControllerEntry, null));
379
380             for (int i = 0; i < 10; i++) {
381                 OvsdbBridgeAugmentation bridge = southboundUtils.getBridge(connectionInfo, vm.name);
382                 assertNotNull("bridge was not found: " + vm.name, bridge);
383                 assertNotNull("ControllerEntry was not found: "
384                                 + southboundUtils.createControllerEntry(controllerTarget),
385                         bridge.getControllerEntry());
386                 List<ControllerEntry> getControllerEntries = bridge.getControllerEntry();
387                 for (ControllerEntry entry : getControllerEntries) {
388                     if (entry.isIsConnected()) {
389                         assertTrue(entry.isIsConnected());
390                         break;
391                     }
392                 }
393                 Thread.sleep(1000);
394             }
395
396             assertTrue(southboundUtils.deleteBridge(connectionInfo, vm.name));
397             Thread.sleep(1000);
398             assertTrue(southboundUtils.disconnectOvsdbNode(connectionInfo));
399         }
400     }
401
402     private class DemoVm {
403         String name;
404         String ipAddr;
405         String ipPort;
406
407         DemoVm(String name, String ipAddr, String ipPort) {
408             this.name = name;
409             this.ipAddr = ipAddr;
410             this.ipPort = ipPort;
411         }
412     }
413
414     private DemoVm[] demoVms = {
415             new DemoVm("sw1", "192.168.50.70", "6640"),
416             //new DemoVm("sw2", "192.168.50.71", "6640"),
417     };
418 }