1 package org.opendaylight.controller.statisticsmanager.internal;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.fail;
6 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
7 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
8 import static org.ops4j.pax.exam.CoreOptions.options;
9 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
10 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
18 import javax.inject.Inject;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
25 import org.opendaylight.controller.sal.action.Action;
26 import org.opendaylight.controller.sal.action.Drop;
27 import org.opendaylight.controller.sal.core.ConstructionException;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.core.NodeConnector;
30 import org.opendaylight.controller.sal.flowprogrammer.Flow;
31 import org.opendaylight.controller.sal.match.Match;
32 import org.opendaylight.controller.sal.match.MatchType;
33 import org.opendaylight.controller.sal.reader.FlowOnNode;
34 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
35 import org.opendaylight.controller.sal.reader.NodeDescription;
36 import org.opendaylight.controller.statisticsmanager.IStatisticsManager;
37 import org.ops4j.pax.exam.Option;
38 import org.ops4j.pax.exam.junit.Configuration;
39 import org.ops4j.pax.exam.junit.PaxExam;
40 import org.ops4j.pax.exam.util.PathUtils;
41 import org.osgi.framework.Bundle;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.framework.ServiceReference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 @RunWith(PaxExam.class)
48 public class StatisticsManagerIT {
49 private Logger log = LoggerFactory
50 .getLogger(StatisticsManagerIT.class);
51 // get the OSGI bundle context
53 private BundleContext bc;
55 private IStatisticsManager manager = null;
57 // Configure the OSGi container
59 public Option[] config() {
62 systemProperty("logback.configurationFile").value(
63 "file:" + PathUtils.getBaseDir()
64 + "/src/test/resources/logback.xml"),
65 // To start OSGi console for inspection remotely
66 systemProperty("osgi.console").value("2401"),
67 // Set the systemPackages (used by clustering)
68 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
69 // List framework bundles
70 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
71 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
72 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
73 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
74 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
75 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
76 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
77 // List logger bundles
78 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
79 mavenBundle("org.slf4j", "log4j-over-slf4j")
80 .versionAsInProject(),
81 mavenBundle("ch.qos.logback", "logback-core")
82 .versionAsInProject(),
83 mavenBundle("ch.qos.logback", "logback-classic")
84 .versionAsInProject(),
85 // needed by statisticsmanager
86 mavenBundle("org.opendaylight.controller", "containermanager")
87 .versionAsInProject(),
88 mavenBundle("org.opendaylight.controller", "containermanager.implementation")
89 .versionAsInProject(),
90 mavenBundle("org.opendaylight.controller", "clustering.services")
91 .versionAsInProject(),
92 mavenBundle("org.opendaylight.controller", "clustering.stub")
93 .versionAsInProject(),
94 // needed by forwardingrulesmanager
95 mavenBundle("org.opendaylight.controller", "configuration")
96 .versionAsInProject(),
97 mavenBundle("org.opendaylight.controller", "configuration.implementation")
98 .versionAsInProject(),
99 mavenBundle("org.opendaylight.controller", "hosttracker")
100 .versionAsInProject(),
102 // List all the bundles on which the test case depends
103 mavenBundle("org.opendaylight.controller", "sal")
104 .versionAsInProject(),
105 mavenBundle("org.opendaylight.controller", "sal.implementation")
106 .versionAsInProject(),
107 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub")
108 .versionAsInProject(),
109 mavenBundle("org.opendaylight.controller", "switchmanager")
110 .versionAsInProject(),
111 mavenBundle("org.opendaylight.controller", "switchmanager.implementation")
112 .versionAsInProject(),
113 mavenBundle("org.opendaylight.controller", "statisticsmanager")
114 .versionAsInProject(),
115 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation")
116 .versionAsInProject(),
117 mavenBundle("org.opendaylight.controller", "forwardingrulesmanager")
118 .versionAsInProject(),
120 // needed by hosttracker
121 mavenBundle("org.opendaylight.controller", "topologymanager")
122 .versionAsInProject(),
123 mavenBundle("org.jboss.spec.javax.transaction",
124 "jboss-transaction-api_1.1_spec").versionAsInProject(),
125 mavenBundle("org.apache.commons", "commons-lang3")
126 .versionAsInProject(),
127 mavenBundle("org.apache.felix",
128 "org.apache.felix.dependencymanager")
129 .versionAsInProject(), junitBundles());
132 private String stateToString(int state) {
136 case Bundle.INSTALLED:
138 case Bundle.RESOLVED:
140 case Bundle.UNINSTALLED:
141 return "UNINSTALLED";
143 return "Not CONVERTED";
148 public void areWeReady() {
150 boolean debugit = false;
151 Bundle b[] = bc.getBundles();
152 for (int i = 0; i < b.length; i++) {
153 int state = b[i].getState();
154 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
155 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
156 + stateToString(state));
161 log.debug("Do some debugging because some bundle is "
165 // Assert if true, if false we are good to go!
166 assertFalse(debugit);
168 ServiceReference r = bc.getServiceReference(IStatisticsManager.class
171 this.manager = (IStatisticsManager) bc.getService(r);
173 // If StatisticsManager is null, cannot run tests.
174 assertNotNull(this.manager);
179 public void testGetFlows() {
181 Node node = new Node("STUB", new Integer(0xCAFE));
182 List<FlowOnNode> flows = this.manager.getFlows(node);
183 FlowOnNode fn = flows.get(0);
184 Assert.assertTrue(fn.getByteCount() == 100);
185 Assert.assertTrue(fn.getDurationNanoseconds() == 400);
186 Assert.assertTrue(fn.getDurationSeconds() == 40);
187 Assert.assertTrue(fn.getTableId() == (byte) 0x1);
188 Assert.assertTrue(fn.getPacketCount() == 200);
190 Match match = new Match();
192 match.setField(MatchType.NW_DST,
193 InetAddress.getByName("1.1.1.1"));
194 } catch (UnknownHostException e) {
195 fail("Couldn't create match");
197 Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
198 Assert.assertTrue(fn.getFlow().getActions().get(0)
199 .equals(new Drop()));
200 } catch (ConstructionException e) {
201 // Got an unexpected exception
202 Assert.assertTrue(false);
208 public void testGetFlowStatistics() {
209 Flow flow = new Flow();
211 Match match = new Match();
213 match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
214 } catch (UnknownHostException e) {
216 flow.setMatch(match);
217 Action action = new Drop();
219 List<Action> actions = new ArrayList<Action>();
221 flow.setActions(actions);
223 flow.setPriority((short) 3500);
224 flow.setIdleTimeout((short) 1000);
225 flow.setHardTimeout((short) 2000);
229 Node node = new Node("STUB", 0xCAFE);
230 FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
231 List<FlowEntry> list = new ArrayList<FlowEntry>();
233 FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
236 Map<Node, List<FlowOnNode>> result = this.manager
237 .getFlowStatisticsForFlowList(null);
238 Assert.assertTrue(result.isEmpty());
239 result = this.manager.getFlowStatisticsForFlowList(list);
240 List<FlowOnNode> results = result.get(node);
241 FlowOnNode fn = results.get(0);
242 Assert.assertTrue(fn.getByteCount() == 100);
243 Assert.assertTrue(fn.getDurationNanoseconds() == 400);
244 Assert.assertTrue(fn.getDurationSeconds() == 40);
245 Assert.assertTrue(fn.getTableId() == (byte) 0x1);
246 Assert.assertTrue(fn.getPacketCount() == 200);
247 Assert.assertTrue(fn.getFlow().equals(flow));
248 } catch (ConstructionException e) {
249 Assert.assertTrue(false);
255 public void testGetFlowsNumber() {
257 Node node = new Node("STUB", 0xCAFE);
258 Assert.assertEquals(21, this.manager.getFlowsNumber(node));
259 }catch(ConstructionException e){
260 Assert.assertTrue(false);
265 public void testGetNodeDescription() {
267 Node node = new Node("STUB", 0xCAFE);
268 NodeDescription desc = this.manager.getNodeDescription(node);
269 Assert.assertTrue(desc.getDescription().equals(
270 "This is a sample node description"));
271 Assert.assertTrue(desc.getHardware().equals("stub hardware"));
272 Assert.assertTrue(desc.getSoftware().equals("stub software"));
273 Assert.assertTrue(desc.getSerialNumber().equals("123"));
274 Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
275 } catch (ConstructionException e) {
276 Assert.assertTrue(false);
282 public void testGetNodeConnectorStatistics() {
284 Node node = new Node("STUB", 0xCAFE);
285 List<NodeConnectorStatistics> stats = this.manager
286 .getNodeConnectorStatistics(node);
287 NodeConnectorStatistics ns = stats.get(0);
288 Assert.assertTrue(ns.getCollisionCount() == 4);
289 Assert.assertTrue(ns.getReceiveByteCount() == 1000);
290 Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
291 Assert.assertTrue(ns.getReceiveDropCount() == 2);
292 Assert.assertTrue(ns.getReceiveErrorCount() == 3);
293 Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
294 Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
295 Assert.assertTrue(ns.getReceivePacketCount() == 250);
296 Assert.assertTrue(ns.getTransmitByteCount() == 5000);
297 Assert.assertTrue(ns.getTransmitDropCount() == 50);
298 Assert.assertTrue(ns.getTransmitErrorCount() == 10);
299 Assert.assertTrue(ns.getTransmitPacketCount() == 500);
301 NodeConnector nc = ns.getNodeConnector();
302 NodeConnectorStatistics ns2 = this.manager
303 .getNodeConnectorStatistics(nc);
304 Assert.assertTrue(ns2.getCollisionCount() == 4);
305 Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
306 Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
307 Assert.assertTrue(ns2.getReceiveDropCount() == 2);
308 Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
309 Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
310 Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
311 Assert.assertTrue(ns2.getReceivePacketCount() == 250);
312 Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
313 Assert.assertTrue(ns2.getTransmitDropCount() == 50);
314 Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
315 Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
317 } catch (ConstructionException e) {
318 Assert.assertTrue(false);