Merge pull request #493 from pingping-lin/master

improved the performance by replace GremlinGroovy
diff --git a/.gitignore b/.gitignore
index dd8d359..7e971f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,7 @@
 .classpath
 .project
 .pydevproject
+.settings
 target
 onos-logs
 onos.log
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..bf95ecc
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,202 @@
+Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "{}"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright 2013 Open Networking Laboratory, USA
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
diff --git a/NOTICE.txt b/NOTICE.txt
new file mode 100644
index 0000000..5be37b5
--- /dev/null
+++ b/NOTICE.txt
@@ -0,0 +1,12 @@
+INDEMNITY AND DISCLAIMER OF WARRANTIES
+
+SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR 27973/00100/SF/5339045.1 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/PARTNERS.txt b/PARTNERS.txt
new file mode 100644
index 0000000..df868d6
--- /dev/null
+++ b/PARTNERS.txt
@@ -0,0 +1 @@
+Add special notes for partnership relationship.
diff --git a/README.md b/README.md
index 9f2c040..cbe9097 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,11 @@
 SDN OS. Currently, it is under active development. ONOS was announced
 and demonstrated at ONS'13.
 
+License
+=======
+Apache 2.0
+
+
 Steps to download and setup a development Virtual Machine
 ==========================================
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java b/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java
index f1f015a..71fecd0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/core/internal/TopoLinkServiceImpl.java
@@ -82,12 +82,11 @@
 			short s_port = 0;
 			short d_port = 0;
 			
-			@SuppressWarnings("unchecked")
-			List<Vertex> V = pipe.next();
-			Vertex src_sw = V.get(0);
-			Vertex dest_sw = V.get(3);
-			Vertex src_port = V.get(1);
-			Vertex dest_port = V.get(2);
+			List<?> V = pipe.next();
+			Vertex src_sw = (Vertex)V.get(0);
+			Vertex dest_sw = (Vertex)V.get(3);
+			Vertex src_port = (Vertex)V.get(1);
+			Vertex dest_port = (Vertex)V.get(2);
 			s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
 			d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
 			s_port = (Short) src_port.getProperty("number");
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowDatabaseOperation.java b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowDatabaseOperation.java
index da407ab..2d5fe5c 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowDatabaseOperation.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowDatabaseOperation.java
@@ -642,6 +642,8 @@
 	FlowEntry flowEntry = new FlowEntry();
 	flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
 	flowEntry.setDpid(new Dpid(switchDpidStr));
+	flowEntry.setIdleTimeout(idleTimeout);
+	flowEntry.setHardTimeout(hardTimeout);
 
 	//
 	// Extract the match conditions
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
index 84b175f..18c34de 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowmanager/FlowManager.java
@@ -368,7 +368,7 @@
 					 FlowEntryId flowEntryId) {
 	// Find the Flow Entry
 	FlowEntry flowEntry = datagridService.getFlowEntry(flowEntryId);
-	if (flowEntryId == null)
+	if (flowEntry == null)
 	    return;		// Flow Entry not found
 
 	// Find the Flow Path
diff --git a/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowPusher.java b/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowPusher.java
index 3f61248..6485c5e 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowPusher.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/flowprogrammer/FlowPusher.java
@@ -735,10 +735,13 @@
 		}
 
 		//
-		// TODO: Set the following flag
-		// fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
-		// See method ForwardingBase::pushRoute()
+		// Set the OFPFF_SEND_FLOW_REM flag if the Flow Entry is not
+		// permanent.
 		//
+		if ((flowEntry.idleTimeout() != 0) ||
+		    (flowEntry.hardTimeout() != 0)) {
+		    fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
+		}
 
 		//
 		// Write the message to the switch
diff --git a/src/main/java/net/onrc/onos/ofcontroller/forwarding/Forwarding.java b/src/main/java/net/onrc/onos/ofcontroller/forwarding/Forwarding.java
index c70d4db..bd51fdc 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/forwarding/Forwarding.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/forwarding/Forwarding.java
@@ -1,5 +1,6 @@
 package net.onrc.onos.ofcontroller.forwarding;
 
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -16,7 +17,9 @@
 import net.floodlightcontroller.core.module.IFloodlightModule;
 import net.floodlightcontroller.core.module.IFloodlightService;
 import net.floodlightcontroller.packet.Ethernet;
