Skip to content
Snippets Groups Projects
Commit 0b7f7ff4 authored by QingWang0909's avatar QingWang0909
Browse files

Update : Fix a bug for StatisticCollector.java causes by a change of OpenFlow...

Update : Fix a bug for StatisticCollector.java causes by a change of OpenFlow protocols, with Parameterized JUnit test
parent ba5479c8
No related branches found
No related tags found
No related merge requests found
...@@ -124,6 +124,17 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic ...@@ -124,6 +124,17 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic
} }
} }
/* This is the original Code with Bugs */
// protected long getSpeed(NodePortTuple npt) {
// IOFSwitch sw = switchService.getSwitch(npt.getNodeId());
// long speed = 0;
// if(sw != null){
// speed = sw.getPort(npt.getPortId()).getCurrSpeed();
// }
// return speed;
// }
/* Fix bug from here */
protected long getSpeed(NodePortTuple npt) { protected long getSpeed(NodePortTuple npt) {
IOFSwitch sw = switchService.getSwitch(npt.getNodeId()); IOFSwitch sw = switchService.getSwitch(npt.getNodeId());
long speed = 0; long speed = 0;
...@@ -160,6 +171,7 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic ...@@ -160,6 +171,7 @@ public class StatisticsCollector implements IFloodlightModule, IStatisticsServic
return speed; return speed;
} }
} }
/** /**
......
...@@ -9,39 +9,38 @@ import net.floodlightcontroller.core.types.NodePortTuple; ...@@ -9,39 +9,38 @@ import net.floodlightcontroller.core.types.NodePortTuple;
import net.floodlightcontroller.test.FloodlightTestCase; import net.floodlightcontroller.test.FloodlightTestCase;
import org.easymock.EasyMock; import org.easymock.EasyMock;
import org.projectfloodlight.openflow.protocol.*; import org.projectfloodlight.openflow.protocol.*;
import static org.projectfloodlight.openflow.protocol.OFVersion.*;
import org.projectfloodlight.openflow.types.DatapathId; import org.projectfloodlight.openflow.types.DatapathId;
import org.projectfloodlight.openflow.types.OFPort; import org.projectfloodlight.openflow.types.OFPort;
import org.projectfloodlight.openflow.protocol.OFFactories; import org.projectfloodlight.openflow.protocol.OFFactories;
import net.floodlightcontroller.threadpool.IThreadPoolService; import net.floodlightcontroller.threadpool.IThreadPoolService;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import static org.easymock.EasyMock.*; import static org.easymock.EasyMock.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.*; import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/** /**
* Created by qing wang on 5/18/17. * Created by qing wang on 5/18/17.
*/ */
@RunWith(Parameterized.class)
public class StatisticsTest extends FloodlightTestCase { public class GetCurrentPortSpeedTest extends FloodlightTestCase {
private FloodlightContext cntx; private FloodlightContext cntx;
private FloodlightModuleContext fmc; private FloodlightModuleContext fmc;
private MockThreadPoolService threadpool; private MockThreadPoolService threadpool;
private IOFSwitchService switchService; private IOFSwitchService switchService;
private StatisticsCollector statsCollector;
private static final OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10);
private static final OFFactory factory11 = OFFactories.getFactory(OFVersion.OF_11); private static final OFFactory factory11 = OFFactories.getFactory(OFVersion.OF_11);
private static final OFFactory factory12 = OFFactories.getFactory(OFVersion.OF_12); private static final OFFactory factory12 = OFFactories.getFactory(OFVersion.OF_12);
private static final OFFactory factory13 = OFFactories.getFactory(OFVersion.OF_13); private static final OFFactory factory13 = OFFactories.getFactory(OFVersion.OF_13);
private static final OFFactory factory14 = OFFactories.getFactory(OFVersion.OF_14); private static final OFFactory factory14 = OFFactories.getFactory(OFVersion.OF_14);
private static final OFFactory factory15 = OFFactories.getFactory(OFVersion.OF_15); private static final OFFactory factory15 = OFFactories.getFactory(OFVersion.OF_15);
private StatisticsCollector statsCollector; private static OFFactory inputFactory;
public static IOFSwitch sw_OF11, sw_OF12, sw_OF13, sw_OF14, sw_OF15; private static Long expectedSpeed;
private DatapathId switchDPID = DatapathId.of(1L);
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
...@@ -62,40 +61,65 @@ public class StatisticsTest extends FloodlightTestCase { ...@@ -62,40 +61,65 @@ public class StatisticsTest extends FloodlightTestCase {
statsCollector.init(fmc); statsCollector.init(fmc);
threadpool.startUp(fmc); threadpool.startUp(fmc);
// Create OpenFlow11 Mock Switch }
sw_OF11 = EasyMock.createMock(IOFSwitch.class);
sw_OF11 = getSwitchByOFVersion(factory11); /**
* This constructor will be called for each row of test data collection
* @param inputFactory
* @param expectedSpeed
*/
public GetCurrentPortSpeedTest(OFFactory inputFactory, Long expectedSpeed) {
this.inputFactory = inputFactory;
this.expectedSpeed = expectedSpeed;
}
// Create OpenFlow12 Mock Switch /**
sw_OF12 = EasyMock.createMock(IOFSwitch.class); * A Collection of Junit Test with various "inputFactory" and "expectedSpeed"
sw_OF12 = getSwitchByOFVersion(factory12); * @return
*/
@Parameterized.Parameters
public static Iterable<Object[]> testData() {
return Arrays.asList(new Object[][] {
{ factory11, 100L },
{ factory12, 100L },
{ factory13, 100L },
{ factory14, 100L },
{ factory15, 100L },
});
}
// Create OpenFlow13 Mock Switch /**
sw_OF13 = EasyMock.createMock(IOFSwitch.class); * Test getSpeed() method works with different Openflow versions
sw_OF13 = getSwitchByOFVersion(factory13); *
* @throws Exception
*/
@Test
public void testGetCurrentPortSpeed() throws Exception {
IOFSwitch sw = getSwitchByOFVersion(inputFactory);
NodePortTuple npt = new NodePortTuple(DatapathId.of(1), OFPort.of(1));
Map<DatapathId, IOFSwitch> switchMap = new HashMap<>();
switchMap.put(sw.getId(), sw);
getMockSwitchService().setSwitches(switchMap);
// Create OpenFlow14 Mock Switch StatisticsCollector.PortStatsCollector statsSpeedCollector = statsCollector.new PortStatsCollector();
sw_OF14 = EasyMock.createMock(IOFSwitch.class); Long speed = statsSpeedCollector.getSpeed(npt);
sw_OF14 = getSwitchByOFVersion(factory14);
// Create OpenFlow15 Mock Switch assertEquals(speed, expectedSpeed);
sw_OF15 = EasyMock.createMock(IOFSwitch.class);
sw_OF15 = getSwitchByOFVersion(factory15);
} }
private IOFSwitch getSwitchByOFVersion(OFFactory factory) { private IOFSwitch getSwitchByOFVersion(OFFactory inputFactory) {
IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); IOFSwitch sw = EasyMock.createMock(IOFSwitch.class);
reset(sw);
expect(sw.getId()).andReturn(DatapathId.of(1L)).anyTimes();
expect(sw.getOFFactory()).andReturn(inputFactory).anyTimes();
OFVersion openflowVer = factory.getVersion(); OFVersion factoryVersion = inputFactory.getVersion();
switch (openflowVer) { switch (factoryVersion){
case OF_11: case OF_11:
case OF_12: case OF_12:
case OF_13: case OF_13:
reset(sw); expect(sw.getPort(OFPort.of(1))).andReturn(inputFactory.buildPortDesc()
expect(sw.getId()).andReturn(switchDPID).anyTimes();
expect(sw.getOFFactory()).andReturn(factory).anyTimes();
expect(sw.getPort(OFPort.of(1))).andReturn(factory.buildPortDesc()
.setPortNo(OFPort.of(1)) .setPortNo(OFPort.of(1))
.setName("eth1") .setName("eth1")
.setCurrSpeed(100L) .setCurrSpeed(100L)
...@@ -105,124 +129,17 @@ public class StatisticsTest extends FloodlightTestCase { ...@@ -105,124 +129,17 @@ public class StatisticsTest extends FloodlightTestCase {
case OF_14: case OF_14:
case OF_15: case OF_15:
reset(sw); expect(sw.getPort(OFPort.of(1))).andReturn(inputFactory.buildPortDesc()
expect(sw.getId()).andReturn(switchDPID).anyTimes();
expect(sw.getOFFactory()).andReturn(factory).anyTimes();
expect(sw.getPort(OFPort.of(1))).andReturn(factory.buildPortDesc()
.setPortNo(OFPort.of(1)) .setPortNo(OFPort.of(1))
.setName("eth1") .setName("eth1")
.setProperties(Collections.singletonList(factory.buildPortDescPropEthernet().setCurrSpeed(100L).build())) .setProperties(Collections.singletonList(inputFactory.buildPortDescPropEthernet().
setCurrSpeed(100L).
build()))
.build()).anyTimes(); .build()).anyTimes();
replay(sw); replay(sw);
break;
default:
break;
} }
return sw; return sw;
} }
/**
* Test getSpeed() method can work with Openflow 1.1
*
* @throws Exception
*/
@Test
public void testGetCurrentPortSpeedOF11() throws Exception {
NodePortTuple npt = new NodePortTuple(sw_OF11.getId(), OFPort.of(1));
Map<DatapathId, IOFSwitch> switchMap = new HashMap<>();
switchMap.put(sw_OF11.getId(), sw_OF11);
getMockSwitchService().setSwitches(switchMap);
StatisticsCollector.PortStatsCollector statsSpeedCollector = statsCollector.new PortStatsCollector();
long speed = statsSpeedCollector.getSpeed(npt);
assertEquals(speed, 100L);
}
/**
* Test getSpeed() method can work with Openflow 1.2
*
* @throws Exception
*/
@Test
public void testGetCurrentPortSpeedOF12() throws Exception {
NodePortTuple npt = new NodePortTuple(sw_OF12.getId(), OFPort.of(1));
Map<DatapathId, IOFSwitch> switchMap = new HashMap<>();
switchMap.put(sw_OF12.getId(), sw_OF12);
getMockSwitchService().setSwitches(switchMap);
StatisticsCollector.PortStatsCollector statsSpeedCollector = statsCollector.new PortStatsCollector();
long speed = statsSpeedCollector.getSpeed(npt);
assertEquals(speed, 100L);
}
/**
* Test getSpeed() method can work with Openflow 1.3
*
* @throws Exception
*/
@Test
public void testGetCurrentPortSpeedOF13() throws Exception {
NodePortTuple npt = new NodePortTuple(sw_OF13.getId(), OFPort.of(1));
Map<DatapathId, IOFSwitch> switchMap = new HashMap<>();
switchMap.put(sw_OF13.getId(), sw_OF13);
getMockSwitchService().setSwitches(switchMap);
StatisticsCollector.PortStatsCollector statsSpeedCollector = statsCollector.new PortStatsCollector();
long speed = statsSpeedCollector.getSpeed(npt);
assertEquals(speed, 100L);
}
/**
* Test getSpeed() method can work with Openflow 1.4
*
* @throws Exception
*/
@Test
public void testGetCurrentPortSpeedOF14() throws Exception {
NodePortTuple npt = new NodePortTuple(sw_OF14.getId(), OFPort.of(1));
Map<DatapathId, IOFSwitch> switchMap = new HashMap<>();
switchMap.put(sw_OF14.getId(), sw_OF14);
getMockSwitchService().setSwitches(switchMap);
StatisticsCollector.PortStatsCollector statsSpeedCollector = statsCollector.new PortStatsCollector();
long speed = statsSpeedCollector.getSpeed(npt);
assertEquals(speed, 100L);
}
/**
* Test getSpeed() method can work with Openflow 1.5
*
* @throws Exception
*/
@Test
public void testGetCurrentPortSpeedOF15() throws Exception {
NodePortTuple npt = new NodePortTuple(sw_OF15.getId(), OFPort.of(1));
Map<DatapathId, IOFSwitch> switchMap = new HashMap<>();
switchMap.put(sw_OF15.getId(), sw_OF15);
getMockSwitchService().setSwitches(switchMap);
StatisticsCollector.PortStatsCollector statsSpeedCollector = statsCollector.new PortStatsCollector();
long speed = statsSpeedCollector.getSpeed(npt);
assertEquals(speed, 100L);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment