Moved ARP on top of network map device storage. This required improvements to the device storage, most notably to improved IP address handling by representing IP addresses as separate vertices rather than a serialized string in the Device vertex
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/IDeviceStorage.java b/src/main/java/net/onrc/onos/ofcontroller/core/IDeviceStorage.java
index 7310d8c..be495b9 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/IDeviceStorage.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/IDeviceStorage.java
@@ -9,7 +9,7 @@
 	public IDeviceObject updateDevice(IDevice device);
 	public void removeDevice(IDevice device);
 	public IDeviceObject getDeviceByMac(String mac);
-	public IDeviceObject getDeviceByIP(String ip);
+	public IDeviceObject getDeviceByIP(int ipv4Address);
 	public void changeDeviceAttachments(IDevice device);
 	public void changeDeviceIPv4Address(IDevice device);	
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjects.java b/src/main/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjects.java
index 7b38fef..b54f6b9 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjects.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjects.java
@@ -149,12 +149,6 @@
 		@Property("dl_addr")
 		public void setMACAddress(String macaddr);
 		
-		@JsonProperty("ipv4")
-		@Property("nw_addr")
-		public String getIPAddress();
-		@Property("nw_addr")
-		public void setIPAddress(String ipaddr);
-		
 		@JsonIgnore
 		@Adjacency(label="host",direction = Direction.IN)
 		public Iterable<IPortObject> getAttachedPorts();
@@ -171,6 +165,23 @@
 		@GremlinGroovy("it.in('host').in('on')")
 		public Iterable<ISwitchObject> getSwitch();
 		
+		//
+		// IPv4 Addresses
+		//
+		@JsonProperty("ipv4addresses")
+		@Adjacency(label="hasAddress")
+		public Iterable<IIpv4Address> getIpv4Addresses();
+
+		@JsonIgnore
+		@GremlinGroovy("it.out('hasAddress').has('ipv4', ipv4Address)")
+		public IIpv4Address getIpv4Address(@GremlinParam("ipv4Address") final int ipv4Address);
+		
+		@Adjacency(label="hasAddress")
+		public void addIpv4Address(final IIpv4Address ipv4Address);
+		
+		@Adjacency(label="hasAddress")
+		public void removeIpv4Address(final IIpv4Address ipv4Address);
+		
 /*		@JsonProperty("dpid")
 		@GremlinGroovy("_().in('host').in('on').next().getProperty('dpid')")
 		public Iterable<String> getSwitchDPID();
@@ -183,7 +194,21 @@
 		@GremlinGroovy("_().in('host').in('on').path(){it.number}{it.dpid}")
 		public Iterable<SwitchPort> getAttachmentPoints();*/
 	}
