Update DeviceImpl

Change-Id: I85fc26c7e7c0fad315d1d9585f8c83ffb1d6e66a
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceImpl.java
index 09597a1..808d4a6 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceImpl.java
@@ -2,57 +2,87 @@
 
 import java.net.InetAddress;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.LinkedList;
+import java.util.Set;
 
 import net.floodlightcontroller.util.MACAddress;
 
 /**
  * @author Toshio Koide (t-koide@onlab.us)
  */
-public class DeviceImpl implements Device {
-	LinkedList<Port> attachmentPoints = new LinkedList<Port>();
-	MACAddress macAddr;
+public class DeviceImpl extends NetworkGraphObject implements Device {
 
-	public DeviceImpl(NetworkGraph graph, MACAddress macAddr) { 
-		this.macAddr = macAddr;
-	}
-	
-	@Override
-	public MACAddress getMacAddress() {
-		return macAddr;
-	}
+    private final MACAddress macAddr;
+    // These should be ConcurrentCollecton if Graph is going to be
+    protected LinkedList<Port> attachmentPoints;
+    protected Set<InetAddress> ipAddresses;
 
-	@Override
-	public Collection<InetAddress> getIpAddress() {
-		// TODO Auto-generated method stub
-		return null;
-	}
+    public DeviceImpl(NetworkGraph graph, MACAddress mac) {
+	super(graph);
+	this.macAddr = mac;
+	this.attachmentPoints = new LinkedList<>();
+	this.ipAddresses = new HashSet<>();
+    }
 
-	/**
-	 * @return ports attached to the device.
-	 * The last added port is stored as the first element.
-	 */
-	@Override
-	public Iterable<Port> getAttachmentPoints() {
-		return attachmentPoints;
-	}
+    @Override
+    public MACAddress getMacAddress() {
+	return this.macAddr;
+    }
 
-	@Override
-	public long getLastSeenTime() {
-		// TODO Auto-generated method stub
-		return 0;
-	}
-	
-	public void addAttachmentPoint(Port port) {
-		attachmentPoints.add(0, port);
-	}
-	
-	public void removeAttachmentPoint(Port port) {
-		attachmentPoints.remove(port);
-	}
-	
-	@Override
-	public String toString() {
-		return macAddr.toString();
-	}
+    @Override
+    public Collection<InetAddress> getIpAddress() {
+	return Collections.unmodifiableSet(ipAddresses);
+    }
+
+    @Override
+    public Iterable<Port> getAttachmentPoints() {
+	return Collections.unmodifiableList(this.attachmentPoints);
+    }
+
+    @Override
+    public long getLastSeenTime() {
+	// TODO Auto-generated method stub
+	return 0;
+    }
+
+    @Override
+    public String toString() {
+	return macAddr.toString();
+    }
+
+    /**
+     * Only {@link NetworkGraphImpl} should use this method
+     * @param p
+     */
+    void addAttachmentPoint(Port p) {
+	this.attachmentPoints.remove(p);
+	this.attachmentPoints.addFirst(p);
+    }
+
+    /**
+     * Only {@link NetworkGraphImpl} should use this method
+     * @param p
+     */
+    boolean removeAttachmentPoint(Port p) {
+	return this.attachmentPoints.remove(p);
+    }
+
+    /**
+     * Only {@link NetworkGraphImpl} should use this method
+     * @param p
+     */
+    boolean addIpAddress(InetAddress addr) {
+	return this.ipAddresses.add(addr);
+    }
+
+    /**
+     * Only {@link NetworkGraphImpl} should use this method
+     * @param p
+     */
+    boolean removeIpAddress(InetAddress addr) {
+	return this.ipAddresses.remove(addr);
+    }
+
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
index e4efc4e..e3a3a39 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
@@ -258,6 +258,13 @@
 	throw new ClassCastException("PortImpl expected, but found: " + p);
     }
 
+    private DeviceImpl getDeviceImpl(Device d) {
+	if (d instanceof DeviceImpl) {
+	    return (DeviceImpl) d;
+	}
+	throw new ClassCastException("DeviceImpl expected, but found: " + d);
+    }
+
     public boolean isSwitchInstanceInTopology(Switch sw) {
         // check if the sw instance is valid in Topology
         if (sw != switches.get(sw.getDpid())) {
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java
index 6f860d0..412113c 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java
@@ -13,12 +13,13 @@
 public class PortImpl extends NetworkGraphObject implements Port {
 
 	private Switch sw;
-	
+
 	private Long number;
 	private String description;
-	
+
 	protected Link outgoingLink;
 	protected Link incomingLink;
+	// These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
 	protected Set<Device> devices;
 
 	public PortImpl(NetworkGraph graph, Switch parentSwitch, Long number) {
@@ -32,12 +33,12 @@
 	public Long getNumber() {
 		return number;
 	}
-	
+
 	@Override
 	public String getDescription() {
 		return description;
 	}
-	
+
 	public void setDescription(String description) {
 		this.description = description;
 	}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java
index b9379a8..1f871d0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java
@@ -21,6 +21,7 @@
 public class SwitchImpl extends NetworkGraphObject implements Switch {
 
 	private Long dpid;
+	// These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
 	private final Map<Long, Port> ports;
 
 	public SwitchImpl(NetworkGraph graph, Long dpid) {