Merge changes I8c23739a,Ia0e70828
[controller.git] / opendaylight / statisticsmanager / integrationtest / src / test / java / org / opendaylight / controller / statisticsmanager / internal / StatisticsManagerIT.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.statisticsmanager.internal;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
13 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
14 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
15 import static org.ops4j.pax.exam.CoreOptions.options;
16 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
17 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
18
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.inject.Inject;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
32 import org.opendaylight.controller.sal.action.Action;
33 import org.opendaylight.controller.sal.action.Drop;
34 import org.opendaylight.controller.sal.core.ConstructionException;
35 import org.opendaylight.controller.sal.core.Node;
36 import org.opendaylight.controller.sal.core.NodeConnector;
37 import org.opendaylight.controller.sal.flowprogrammer.Flow;
38 import org.opendaylight.controller.sal.match.Match;
39 import org.opendaylight.controller.sal.match.MatchType;
40 import org.opendaylight.controller.sal.reader.FlowOnNode;
41 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics;
42 import org.opendaylight.controller.sal.reader.NodeDescription;
43 import org.opendaylight.controller.statisticsmanager.IStatisticsManager;
44 import org.ops4j.pax.exam.Option;
45 import org.ops4j.pax.exam.Configuration;
46 import org.ops4j.pax.exam.junit.PaxExam;
47 import org.ops4j.pax.exam.util.PathUtils;
48 import org.osgi.framework.Bundle;
49 import org.osgi.framework.BundleContext;
50 import org.osgi.framework.ServiceReference;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 @RunWith(PaxExam.class)
55 public class StatisticsManagerIT {
56     private Logger log = LoggerFactory
57             .getLogger(StatisticsManagerIT.class);
58     // get the OSGI bundle context
59     @Inject
60     private BundleContext bc;
61
62     private IStatisticsManager manager = null;
63
64     // Configure the OSGi container
65     @Configuration
66     public Option[] config() {
67         return options(
68                 //
69                 systemProperty("logback.configurationFile").value(
70                         "file:" + PathUtils.getBaseDir()
71                                 + "/src/test/resources/logback.xml"),
72                 // To start OSGi console for inspection remotely
73                 systemProperty("osgi.console").value("2401"),
74                 // Set the systemPackages (used by clustering)
75                 systemPackages("sun.reflect", "sun.reflect.misc", "sun.misc"),
76                 // List framework bundles
77                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.console").versionAsInProject(),
78                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.util").versionAsInProject(),
79                 mavenBundle("equinoxSDK381", "org.eclipse.osgi.services").versionAsInProject(),
80                 mavenBundle("equinoxSDK381", "org.eclipse.equinox.ds").versionAsInProject(),
81                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.command").versionAsInProject(),
82                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.runtime").versionAsInProject(),
83                 mavenBundle("equinoxSDK381", "org.apache.felix.gogo.shell").versionAsInProject(),
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.it.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                 mavenBundle("org.opendaylight.controller", "connectionmanager.implementation").
127                     versionAsInProject(),
128                 mavenBundle("org.opendaylight.controller",  "connectionmanager").
129                     versionAsInProject(),
130                 mavenBundle("org.opendaylight.controller",  "sal.connection").
131                     versionAsInProject(),
132                 mavenBundle("org.opendaylight.controller",  "sal.connection.implementation").
133                     versionAsInProject(),
134
135                 // needed by hosttracker
136                 mavenBundle("org.opendaylight.controller", "topologymanager")
137                         .versionAsInProject(),
138                 mavenBundle("org.jboss.spec.javax.transaction",
139                         "jboss-transaction-api_1.1_spec").versionAsInProject(),
140                 mavenBundle("org.apache.commons", "commons-lang3")
141                         .versionAsInProject(),
142                 mavenBundle("org.apache.felix",
143                         "org.apache.felix.dependencymanager")
144                         .versionAsInProject(), junitBundles());
145     }
146
147     private String stateToString(int state) {
148         switch (state) {
149         case Bundle.ACTIVE:
150             return "ACTIVE";
151         case Bundle.INSTALLED:
152             return "INSTALLED";
153         case Bundle.RESOLVED:
154             return "RESOLVED";
155         case Bundle.UNINSTALLED:
156             return "UNINSTALLED";
157         default:
158             return "Not CONVERTED";
159         }
160     }
161
162     @Before
163     public void areWeReady() {
164         assertNotNull(bc);
165         boolean debugit = false;
166         Bundle b[] = bc.getBundles();
167         for (int i = 0; i < b.length; i++) {
168             int state = b[i].getState();
169             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
170                 log.debug("Bundle:" + b[i].getSymbolicName() + " state:"
171                         + stateToString(state));
172                 debugit = true;
173             }
174         }
175         if (debugit) {
176             log.debug("Do some debugging because some bundle is "
177                     + "unresolved");
178         }
179
180         // Assert if true, if false we are good to go!
181         assertFalse(debugit);
182
183         ServiceReference r = bc.getServiceReference(IStatisticsManager.class
184                 .getName());
185         if (r != null) {
186             this.manager = (IStatisticsManager) bc.getService(r);
187         }
188         // If StatisticsManager is null, cannot run tests.
189         assertNotNull(this.manager);
190
191     }
192
193     @Test
194     public void testGetFlows() {
195         try {
196             Node node = new Node("STUB", new Integer(0xCAFE));
197             List<FlowOnNode> flows = this.manager.getFlows(node);
198             FlowOnNode fn = flows.get(0);
199             Assert.assertTrue(fn.getByteCount() == 100);
200             Assert.assertTrue(fn.getDurationNanoseconds() == 400);
201             Assert.assertTrue(fn.getDurationSeconds() == 40);
202             Assert.assertTrue(fn.getTableId() == (byte) 0x1);
203             Assert.assertTrue(fn.getPacketCount() == 200);
204
205             Match match = new Match();
206             try {
207                 match.setField(MatchType.NW_DST,
208                         InetAddress.getByName("1.1.1.1"));
209             } catch (UnknownHostException e) {
210                 fail("Couldn't create match");
211             }
212             Assert.assertTrue(match.equals(fn.getFlow().getMatch()));
213             Assert.assertTrue(fn.getFlow().getActions().get(0)
214                     .equals(new Drop()));
215         } catch (ConstructionException e) {
216             // Got an unexpected exception
217             Assert.assertTrue(false);
218         }
219
220     }
221
222     @Test
223     public void testGetFlowStatistics() {
224         Flow flow = new Flow();
225
226         Match match = new Match();
227         try {
228             match.setField(MatchType.NW_DST, InetAddress.getByName("1.1.1.1"));
229         } catch (UnknownHostException e) {
230         }
231         flow.setMatch(match);
232         Action action = new Drop();
233
234         List<Action> actions = new ArrayList<Action>();
235         actions.add(action);
236         flow.setActions(actions);
237         // as in stub
238         flow.setPriority((short) 3500);
239         flow.setIdleTimeout((short) 1000);
240         flow.setHardTimeout((short) 2000);
241         flow.setId(12345);
242
243         try {
244             Node node = new Node("STUB", 0xCAFE);
245             FlowEntry fe = new FlowEntry("g1", "f1", flow, node);
246             List<FlowEntry> list = new ArrayList<FlowEntry>();
247             list.add(fe);
248             FlowEntry fe2 = new FlowEntry("g1", "f2", flow, node);
249             list.add(fe2);
250
251             Map<Node, List<FlowOnNode>> result = this.manager
252                     .getFlowStatisticsForFlowList(null);
253             Assert.assertTrue(result.isEmpty());
254             result = this.manager.getFlowStatisticsForFlowList(list);
255             List<FlowOnNode> results = result.get(node);
256             FlowOnNode fn = results.get(0);
257             Assert.assertTrue(fn.getByteCount() == 100);
258             Assert.assertTrue(fn.getDurationNanoseconds() == 400);
259             Assert.assertTrue(fn.getDurationSeconds() == 40);
260             Assert.assertTrue(fn.getTableId() == (byte) 0x1);
261             Assert.assertTrue(fn.getPacketCount() == 200);
262             Assert.assertTrue(fn.getFlow().equals(flow));
263         } catch (ConstructionException e) {
264             Assert.assertTrue(false);
265         }
266
267     }
268
269     @Test
270     public void testGetFlowsNumber() {
271         try {
272             Node node = new Node("STUB", 0xCAFE);
273             Assert.assertEquals(21, this.manager.getFlowsNumber(node));
274         }catch(ConstructionException e){
275             Assert.assertTrue(false);
276         }
277     }
278
279     @Test
280     public void testGetNodeDescription() {
281         try {
282             Node node = new Node("STUB", 0xCAFE);
283             NodeDescription desc = this.manager.getNodeDescription(node);
284             Assert.assertTrue(desc.getDescription().equals(
285                     "This is a sample node description"));
286             Assert.assertTrue(desc.getHardware().equals("stub hardware"));
287             Assert.assertTrue(desc.getSoftware().equals("stub software"));
288             Assert.assertTrue(desc.getSerialNumber().equals("123"));
289             Assert.assertTrue(desc.getManufacturer().equals("opendaylight"));
290         } catch (ConstructionException e) {
291             Assert.assertTrue(false);
292         }
293
294     }
295
296     @Test
297     public void testGetNodeConnectorStatistics() {
298         try {
299             Node node = new Node("STUB", 0xCAFE);
300             List<NodeConnectorStatistics> stats = this.manager
301                     .getNodeConnectorStatistics(node);
302             NodeConnectorStatistics ns = stats.get(0);
303             Assert.assertTrue(ns.getCollisionCount() == 4);
304             Assert.assertTrue(ns.getReceiveByteCount() == 1000);
305             Assert.assertTrue(ns.getReceiveCRCErrorCount() == 1);
306             Assert.assertTrue(ns.getReceiveDropCount() == 2);
307             Assert.assertTrue(ns.getReceiveErrorCount() == 3);
308             Assert.assertTrue(ns.getReceiveFrameErrorCount() == 5);
309             Assert.assertTrue(ns.getReceiveOverRunErrorCount() == 6);
310             Assert.assertTrue(ns.getReceivePacketCount() == 250);
311             Assert.assertTrue(ns.getTransmitByteCount() == 5000);
312             Assert.assertTrue(ns.getTransmitDropCount() == 50);
313             Assert.assertTrue(ns.getTransmitErrorCount() == 10);
314             Assert.assertTrue(ns.getTransmitPacketCount() == 500);
315
316             NodeConnector nc = ns.getNodeConnector();
317             NodeConnectorStatistics ns2 = this.manager
318                     .getNodeConnectorStatistics(nc);
319             Assert.assertTrue(ns2.getCollisionCount() == 4);
320             Assert.assertTrue(ns2.getReceiveByteCount() == 1000);
321             Assert.assertTrue(ns2.getReceiveCRCErrorCount() == 1);
322             Assert.assertTrue(ns2.getReceiveDropCount() == 2);
323             Assert.assertTrue(ns2.getReceiveErrorCount() == 3);
324             Assert.assertTrue(ns2.getReceiveFrameErrorCount() == 5);
325             Assert.assertTrue(ns2.getReceiveOverRunErrorCount() == 6);
326             Assert.assertTrue(ns2.getReceivePacketCount() == 250);
327             Assert.assertTrue(ns2.getTransmitByteCount() == 5000);
328             Assert.assertTrue(ns2.getTransmitDropCount() == 50);
329             Assert.assertTrue(ns2.getTransmitErrorCount() == 10);
330             Assert.assertTrue(ns2.getTransmitPacketCount() == 500);
331
332         } catch (ConstructionException e) {
333             Assert.assertTrue(false);
334         }
335     }
336
337 }