-
+		
+	public interface IIpv4Address extends IBaseObject {
+		
+		@JsonProperty("ipv4")
+		@Property("ipv4_address")
+		public int getIpv4Address();
+		
+		@Property("ipv4_address")
+		public void setIpv4Address(int ipv4Address);
+		
+		@JsonIgnore
+		@GremlinGroovy("it.in('hasAddress')")
+		public IDeviceObject getDevice();
+	}
+	
 public interface IFlowPath extends IBaseObject {
 		@JsonProperty("flowId")
 		@Property("flow_id")
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
index 63eaacb..256008a 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/DeviceStorageImpl.java
@@ -1,30 +1,31 @@
 package net.onrc.onos.ofcontroller.core.internal;
 
-import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+
+import net.floodlightcontroller.devicemanager.IDevice;
+import net.floodlightcontroller.devicemanager.SwitchPort;
+import net.onrc.onos.graph.GraphDBOperation;
+import net.onrc.onos.ofcontroller.core.IDeviceStorage;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
+
 import org.openflow.util.HexString;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.Lists;
 import com.thinkaurelius.titan.core.TitanException;
-import net.floodlightcontroller.devicemanager.IDevice;
-import net.floodlightcontroller.devicemanager.SwitchPort;
-import net.floodlightcontroller.packet.IPv4;
-import net.onrc.onos.graph.GraphDBOperation;
-import net.onrc.onos.ofcontroller.core.IDeviceStorage;
-import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
-import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
-import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
 
 /**
  * This is the class for storing the information of devices into CassandraDB
  * @author Pankaj
  */
 public class DeviceStorageImpl implements IDeviceStorage {
+	protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
 	
 	private GraphDBOperation ope;
-	protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
 
 	/***
 	 * Initialize function. Before you use this class, please call this method
@@ -32,10 +33,10 @@
 	 */
 	@Override
 	public void init(String conf) {
-		try{
+		try {
 			ope = new GraphDBOperation(conf);
-		} catch(Exception e) {
-			log.error(e.getMessage());
+		} catch (TitanException e) {
+			log.error("Couldn't open graph operation", e);
 		}
 	}	
 	
@@ -67,39 +68,31 @@
 		IDeviceObject obj = null;
  		try {
  			if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
- 				log.debug("Adding device {}: found existing device",device.getMACAddressString());
+ 				log.debug("Adding device {}: found existing device", device.getMACAddressString());
             } else {
             	obj = ope.newDevice();
-                log.debug("Adding device {}: creating new device",device.getMACAddressString());
+                log.debug("Adding device {}: creating new device", device.getMACAddressString());
             }
-	            changeDeviceAttachments(device, obj);
-	
-				String multiIntString = "";
-				for(Integer intValue : device.getIPv4Addresses()) {
-				   if (multiIntString == null || multiIntString.isEmpty()){
-				     multiIntString = IPv4.fromIPv4Address(intValue);
-				     multiIntString = "[" + IPv4.fromIPv4Address(intValue);
-				   }else{
-				        multiIntString += "," + IPv4.fromIPv4Address(intValue);
-				   }
-				}
-	 	            
-	            if(multiIntString.toString() != null && !multiIntString.toString().isEmpty()){
-	            	obj.setIPAddress(multiIntString + "]");
-	            }
-	            
-	 			obj.setMACAddress(device.getMACAddressString());
-	 			obj.setType("device");
-	 			obj.setState("ACTIVE");
-	 			ope.commit();
  			
-	 			log.debug("Adding device {}",device.getMACAddressString());
-		} catch (Exception e) {
+            changeDeviceAttachments(device, obj);
+	            
+            for (Integer intIpv4Address : device.getIPv4Addresses()) {
+            	obj.addIpv4Address(ope.ensureIpv4Address(intIpv4Address));
+            }
+            
+ 			obj.setMACAddress(device.getMACAddressString());
+ 			obj.setType("device");
+ 			obj.setState("ACTIVE");
+ 			ope.commit();
+		
+ 			//log.debug("Adding device {}",device.getMACAddressString());
+		} catch (TitanException e) {
 			ope.rollback();
 			log.error(":addDevice mac:{} failed", device.getMACAddressString());
 			obj = null;
-		}	
- 			return obj;		
+		}
+ 		
+		return obj;		
 	}
 
 	/***
@@ -125,7 +118,7 @@
              	ope.commit();
             	log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
             }
-		} catch (Exception e) {
+		} catch (TitanException e) {
 			ope.rollback();
 			log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
 		}
@@ -147,23 +140,15 @@
 	 * @return IDeviceObject you want to get.
 	 */
 	@Override
