Change for device discovery on new-data model.
RcDevice is not implemented completely.

It is temporary implementation until all device archtecture is decicded.

Change-Id: Iea73bddbf859b869312d2590e0f31a18bd31aaf5
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java
index 8bea7f8..1846d8b 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java
@@ -27,7 +27,7 @@
     private final MACAddress mac;
     protected List<SwitchPort> attachmentPoints;
     protected Set<InetAddress> ipAddresses;
-
+    private long lastSeenTime;
 
     /**
      * Default constructor for Serializer to use.
@@ -68,11 +68,11 @@
     }
 
 
-    boolean addIpAddress(InetAddress addr) {
+    public boolean addIpAddress(InetAddress addr) {
 	return this.ipAddresses.add(addr);
     }
 
-    boolean removeIpAddress(InetAddress addr) {
+    public boolean removeIpAddress(InetAddress addr) {
 	return this.ipAddresses.remove(addr);
     }
 
@@ -93,4 +93,12 @@
     public ByteBuffer getIDasByteBuffer() {
 	return getDeviceID(mac.toBytes());
     }
+
+	public long getLastSeenTime() {
+		return lastSeenTime;
+	}
+
+	public void setLastSeenTime(long lastSeenTime) {
+		this.lastSeenTime = lastSeenTime;
+	}
 }
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 2b8930b..8f46f31 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceImpl.java
@@ -17,38 +17,42 @@
     private final MACAddress macAddr;
     protected LinkedList<Port> attachmentPoints;
     protected Set<InetAddress> ipAddresses;
+    private long lastSeenTime;
 
     public DeviceImpl(NetworkGraph graph, MACAddress mac) {
-	super(graph);
-	this.macAddr = mac;
-	this.attachmentPoints = new LinkedList<>();
-	this.ipAddresses = new HashSet<>();
+		super(graph);
+		this.macAddr = mac;
+		this.attachmentPoints = new LinkedList<>();
+		this.ipAddresses = new HashSet<>();
     }
 
     @Override
     public MACAddress getMacAddress() {
-	return this.macAddr;
+    	return this.macAddr;
     }
 
     @Override
     public Collection<InetAddress> getIpAddress() {
-	return Collections.unmodifiableSet(ipAddresses);
+    	return Collections.unmodifiableSet(ipAddresses);
     }
 
     @Override
     public Iterable<Port> getAttachmentPoints() {
-	return Collections.unmodifiableList(this.attachmentPoints);
+    	return Collections.unmodifiableList(this.attachmentPoints);
     }
 
     @Override
     public long getLastSeenTime() {
-	// TODO Auto-generated method stub
-	return 0;
+    	return lastSeenTime;
     }
 
     @Override
     public String toString() {
-	return macAddr.toString();
+    	return macAddr.toString();
+    }
+    
+    void setLastSeenTime(long lastSeenTime) {
+    	this.lastSeenTime = lastSeenTime;
     }
 
     /**
@@ -56,8 +60,8 @@
      * @param p
      */
     void addAttachmentPoint(Port p) {
-	this.attachmentPoints.remove(p);
-	this.attachmentPoints.addFirst(p);
+    	this.attachmentPoints.remove(p);
+    	this.attachmentPoints.addFirst(p);
     }
 
     /**
@@ -65,7 +69,7 @@
      * @param p
      */
     boolean removeAttachmentPoint(Port p) {
-	return this.attachmentPoints.remove(p);
+    	return this.attachmentPoints.remove(p);
     }
 
     /**
@@ -73,7 +77,7 @@
      * @param p
      */
     boolean addIpAddress(InetAddress addr) {
-	return this.ipAddresses.add(addr);
+    	return this.ipAddresses.add(addr);
     }
 
     /**
@@ -81,6 +85,6 @@
      * @param p
      */
     boolean removeIpAddress(InetAddress addr) {
-	return this.ipAddresses.remove(addr);
+    	return this.ipAddresses.remove(addr);
     }
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
index 30b2f73..63f37d8 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
@@ -1,21 +1,27 @@
 package net.onrc.onos.ofcontroller.networkgraph;
 
+import java.net.Inet4Address;
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Collection;
 
 import net.onrc.onos.datastore.DataStoreClient;
 import net.onrc.onos.datastore.IKVClient;
