1 package org.opendaylight.controller.statisticsmanager.internal;
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.util.ArrayList;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.osgi.framework.ServiceReference;
12 import org.osgi.framework.Bundle;
13 import javax.inject.Inject;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.junit.Before;
18 import org.junit.runner.RunWith;
19 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
20 import org.opendaylight.controller.sal.action.Action;
21 import org.opendaylight.controller.sal.action.Drop;
22 import org.opendaylight.controller.sal.core.ConstructionException;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.flowprogrammer.Flow;
26 import org.opendaylight.controller.sal.match.Match;
27 import org.opendaylight.controller.sal.match.MatchType;
28 import org.opendaylight.controller.sal.reader.FlowOnNode;
29 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
30 import org.opendaylight.controller.sal.reader.NodeDescription;
31 import org.opendaylight.controller.sal.utils.NodeCreator;
32 import org.opendaylight.controller.statisticsmanager.*;
33 import org.ops4j.pax.exam.junit.PaxExam;
34 import org.osgi.framework.BundleContext;
35 import static org.junit.Assert.*;
36 import org.ops4j.pax.exam.junit.Configuration;
37 import static org.ops4j.pax.exam.CoreOptions.*;
39 import org.ops4j.pax.exam.Option;
40 import org.ops4j.pax.exam.util.PathUtils;
42 @RunWith(PaxExam.class)
43 public class StatisticsManagerIT {
44 private Logger log = LoggerFactory
45 .getLogger(StatisticsManagerIT.class);
46 // get the OSGI bundle context
48 private BundleContext bc;
50 private IStatisticsManager manager = null;
52 // Configure the OSGi container
54 public Option[] config() {
57 systemProperty("logback.configurationFile").value(
58 "file:" + PathUtils.getBaseDir()
59 + "/src/test/resources/logback.xml"),
60 // To start OSGi console for inspection remotely
61 systemProperty("osgi.console").value("2401"),
62 // Set the systemPackages (used by clustering)
63 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
64 // List framework bundles
65 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
66 "1.0.0.v20120522-1841"),
67 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
68 "1.0.400.v20120522-2049"),
69 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
70 "3.3.100.v20120522-1822"),
71 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
72 "1.4.0.v20120522-1841"),
73 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
74 "0.8.0.v201108120515"),
75 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
76 "0.8.0.v201108120515"),
77 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
78 "0.8.0.v201110170705"),
79 // List logger bundles
80 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
81 mavenBundle("org.slf4j", "log4j-over-slf4j")
82 .versionAsInProject(),
83 mavenBundle("ch.qos.logback", "logback-core")
84 .versionAsInProject(),
85 mavenBundle("ch.qos.logback", "logback-classic")
86 .versionAsInProject(),
87 // List all the bundles on which the test case depends
88 mavenBundle("org.opendaylight.controller", "sal")
89 .versionAsInProject(),
90 mavenBundle("org.opendaylight.controller", "sal.implementation")
91 .versionAsInProject(),
92 mavenBundle("org.opendaylight.controller", "statisticsmanager")
93 .versionAsInProject(),
94 mavenBundle("org.opendaylight.controller",
95 "statisticsmanager.implementation")
96 .versionAsInProject(),
97 mavenBundle("org.opendaylight.controller",
98 "protocol_plugins.stub").versionAsInProject(),
99 // needed by statisticsmanager
100 mavenBundle("org.opendaylight.controller", "containermanager")
101 .versionAsInProject(),
102 mavenBundle("org.opendaylight.controller",
103 "containermanager.implementation").versionAsInProject(),
104 mavenBundle("org.opendaylight.controller",
105 "forwardingrulesmanager").versionAsInProject(),
107 mavenBundle("org.opendaylight.controller",
108 "clustering.services").versionAsInProject(),
109 mavenBundle("org.opendaylight.controller", "clustering.stub")
110 .versionAsInProject(),
112 // needed by forwardingrulesmanager
113 mavenBundle("org.opendaylight.controller", "switchmanager")
114 .versionAsInProject(),
115 mavenBundle("org.opendaylight.controller", "configuration")
116 .versionAsInProject(),
118 mavenBundle("org.opendaylight.controller",
119 "configuration.implementation").versionAsInProject(),
120 mavenBundle("org.opendaylight.controller", "hosttracker")
121 .versionAsInProject(),
123 // needed by hosttracker
124 mavenBundle("org.opendaylight.controller", "topologymanager")
125 .versionAsInProject(),
127 mavenBundle("org.jboss.spec.javax.transaction",
128 "jboss-transaction-api_1.1_spec").versionAsInProject(),
129 mavenBundle("org.apache.commons", "commons-lang3")
130 .versionAsInProject(),
131 mavenBundle("org.apache.felix",
132 "org.apache.felix.dependencymanager")
133 .versionAsInProject(), junitBundles());
136 private String stateToString(int state) {
140 case Bundle.INSTALLED:
142 case Bundle.RESOLVED:
144 case Bundle.UNINSTALLED:
145 return "UNINSTALLED";
147 return "Not CONVERTED";
152 public void areWeReady() {
154 boolean debugit = false;
155 Bundle b[] = bc.getBundles();
156 for (int i = 0; i < b.length; i++) {
157 int state = b[i].getState();
158 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
159 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
160 + stateToString(state));
165 log.debug("Do some debugging because some bundle is "
169 // Assert if true, if false we are good to go!
170 assertFalse(debugit);
172 ServiceReference r = bc.getServiceReference(IStatisticsManager.class
175 this.manager = (IStatisticsManager) bc.getService(r);
177 // If StatisticsManager is null, cannot run tests.
178 assertNotNull(this.manager);
183 public void testGetFlows() {
185 Node node = new Node("STUB", new Integer(0xCAFE));
186 List<FlowOnNode> flows = this.manager.getFlows(node);
187 FlowOnNode fn = flows.get(0);
188 Assert.assertTrue(fn.getByteCount() == 100);
189 Assert.assertTrue(fn.getDurationNanoseconds() == 400);
190 Assert.assertTrue(fn.getDurationSeconds() == 40);
191 Assert.assertTrue(fn.getTableId() == (byte) 0x1);
192 Assert.assertTrue(fn.getPacketCount() == 200);
194 Match match = new Match();
196 match.setField(MatchType.NW_DST,
197 InetAddress.getByName("1.1.1.1"));
198 } catch (UnknownHostException e) {
199 fail("Couldn't create match");
201 Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
202 Assert.assertTrue(fn.getFlow().getActions().get(0)
203 .equals(new Drop()));
204 } catch (ConstructionException e) {
205 // Got an unexpected exception
206 Assert.assertTrue(false);
212 public void testGetFlowStatistics() {
213 Flow flow = new Flow();
215 Match match = new Match();
217 match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
218 } catch (UnknownHostException e) {
220 flow.setMatch(match);
221 Action action = new Drop();
223 List<Action> actions = new ArrayList<Action>();
225 flow.setActions(actions);
228 Node node = new Node("STUB", 0xCAFE);
229 FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
230 List<FlowEntry> list = new ArrayList<FlowEntry>();
232 FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
235 Map<Node, List<FlowOnNode>> result = this.manager
236 .getFlowStatisticsForFlowList(null);
237 Assert.assertTrue(result.isEmpty());
238 result = this.manager.getFlowStatisticsForFlowList(list);
239 List<FlowOnNode> results = result.get(node);
240 FlowOnNode fn = results.get(0);
241 Assert.assertTrue(fn.getByteCount() == 100);
242 Assert.assertTrue(fn.getDurationNanoseconds() == 400);
243 Assert.assertTrue(fn.getDurationSeconds() == 40);
244 Assert.assertTrue(fn.getTableId() == (byte) 0x1);
245 Assert.assertTrue(fn.getPacketCount() == 200);
246 Assert.assertTrue(fn.getFlow().equals(flow));
247 } catch (ConstructionException e) {
248 Assert.assertTrue(false);
254 public void testGetFlowsNumber() {
256 Node node = new Node("STUB", 0xCAFE);
257 Assert.assertEquals(21, this.manager.getFlowsNumber(node));
258 }catch(ConstructionException e){
259 Assert.assertTrue(false);
264 public void testGetNodeDescription() {
266 Node node = new Node("STUB", 0xCAFE);
267 NodeDescription desc = this.manager.getNodeDescription(node);
268 Assert.assertTrue(desc.getDescription().equals(
269 "This is a sample node description"));
270 Assert.assertTrue(desc.getHardware().equals("stub hardware"));
271 Assert.assertTrue(desc.getSoftware().equals("stub software"));
272 Assert.assertTrue(desc.getSerialNumber().equals("123"));
273 Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
274 } catch (ConstructionException e) {
275 Assert.assertTrue(false);
281 public void testGetNodeConnectorStatistics() {
283 Node node = new Node("STUB", 0xCAFE);
284 List<NodeConnectorStatistics> stats = this.manager
285 .getNodeConnectorStatistics(node);
286 NodeConnectorStatistics ns = stats.get(0);
287 Assert.assertTrue(ns.getCollisionCount() == 4);
288 Assert.assertTrue(ns.getReceiveByteCount() == 1000);
289 Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
290 Assert.assertTrue(ns.getReceiveDropCount() == 2);
291 Assert.assertTrue(ns.getReceiveErrorCount() == 3);
292 Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
293 Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
294 Assert.assertTrue(ns.getReceivePacketCount() == 250);
295 Assert.assertTrue(ns.getTransmitByteCount() == 5000);
296 Assert.assertTrue(ns.getTransmitDropCount() == 50);
297 Assert.assertTrue(ns.getTransmitErrorCount() == 10);
298 Assert.assertTrue(ns.getTransmitPacketCount() == 500);
300 NodeConnector nc = ns.getNodeConnector();
301 NodeConnectorStatistics ns2 = this.manager
302 .getNodeConnectorStatistics(nc);
303 Assert.assertTrue(ns2.getCollisionCount() == 4);
304 Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
305 Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
306 Assert.assertTrue(ns2.getReceiveDropCount() == 2);
307 Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
308 Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
309 Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
310 Assert.assertTrue(ns2.getReceivePacketCount() == 250);
311 Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
312 Assert.assertTrue(ns2.getTransmitDropCount() == 50);
313 Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
314 Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
316 } catch (ConstructionException e) {
317 Assert.assertTrue(false);