Finished transition to new configuration format
diff --git a/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java b/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java
index c57d4d8..baa825b 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java
@@ -6,12 +6,10 @@
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.ConcurrentSkipListSet;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -61,8 +59,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.net.InetAddresses;
-
 public class BgpRoute implements IFloodlightModule, IBgpRouteService, 
 									ITopologyListener, IOFSwitchListener {
 	
@@ -99,10 +95,9 @@
 	protected final int TOPO_DETECTION_WAIT = 2; //seconds
 	
 	//Configuration stuff
-	protected Map<String, GatewayRouter> gatewayRouters;
 	protected List<String> switches;
 	protected Map<String, Interface> interfaces;
-	protected List<BgpPeer> bgpPeers;
+	protected Map<InetAddress, BgpPeer> bgpPeers;
 	protected SwitchPort bgpdAttachmentPoint;
 	
 	//True when all switches have connected
@@ -110,18 +105,15 @@
 	//True when we have a full mesh of shortest paths between gateways
 	protected volatile boolean topologyReady = false;
 
-	//protected ConcurrentSkipListSet<LDUpdate> linkUpdates;
 	protected ArrayList<LDUpdate> linkUpdates;
 	protected SingletonTask topologyChangeDetectorTask;
 	
-	//protected ILinkStorage linkStorage;//XXX
-	
 	protected class TopologyChangeDetector implements Runnable {
 		@Override
 		public void run() {
 			log.debug("Running topology change detection task");
 			synchronized (linkUpdates) {
-				//This is the model the REST API uses to retrive network graph info
+				//This is the model the REST API uses to retrieve network graph info
 				ITopoLinkService topoLinkService = new TopoLinkServiceImpl();
 				
 				List<Link> activeLinks = topoLinkService.getActiveLinks();
@@ -163,16 +155,16 @@
 		try {
 			Configuration config = mapper.readValue(gatewaysFile, Configuration.class);
 			
-			gatewayRouters = config.getGateways();
 			switches = config.getSwitches();
 			interfaces = config.getInterfaces();
-			bgpPeers = config.getPeers();
+			bgpPeers = new HashMap<InetAddress, BgpPeer>();
+			for (BgpPeer peer : config.getPeers()){
+				bgpPeers.put(peer.getIpAddress(), peer);
+			}
 			
 			bgpdAttachmentPoint = new SwitchPort(
 					new Dpid(config.getBgpdAttachmentDpid()),
 					new Port(config.getBgpdAttachmentPort()));
-			//bgpdAttachmentDpid = config.getBgpdAttachmentDpid();
-			//bgpdAttachmentPort = config.getBgpdAttachmentPort();
 			
 		} catch (JsonParseException e) {
 			log.error("Error in JSON file", e);
@@ -219,9 +211,7 @@
 			throws FloodlightModuleException {
 	    
 	    ptree = new Ptree(32);
-	    
-	    //routerIpAddresses = new HashSet<InetAddress>();
-		
+	    	
 		// Register floodlight provider and REST handler.
 		floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
 		topology = context.getServiceImpl(ITopologyService.class);
@@ -233,24 +223,6 @@
 		//part of the controller core
 		proxyArp = new ProxyArpManager(floodlightProvider, topology);
 		
-		/*
-		linkStorage = new LinkStorageImpl();
-		//XXX Hack to pull out the database location from NetworkGraphPublisher's config
-		String databaseConfig = null;
-		for (IFloodlightModule fm : context.getAllModules()){
-			if (fm instanceof NetworkGraphPublisher){
-				Map<String, String> configMap = context.getConfigParams(fm);
-				databaseConfig = configMap.get("dbconf");
-				break;
-			}
-		}	
-		if (databaseConfig == null){
-			log.error("Couldn't find database config string \"dbconf\"");
-			System.exit(1);
-		}
-		linkStorage.init(databaseConfig);
-		*/
-		//linkUpdates = new ConcurrentSkipListSet<ILinkDiscovery.LDUpdate>();
 		linkUpdates = new ArrayList<LDUpdate>();
 		ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
 		topologyChangeDetectorTask = new SingletonTask(executor, new TopologyChangeDetector());
@@ -447,34 +419,45 @@
 		
 		String prefix = getPrefixFromPtree(node);
 		
-		log.debug("New prefix {} added, next hop {}", 
-				prefix, node.rib.nextHop.toString());
+		log.debug("New prefix {} added, next hop {}, routerId {}", 
+				new Object[] {prefix, node.rib.nextHop.toString(), 
+				node.rib.routerId.getHostAddress()});
 		
-		//Add a flow to rewrite mac for this prefix to all border switches
-		GatewayRouter thisRouter = gatewayRouters
-				.get(InetAddresses.toAddrString(node.rib.nextHop));
+		//TODO this is wrong, we shouldn't be dealing with BGP peers here.
+		//We need to figure out where the device is attached and what it's
+		//mac address is by learning. 
+		//The next hop is not necessarily the peer, and the peer's attachment
+		//point is not necessarily the next hop's attachment point.
+		BgpPeer peer = bgpPeers.get(node.rib.nextHop);
 		
-		if (thisRouter == null){
-			//TODO local router isn't in gateway list so this will get thrown
+		if (peer == null){
+			//TODO local router isn't in peers list so this will get thrown
 			//Need to work out what to do about local prefixes with next hop 0.0.0.0.
+			
+			//The other scenario is this is a route server route. In that
+			//case the next hop is not in our configuration
 			log.error("Couldn't find next hop router in router {} in config"
 					, node.rib.nextHop.toString());
 			return; //just quit out here? This is probably a configuration error
 		}
+		
+		Interface peerInterface = interfaces.get(peer.getInterfaceName());
 
-		for (GatewayRouter ingressRouter : gatewayRouters.values()){
-			if (ingressRouter == thisRouter) {
+		//Add a flow to rewrite mac for this prefix to all border switches
+		for (Interface srcInterface : interfaces.values()) {
+			if (srcInterface == peerInterface) {
+				//Don't push a flow for the switch where this peer is attached
 				continue;
 			}
-			
+						
 			DataPath shortestPath = topoRouteService.getShortestPath(
-					ingressRouter.getAttachmentPoint(), 
-					thisRouter.getAttachmentPoint());
+					srcInterface.getSwitchPort(),
+					peerInterface.getSwitchPort());
 			
 			if (shortestPath == null){
 				log.debug("Shortest path between {} and {} not found",
-						ingressRouter.getAttachmentPoint(), 
-						thisRouter.getAttachmentPoint());
+						srcInterface.getSwitchPort(),
+						peerInterface.getSwitchPort());
 				return; // just quit here?
 			}
 			
@@ -491,8 +474,6 @@
 	        .setBufferId(OFPacketOut.BUFFER_ID_NONE)
 	        .setCookie(MAC_RW_COOKIE)
 	        .setCommand(OFFlowMod.OFPFC_ADD)
-	        //.setMatch(match)
-	        //.setActions(actions)
 	        .setPriority(SDNIP_PRIORITY)
 	        .setLengthU(OFFlowMod.MINIMUM_LENGTH
 	        		+ OFActionDataLayerDestination.MINIMUM_LENGTH
@@ -502,8 +483,9 @@
 	        match.setDataLayerType(Ethernet.TYPE_IPv4);
 	        match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
 	        
-	        match.setDataLayerSource(ingressRouter.getRouterMac().toBytes());
-	        match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
+	        //match.setDataLayerSource(ingressRouter.getRouterMac().toBytes());
+	        //match.setDataLayerSource(peer.getMacAddress().toBytes());
+	        //match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
 
 	        InetAddress address = null;
 	        try {
@@ -519,7 +501,9 @@
 	        
 	        //Set up MAC rewrite action
 	        OFActionDataLayerDestination macRewriteAction = new OFActionDataLayerDestination();
-	        macRewriteAction.setDataLayerAddress(thisRouter.getRouterMac().toBytes());
+	        //TODO use ARP module rather than configured mac addresses
+	        //TODO the peer's mac address is not necessarily the next hop's...
+	        macRewriteAction.setDataLayerAddress(peer.getMacAddress().toBytes());
 	        
 	        //Set up output action
 	        OFActionOutput outputAction = new OFActionOutput();
@@ -535,7 +519,7 @@
 	        
 	        //Write to switch
 	        IOFSwitch sw = floodlightProvider.getSwitches()
-	        		.get(ingressRouter.getAttachmentPoint().dpid().value());
+	        			.get(srcInterface.getDpid());
 	        
             if (sw == null){
             	log.warn("Switch not found when pushing flow mod");
@@ -553,6 +537,7 @@
 		}
 	}
 	
+	//TODO this is largely untested
 	public void prefixDeleted(PtreeNode node) {
 		if (!topologyReady) {
 			return;
@@ -564,11 +549,16 @@
 				prefix, node.rib.nextHop.toString());
 		
 		//Remove MAC rewriting flows from other border switches
-		GatewayRouter thisRouter = gatewayRouters
-				.get(InetAddresses.toAddrString(node.rib.nextHop));
+		BgpPeer peer = bgpPeers.get(node.rib.nextHop);
+		if (peer == null){
+			//either a router server route or local route. Can't handle right now
+			return;
+		}
 		
-		for (GatewayRouter ingressRouter : gatewayRouters.values()){
-			if (ingressRouter == thisRouter) {
+		Interface peerInterface = interfaces.get(peer.getInterfaceName());
+		
+		for (Interface srcInterface : interfaces.values()) {
+			if (srcInterface == peerInterface) {
 				continue;
 			}
 			
@@ -582,8 +572,6 @@
 	        .setBufferId(OFPacketOut.BUFFER_ID_NONE)
 	        .setCookie(MAC_RW_COOKIE)
 	        .setCommand(OFFlowMod.OFPFC_DELETE)
-	        //.setMatch(match)
-	        //.setActions(actions)
 	        .setPriority(SDNIP_PRIORITY)
 	        .setLengthU(OFFlowMod.MINIMUM_LENGTH);
 	        		//+ OFActionDataLayerDestination.MINIMUM_LENGTH
@@ -593,8 +581,9 @@
 	        match.setDataLayerType(Ethernet.TYPE_IPv4);
 	        match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
 	        
-	        match.setDataLayerSource(ingressRouter.getRouterMac().toBytes());
-	        match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
+	        //match.setDataLayerSource(ingressRouter.getRouterMac().toBytes());
+	        //match.setDataLayerSource(peer.getMacAddress().toBytes());
+	        //match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
 	        
 	        InetAddress address = null;
 	        try {
@@ -610,7 +599,7 @@
 	        
 	        //Write to switch
 	        IOFSwitch sw = floodlightProvider.getSwitches()
-	        		.get(ingressRouter.getAttachmentPoint().dpid().value());
+	        		.get(srcInterface.getDpid());
 	        
             if (sw == null){
             	log.warn("Switch not found when pushing flow mod");
@@ -633,60 +622,36 @@
 	 * switches
 	 */
 	private void setupFullMesh(){
-		Map<IOFSwitch, SwitchPort> gatewaySwitches = new HashMap<IOFSwitch, SwitchPort>();
-		
-		//have to account for switches not being there, paths not being found.
-		
-		for (GatewayRouter router : gatewayRouters.values()){
-			SwitchPort switchPort = router.getAttachmentPoint();
-			
-			IOFSwitch sw = floodlightProvider.getSwitches().get(switchPort.dpid().value());
-			
-			if (sw == null){
-				log.debug("Gateway switch {} not here yet", switchPort.dpid().value());
-				return; // just quit here?
-			}
-			
-			//Only need to know 1 external-facing port from each gateway switch
-			//which we can feed into shortest path calculation
-			if (!gatewaySwitches.containsKey(sw)){
-				gatewaySwitches.put(sw, switchPort);
-			}
-			
-		}
-		
 		//For each border router, calculate and install a path from every other
 		//border switch to said border router. However, don't install the entry
 		//in to the first hop switch, as we need to install an entry to rewrite
 		//for each prefix received. This will be done later when prefixes have 
 		//actually been received.
 		
-		for (GatewayRouter dstRouter : gatewayRouters.values()){
-			SwitchPort routerAttachmentPoint = dstRouter.getAttachmentPoint();
-			for (Map.Entry<IOFSwitch, SwitchPort> src : gatewaySwitches.entrySet()) {
-		
-				if (routerAttachmentPoint.dpid().value() == 
-						src.getKey().getId()){
+		for (BgpPeer peer : bgpPeers.values()) {
+			Interface peerInterface = interfaces.get(peer.getInterfaceName());
+			for (Map.Entry<String, Interface> intfEntry : interfaces.entrySet()) {
+				Interface srcInterface = intfEntry.getValue();
+				if (peer.getInterfaceName().equals(intfEntry.getKey())){
 					continue;
 				}
 				
 				DataPath shortestPath = topoRouteService.getShortestPath(
-						src.getValue(), routerAttachmentPoint);
+							srcInterface.getSwitchPort(), peerInterface.getSwitchPort()); 
 				
 				if (shortestPath == null){
 					log.debug("Shortest path between {} and {} not found",
-							src.getValue(), routerAttachmentPoint);
+							srcInterface.getSwitchPort(), peerInterface.getSwitchPort());
 					return; // just quit here?
 				}
 				
 				//install flows
-				installPath(shortestPath.flowEntries(), dstRouter);
+				installPath(shortestPath.flowEntries(), peer);
 			}
 		}
 	}
 	
-	private void installPath(List<FlowEntry> flowEntries, GatewayRouter router){
-
+	private void installPath(List<FlowEntry> flowEntries, BgpPeer peer){
 		//Set up the flow mod
 		OFFlowMod fm =
                 (OFFlowMod) floodlightProvider.getOFMessageFactory()
@@ -702,7 +667,6 @@
         .setBufferId(OFPacketOut.BUFFER_ID_NONE)
         .setCookie(L2_FWD_COOKIE)
         .setCommand(OFFlowMod.OFPFC_ADD)
-        //.setMatch(match)
         .setActions(actions)
         .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
         
@@ -712,7 +676,8 @@
         	FlowEntry flowEntry = flowEntries.get(i);
            
             OFMatch match = new OFMatch();
-            match.setDataLayerDestination(router.getRouterMac().toBytes());
+            //TODO Again using MAC address from configuration
+            match.setDataLayerDestination(peer.getMacAddress().toBytes());
             match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
             ((OFActionOutput) fm.getActions().get(0)).setPort(flowEntry.outPort().value());
             
@@ -743,7 +708,7 @@
 	}
 	
 	private void setupBgpPaths(){
-		for (BgpPeer bgpPeer : bgpPeers){
+		for (BgpPeer bgpPeer : bgpPeers.values()){
 			Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName());
 			
 			DataPath path = topoRouteService.getShortestPath(
@@ -891,23 +856,21 @@
 		switchesConnected = true;
 	}
 	
+	//Actually we only need to go half way round to verify full mesh connectivity
+	//(n^2)/2
 	private void checkTopologyReady(){
-		for (GatewayRouter dstRouter : gatewayRouters.values()){
-			SwitchPort dstAttachmentPoint = dstRouter.getAttachmentPoint();
-			for (GatewayRouter srcRouter : gatewayRouters.values()) {
-				
-				if (dstRouter == srcRouter){
+		for (Interface dstInterface : interfaces.values()) {
+			for (Interface srcInterface : interfaces.values()) {			
+				if (dstInterface == srcInterface) {
 					continue;
 				}
 				
-				SwitchPort srcAttachmentPoint = srcRouter.getAttachmentPoint();
-				
 				DataPath shortestPath = topoRouteService.getShortestPath(
-						srcAttachmentPoint, dstAttachmentPoint);
+						srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
 				
 				if (shortestPath == null){
 					log.debug("Shortest path between {} and {} not found",
-							srcAttachmentPoint, dstAttachmentPoint);
+							srcInterface.getSwitchPort(), dstInterface.getSwitchPort());
 					return;
 				}
 			}
@@ -956,23 +919,7 @@
 			}
 			
 			log.debug("Topo change {}", ldu.getOperation());
-			/*
-			if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){
-				log.debug("Link Added: src={} outPort={} dst={} inPort={}",
-						new Object[] {
-						HexString.toHexString(ldu.getSrc()), ldu.getSrcPort(),
-						HexString.toHexString(ldu.getDst()), ldu.getDstPort()});
-				TopoLinkServiceImpl impl = new TopoLinkServiceImpl();
-				
-				List<Link> retval = impl.getActiveLinks();
-				
-				log.debug("retval size {}", retval.size());
-				
-				for (Link l : retval){
-					log.debug("link {}", l);
-				}
-			}
-			*/
+			
 			if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){
 				synchronized (linkUpdates) {
 					linkUpdates.add(ldu);
@@ -982,13 +929,6 @@
 		
 		if (refreshNeeded){
 			topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS);
-			/*if (topologyReady){
-				setupFullMesh();
-			}
-			else{
-				checkStatus();
-			}*/
-			
 		}
 	}
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/bgproute/Configuration.java b/src/main/java/net/onrc/onos/ofcontroller/bgproute/Configuration.java
index 65617c8..08236a7 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/bgproute/Configuration.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/bgproute/Configuration.java
@@ -12,7 +12,7 @@
 	private List<String> switches;
 	private Map<String, Interface> interfaces;
 	private List<BgpPeer> peers;
-	private Map<String, GatewayRouter> gateways;
+	//private Map<String, GatewayRouter> gateways;
 	
 	public Configuration() {
 		// TODO Auto-generated constructor stub
@@ -63,6 +63,7 @@
 		this.peers = peers;
 	}
 
+	/*
 	public Map<String, GatewayRouter> getGateways() {
 		return gateways;
 	}
@@ -70,6 +71,6 @@
 	@JsonProperty("gateways")
 	public void setGateways(Map<String, GatewayRouter> gateways) {
 		this.gateways = gateways;
-	}
+	}*/
 
 }
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 b7fc896..a5246c0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
@@ -39,7 +39,6 @@
 	protected IFloodlightProviderService floodlightProvider;
 	protected ITopologyService topology;
 	
-	
 	protected Map<InetAddress, ArpTableEntry> arpTable;
 	
 	public ProxyArpManager(IFloodlightProviderService floodlightProvider,
@@ -57,13 +56,11 @@
 
 	@Override
 	public boolean isCallbackOrderingPrereq(OFType type, String name) {
-		// TODO Auto-generated method stub
 		return false;
 	}
 
 	@Override
 	public boolean isCallbackOrderingPostreq(OFType type, String name) {
-		// TODO Auto-generated method stub
 		return false;
 	}
 
@@ -115,7 +112,6 @@
 			}
 		}
 		
-		
 		return Command.CONTINUE;
 	}
 
@@ -180,14 +176,11 @@
 			OFPacketOut po = new OFPacketOut();
 			po.setInPort(OFPort.OFPP_NONE)
 				.setBufferId(-1)
-				//.setLengthU(OFActionOutput.MINIMUM_LENGTH);
 				.setPacketData(pi.getPacketData());
 				
 			List<OFAction> actions = new ArrayList<OFAction>();
 			
 			for (short portNum : enabledPorts){
-				//log.debug("linkPorts {}", linkPorts);
-				//log.debug("portNum {}", portNum);
 				if (linkPorts.contains(portNum) || 
 						(sw.getId() == inSwitch && portNum == inPort)){
 					//If this port isn't an edge port or is the ingress port
@@ -258,14 +251,15 @@
 			log.warn("Failure writing packet out to switch", e);
 		}
 	}
-	
+
+	//TODO this should be put somewhere more central. I use it in BgpRoute as well.
+	//We need a HexString.toHexString() equivalent.
 	private String bytesToStringAddr(byte[] bytes){
 		InetAddress addr;
 		try {
 			addr = InetAddress.getByAddress(bytes);
 		} catch (UnknownHostException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
+			log.warn(" ", e);
 			return "";
 		}
 		if (addr == null) return "";