+import net.floodlightcontroller.packet.IPv4;
 import net.floodlightcontroller.util.MACAddress;
+import net.onrc.onos.datagrid.IDatagridService;
 import net.onrc.onos.ofcontroller.core.IDeviceStorage;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
@@ -24,6 +27,7 @@
 import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
 import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
 import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
+import net.onrc.onos.ofcontroller.proxyarp.ArpMessage;
 import net.onrc.onos.ofcontroller.topology.TopologyManager;
 import net.onrc.onos.ofcontroller.util.CallerId;
 import net.onrc.onos.ofcontroller.util.DataPath;
@@ -49,6 +53,7 @@
 
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
+import com.google.common.net.InetAddresses;
 
 public class Forwarding implements IOFMessageListener, IFloodlightModule,
 									IForwardingService {
@@ -60,6 +65,7 @@
 	private IFloodlightProviderService floodlightProvider;
 	private IFlowService flowService;
 	private IFlowPusherService flowPusher;
+	private IDatagridService datagrid;
 	
 	private IDeviceStorage deviceStorage;
 	private TopologyManager topologyService;
@@ -142,6 +148,7 @@
 				context.getServiceImpl(IFloodlightProviderService.class);
 		flowService = context.getServiceImpl(IFlowService.class);
 		flowPusher = context.getServiceImpl(IFlowPusherService.class);
+		datagrid = context.getServiceImpl(IDatagridService.class);
 		
 		floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
 		
@@ -190,17 +197,46 @@
 		Ethernet eth = IFloodlightProviderService.bcStore.
 				get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
 		
-		// We only want to handle unicast IPv4
-		if (eth.isBroadcast() || eth.isMulticast() || 
-				eth.getEtherType() != Ethernet.TYPE_IPv4) {
+		if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
 			return Command.CONTINUE;
 		}
 		
-		handlePacketIn(sw, pi, eth);
+		if (eth.isBroadcast() || eth.isMulticast()) {
+			handleBroadcast(sw, pi, eth);
+			//return Command.CONTINUE;
+		}
+		else {
+			// Unicast
+			handlePacketIn(sw, pi, eth);
+		}
 		
 		return Command.STOP;
 	}
 	
+	private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
+		if (log.isTraceEnabled()) {
+			log.trace("Sending broadcast packet to other ONOS instances");
+		}
+		
+		IPv4 ipv4Packet = (IPv4) eth.getPayload();
+		
+		// TODO We'll put the destination address here, because the current
+		// architecture needs an address. Addresses are only used for replies
+		// however, which don't apply to non-ARP packets. The ArpMessage class
+		// has become a bit too overloaded and should be refactored to 
+		// handle all use cases nicely.
+		 InetAddress targetAddress = 
+				InetAddresses.fromInteger(ipv4Packet.getDestinationAddress());
+		
+		// Piggy-back on the ARP mechanism to broadcast this packet out the
+		// edge. Luckily the ARP module doesn't check that the packet is
+		// actually ARP before broadcasting, so we can trick it into sending
+		// our non-ARP packets.
+		// TODO This should be refactored later to account for the new use case.
+		datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(),
+				sw.getId(), pi.getInPort()));
+	}
+	
 	private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
 		String destinationMac = 
 				HexString.toHexString(eth.getDestinationMACAddress()); 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ArpMessage.java b/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ArpMessage.java
index ee8f23d..44b9ea0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ArpMessage.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ArpMessage.java
@@ -4,38 +4,40 @@
 import java.net.InetAddress;
 import net.floodlightcontroller.util.MACAddress;
 
