Added method for getting non-cached flow statistics
[controller.git] / opendaylight / statisticsmanager / integrationtest / src / test / java / org / opendaylight / controller / statisticsmanager / internal / StatisticsManagerIT.java
1 package org.opendaylight.controller.statisticsmanager.internal;
2
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;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17
18 import javax.inject.Inject;
19
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;
46
47 @RunWith(PaxExam.class)
48 public class StatisticsManagerIT {
49     private Logger log = LoggerFactory
50             .getLogger(StatisticsManagerIT.class);
51     // get the OSGI bundle context
52     @Inject
53     private BundleContext bc;
54
55     private IStatisticsManager manager = null;
56
57     // Configure the OSGi container
58     @Configuration
59     public Option[] config() {
60         return options(
61                 //
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.it.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(),
101
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(),
119                 mavenBundle("org.opendaylight.controller", "connectionmanager.implementation").
120                     versionAsInProject(),
121                 mavenBundle("org.opendaylight.controller",  "connectionmanager").
122                     versionAsInProject(),
123                 mavenBundle("org.opendaylight.controller",  "sal.connection").
124                     versionAsInProject(),
125                 mavenBundle("org.opendaylight.controller",  "sal.connection.implementation").
126                     versionAsInProject(),
127
128                 // needed by hosttracker
129                 mavenBundle("org.opendaylight.controller", "topologymanager")
130                         .versionAsInProject(),
131                 mavenBundle("org.jboss.spec.javax.transaction",
132                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
133                 mavenBundle("org.apache.commons", "commons-lang3")
134                         .versionAsInProject(),
135                 mavenBundle("org.apache.felix",
136                         "org.apache.felix.dependencymanager")
137                         .versionAsInProject(), junitBundles());
138     }
139
140     private String stateToString(int state) {
141         switch (state) {
142         case Bundle.ACTIVE:
143             return "ACTIVE";
144         case Bundle.INSTALLED:
145             return "INSTALLED";
146         case Bundle.RESOLVED:
147             return "RESOLVED";
148         case Bundle.UNINSTALLED:
149             return "UNINSTALLED";
150         default:
151             return "Not CONVERTED";
152         }
153     }
154
155     @Before
156     public void areWeReady() {
157         assertNotNull(bc);
158         boolean debugit = false;
159         Bundle b[] = bc.getBundles();
160         for (int i = 0; i < b.length; i++) {
161             int state = b[i].getState();
162             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
163                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
164                         + stateToString(state));
165                 debugit = true;
166             }
167         }
168         if (debugit) {
169             log.debug("Do some debugging because some bundle is "
170                     + "unresolved");
171         }
172
173         // Assert if true, if false we are good to go!
174         assertFalse(debugit);
175
176         ServiceReference r = bc.getServiceReference(IStatisticsManager.class
177                 .getName());
178         if (r != null) {
179             this.manager = (IStatisticsManager) bc.getService(r);
180         }
181         // If StatisticsManager is null, cannot run tests.
182         assertNotNull(this.manager);
183
184     }
185
186     @Test
187     public void testGetFlows() {
188         try {
189             Node node = new Node("STUB", new Integer(0xCAFE));
190             List<FlowOnNode> flows = this.manager.getFlows(node);
191             FlowOnNode fn = flows.get(0);
192             Assert.assertTrue(fn.getByteCount() == 100);
193             Assert.assertTrue(fn.getDurationNanoseconds() == 400);
194             Assert.assertTrue(fn.getDurationSeconds() == 40);
195             Assert.assertTrue(fn.getTableId() == (byte) 0x1);
196             Assert.assertTrue(fn.getPacketCount() == 200);
197
198             Match match = new Match();
199             try {
200                 match.setField(MatchType.NW_DST,
201                         InetAddress.getByName("1.1.1.1"));
202             } catch (UnknownHostException e) {
203                 fail("Couldn't create match");
204             }
205             Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
206             Assert.assertTrue(fn.getFlow().getActions().get(0)
207                     .equals(new Drop()));
208         } catch (ConstructionException e) {
209             // Got an unexpected exception
210             Assert.assertTrue(false);
211         }
212
213     }
214
215     @Test
216     public void testGetFlowStatistics() {
217         Flow flow = new Flow();
218
219         Match match = new Match();
220         try {
221             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
222         } catch (UnknownHostException e) {
223         }
224         flow.setMatch(match);
225         Action action = new Drop();
226
227         List<Action> actions = new ArrayList<Action>();
228         actions.add(action);
229         flow.setActions(actions);
230         // as in stub
231         flow.setPriority((short) 3500);
232         flow.setIdleTimeout((short) 1000);
233         flow.setHardTimeout((short) 2000);
234         flow.setId(12345);
235
236         try {
237             Node node = new Node("STUB", 0xCAFE);
238             FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
239             List<FlowEntry> list = new ArrayList<FlowEntry>();
240             list.add(fe);
241             FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
242             list.add(fe2);
243
244             Map<Node, List<FlowOnNode>> result = this.manager
245                     .getFlowStatisticsForFlowList(null);
246             Assert.assertTrue(result.isEmpty());
247             result = this.manager.getFlowStatisticsForFlowList(list);
248             List<FlowOnNode> results = result.get(node);
249             FlowOnNode fn = results.get(0);
250             Assert.assertTrue(fn.getByteCount() == 100);
251             Assert.assertTrue(fn.getDurationNanoseconds() == 400);
252             Assert.assertTrue(fn.getDurationSeconds() == 40);
253             Assert.assertTrue(fn.getTableId() == (byte) 0x1);
254             Assert.assertTrue(fn.getPacketCount() == 200);
255             Assert.assertTrue(fn.getFlow().equals(flow));
256         } catch (ConstructionException e) {
257             Assert.assertTrue(false);
258         }
259
260     }
261
262     @Test
263     public void testGetFlowsNumber() {
264         try {
265             Node node = new Node("STUB", 0xCAFE);
266             Assert.assertEquals(21, this.manager.getFlowsNumber(node));
267         }catch(ConstructionException e){
268             Assert.assertTrue(false);
269         }
270     }
271
272     @Test
273     public void testGetNodeDescription() {
274         try {
275             Node node = new Node("STUB", 0xCAFE);
276             NodeDescription desc = this.manager.getNodeDescription(node);
277             Assert.assertTrue(desc.getDescription().equals(
278                     "This is a sample node description"));
279             Assert.assertTrue(desc.getHardware().equals("stub hardware"));
280             Assert.assertTrue(desc.getSoftware().equals("stub software"));
281             Assert.assertTrue(desc.getSerialNumber().equals("123"));
282             Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
283         } catch (ConstructionException e) {
284             Assert.assertTrue(false);
285         }
286
287     }
288
289     @Test
290     public void testGetNodeConnectorStatistics() {
291         try {
292             Node node = new Node("STUB", 0xCAFE);
293             List<NodeConnectorStatistics> stats = this.manager
294                     .getNodeConnectorStatistics(node);
295             NodeConnectorStatistics ns = stats.get(0);
296             Assert.assertTrue(ns.getCollisionCount() == 4);
297             Assert.assertTrue(ns.getReceiveByteCount() == 1000);
298             Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
299             Assert.assertTrue(ns.getReceiveDropCount() == 2);
300             Assert.assertTrue(ns.getReceiveErrorCount() == 3);
301             Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
302             Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
303             Assert.assertTrue(ns.getReceivePacketCount() == 250);
304             Assert.assertTrue(ns.getTransmitByteCount() == 5000);
305             Assert.assertTrue(ns.getTransmitDropCount() == 50);
306             Assert.assertTrue(ns.getTransmitErrorCount() == 10);
307             Assert.assertTrue(ns.getTransmitPacketCount() == 500);
308
309             NodeConnector nc = ns.getNodeConnector();
310             NodeConnectorStatistics ns2 = this.manager
311                     .getNodeConnectorStatistics(nc);
312             Assert.assertTrue(ns2.getCollisionCount() == 4);
313             Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
314             Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
315             Assert.assertTrue(ns2.getReceiveDropCount() == 2);
316             Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
317             Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
318             Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
319             Assert.assertTrue(ns2.getReceivePacketCount() == 250);
320             Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
321             Assert.assertTrue(ns2.getTransmitDropCount() == 50);
322             Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
323             Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
324
325         } catch (ConstructionException e) {
326             Assert.assertTrue(false);
327         }
328     }
329
330 }