VirtualNetworkFilter implements IFloodlightModule, IVirtualNetworkService, IOFMessageListener, IDeviceListener { @Override public void createNetwork(String guid, String network, Integer gateway) { if (log.isDebugEnabled()) { String gw = null; try { gw = IPv4.fromIPv4Address(gateway); } catch (Exception e) { } log.debug("Creating network {} with ID {} and gateway {}", new Object[] {network, guid, gw}); } if (!nameToGuid.isEmpty()) { for (Entry<String, String> entry : nameToGuid.entrySet()) { if (entry.getValue().equals(guid)) { nameToGuid.remove(entry.getKey()); break; } } } nameToGuid.put(network, guid); if (vNetsByGuid.containsKey(guid)) vNetsByGuid.get(guid).setName(network); else vNetsByGuid.put(guid, new VirtualNetwork(network, guid)); if ((gateway != null) && (gateway != 0)) { addGateway(guid, gateway); if(vNetsByGuid.get(guid)!=null) vNetsByGuid.get(guid).setGateway(IPv4.fromIPv4Address(gateway)); } }  @Override void createNetwork(String guid, String network, Integer gateway); @Override void deleteNetwork(String guid); @Override void addHost(MACAddress mac, String guid, String port); @Override void deleteHost(MACAddress mac, String port); @Override Collection<Class<? extends IFloodlightService>> getModuleServices(); @Override Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls(); @Override Collection<Class<? extends IFloodlightService>> getModuleDependencies(); @Override void init(FloodlightModuleContext context); @Override void startUp(FloodlightModuleContext context); @Override String getName(); @Override boolean isCallbackOrderingPrereq(OFType type, String name); @Override boolean isCallbackOrderingPostreq(OFType type, String name); @Override Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx); @Override void deviceAdded(IDevice device); @Override void deviceRemoved(IDevice device); @Override void deviceIPV4AddrChanged(IDevice device); @Override void deviceMoved(IDevice device); @Override void deviceVlanChanged(IDevice device); @Override Collection <VirtualNetwork> listNetworks();  }
@Test public void testCreateNetwork() { vns.createNetwork(guid1, net1, IPv4.toIPv4Address(gw1)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid1)); assertTrue(vns.nameToGuid.get(net1).equals(guid1)); assertTrue(vns.guidToGateway.get(guid1).equals(IPv4.toIPv4Address(gw1))); assertTrue(vns.vNetsByGuid.get(guid1).name.equals(net1)); assertTrue(vns.vNetsByGuid.get(guid1).guid.equals(guid1)); assertTrue(vns.vNetsByGuid.get(guid1).gateway.equals(gw1)); assertTrue(vns.vNetsByGuid.get(guid1).hosts.size()==0); vns.createNetwork(guid2, net2, null); assertTrue(vns.nameToGuid.get(net2).equals(guid2)); assertTrue(vns.guidToGateway.get(guid2) == null); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).size() == 1); assertTrue(vns.vNetsByGuid.get(guid2).name.equals(net2)); assertTrue(vns.vNetsByGuid.get(guid2).guid.equals(guid2)); assertTrue(vns.vNetsByGuid.get(guid2).gateway == null); assertTrue(vns.vNetsByGuid.get(guid2).hosts.size()==0); vns.createNetwork(guid3, net3, IPv4.toIPv4Address(gw1)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid1)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid3)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).size() == 2); assertTrue(vns.nameToGuid.get(net3).equals(guid3)); assertTrue(vns.guidToGateway.get(guid3).equals(IPv4.toIPv4Address(gw1))); assertTrue(vns.vNetsByGuid.get(guid3).name.equals(net3)); assertTrue(vns.vNetsByGuid.get(guid3).guid.equals(guid3)); assertTrue(vns.vNetsByGuid.get(guid3).gateway.equals(gw1)); assertTrue(vns.vNetsByGuid.get(guid3).hosts.size()==0); }
@Test public void testModifyNetwork() { testCreateNetwork(); vns.createNetwork(guid2, net2, IPv4.toIPv4Address(gw1)); assertTrue(vns.nameToGuid.get(net2).equals(guid2)); assertTrue(vns.guidToGateway.get(guid2).equals(IPv4.toIPv4Address(gw1))); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid1)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid2)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid3)); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).size() == 3); vns.createNetwork(guid2, "newnet2", null); assertTrue(vns.gatewayToGuid.get(IPv4.toIPv4Address(gw1)).contains(guid2)); assertTrue(vns.vNetsByGuid.get(guid2).gateway.equals(gw1)); assertTrue(vns.nameToGuid.get("newnet2").equals(guid2)); assertTrue(vns.vNetsByGuid.get(guid2).name.equals("newnet2")); assertFalse(vns.nameToGuid.containsKey(net2)); }