+// TODO This is getting very messy!!! Needs refactoring
 public class ArpMessage implements Serializable {
 
-	/**
-	 * 
-	 */
 	private static final long serialVersionUID = 1L;
 	
 	private final Type type;
 	private final InetAddress forAddress;
 	private final byte[] packetData;
 	
-	//ARP reply message needs MAC info
+	// ARP reply message needs MAC info
 	private final MACAddress mac;
-	//only send the ARP request message to the device attachment needs the attachment switch and port. 
+	// Only send the ARP request message to the device attachment needs the 
+	// attachment switch and port. 
 	private final long outSwitch; 
 	private final short outPort;
 	
-
+	private final long inSwitch;
+	private final short inPort;
 
 	public enum Type {
 		REQUEST,
 		REPLY
 	}
 	
-	private ArpMessage(Type type, InetAddress address, byte[] eth) {
-		// TODO Auto-generated constructor stub
+	private ArpMessage(Type type, InetAddress address, byte[] eth, 
+			long outSwitch, short outPort, long inSwitch, short inPort) {
 		this.type = type;
 		this.forAddress = address;
 		this.packetData = eth;
 		this.mac = null;
 		this.outSwitch = -1;
 		this.outPort = -1;
+		this.inSwitch = inSwitch;
+		this.inPort = inPort;
 	}
 	
 	private ArpMessage(Type type, InetAddress address) {
@@ -46,6 +48,8 @@
 		this.outSwitch = -1;
 		this.outPort = -1;
 		
+		this.inSwitch = -1;
+		this.inPort = -1;
 	}
 	// the ARP reply message with MAC
 	private ArpMessage(Type type, InetAddress address, MACAddress mac) {
@@ -55,6 +59,9 @@
 		this.mac = mac;
 		this.outSwitch = -1;
 		this.outPort = -1;
+		
+		this.inSwitch = -1;
+		this.inPort = -1;
 	}
 	
 	// construct ARP request message with attachment switch and port
@@ -66,24 +73,32 @@
 		this.mac = null;
 		this.outSwitch = outSwitch; 
 		this.outPort = outPort;	
+		
+		this.inSwitch = -1;
+		this.inPort = -1;
 	}
 
-	public static ArpMessage newRequest(InetAddress forAddress, byte[] arpRequest) {
-		return new ArpMessage(Type.REQUEST, forAddress, arpRequest);
+	// TODO Awful quick fix - caller has to supply dummy outSwitch and outPort
+	public static ArpMessage newRequest(InetAddress forAddress, byte[] arpRequest,
+			long outSwitch, short outPort, long inSwitch, short inPort) {
+		return new ArpMessage(Type.REQUEST, forAddress, arpRequest, 
+				outSwitch, outPort, inSwitch, inPort);
 	}
 	
 	public static ArpMessage newReply(InetAddress forAddress) {
 		return new ArpMessage(Type.REPLY, forAddress);
 	}
+	
 	//ARP reply message with MAC
 	public static ArpMessage newReply(InetAddress forAddress, MACAddress mac) {
 		return new ArpMessage(Type.REPLY, forAddress, mac);
-
 	}
-	//ARP reqsuest message with attachment switch and port
-	public static ArpMessage newRequest(InetAddress forAddress, byte[] arpRequest, long outSwitch, short outPort ) {
-		return new ArpMessage(Type.REQUEST, forAddress, arpRequest, outSwitch, outPort);
-
+	
+	//ARP request message with attachment switch and port
+	public static ArpMessage newRequest(InetAddress forAddress, 
+			byte[] arpRequest, long outSwitch, short outPort ) {
+		return new ArpMessage(Type.REQUEST, forAddress, arpRequest, outSwitch, 
+				outPort);
 	}
 
 	public Type getType() {
@@ -97,6 +112,7 @@
 	public byte[] getPacket() {
 		return packetData;
 	}
+	
 	public MACAddress getMAC() {
 		return mac;
 	}
@@ -108,5 +124,12 @@
 	public short getOutPort() {
 		return outPort;
 	}
+	
+	public long getInSwitch() {
+		return inSwitch;
+	}
 
+	public short getInPort() {
+		return inPort;
+	}
 }
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 76723fa..3962efb 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/proxyarp/ProxyArpManager.java
@@ -299,10 +299,12 @@
 					handleArpReply(sw, pi, arp);
 					sendToOtherNodesReply(eth, pi);
 			}
+			
+			// Stop ARP packets here
+			return Command.STOP;
 		}
 		
-		//TODO should we propagate ARP or swallow it?
-		//Always propagate for now so DeviceManager can learn the host location
+		// Propagate everything else
 		return Command.CONTINUE;
 	}
 	
