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