Including nodeId in DefaultPacketRequest so that we do not prematurely withdraw intercepts when a node is shutdown

Change-Id: If6ae0be8f53a4a158af37e6cc4938309a5e9991b
diff --git a/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java b/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java
index ce2eb11..605ab4b 100644
--- a/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java
+++ b/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java
@@ -16,6 +16,8 @@
 package org.onosproject.net.packet;
 
 import com.google.common.base.MoreObjects;
+
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.flow.TrafficSelector;
 
@@ -28,6 +30,7 @@
     private final TrafficSelector selector;
     private final PacketPriority priority;
     private final ApplicationId appId;
+    private final NodeId nodeId;
 
     /**
      * Creates a new packet request.
@@ -35,29 +38,40 @@
      * @param selector  traffic selector
      * @param priority  intercept priority
      * @param appId     application id
+     * @param nodeId    identifier of node where request originated
      */
     public DefaultPacketRequest(TrafficSelector selector, PacketPriority priority,
-                                ApplicationId appId) {
+                                ApplicationId appId,
+                                NodeId nodeId) {
         this.selector = selector;
         this.priority = priority;
         this.appId = appId;
+        this.nodeId = nodeId;
     }
 
+    @Override
     public TrafficSelector selector() {
         return selector;
     }
 
+    @Override
     public PacketPriority priority() {
         return priority;
     }
 
+    @Override
     public ApplicationId appId() {
         return appId;
     }
 
     @Override
+    public NodeId nodeId() {
+        return nodeId;
+    }
+
+    @Override
     public int hashCode() {
-        return Objects.hash(selector, priority, appId);
+        return Objects.hash(selector, priority, appId, nodeId);
     }
 
     @Override
@@ -71,7 +85,8 @@
         final DefaultPacketRequest other = (DefaultPacketRequest) obj;
         return Objects.equals(this.selector, other.selector)
                 && Objects.equals(this.priority, other.priority)
-                && Objects.equals(this.appId, other.appId);
+                && Objects.equals(this.appId, other.appId)
+                && Objects.equals(this.nodeId, other.nodeId);
     }
 
     @Override
@@ -79,6 +94,7 @@
         return MoreObjects.toStringHelper(this.getClass())
                 .add("selector", selector)
                 .add("priority", priority)
-                .add("appId", appId).toString();
+                .add("appId", appId)
+                .add("nodeId", nodeId).toString();
     }
 }
\ No newline at end of file
diff --git a/core/api/src/main/java/org/onosproject/net/packet/PacketRequest.java b/core/api/src/main/java/org/onosproject/net/packet/PacketRequest.java
index dc09219..32f7cd5 100644
--- a/core/api/src/main/java/org/onosproject/net/packet/PacketRequest.java
+++ b/core/api/src/main/java/org/onosproject/net/packet/PacketRequest.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.packet;
 
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.flow.TrafficSelector;
 
@@ -44,4 +45,10 @@
      */
     ApplicationId appId();
 
+    /**
+     * Obtain the node id.
+     *
+     * @return an node id
+     */
+    NodeId nodeId();
 }
diff --git a/core/api/src/test/java/org/onosproject/net/NetTestTools.java b/core/api/src/test/java/org/onosproject/net/NetTestTools.java
index 176fe40..1cb7825 100644
--- a/core/api/src/test/java/org/onosproject/net/NetTestTools.java
+++ b/core/api/src/test/java/org/onosproject/net/NetTestTools.java
@@ -18,6 +18,7 @@
 import org.onlab.junit.TestUtils;
 import org.onlab.packet.ChassisId;
 import org.onosproject.TestApplicationId;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.event.EventDeliveryService;
 import org.onosproject.net.provider.ProviderId;
@@ -44,6 +45,7 @@
 
     public static final ProviderId PID = new ProviderId("of", "foo");
     public static final ApplicationId APP_ID = new TestApplicationId("foo");
