00629a43b11470f66b6b1f2045e4ae3d3aabbf76
[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",
71                         "1.0.0.v20120522-1841"),
72                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
73                         "1.0.400.v20120522-2049"),
74                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
75                         "3.3.100.v20120522-1822"),
76                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
77                         "1.4.0.v20120522-1841"),
78                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
79                         "0.8.0.v201108120515"),
80                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
81                         "0.8.0.v201108120515"),
82                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
83                         "0.8.0.v201110170705"),
84                 // List logger bundles
85                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
86                 mavenBundle("org.slf4j", "log4j-over-slf4j")
87                         .versionAsInProject(),
88                 mavenBundle("ch.qos.logback", "logback-core")
89                         .versionAsInProject(),
90                 mavenBundle("ch.qos.logback", "logback-classic")
91                         .versionAsInProject(),
92                 // needed by statisticsmanager
93                 mavenBundle("org.opendaylight.controller", "containermanager")
94                     .versionAsInProject(),
95                 mavenBundle("org.opendaylight.controller", "containermanager.implementation")
96                     .versionAsInProject(),
97                 mavenBundle("org.opendaylight.controller", "clustering.services")
98                     .versionAsInProject(),
99                 mavenBundle("org.opendaylight.controller", "clustering.stub")
100                     .versionAsInProject(),
101                 // needed by forwardingrulesmanager
102                 mavenBundle("org.opendaylight.controller", "configuration")
103                     .versionAsInProject(),
104                 mavenBundle("org.opendaylight.controller", "configuration.implementation")
105                     .versionAsInProject(),
106                 mavenBundle("org.opendaylight.controller", "hosttracker")
107                     .versionAsInProject(),
108
109                 // List all the bundles on which the test case depends
110                 mavenBundle("org.opendaylight.controller", "sal")
111                     .versionAsInProject(),
112                 mavenBundle("org.opendaylight.controller", "sal.implementation")
113                     .versionAsInProject(),
114                 mavenBundle("org.opendaylight.controller", "protocol_plugins.stub")
115                     .versionAsInProject(),
116                 mavenBundle("org.opendaylight.controller", "switchmanager")
117                     .versionAsInProject(),
118                 mavenBundle("org.opendaylight.controller", "switchmanager.implementation")
119                     .versionAsInProject(),
120                 mavenBundle("org.opendaylight.controller", "statisticsmanager")
121                     .versionAsInProject(),
122                 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation")
123                     .versionAsInProject(),
124                 mavenBundle("org.opendaylight.controller", "forwardingrulesmanager")
125                     .versionAsInProject(),
126
127                 // needed by hosttracker
128                 mavenBundle("org.opendaylight.controller", "topologymanager")
129                         .versionAsInProject(),
130                 mavenBundle("org.jboss.spec.javax.transaction",
131                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
132                 mavenBundle("org.apache.commons", "commons-lang3")
133                         .versionAsInProject(),
134                 mavenBundle("org.apache.felix",
135                         "org.apache.felix.dependencymanager")
136                         .versionAsInProject(), junitBundles());
137     }
138
139     private String stateToString(int state) {
140         switch (state) {
141         case Bundle.ACTIVE:
142             return "ACTIVE";
143         case Bundle.INSTALLED:
144             return "INSTALLED";
145         case Bundle.RESOLVED:
146             return "RESOLVED";
147         case Bundle.UNINSTALLED:
148             return "UNINSTALLED";
149         default:
150             return "Not CONVERTED";
151         }
152     }
153
154     @Before
155     public void areWeReady() {
156         assertNotNull(bc);
157         boolean debugit = false;
158         Bundle b[] = bc.getBundles();
159         for (int i = 0; i < b.length; i++) {
160             int state = b[i].getState();
161             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
162                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
163                         + stateToString(state));
164                 debugit = true;
165             }
166         }
167         if (debugit) {
168             log.debug("Do some debugging because some bundle is "
169                     + "unresolved");
170         }
171
172         // Assert if true, if false we are good to go!
173         assertFalse(debugit);
174
175         ServiceReference r = bc.getServiceReference(IStatisticsManager.class
176                 .getName());
177         if (r != null) {
178             this.manager = (IStatisticsManager) bc.getService(r);
179         }
180         // If StatisticsManager is null, cannot run tests.
181         assertNotNull(this.manager);
182
183     }
184
185     @Test
186     public void testGetFlows() {
187         try {
188             Node node = new Node("STUB", new Integer(0xCAFE));
189             List<FlowOnNode> flows = this.manager.getFlows(node);
190             FlowOnNode fn = flows.get(0);
191             Assert.assertTrue(fn.getByteCount() == 100);
192             Assert.assertTrue(fn.getDurationNanoseconds() == 400);
193             Assert.assertTrue(fn.getDurationSeconds() == 40);
194             Assert.assertTrue(fn.getTableId() == (byte) 0x1);
195             Assert.assertTrue(fn.getPacketCount() == 200);
196
197             Match match = new Match();
198             try {
199                 match.setField(MatchType.NW_DST,
200                         InetAddress.getByName("1.1.1.1"));
201             } catch (UnknownHostException e) {
202                 fail("Couldn't create match");
203             }
204             Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
205             Assert.assertTrue(fn.getFlow().getActions().get(0)
206                     .equals(new Drop()));
207         } catch (ConstructionException e) {
208             // Got an unexpected exception
209             Assert.assertTrue(false);
210         }
211
212     }
213
214     @Test
215     public void testGetFlowStatistics() {
216         Flow flow = new Flow();
217
218         Match match = new Match();
219         try {
220             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
221         } catch (UnknownHostException e) {
222         }
223         flow.setMatch(match);
224         Action action = new Drop();
225
226         List<Action> actions = new ArrayList<Action>();
227         actions.add(action);
228         flow.setActions(actions);
229         // as in stub
230         flow.setPriority((short) 3500);
231         flow.setIdleTimeout((short) 1000);
232         flow.setHardTimeout((short) 2000);
233         flow.setId(12345);
234
235         try {
236             Node node = new Node("STUB", 0xCAFE);
237             FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
238             List<FlowEntry> list = new ArrayList<FlowEntry>();
239             list.add(fe);
240             FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
241             list.add(fe2);
242
243             Map<Node, List<FlowOnNode>> result = this.manager
244                     .getFlowStatisticsForFlowList(null);
245             Assert.assertTrue(result.isEmpty());
246             result = this.manager.getFlowStatisticsForFlowList(list);
247             List<FlowOnNode> results = result.get(node);
248             FlowOnNode fn = results.get(0);
249             Assert.assertTrue(fn.getByteCount() == 100);
250             Assert.assertTrue(fn.getDurationNanoseconds() == 400);
251             Assert.assertTrue(fn.getDurationSeconds() == 40);
252             Assert.assertTrue(fn.getTableId() == (byte) 0x1);
253             Assert.assertTrue(fn.getPacketCount() == 200);
254             Assert.assertTrue(fn.getFlow().equals(flow));
255         } catch (ConstructionException e) {
256             Assert.assertTrue(false);
257         }
258
259     }
260
261     @Test
262     public void testGetFlowsNumber() {
263         try {
264             Node node = new Node("STUB", 0xCAFE);
265             Assert.assertEquals(21, this.manager.getFlowsNumber(node));
266         }catch(ConstructionException e){
267             Assert.assertTrue(false);
268         }
269     }
270
271     @Test
272     public void testGetNodeDescription() {
273         try {
274             Node node = new Node("STUB", 0xCAFE);
275             NodeDescription desc = this.manager.getNodeDescription(node);
276             Assert.assertTrue(desc.getDescription().equals(
277                     "This is a sample node description"));
278             Assert.assertTrue(desc.getHardware().equals("stub hardware"));
279             Assert.assertTrue(desc.getSoftware().equals("stub software"));
280             Assert.assertTrue(desc.getSerialNumber().equals("123"));
281             Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
282         } catch (ConstructionException e) {
283             Assert.assertTrue(false);
284         }
285
286     }
287
288     @Test
289     public void testGetNodeConnectorStatistics() {
290         try {
291             Node node = new Node("STUB", 0xCAFE);
292             List<NodeConnectorStatistics> stats = this.manager
293                     .getNodeConnectorStatistics(node);
294             NodeConnectorStatistics ns = stats.get(0);
295             Assert.assertTrue(ns.getCollisionCount() == 4);
296             Assert.assertTrue(ns.getReceiveByteCount() == 1000);
297             Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
298             Assert.assertTrue(ns.getReceiveDropCount() == 2);
299             Assert.assertTrue(ns.getReceiveErrorCount() == 3);
300             Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
301             Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
302             Assert.assertTrue(ns.getReceivePacketCount() == 250);
303             Assert.assertTrue(ns.getTransmitByteCount() == 5000);
304             Assert.assertTrue(ns.getTransmitDropCount() == 50);
305             Assert.assertTrue(ns.getTransmitErrorCount() == 10);
306             Assert.assertTrue(ns.getTransmitPacketCount() == 500);
307
308             NodeConnector nc = ns.getNodeConnector();
309             NodeConnectorStatistics ns2 = this.manager
310                     .getNodeConnectorStatistics(nc);
311             Assert.assertTrue(ns2.getCollisionCount() == 4);
312             Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
313             Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
314             Assert.assertTrue(ns2.getReceiveDropCount() == 2);
315             Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
316             Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
317             Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
318             Assert.assertTrue(ns2.getReceivePacketCount() == 250);
319             Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
320             Assert.assertTrue(ns2.getTransmitDropCount() == 50);
321             Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
322             Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
323
324         } catch (ConstructionException e) {
325             Assert.assertTrue(false);
326         }
327     }
328
329 }