Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.virtualnetwork; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Iterator; |
| 6 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 7 | |
| 8 | import net.floodlightcontroller.util.MACAddress; |
| 9 | |
| 10 | /** |
| 11 | * Data structure for storing and outputing information of a virtual network created |
| 12 | * by VirtualNetworkFilter |
| 13 | * |
| 14 | * @author KC Wang |
| 15 | */ |
| 16 | |
| 17 | @JsonSerialize(using=VirtualNetworkSerializer.class) |
| 18 | public class VirtualNetwork{ |
| 19 | protected String name; // network name |
| 20 | protected String guid; // network id |
| 21 | protected String gateway; // network gateway |
| 22 | protected Collection<MACAddress> hosts; // array of hosts explicitly added to this network |
| 23 | |
| 24 | /** |
| 25 | * Constructor requires network name and id |
| 26 | * @param name: network name |
| 27 | * @param guid: network id |
| 28 | */ |
| 29 | public VirtualNetwork(String name, String guid) { |
| 30 | this.name = name; |
| 31 | this.guid = guid; |
| 32 | this.gateway = null; |
| 33 | this.hosts = new ArrayList<MACAddress>(); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Sets network name |
| 39 | * @param gateway: IP address as String |
| 40 | */ |
| 41 | public void setName(String name){ |
| 42 | this.name = name; |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Sets network gateway IP address |
| 48 | * @param gateway: IP address as String |
| 49 | */ |
| 50 | public void setGateway(String gateway){ |
| 51 | this.gateway = gateway; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Adds a host to this network record |
| 57 | * @param host: MAC address as MACAddress |
| 58 | */ |
| 59 | public void addHost(MACAddress host){ |
| 60 | this.hosts.add(host); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Removes a host from this network record |
| 66 | * @param host: MAC address as MACAddress |
| 67 | * @return boolean: true: removed, false: host not found |
| 68 | */ |
| 69 | public boolean removeHost(MACAddress host){ |
| 70 | Iterator<MACAddress> iter = this.hosts.iterator(); |
| 71 | while(iter.hasNext()){ |
| 72 | MACAddress element = iter.next(); |
| 73 | if(element.equals(host) ){ |
| 74 | //assuming MAC address for host is unique |
| 75 | iter.remove(); |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Removes all hosts from this network record |
| 84 | */ |
| 85 | public void clearHosts(){ |
| 86 | this.hosts.clear(); |
| 87 | } |
| 88 | } |