Fixed some dependencies for
[controller.git] / opendaylight / statisticsmanager / integrationtest / src / test / java / org / opendaylight / controller / statisticsmanager / internal / StatisticsManagerIntegrationTest.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.Node;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.flowprogrammer.Flow;
25 import org.opendaylight.controller.sal.match.Match;
26 import org.opendaylight.controller.sal.match.MatchType;
27 import org.opendaylight.controller.sal.reader.FlowOnNode;
28 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
29 import org.opendaylight.controller.sal.reader.NodeDescription;
30 import org.opendaylight.controller.sal.utils.NodeCreator;
31 import org.opendaylight.controller.statisticsmanager.*;
32 import org.ops4j.pax.exam.junit.PaxExam;
33 import org.osgi.framework.BundleContext;
34 import static org.junit.Assert.*;
35 import org.ops4j.pax.exam.junit.Configuration;
36 import static org.ops4j.pax.exam.CoreOptions.*;
37
38 import org.ops4j.pax.exam.Option;
39 import org.ops4j.pax.exam.util.PathUtils;
40
41 @RunWith(PaxExam.class)
42 public class StatisticsManagerIntegrationTest {
43     private Logger log = LoggerFactory
44             .getLogger(StatisticsManagerIntegrationTest.class);
45     // get the OSGI bundle context
46     @Inject
47     private BundleContext bc;
48
49     private IStatisticsManager manager = null;
50
51     // Configure the OSGi container
52     @Configuration
53     public Option[] config() {
54         return options(
55                 //
56                 systemProperty("logback.configurationFile").value(
57                         "file:" + PathUtils.getBaseDir()
58                                 + "/src/test/resources/logback.xml"),
59                 // To start OSGi console for inspection remotely
60                 systemProperty("osgi.console").value("2401"),
61                 // Set the systemPackages (used by clustering)
62                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
63                 // List framework bundles
64                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console",
65                         "1.0.0.v20120522-1841"),
66                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util",
67                         "1.0.400.v20120522-2049"),
68                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services",
69                         "3.3.100.v20120522-1822"),
70                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds",
71                         "1.4.0.v20120522-1841"),
72                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command",
73                         "0.8.0.v201108120515"),
74                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime",
75                         "0.8.0.v201108120515"),
76                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell",
77                         "0.8.0.v201110170705"),
78                 // List logger bundles
79                 mavenBundle("org.slf4j", "slf4j-api", "1.7.2"),
80                 mavenBundle("org.slf4j", "log4j-over-slf4j", "1.7.2"),
81                 mavenBundle("ch.qos.logback", "logback-core", "1.0.9"),
82                 mavenBundle("ch.qos.logback", "logback-classic", "1.0.9"),
83                 // List all the bundles on which the test case depends
84                 mavenBundle("org.opendaylight.controller", "sal",
85                         "0.4.0-SNAPSHOT"),
86                 mavenBundle("org.opendaylight.controller",
87                         "sal.implementation", "0.4.0-SNAPSHOT"),
88                 mavenBundle("org.opendaylight.controller", "statisticsmanager",
89                         "0.4.0-SNAPSHOT"),
90                 mavenBundle("org.opendaylight.controller", "statisticsmanager.implementation",
91                                 "0.4.0-SNAPSHOT"),
92                 mavenBundle("org.opendaylight.controller",
93                         "protocol_plugins.stub", "0.4.0-SNAPSHOT"),
94                 // needed by statisticsmanager
95                 mavenBundle("org.opendaylight.controller", "containermanager",
96                         "0.4.0-SNAPSHOT"),
97                 mavenBundle("org.opendaylight.controller",
98                         "containermanager.implementation", "0.4.0-SNAPSHOT"),
99                 mavenBundle("org.opendaylight.controller",
100                         "forwardingrulesmanager", "0.4.0-SNAPSHOT"),
101
102                 mavenBundle("org.opendaylight.controller",
103                         "clustering.services", "0.4.0-SNAPSHOT"),
104                 mavenBundle("org.opendaylight.controller",
105                         "clustering.stub", "0.4.0-SNAPSHOT"),
106
107                 // needed by forwardingrulesmanager
108                 mavenBundle("org.opendaylight.controller", "switchmanager",
109                         "0.4.0-SNAPSHOT"),
110                 mavenBundle("org.opendaylight.controller", "configuration",
111                         "0.4.0-SNAPSHOT"),
112
113                 mavenBundle("org.opendaylight.controller",
114                         "configuration.implementation", "0.4.0-SNAPSHOT"),
115                 mavenBundle("org.opendaylight.controller", "hosttracker",
116                         "0.4.0-SNAPSHOT"),
117
118                 // needed by hosttracker
119                 mavenBundle("org.opendaylight.controller", "topologymanager",
120                         "0.4.0-SNAPSHOT"),
121
122                 mavenBundle("org.jboss.spec.javax.transaction",
123                         "jboss-transaction-api_1.1_spec", "1.0.1.Final"),
124                 mavenBundle("org.apache.commons", "commons-lang3", "3.1"),
125                 mavenBundle("org.apache.felix",
126                         "org.apache.felix.dependencymanager", "3.1.0"),
127                 junitBundles());
128     }
129
130     private String stateToString(int state) {
131         switch (state) {
132         case Bundle.ACTIVE:
133             return "ACTIVE";
134         case Bundle.INSTALLED:
135             return "INSTALLED";
136         case Bundle.RESOLVED:
137             return "RESOLVED";
138         case Bundle.UNINSTALLED:
139             return "UNINSTALLED";
140         default:
141             return "Not CONVERTED";
142         }
143     }
144
145     @Before
146     public void areWeReady() {
147         assertNotNull(bc);
148         boolean debugit = false;
149         Bundle b[] = bc.getBundles();
150         for (int i = 0; i < b.length; i++) {
151             int state = b[i].getState();
152             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
153                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
154                         + stateToString(state));
155                 debugit = true;
156             }
157         }
158         if (debugit) {
159             log.debug("Do some debugging because some bundle is "
160                     + "unresolved");
161         }
162
163         // Assert if true, if false we are good to go!
164         assertFalse(debugit);
165
166         ServiceReference r = bc.getServiceReference(IStatisticsManager.class
167                 .getName());
168         if (r != null) {
169             this.manager = (IStatisticsManager) bc.getService(r);
170         }
171         // If StatisticsManager is null, cannot run tests.
172         assertNotNull(this.manager);
173
174     }
175
176     @Test
177     public void testGetFlows() {
178         Node node = NodeCreator.createOFNode(1L);
179         List<FlowOnNode> flows = this.manager.getFlows(node);
180         FlowOnNode fn = flows.get(0);
181         Assert.assertTrue(fn.getByteCount() == 100);
182         Assert.assertTrue(fn.getDurationNanoseconds() == 400);
183         Assert.assertTrue(fn.getDurationSeconds() == 40);
184         Assert.assertTrue(fn.getTableId() == (byte) 0x1);
185         Assert.assertTrue(fn.getPacketCount() == 200);
186
187         Match match = new Match();
188         try {
189             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
190         } catch (UnknownHostException e) {
191             fail("Couldn't create match");
192         }
193         Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
194         Assert.assertTrue(fn.getFlow().getActions().get(0).equals(new Drop()));
195     }
196
197     @Test
198     public void testGetFlowStatistics() {
199         Flow flow = new Flow();
200
201         Match match = new Match();
202         try {
203             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
204         } catch (UnknownHostException e) {
205         }
206         flow.setMatch(match);
207         Action action = new Drop();
208
209         List<Action> actions = new ArrayList<Action>();
210         actions.add(action);
211         flow.setActions(actions);
212
213         Node node = NodeCreator.createOFNode(1L);
214         FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
215         List<FlowEntry> list = new ArrayList<FlowEntry>();
216         list.add(fe);
217         FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
218         list.add(fe2);
219
220         Map<Node, List<FlowOnNode>> result = this.manager
221                 .getFlowStatisticsForFlowList(null);
222         Assert.assertTrue(result.isEmpty());
223         result = this.manager.getFlowStatisticsForFlowList(list);
224         List<FlowOnNode> results = result.get(node);
225         FlowOnNode fn = results.get(0);
226         Assert.assertTrue(fn.getByteCount() == 100);
227         Assert.assertTrue(fn.getDurationNanoseconds() == 400);
228         Assert.assertTrue(fn.getDurationSeconds() == 40);
229         Assert.assertTrue(fn.getTableId() == (byte) 0x1);
230         Assert.assertTrue(fn.getPacketCount() == 200);
231         Assert.assertTrue(fn.getFlow().equals(flow));
232
233     }
234
235     @Test
236     public void testGetFlowsNumber() {
237         Node node = NodeCreator.createOFNode(1L);
238         Assert.assertTrue(this.manager.getFlowsNumber(node) == 1);
239     }
240
241     @Test
242     public void testGetNodeDescription() {
243         Node node = NodeCreator.createOFNode(1L);
244         NodeDescription desc = this.manager.getNodeDescription(node);
245         Assert.assertTrue(desc.getDescription().equals(
246                 "This is a sample node description"));
247         Assert.assertTrue(desc.getHardware().equals("stub hardware"));
248         Assert.assertTrue(desc.getSoftware().equals("stub software"));
249         Assert.assertTrue(desc.getSerialNumber().equals("123"));
250         Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
251
252     }
253
254     @Test
255     public void testGetNodeConnectorStatistics() {
256         Node node = NodeCreator.createOFNode(1L);
257         List<NodeConnectorStatistics> stats = this.manager
258                 .getNodeConnectorStatistics(node);
259         NodeConnectorStatistics ns = stats.get(0);
260         Assert.assertTrue(ns.getCollisionCount() == 4);
261         Assert.assertTrue(ns.getReceiveByteCount() == 1000);
262         Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
263         Assert.assertTrue(ns.getReceiveDropCount() == 2);
264         Assert.assertTrue(ns.getReceiveErrorCount() == 3);
265         Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
266         Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
267         Assert.assertTrue(ns.getReceivePacketCount() == 250);
268         Assert.assertTrue(ns.getTransmitByteCount() == 5000);
269         Assert.assertTrue(ns.getTransmitDropCount() == 50);
270         Assert.assertTrue(ns.getTransmitErrorCount() == 10);
271         Assert.assertTrue(ns.getTransmitPacketCount() == 500);
272
273         NodeConnector nc = ns.getNodeConnector();
274         NodeConnectorStatistics ns2 = this.manager
275                 .getNodeConnectorStatistics(nc);
276         Assert.assertTrue(ns2.getCollisionCount() == 4);
277         Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
278         Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
279         Assert.assertTrue(ns2.getReceiveDropCount() == 2);
280         Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
281         Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
282         Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
283         Assert.assertTrue(ns2.getReceivePacketCount() == 250);
284         Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
285         Assert.assertTrue(ns2.getTransmitDropCount() == 50);
286         Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
287         Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
288     }
289
290 }