-	public IDeviceObject getDeviceByIP(String ip) {
-		try
-		{
-			for(IDeviceObject dev : ope.getDevices()){
-				String ips;
-				if((ips = dev.getIPAddress()) != null){
-					String nw_addr_wob = ips.replace("[", "").replace("]", "");
-					ArrayList<String> iplists = Lists.newArrayList(nw_addr_wob.split(","));	
-					if(iplists.contains(ip)){
-						return dev;
-					}
-				}
+	public IDeviceObject getDeviceByIP(int ipv4Address) {
+		try {
+			IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
+			if (ipv4AddressVertex != null) {
+				return ipv4AddressVertex.getDevice();
 			}
 			return null;
 		}
-		catch (Exception e)
-		{
+		catch (TitanException e) {
 			log.error("DeviceStorage:getDeviceByIP:{} failed");
 			return null;
 		}
@@ -178,14 +163,14 @@
 		IDeviceObject obj = null;
  		try {
             if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
-                log.debug("Changing device ports {}: found existing device",device.getMACAddressString());
+                log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
                 changeDeviceAttachments(device, obj);
                 ope.commit();
             } else {
-   				log.debug("failed to search device...now adding {}",device.getMACAddressString());
+   				log.debug("failed to search device...now adding {}", device.getMACAddressString());
    				addDevice(device);
-            }            			
-		} catch (Exception e) {
+            }
+		} catch (TitanException e) {
 			ope.rollback();
 			log.error(":addDevice mac:{} failed", device.getMACAddressString());
 		}	
@@ -199,33 +184,33 @@
 	public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
 		SwitchPort[] attachmentPoints = device.getAttachmentPoints();
 		List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
-		
-        for (SwitchPort ap : attachmentPoints) {
-        	//Check weather there is the port
-          	 IPortObject port = ope.searchPort( HexString.toHexString(ap.getSwitchDPID()),
-												(short) ap.getPort());
-          	 log.debug("New Switch Port is {},{}", HexString.toHexString(ap.getSwitchDPID()),(short) ap.getPort());
-	       	 
-	       	 if(port != null){
-	   			if(attachedPorts.contains(port))
-	       		{
-		       		log.debug("This is the port you already attached {}: do nothing",device.getMACAddressString());
-		       		//This port will be remained, so remove from the removed port lists.
-		       		attachedPorts.remove(port);
-		       	} else {
-	     		   log.debug("Adding device {}: attaching to port",device.getMACAddressString());
-		       		port.setDevice(obj);  
-		        }
-	    	
-		       	log.debug("port number is {}", port.getNumber().toString());
-		        log.debug("port desc is {}", port.getDesc());  
-	       	 }
-        }      		 
-	            
-	    for (IPortObject port: attachedPorts) {
-            log.debug("Detouching the device {}: detouching from port",device.getMACAddressString());
-       		port.removeDevice(obj);
-        }
+
+		for (SwitchPort ap : attachmentPoints) {
+			//Check if there is the port
+			IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
+					(short) ap.getPort());
+			log.debug("New Switch Port is {},{}", 
+					HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
+
+			if (port != null){
+				if (attachedPorts.contains(port)) {
+					log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
+					//This port will be remained, so remove from the removed port lists.
+					attachedPorts.remove(port);
+				} else {
+					log.debug("Adding device {}: attaching to port", device.getMACAddressString());
+					port.setDevice(obj);  
+				}
+
+				log.debug("port number is {}", port.getNumber().toString());
+				log.debug("port desc is {}", port.getDesc());  
+			}
+		}      		 
+
+		for (IPortObject port: attachedPorts) {
+			log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
+			port.removeDevice(obj);
+		}
 	}
 
 	/***
@@ -234,22 +219,24 @@
 	 */
 	@Override
 	public void changeDeviceIPv4Address(IDevice device) {
+		log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
+				device.getIPv4Addresses());
 		IDeviceObject obj;
   		try {
   			if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
-
-  				String multiIntString = "";
-  				for(Integer intValue : device.getIPv4Addresses()){
-  				   if (multiIntString == null || multiIntString.isEmpty()){
-  				     multiIntString = "[" + IPv4.fromIPv4Address(intValue);
-  				   } else {
-  				        multiIntString += "," + IPv4.fromIPv4Address(intValue);
-  				   }
+  				for (int ipv4Address : device.getIPv4Addresses()) {
+  					if (obj.getIpv4Address(ipv4Address) == null) {
+  						IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
+  						obj.addIpv4Address(dbIpv4Address);
+  					}
   				}
   				
-  	            if(multiIntString != null && !multiIntString.isEmpty()){
-  	            	obj.setIPAddress(multiIntString + "]");
-  	            }
+  				List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
+  				for (IIpv4Address dbIpv4Address : obj.getIpv4Addresses()) {
+  					if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
+  						obj.removeIpv4Address(dbIpv4Address);
+  					}
+  				}
   	            
               	ope.commit();
   			} else {
@@ -257,7 +244,7 @@
             }		
   		} catch (TitanException e) {
   			ope.rollback();
-			log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(),e);
+			log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
 		}
 	}
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
index 26d4e40..8d93acf 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/SwitchStorageImpl.java
@@ -333,6 +333,11 @@
         }
 	}
 