+import net.onrc.onos.datastore.topology.KVDevice;
 import net.onrc.onos.datastore.topology.KVLink;
 import net.onrc.onos.datastore.topology.KVPort;
 import net.onrc.onos.datastore.topology.KVPort.STATUS;
 import net.onrc.onos.datastore.topology.KVSwitch;
 import net.onrc.onos.datastore.utils.KVObject;
 import net.onrc.onos.datastore.utils.KVObject.WriteOp;
+import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.net.InetAddresses;
+
 /**
  * The southbound interface to the network graph which allows clients to
  * mutate the graph. This class will maintain the invariants of the network
@@ -186,8 +192,24 @@
 	 * @return true on success, otherwise false.
 	 */
 	public boolean addDevice(DeviceEvent device) {
-		// TODO implement
-		return false;			// Failure: not implemented yet
+		log.debug("Adding device into DB. mac {}", device.getMac());
+
+		KVDevice rcDevice = new KVDevice(device.getMac().toBytes());
+		rcDevice.setLastSeenTime(device.getLastSeenTime());
+		
+		for(SwitchPort sp : device.getAttachmentPoints()) {
+			byte[] portId = KVPort.getPortID(sp.getDpid(), sp.getNumber());		
+			rcDevice.addPortId(portId);
+		}
+		
+		for(InetAddress addr : device.getIpAddresses()) {
+			//It assume only one ip on a device now.
+			rcDevice.setIp(InetAddresses.coerceToInteger(addr));	
+		}
+
+		rcDevice.forceCreate();
+		
+		return true;
 	}
 
 	/**
@@ -197,7 +219,11 @@
 	 * @return true on success, otherwise false.
 	 */
 	public boolean removeDevice(DeviceEvent device) {
-		// TODO implement
-		return false;			// Failure: not implemented yet
+		log.debug("Removing device into DB. mac {}", device.getMac());
+		
+		KVDevice rcDevice = new KVDevice(device.getMac().toBytes());
+		rcDevice.forceDelete();
+		
+		return true;
 	}
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
index a323726..ccd3601 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
@@ -493,7 +493,7 @@
 	    // Send out notification
 	    TopologyEvent topologyEvent = new TopologyEvent(switchEvent);
 	    eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
-
+	    
 	    // Send out notification for each port
 	    for (PortEvent portEvent : portEvents) {
 		topologyEvent = new TopologyEvent(portEvent);
@@ -719,7 +719,8 @@
 	    // Send out notification
 	    TopologyEvent topologyEvent = new TopologyEvent(deviceEvent);
 	    eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
-
+	    log.debug("Put the device info into the cache of the graph. mac {}", deviceEvent.getMac());
+	    
 	    // Store the new Device Event in the local cache
 	    // TODO: The implementation below is probably wrong
 	    for (SwitchPort swp : deviceEvent.getAttachmentPoints()) {
@@ -746,6 +747,7 @@
 	if (datastore.removeDevice(deviceEvent)) {
 	    // Send out notification
 	    eventChannel.removeEntry(deviceEvent.getID());
+	    log.debug("Remove the device info into the cache of the graph. mac {}", deviceEvent.getMac());
 
 	    // Cleanup the Device Event from the local cache
 	    // TODO: The implementation below is probably wrong
@@ -998,9 +1000,12 @@
      */
     private void addDevice(DeviceEvent deviceEvent) {
 	Device device = networkGraph.getDeviceByMac(deviceEvent.getMac());
+	
 	if (device == null) {
+		log.debug("Existing device was not found in networkGraph. New device. mac {}", deviceEvent.getMac());
 	    device = new DeviceImpl(networkGraph, deviceEvent.getMac());
 	}
+	
 	DeviceImpl deviceImpl = getDeviceImpl(device);
 
 	// Update the IP addresses
@@ -1036,6 +1041,7 @@
 
 	// Update the device in the Network Graph
 	if (attachmentFound) {
+    	log.debug("Storing the info into networkGraph. mac {}", deviceEvent.getMac());
 	    networkGraph.putDevice(device);
 	    apiAddedDeviceEvents.add(deviceEvent);
 	}