* Test "normal" OFPortStatus handling. No name<->number
* conflicts or exception testing.
*/
@Test
public void testBasicPortStatusOperation() {
OFPortStatus ps = (OFPortStatus)
BasicFactory.getInstance().getMessage(OFType.PORT_STATUS);
List<ImmutablePort> ports = new ArrayList<ImmutablePort>();
ports.add(p1a);
ports.add(p2a);
// Set p1a and p2a as baseline
PortChangeEvent evP1aAdded =
new PortChangeEvent(p1a, PortChangeType.ADD);
PortChangeEvent evP2aAdded =
new PortChangeEvent(p2a, PortChangeType.ADD);
Collection<PortChangeEvent> expectedChanges =
new ArrayList<IOFSwitch.PortChangeEvent>();
expectedChanges.add(evP1aAdded);
expectedChanges.add(evP2aAdded);
Collection<PortChangeEvent> actualChanges = sw.comparePorts(ports);
assertEquals(0, sw.getPorts().size());
assertEquals(0, sw.getEnabledPorts().size());
assertEquals(0, sw.getEnabledPortNumbers().size());
assertEquals(2, actualChanges.size());
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
actualChanges = sw.setPorts(ports);
assertEquals(2, actualChanges.size());
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
assertTrue("enabled ports should be empty",
sw.getEnabledPortNumbers().isEmpty());
assertTrue("enabled ports should be empty",
sw.getEnabledPorts().isEmpty());
assertEquals(p1a, sw.getPort((short)1));
assertEquals(p1a, sw.getPort("port1"));
assertEquals(p1a, sw.getPort("PoRt1")); // case insensitive get
assertEquals(p2a, sw.getPort((short)2));
assertEquals(p2a, sw.getPort("port2"));
assertEquals(p2a, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// P1a -> p1b. Should receive a port up
ports.clear();
ports.add(p2a);
ports.add(p1b);
ps.setReason(OFPortReason.OFPPR_MODIFY.getReasonCode());
ps.setDesc(p1b.toOFPhysicalPort());
PortChangeEvent evP1bUp = new PortChangeEvent(p1b, PortChangeType.UP);
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
expectedChanges.add(evP1bUp);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
List<ImmutablePort> enabledPorts = new ArrayList<ImmutablePort>();
enabledPorts.add(p1b);
List<Short> enabledPortNumbers = new ArrayList<Short>();
enabledPortNumbers.add((short)1);
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1b, sw.getPort((short)1));
assertEquals(p1b, sw.getPort("port1"));
assertEquals(p1b, sw.getPort("PoRt1")); // case insensitive get
assertEquals(p2a, sw.getPort((short)2));
assertEquals(p2a, sw.getPort("port2"));
assertEquals(p2a, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// p2a -> p2b. Should receive a port modify
ports.clear();
ports.add(p2b);
ports.add(p1b);
PortChangeEvent evP2bModified =
new PortChangeEvent(p2b, PortChangeType.OTHER_UPDATE);
ps.setReason(OFPortReason.OFPPR_MODIFY.getReasonCode());
ps.setDesc(p2b.toOFPhysicalPort());
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
expectedChanges.add(evP2bModified);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts = new ArrayList<ImmutablePort>();
enabledPorts.add(p1b);
enabledPortNumbers = new ArrayList<Short>();
enabledPortNumbers.add((short)1);
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1b, sw.getPort((short)1));
assertEquals(p1b, sw.getPort("port1"));
assertEquals(p1b, sw.getPort("PoRt1")); // case insensitive get
assertEquals(p2b, sw.getPort((short)2));
assertEquals(p2b, sw.getPort("port2"));
assertEquals(p2b, sw.getPort("PoRt2")); // case insensitive get
assertEquals(null, sw.getPort((short)3));
assertEquals(null, sw.getPort("port3"));
assertEquals(null, sw.getPort("PoRt3")); // case insensitive get
//----------------------------------------------------
// p1b -> p1a. Via an OFPPR_ADD, Should receive a port DOWN
ports.clear();
ports.add(p2b);
ports.add(p1a);
// we use an ADD here. We treat ADD and MODIFY the same way
ps.setReason(OFPortReason.OFPPR_ADD.getReasonCode());
ps.setDesc(p1a.toOFPhysicalPort());
PortChangeEvent evP1aDown =
new PortChangeEvent(p1a, PortChangeType.DOWN);
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
expectedChanges.add(evP1aDown);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPortNumbers.clear();
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1a, sw.getPort((short)1));
assertEquals(p1a, sw.getPort("port1"));
assertEquals(p1a, sw.getPort("PoRt1")); // case insensitive get
assertEquals(p2b, sw.getPort((short)2));
assertEquals(p2b, sw.getPort("port2"));
assertEquals(p2b, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// p2b -> p2a. Via an OFPPR_ADD, Should receive a port MODIFY
ports.clear();
ports.add(p2a);
ports.add(p1a);
// we use an ADD here. We treat ADD and MODIFY the same way
ps.setReason(OFPortReason.OFPPR_ADD.getReasonCode());
ps.setDesc(p2a.toOFPhysicalPort());
PortChangeEvent evP2aModify =
new PortChangeEvent(p2a, PortChangeType.OTHER_UPDATE);
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
expectedChanges.add(evP2aModify);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPortNumbers.clear();
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1a, sw.getPort((short)1));
assertEquals(p1a, sw.getPort("port1"));
assertEquals(p1a, sw.getPort("PoRt1")); // case insensitive get
assertEquals(p2a, sw.getPort((short)2));
assertEquals(p2a, sw.getPort("port2"));
assertEquals(p2a, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// Remove p2a
ports.clear();
ports.add(p1a);
ps.setReason(OFPortReason.OFPPR_DELETE.getReasonCode());
ps.setDesc(p2a.toOFPhysicalPort());
PortChangeEvent evP2aDel =
new PortChangeEvent(p2a, PortChangeType.DELETE);
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
expectedChanges.add(evP2aDel);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPortNumbers.clear();
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1a, sw.getPort((short)1));
assertEquals(p1a, sw.getPort("port1"));
assertEquals(p1a, sw.getPort("PoRt1")); // case insensitive get
assertEquals(null, sw.getPort((short)2));
assertEquals(null, sw.getPort("port2"));
assertEquals(null, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// Remove p2a again. Nothing should happen.
ports.clear();
ports.add(p1a);
ps.setReason(OFPortReason.OFPPR_DELETE.getReasonCode());
ps.setDesc(p2a.toOFPhysicalPort());
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPortNumbers.clear();
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1a, sw.getPort((short)1));
assertEquals(p1a, sw.getPort("port1"));
assertEquals(p1a, sw.getPort("PoRt1")); // case insensitive get
assertEquals(null, sw.getPort((short)2));
assertEquals(null, sw.getPort("port2"));
assertEquals(null, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// Remove p1a
ports.clear();
ps.setReason(OFPortReason.OFPPR_DELETE.getReasonCode());
ps.setDesc(p1a.toOFPhysicalPort());
PortChangeEvent evP1aDel =
new PortChangeEvent(p1a, PortChangeType.DELETE);
actualChanges = sw.processOFPortStatus(ps);
expectedChanges.clear();
expectedChanges.add(evP1aDel);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPortNumbers.clear();
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(null, sw.getPort((short)1));
assertEquals(null, sw.getPort("port1"));
assertEquals(null, sw.getPort("PoRt1")); // case insensitive get
assertEquals(null, sw.getPort((short)2));
assertEquals(null, sw.getPort("port2"));
assertEquals(null, sw.getPort("PoRt2")); // case insensitive get
//----------------------------------------------------
// Add p3, should receive an add
ports.clear();
ports.add(p3);
PortChangeEvent evP3Add =
new PortChangeEvent(p3, PortChangeType.ADD);
expectedChanges.clear();
expectedChanges.add(evP3Add);
ps.setReason(OFPortReason.OFPPR_ADD.getReasonCode());
ps.setDesc(p3.toOFPhysicalPort());
actualChanges = sw.processOFPortStatus(ps);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPorts.add(p3);
enabledPortNumbers.clear();
enabledPortNumbers.add((short)3);
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(null, sw.getPort((short)1));
assertEquals(null, sw.getPort("port1"));
assertEquals(null, sw.getPort("PoRt1")); // case insensitive get
assertEquals(null, sw.getPort((short)2));
assertEquals(null, sw.getPort("port2"));
assertEquals(null, sw.getPort("PoRt2")); // case insensitive get
assertEquals(p3, sw.getPort((short)3));
assertEquals(p3, sw.getPort("port3"));
assertEquals(p3, sw.getPort("PoRt3")); // case insensitive get
//----------------------------------------------------
// Add p1b, back should receive an add
ports.clear();
ports.add(p1b);
ports.add(p3);
PortChangeEvent evP1bAdd =
new PortChangeEvent(p1b, PortChangeType.ADD);
expectedChanges.clear();
expectedChanges.add(evP1bAdd);
// use a modify to add the port
ps.setReason(OFPortReason.OFPPR_MODIFY.getReasonCode());
ps.setDesc(p1b.toOFPhysicalPort());
actualChanges = sw.processOFPortStatus(ps);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();
enabledPorts.add(p3);
enabledPorts.add(p1b);
enabledPortNumbers.clear();
enabledPortNumbers.add((short)3);
enabledPortNumbers.add((short)1);
assertCollectionEqualsNoOrder(enabledPorts, sw.getEnabledPorts());
assertCollectionEqualsNoOrder(enabledPortNumbers,
sw.getEnabledPortNumbers());
assertEquals(p1b, sw.getPort((short)1));
assertEquals(p1b, sw.getPort("port1"));
assertEquals(p1b, sw.getPort("PoRt1")); // case insensitive get
assertEquals(null, sw.getPort((short)2));
assertEquals(null, sw.getPort("port2"));
assertEquals(null, sw.getPort("PoRt2")); // case insensitive get
assertEquals(p3, sw.getPort((short)3));
assertEquals(p3, sw.getPort("port3"));
assertEquals(p3, sw.getPort("PoRt3")); // case insensitive get
//----------------------------------------------------
// Modify, but nothing really changed
ports.clear();
ports.add(p1b);
ports.add(p3);
expectedChanges.clear();
// use a modify to add the port
ps.setReason(OFPortReason.OFPPR_MODIFY.getReasonCode());
ps.setDesc(p1b.toOFPhysicalPort());
actualChanges = sw.processOFPortStatus(ps);
assertCollectionEqualsNoOrder(expectedChanges, actualChanges);
assertCollectionEqualsNoOrder(ports, sw.getPorts());
enabledPorts.clear();