Merge changes I029118f9,I4a0320b9
[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 java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Map;
8
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;
14
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.*;
38
39 import org.ops4j.pax.exam.Option;
40 import org.ops4j.pax.exam.util.PathUtils;
41
42 @RunWith(PaxExam.class)
43 public class StatisticsManagerIT {
44     private Logger log = LoggerFactory
45             .getLogger(StatisticsManagerIT.class);
46     // get the OSGI bundle context
47     @Inject
48     private BundleContext bc;
49
50     private IStatisticsManager manager = null;
51
52     // Configure the OSGi container
53     @Configuration
54     public Option[] config() {
55         return options(
56                 //
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(),
106
107                 mavenBundle("org.opendaylight.controller",
108                         "clustering.services").versionAsInProject(),
109                 mavenBundle("org.opendaylight.controller", "clustering.stub")
110                         .versionAsInProject(),
111
112                 // needed by forwardingrulesmanager
113                 mavenBundle("org.opendaylight.controller", "switchmanager")
114                         .versionAsInProject(),
115                 mavenBundle("org.opendaylight.controller", "configuration")
116                         .versionAsInProject(),
117
118                 mavenBundle("org.opendaylight.controller",
119                         "configuration.implementation").versionAsInProject(),
120                 mavenBundle("org.opendaylight.controller", "hosttracker")
121                         .versionAsInProject(),
122
123                 // needed by hosttracker
124                 mavenBundle("org.opendaylight.controller", "topologymanager")
125                         .versionAsInProject(),
126
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());
134     }
135
136     private String stateToString(int state) {
137         switch (state) {
138         case Bundle.ACTIVE:
139             return "ACTIVE";
140         case Bundle.INSTALLED:
141             return "INSTALLED";
142         case Bundle.RESOLVED:
143             return "RESOLVED";
144         case Bundle.UNINSTALLED:
145             return "UNINSTALLED";
146         default:
147             return "Not CONVERTED";
148         }
149     }
150
151     @Before
152     public void areWeReady() {
153         assertNotNull(bc);
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));
161                 debugit = true;
162             }
163         }
164         if (debugit) {
165             log.debug("Do some debugging because some bundle is "
166                     + "unresolved");
167         }
168
169         // Assert if true, if false we are good to go!
170         assertFalse(debugit);
171
172         ServiceReference r = bc.getServiceReference(IStatisticsManager.class
173                 .getName());
174         if (r != null) {
175             this.manager = (IStatisticsManager) bc.getService(r);
176         }
177         // If StatisticsManager is null, cannot run tests.
178         assertNotNull(this.manager);
179
180     }
181
182     @Test
183     public void testGetFlows() {
184         try {
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);
193
194             Match match = new Match();
195             try {
196                 match.setField(MatchType.NW_DST,
197                         InetAddress.getByName("1.1.1.1"));
198             } catch (UnknownHostException e) {
199                 fail("Couldn't create match");
200             }
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);
207         }
208
209     }
210
211     @Test
212     public void testGetFlowStatistics() {
213         Flow flow = new Flow();
214
215         Match match = new Match();
216         try {
217             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
218         } catch (UnknownHostException e) {
219         }
220         flow.setMatch(match);
221         Action action = new Drop();
222
223         List<Action> actions = new ArrayList<Action>();
224         actions.add(action);
225         flow.setActions(actions);
226
227         try {
228             Node node = new Node("STUB", 0xCAFE);
229             FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
230             List<FlowEntry> list = new ArrayList<FlowEntry>();
231             list.add(fe);
232             FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
233             list.add(fe2);
234
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);
249         }
250
251     }
252
253     @Test
254     public void testGetFlowsNumber() {
255         try {
256             Node node = new Node("STUB", 0xCAFE);
257             Assert.assertEquals(21, this.manager.getFlowsNumber(node));
258         }catch(ConstructionException e){
259             Assert.assertTrue(false);
260         }
261     }
262
263     @Test
264     public void testGetNodeDescription() {
265         try {
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);
276         }
277
278     }
279
280     @Test
281     public void testGetNodeConnectorStatistics() {
282         try {
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);
299
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);
315
316         } catch (ConstructionException e) {
317             Assert.assertTrue(false);
318         }
319     }
320
321 }