+	// TODO There's an issue here where a port with that ID could already
+	// exist when we try to add this one (because it's left over from an
+	// old topology). We need to remove an old port with the same ID when
+	// we add the new port. Also it seems that old ports like this are 
+	// never cleaned up and will remain in the DB in the ACTIVE state forever.
 	private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
 		IPortObject p = op.newPort(sw.getDPID(), portNum);
 		p.setState("ACTIVE");
diff --git a/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java b/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
index a745aaf..a5dabc9 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
@@ -17,7 +17,6 @@
 import net.floodlightcontroller.core.IFloodlightProviderService;
 import net.floodlightcontroller.core.IOFMessageListener;
 import net.floodlightcontroller.core.IOFSwitch;
-import net.floodlightcontroller.devicemanager.IDevice;
 import net.floodlightcontroller.devicemanager.IDeviceService;
 import net.floodlightcontroller.packet.ARP;
 import net.floodlightcontroller.packet.Ethernet;
@@ -26,7 +25,10 @@
 import net.floodlightcontroller.topology.ITopologyService;
 import net.floodlightcontroller.util.MACAddress;
 import net.onrc.onos.ofcontroller.bgproute.Interface;
+import net.onrc.onos.ofcontroller.core.IDeviceStorage;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
 import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
+import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
 
 import org.openflow.protocol.OFMessage;
 import org.openflow.protocol.OFPacketIn;
@@ -57,6 +59,8 @@
 	private IConfigInfoService configService;
 	private IRestApiService restApi;
 	
+	private IDeviceStorage deviceStorage;
+	
 	private short vlan;
 	private static final short NO_VLAN = 0;
 	
@@ -141,6 +145,9 @@
 		restApi.addRestletRoutable(new ArpWebRoutable());
 		floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
 		
+		deviceStorage = new DeviceStorageImpl();
+		deviceStorage.init("");
+		
 		Timer arpTimer = new Timer("arp-processing");
 		arpTimer.scheduleAtFixedRate(new TimerTask() {
 			@Override
@@ -285,13 +292,15 @@
 		//MACAddress macAddress = arpCache.lookup(target);
 		
 		//IDevice dstDevice = deviceService.fcStore.get(cntx, IDeviceService.CONTEXT_DST_DEVICE);
-		Iterator<? extends IDevice> it = deviceService.queryDevices(
-				null, null, InetAddresses.coerceToInteger(target), null, null);
+		//Iterator<? extends IDevice> it = deviceService.queryDevices(
+				//null, null, InetAddresses.coerceToInteger(target), null, null);
 		
-		IDevice targetDevice = null;
-		if (it.hasNext()) {
-			targetDevice = it.next();
-		}
+		//IDevice targetDevice = null;
+		//if (it.hasNext()) {
+			//targetDevice = it.next();
+		//}
+		IDeviceObject targetDevice = 
+				deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
 		
 		if (targetDevice != null) {
 			//We have the device in our database, so send a reply