+    public static final NodeId NODE_ID = new NodeId("node1");
 
     // Short-hand for producing a device id from a string
     public static DeviceId did(String id) {
diff --git a/core/api/src/test/java/org/onosproject/net/packet/DefaultPacketRequestTest.java b/core/api/src/test/java/org/onosproject/net/packet/DefaultPacketRequestTest.java
index 592cd98..a7d07e8 100644
--- a/core/api/src/test/java/org/onosproject/net/packet/DefaultPacketRequestTest.java
+++ b/core/api/src/test/java/org/onosproject/net/packet/DefaultPacketRequestTest.java
@@ -16,6 +16,7 @@
 package org.onosproject.net.packet;
 
 import org.junit.Test;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.DefaultApplicationId;
 import org.onosproject.net.NetTestTools;
 import org.onosproject.net.flow.DefaultTrafficSelector;
@@ -40,23 +41,28 @@
     private final DefaultPacketRequest packetRequest1 =
             new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
                                      PacketPriority.CONTROL,
-                                     NetTestTools.APP_ID);
+                                     NetTestTools.APP_ID,
+                                     NetTestTools.NODE_ID);
     private final DefaultPacketRequest sameAsacketRequest1 =
             new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
                                      PacketPriority.CONTROL,
-                                     NetTestTools.APP_ID);
+                                     NetTestTools.APP_ID,
+                                     NetTestTools.NODE_ID);
     private final DefaultPacketRequest packetRequest2 =
             new DefaultPacketRequest(selector,
                                      PacketPriority.CONTROL,
-                                     NetTestTools.APP_ID);
+                                     NetTestTools.APP_ID,
+                                     NetTestTools.NODE_ID);
     private final DefaultPacketRequest packetRequest3 =
             new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
                                      PacketPriority.REACTIVE,
-                                     NetTestTools.APP_ID);
+                                     NetTestTools.APP_ID,
+                                     NetTestTools.NODE_ID);
     private final DefaultPacketRequest packetRequest4 =
             new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
                                      PacketPriority.CONTROL,
-                                     new DefaultApplicationId(1, "foo"));
+                                     new DefaultApplicationId(1, "foo"),
+                                     new NodeId("node1"));
 
     /**
      * Tests the operation of the equals(), toAstring() and hashCode() methods.
diff --git a/core/net/src/main/java/org/onosproject/net/packet/impl/PacketManager.java b/core/net/src/main/java/org/onosproject/net/packet/impl/PacketManager.java
index 793030f..79ac4b0 100644
--- a/core/net/src/main/java/org/onosproject/net/packet/impl/PacketManager.java
+++ b/core/net/src/main/java/org/onosproject/net/packet/impl/PacketManager.java
@@ -17,12 +17,15 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.net.Device;
@@ -87,6 +90,9 @@
     private CoreService coreService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private ClusterService clusterService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     private DeviceService deviceService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -105,11 +111,13 @@
     private final List<ProcessorEntry> processors = Lists.newCopyOnWriteArrayList();
 
     private ApplicationId appId;
+    private NodeId localNodeId;
 
     @Activate
     public void activate() {
         eventHandlingExecutor = Executors.newSingleThreadExecutor(
                 groupedThreads("onos/net/packet", "event-handler"));
+        localNodeId = clusterService.getLocalNode().id();
         appId = coreService.getAppId(CoreService.CORE_APP_NAME);
         store.setDelegate(delegate);
         deviceService.addListener(deviceListener);
@@ -167,7 +175,7 @@
         checkNotNull(selector, "Selector cannot be null");
         checkNotNull(appId, "Application ID cannot be null");
 
-        PacketRequest request = new DefaultPacketRequest(selector, priority, appId);
+        PacketRequest request = new DefaultPacketRequest(selector, priority, appId, localNodeId);
         store.requestPackets(request);
     }
 
@@ -178,7 +186,7 @@
         checkNotNull(selector, "Selector cannot be null");
         checkNotNull(appId, "Application ID cannot be null");
 
-        PacketRequest request = new DefaultPacketRequest(selector, priority, appId);
+        PacketRequest request = new DefaultPacketRequest(selector, priority, appId, localNodeId);
         store.cancelPackets(request);
     }