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 StatisticsManagerIntegrationTest {
44 private Logger log = LoggerFactory
45 .getLogger(StatisticsManagerIntegrationTest.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", "1.7.2"),
81 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
82 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
83 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
84 // List all the bundles on which the test case depends
85 mavenBundle("org.opendaylight.controller", "sal",
87 mavenBundle("org.opendaylight.controller",
88 "sal.implementation", "0.4.0-SNAPSHOT"),
89 mavenBundle("org.opendaylight.controller", "statisticsmanager",
91 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation",
93 mavenBundle("org.opendaylight.controller",
94 "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
95 // needed by statisticsmanager
96 mavenBundle("org.opendaylight.controller", "containermanager",
98 mavenBundle("org.opendaylight.controller",
99 "containermanager.implementation", "0.4.0-SNAPSHOT"),
100 mavenBundle("org.opendaylight.controller",
101 "forwardingrulesmanager", "0.4.0-SNAPSHOT"),
103 mavenBundle("org.opendaylight.controller",
104 "clustering.services", "0.4.0-SNAPSHOT"),
105 mavenBundle("org.opendaylight.controller",
106 "clustering.stub", "0.4.0-SNAPSHOT"),
108 // needed by forwardingrulesmanager
109 mavenBundle("org.opendaylight.controller", "switchmanager",
111 mavenBundle("org.opendaylight.controller", "configuration",
114 mavenBundle("org.opendaylight.controller",
115 "configuration.implementation", "0.4.0-SNAPSHOT"),
116 mavenBundle("org.opendaylight.controller", "hosttracker",
119 // needed by hosttracker
120 mavenBundle("org.opendaylight.controller", "topologymanager",
123 mavenBundle("org.jboss.spec.javax.transaction",
124 "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
125 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
126 mavenBundle("org.apache.felix",
127 "org.apache.felix.dependencymanager", "3.1.0"),
131 private String stateToString(int state) {
135 case Bundle.INSTALLED:
137 case Bundle.RESOLVED:
139 case Bundle.UNINSTALLED:
140 return "UNINSTALLED";
142 return "Not CONVERTED";
147 public void areWeReady() {
149 boolean debugit = false;
150 Bundle b[] = bc.getBundles();
151 for (int i = 0; i < b.length; i++) {
152 int state = b[i].getState();
153 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
154 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
155 + stateToString(state));
160 log.debug("Do some debugging because some bundle is "
164 // Assert if true, if false we are good to go!
165 assertFalse(debugit);
167 ServiceReference r = bc.getServiceReference(IStatisticsManager.class
170 this.manager = (IStatisticsManager) bc.getService(r);
172 // If StatisticsManager is null, cannot run tests.
173 assertNotNull(this.manager);
178 public void testGetFlows() {
180 Node node = new Node("STUB", new Integer(0xCAFE));
181 List<FlowOnNode> flows = this.manager.getFlows(node);
182 FlowOnNode fn = flows.get(0);
183 Assert.assertTrue(fn.getByteCount() == 100);
184 Assert.assertTrue(fn.getDurationNanoseconds() == 400);
185 Assert.assertTrue(fn.getDurationSeconds() == 40);
186 Assert.assertTrue(fn.getTableId() == (byte) 0x1);
187 Assert.assertTrue(fn.getPacketCount() == 200);
189 Match match = new Match();
191 match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
192 } catch (UnknownHostException e) {
193 fail("Couldn't create match");
195 Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
196 Assert.assertTrue(fn.getFlow().getActions().get(0).equals(new Drop()));
197 } catch (ConstructionException e) {
198 // Got an unexpected exception
199 Assert.assertTrue(false);
205 public void testGetFlowStatistics() {
206 Flow flow = new Flow();
208 Match match = new Match();
210 match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
211 } catch (UnknownHostException e) {
213 flow.setMatch(match);
214 Action action = new Drop();
216 List<Action> actions = new ArrayList<Action>();
218 flow.setActions(actions);
221 Node node = new Node("STUB", 0xCAFE);
222 FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
223 List<FlowEntry> list = new ArrayList<FlowEntry>();
225 FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
228 Map<Node, List<FlowOnNode>> result = this.manager
229 .getFlowStatisticsForFlowList(null);
230 Assert.assertTrue(result.isEmpty());
231 result = this.manager.getFlowStatisticsForFlowList(list);
232 List<FlowOnNode> results = result.get(node);
233 FlowOnNode fn = results.get(0);
234 Assert.assertTrue(fn.getByteCount() == 100);
235 Assert.assertTrue(fn.getDurationNanoseconds() == 400);
236 Assert.assertTrue(fn.getDurationSeconds() == 40);
237 Assert.assertTrue(fn.getTableId() == (byte) 0x1);
238 Assert.assertTrue(fn.getPacketCount() == 200);
239 Assert.assertTrue(fn.getFlow().equals(flow));
240 }catch(ConstructionException e){
241 Assert.assertTrue(false);
247 public void testGetFlowsNumber() {
249 Node node = new Node("STUB", 0xCAFE);
250 Assert.assertTrue(this.manager.getFlowsNumber(node) == 1);
251 }catch(ConstructionException e){
252 Assert.assertTrue(false);
257 public void testGetNodeDescription() {
259 Node node = new Node("STUB", 0xCAFE);
260 NodeDescription desc = this.manager.getNodeDescription(node);
261 Assert.assertTrue(desc.getDescription().equals(
262 "This is a sample node description"));
263 Assert.assertTrue(desc.getHardware().equals("stub hardware"));
264 Assert.assertTrue(desc.getSoftware().equals("stub software"));
265 Assert.assertTrue(desc.getSerialNumber().equals("123"));
266 Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
267 }catch(ConstructionException e){
268 Assert.assertTrue(false);
274 public void testGetNodeConnectorStatistics() {
276 Node node = new Node("STUB", 0xCAFE);
277 List<NodeConnectorStatistics> stats = this.manager
278 .getNodeConnectorStatistics(node);
279 NodeConnectorStatistics ns = stats.get(0);
280 Assert.assertTrue(ns.getCollisionCount() == 4);
281 Assert.assertTrue(ns.getReceiveByteCount() == 1000);
282 Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
283 Assert.assertTrue(ns.getReceiveDropCount() == 2);
284 Assert.assertTrue(ns.getReceiveErrorCount() == 3);
285 Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
286 Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
287 Assert.assertTrue(ns.getReceivePacketCount() == 250);
288 Assert.assertTrue(ns.getTransmitByteCount() == 5000);
289 Assert.assertTrue(ns.getTransmitDropCount() == 50);
290 Assert.assertTrue(ns.getTransmitErrorCount() == 10);
291 Assert.assertTrue(ns.getTransmitPacketCount() == 500);
293 NodeConnector nc = ns.getNodeConnector();
294 NodeConnectorStatistics ns2 = this.manager
295 .getNodeConnectorStatistics(nc);
296 Assert.assertTrue(ns2.getCollisionCount() == 4);
297 Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
298 Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
299 Assert.assertTrue(ns2.getReceiveDropCount() == 2);
300 Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
301 Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
302 Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
303 Assert.assertTrue(ns2.getReceivePacketCount() == 250);
304 Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
305 Assert.assertTrue(ns2.getTransmitDropCount() == 50);
306 Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
307 Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
309 }catch(ConstructionException e){
310 Assert.assertTrue(false);