Skip to content
Snippets Groups Projects
Commit 9e304078 authored by Jason Parraga's avatar Jason Parraga
Browse files

Merge pull request #440 from hwchiu/master

List ports on virtual network
parents 57906810 cb021837
No related branches found
No related tags found
No related merge requests found
......@@ -16,9 +16,9 @@
package net.floodlightcontroller.virtualnetwork;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import net.floodlightcontroller.util.MACAddress;
......@@ -35,8 +35,7 @@ public class VirtualNetwork{
protected String name; // network name
protected String guid; // network id
protected String gateway; // network gateway
protected Collection<MACAddress> hosts; // array of hosts explicitly added to this network
protected Map<String,MACAddress> portToMac; //port's logical namd and the host's mac address connected
/**
* Constructor requires network name and id
* @param name: network name
......@@ -46,7 +45,7 @@ public class VirtualNetwork{
this.name = name;
this.guid = guid;
this.gateway = null;
this.hosts = new ArrayList<MACAddress>();
this.portToMac = new ConcurrentHashMap<String,MACAddress>();
return;
}
......@@ -72,8 +71,8 @@ public class VirtualNetwork{
* Adds a host to this network record
* @param host: MAC address as MACAddress
*/
public void addHost(MACAddress host){
this.hosts.add(host);
public void addHost(String port,MACAddress host){
this.portToMac.put(port,host); // ignore old mapping
return;
}
......@@ -83,22 +82,19 @@ public class VirtualNetwork{
* @return boolean: true: removed, false: host not found
*/
public boolean removeHost(MACAddress host){
Iterator<MACAddress> iter = this.hosts.iterator();
while(iter.hasNext()){
MACAddress element = iter.next();
if(element.equals(host) ){
//assuming MAC address for host is unique
iter.remove();
return true;
}
}
return false;
for (Entry<String,MACAddress> entry : this.portToMac.entrySet()){
if(entry.getValue().equals(host)){
this.portToMac.remove(entry.getKey());
return true;
}
}
return false;
}
/**
* Removes all hosts from this network record
*/
public void clearHosts(){
this.hosts.clear();
this.portToMac.clear();
}
}
\ No newline at end of file
}
......@@ -235,7 +235,7 @@ public class VirtualNetworkFilter
macToGuid.put(mac, guid);
portToMac.put(port, mac);
if(vNetsByGuid.get(guid)!=null)
vNetsByGuid.get(guid).addHost(new MACAddress(mac.toBytes()));
vNetsByGuid.get(guid).addHost(port,new MACAddress(mac.toBytes()));
} else {
log.warn("Could not add MAC {} to network ID {} on port {}, the network does not exist",
new Object[] {mac, guid, port});
......@@ -250,9 +250,10 @@ public class VirtualNetworkFilter
if (mac == null && port == null) return;
if (port != null) {
MACAddress host = portToMac.remove(port);
if(vNetsByGuid.get(macToGuid.get(host)) != null)
if(host !=null && vNetsByGuid.get(macToGuid.get(host)) != null)
vNetsByGuid.get(macToGuid.get(host)).removeHost(host);
macToGuid.remove(host);
if(host !=null)
macToGuid.remove(host);
} else if (mac != null) {
if (!portToMac.isEmpty()) {
for (Entry<String, MACAddress> entry : portToMac.entrySet()) {
......
......@@ -18,6 +18,7 @@ package net.floodlightcontroller.virtualnetwork;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import net.floodlightcontroller.util.MACAddress;
......@@ -42,10 +43,15 @@ public class VirtualNetworkSerializer extends JsonSerializer<VirtualNetwork> {
jGen.writeStringField("guid", vNet.guid);
jGen.writeStringField("gateway", vNet.gateway);
jGen.writeArrayFieldStart("mac");
Iterator<MACAddress> hit = vNet.hosts.iterator();
while (hit.hasNext())
jGen.writeString(hit.next().toString());
jGen.writeArrayFieldStart("portMac");
Iterator entries = vNet.portToMac.entrySet().iterator();
while (entries.hasNext()){
jGen.writeStartObject();
Entry entry = (Entry)entries.next();
jGen.writeStringField("port",entry.getKey().toString());
jGen.writeStringField("mac",entry.getValue().toString());
jGen.writeEndObject();
}
jGen.writeEndArray();
jGen.writeEndObject();
......
......@@ -240,7 +240,7 @@ public class VirtualNetworkFilterTest extends FloodlightTestCase {
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);
assertTrue(vns.vNetsByGuid.get(guid1).portToMac.size()==0);
// Test creating network without a gateway
vns.createNetwork(guid2, net2, null);
......@@ -250,7 +250,7 @@ public class VirtualNetworkFilterTest extends FloodlightTestCase {
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);
assertTrue(vns.vNetsByGuid.get(guid2).portToMac.size()==0);
// Test creating a network that shares the gateway with net1
vns.createNetwork(guid3, net3, IPv4.toIPv4Address(gw1));
......@@ -262,7 +262,7 @@ public class VirtualNetworkFilterTest extends FloodlightTestCase {
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);
assertTrue(vns.vNetsByGuid.get(guid3).portToMac.size()==0);
}
......@@ -310,14 +310,14 @@ public class VirtualNetworkFilterTest extends FloodlightTestCase {
vns.addHost(mac1, guid1, hostPort1);
assertTrue(vns.macToGuid.get(mac1).equals(guid1));
assertTrue(vns.portToMac.get(hostPort1).equals(mac1));
assertTrue(vns.vNetsByGuid.get(guid1).hosts.contains(mac1));
assertTrue(vns.vNetsByGuid.get(guid1).portToMac.containsValue(mac1));
vns.addHost(mac2, guid1, hostPort2);
assertTrue(vns.macToGuid.get(mac2).equals(guid1));
assertTrue(vns.portToMac.get(hostPort2).equals(mac2));
assertTrue(vns.vNetsByGuid.get(guid1).hosts.contains(mac2));
assertTrue(vns.vNetsByGuid.get(guid1).portToMac.containsValue(mac2));
vns.addHost(mac3, guid3, hostPort3);
vns.addHost(mac4, guid3, hostPort4);
assertTrue(vns.vNetsByGuid.get(guid3).hosts.contains(mac4));
assertTrue(vns.vNetsByGuid.get(guid3).portToMac.containsValue(mac4));
}
@Test
......@@ -328,19 +328,19 @@ public class VirtualNetworkFilterTest extends FloodlightTestCase {
vns.deleteHost(mac1, null);
assertFalse(vns.macToGuid.containsKey(mac1));
assertFalse(vns.portToMac.containsKey(hostPort1));
assertFalse(vns.vNetsByGuid.get(host1Guid).hosts.contains(mac1));
assertFalse(vns.vNetsByGuid.get(host1Guid).portToMac.containsValue(mac1));
String host2Guid = vns.macToGuid.get(vns.portToMac.get(hostPort2));
vns.deleteHost(null, hostPort2);
assertFalse(vns.macToGuid.containsKey(mac2));
assertFalse(vns.portToMac.containsKey(hostPort2));
assertFalse(vns.vNetsByGuid.get(host2Guid).hosts.contains(mac2));
assertFalse(vns.vNetsByGuid.get(host2Guid).portToMac.containsValue(mac2));
String host3Guid = vns.macToGuid.get(mac3);
vns.deleteHost(mac3, hostPort3);
assertFalse(vns.macToGuid.containsKey(mac3));
assertFalse(vns.portToMac.containsKey(hostPort3));
assertFalse(vns.vNetsByGuid.get(host3Guid).hosts.contains(mac3));
assertFalse(vns.vNetsByGuid.get(host3Guid).portToMac.containsValue(mac3));
}
......
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