@@ -361,7 +363,7 @@
 
 			if(!outPorts.iterator().hasNext()){
 				log.debug("outPort : null");
-				sendToOtherNodes(eth, pi);
+				sendToOtherNodes(eth, sw.getId(), pi);
 			}else{
 
 				for (IPortObject portObject : outPorts) {
@@ -386,12 +388,11 @@
 			log.debug("The Device info in DB is {} for IP {}", targetDevice, inetAddressToString(arp.getTargetProtocolAddress()));
 
 			// We don't know the device so broadcast the request out
-			sendToOtherNodes(eth, pi);
+			sendToOtherNodes(eth, sw.getId(), pi);
 		}
  
 	}
 	
-	@SuppressWarnings("unused")
 	private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
 		if (log.isTraceEnabled()) {
 			log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] { 
@@ -512,7 +513,7 @@
 		}
 	}
 	
-	private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
+	private void sendToOtherNodes(Ethernet eth, long inSwitchId, OFPacketIn pi) {
 		ARP arp = (ARP) eth.getPayload();
 		
 		if (log.isTraceEnabled()) {
@@ -528,7 +529,8 @@
 			return;
 		}
 		
-		datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
+		datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(),
+				-1L, (short)-1, inSwitchId, pi.getInPort()));
 	}
 	//hazelcast to other ONOS instances to send the ARP packet out on outPort of outSwitch
 	private void sendToOtherNodes(Ethernet eth, OFPacketIn pi, long outSwitch, short outPort) {
@@ -624,7 +626,8 @@
 		}
 	}
 	
-	private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
+	private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
+			long inSwitch, short inPort) {
 		List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
 		
 		for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
@@ -644,9 +647,17 @@
 			
 			for (IPortObject portObject : ports) {
 				if (!portObject.getLinkedPorts().iterator().hasNext()) {
+					short portNumber = portObject.getNumber();
+					
+					if (sw.getId() == inSwitch && portNumber == inPort) {
+						// This is the port that the ARP message came in,
+						// so don't broadcast out this port
+						continue;
+					}
+					
 					switchPorts.add(new SwitchPort(new Dpid(sw.getId()), 
-							new Port(portObject.getNumber())));
-					actions.add(new OFActionOutput(portObject.getNumber()));
+							new Port(portNumber)));
+					actions.add(new OFActionOutput(portNumber));
 				}
 			}
 			
@@ -810,7 +821,8 @@
 		switch (arpMessage.getType()){
 		case REQUEST:
 			if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){	
-				broadcastArpRequestOutMyEdge(arpMessage.getPacket());					
+				broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
+						arpMessage.getInSwitch(), arpMessage.getInPort());					
 			}else{					
 				sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
 				log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
diff --git a/src/main/java/net/onrc/onos/ofcontroller/topology/ShortestPath.java b/src/main/java/net/onrc/onos/ofcontroller/topology/ShortestPath.java
index f187c27..9a8345c 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/topology/ShortestPath.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/topology/ShortestPath.java
@@ -69,8 +69,11 @@
 	//
 	// Test whether we are computing a path from/to the same DPID.
 	// If "yes", then just add a single flow entry in the return result.
+	// However, if the "in" and "out" ports are same, return null.
 	//
 	if (dpid_src.equals(dpid_dest)) {
+	    if (src.port().value() == dest.port().value())
+		return null;		// "In" and "Out" ports are same
 	    FlowEntry flowEntry = new FlowEntry();
 	    flowEntry.setDpid(src.dpid());
 	    flowEntry.setInPort(src.port());