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