alshabib
Committed by Gerrit Code Review

[FALCON] adding api to enable packet requests to a single device.

Change-Id: Id870c13ece658fe61f46194c1e795fb1d209a6a8
...@@ -19,9 +19,11 @@ import com.google.common.base.MoreObjects; ...@@ -19,9 +19,11 @@ import com.google.common.base.MoreObjects;
19 19
20 import org.onosproject.cluster.NodeId; 20 import org.onosproject.cluster.NodeId;
21 import org.onosproject.core.ApplicationId; 21 import org.onosproject.core.ApplicationId;
22 +import org.onosproject.net.DeviceId;
22 import org.onosproject.net.flow.TrafficSelector; 23 import org.onosproject.net.flow.TrafficSelector;
23 24
24 import java.util.Objects; 25 import java.util.Objects;
26 +import java.util.Optional;
25 27
26 /** 28 /**
27 * Default implementation of a packet request. 29 * Default implementation of a packet request.
...@@ -31,22 +33,24 @@ public final class DefaultPacketRequest implements PacketRequest { ...@@ -31,22 +33,24 @@ public final class DefaultPacketRequest implements PacketRequest {
31 private final PacketPriority priority; 33 private final PacketPriority priority;
32 private final ApplicationId appId; 34 private final ApplicationId appId;
33 private final NodeId nodeId; 35 private final NodeId nodeId;
36 + private final Optional<DeviceId> deviceId;
37 +
34 38
35 /** 39 /**
36 * Creates a new packet request. 40 * Creates a new packet request.
37 - * 41 + * @param selector traffic selector
38 - * @param selector traffic selector
39 * @param priority intercept priority 42 * @param priority intercept priority
40 * @param appId application id 43 * @param appId application id
41 * @param nodeId identifier of node where request originated 44 * @param nodeId identifier of node where request originated
45 + * @param deviceId device id
42 */ 46 */
43 public DefaultPacketRequest(TrafficSelector selector, PacketPriority priority, 47 public DefaultPacketRequest(TrafficSelector selector, PacketPriority priority,
44 - ApplicationId appId, 48 + ApplicationId appId, NodeId nodeId, Optional<DeviceId> deviceId) {
45 - NodeId nodeId) {
46 this.selector = selector; 49 this.selector = selector;
47 this.priority = priority; 50 this.priority = priority;
48 this.appId = appId; 51 this.appId = appId;
49 this.nodeId = nodeId; 52 this.nodeId = nodeId;
53 + this.deviceId = deviceId;
50 } 54 }
51 55
52 @Override 56 @Override
...@@ -64,6 +68,10 @@ public final class DefaultPacketRequest implements PacketRequest { ...@@ -64,6 +68,10 @@ public final class DefaultPacketRequest implements PacketRequest {
64 return appId; 68 return appId;
65 } 69 }
66 70
71 + public Optional<DeviceId> deviceId() {
72 + return deviceId;
73 + }
74 +
67 @Override 75 @Override
68 public NodeId nodeId() { 76 public NodeId nodeId() {
69 return nodeId; 77 return nodeId;
...@@ -71,7 +79,7 @@ public final class DefaultPacketRequest implements PacketRequest { ...@@ -71,7 +79,7 @@ public final class DefaultPacketRequest implements PacketRequest {
71 79
72 @Override 80 @Override
73 public int hashCode() { 81 public int hashCode() {
74 - return Objects.hash(selector, priority, appId, nodeId); 82 + return Objects.hash(selector, priority, appId, nodeId, deviceId);
75 } 83 }
76 84
77 @Override 85 @Override
...@@ -86,7 +94,8 @@ public final class DefaultPacketRequest implements PacketRequest { ...@@ -86,7 +94,8 @@ public final class DefaultPacketRequest implements PacketRequest {
86 return Objects.equals(this.selector, other.selector) 94 return Objects.equals(this.selector, other.selector)
87 && Objects.equals(this.priority, other.priority) 95 && Objects.equals(this.priority, other.priority)
88 && Objects.equals(this.appId, other.appId) 96 && Objects.equals(this.appId, other.appId)
89 - && Objects.equals(this.nodeId, other.nodeId); 97 + && Objects.equals(this.nodeId, other.nodeId)
98 + && Objects.equals(this.deviceId, other.deviceId);
90 } 99 }
91 100
92 @Override 101 @Override
...@@ -95,6 +104,8 @@ public final class DefaultPacketRequest implements PacketRequest { ...@@ -95,6 +104,8 @@ public final class DefaultPacketRequest implements PacketRequest {
95 .add("selector", selector) 104 .add("selector", selector)
96 .add("priority", priority) 105 .add("priority", priority)
97 .add("appId", appId) 106 .add("appId", appId)
98 - .add("nodeId", nodeId).toString(); 107 + .add("nodeId", nodeId)
108 + .add("applies to", deviceId.isPresent() ? deviceId.get() : "all")
109 + .toString();
99 } 110 }
100 } 111 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -17,8 +17,11 @@ package org.onosproject.net.packet; ...@@ -17,8 +17,11 @@ package org.onosproject.net.packet;
17 17
18 import org.onosproject.cluster.NodeId; 18 import org.onosproject.cluster.NodeId;
19 import org.onosproject.core.ApplicationId; 19 import org.onosproject.core.ApplicationId;
20 +import org.onosproject.net.DeviceId;
20 import org.onosproject.net.flow.TrafficSelector; 21 import org.onosproject.net.flow.TrafficSelector;
21 22
23 +import java.util.Optional;
24 +
22 /** 25 /**
23 * Represents a packet request made to devices. 26 * Represents a packet request made to devices.
24 */ 27 */
...@@ -51,4 +54,12 @@ public interface PacketRequest { ...@@ -51,4 +54,12 @@ public interface PacketRequest {
51 * @return an node id 54 * @return an node id
52 */ 55 */
53 NodeId nodeId(); 56 NodeId nodeId();
57 +
58 + /**
59 + * Obtains the optional device id.
60 + *
61 + * @return an optional containing a device id
62 + */
63 + Optional<DeviceId> deviceId();
64 +
54 } 65 }
......
...@@ -17,9 +17,11 @@ package org.onosproject.net.packet; ...@@ -17,9 +17,11 @@ package org.onosproject.net.packet;
17 17
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 import org.onosproject.core.ApplicationId; 19 import org.onosproject.core.ApplicationId;
20 +import org.onosproject.net.DeviceId;
20 import org.onosproject.net.flow.TrafficSelector; 21 import org.onosproject.net.flow.TrafficSelector;
21 22
22 import java.util.List; 23 import java.util.List;
24 +import java.util.Optional;
23 25
24 /** 26 /**
25 * Service for intercepting data plane packets and for emitting synthetic 27 * Service for intercepting data plane packets and for emitting synthetic
...@@ -66,9 +68,25 @@ public interface PacketService { ...@@ -66,9 +68,25 @@ public interface PacketService {
66 * @param priority the priority of the rule 68 * @param priority the priority of the rule
67 * @param appId the application ID of the requester 69 * @param appId the application ID of the requester
68 */ 70 */
71 + @Deprecated
69 void requestPackets(TrafficSelector selector, PacketPriority priority, 72 void requestPackets(TrafficSelector selector, PacketPriority priority,
70 ApplicationId appId); 73 ApplicationId appId);
71 74
75 +
76 + /**
77 + * Requests that packets matching the given selector are punted from the
78 + * dataplane to the controller. If a deviceId is specified then the
79 + * packet request is only installed at the device represented by that
80 + * deviceId.
81 + *
82 + * @param selector the traffic selector used to match packets
83 + * @param priority the priority of the rule
84 + * @param appId the application ID of the requester
85 + * @param deviceId an optional deviceId
86 + */
87 + void requestPackets(TrafficSelector selector, PacketPriority priority,
88 + ApplicationId appId, Optional<DeviceId> deviceId);
89 +
72 /** 90 /**
73 * Cancels previous packet requests for packets matching the given 91 * Cancels previous packet requests for packets matching the given
74 * selector to be punted from the dataplane to the controller. 92 * selector to be punted from the dataplane to the controller.
...@@ -77,10 +95,26 @@ public interface PacketService { ...@@ -77,10 +95,26 @@ public interface PacketService {
77 * @param priority the priority of the rule 95 * @param priority the priority of the rule
78 * @param appId the application ID of the requester 96 * @param appId the application ID of the requester
79 */ 97 */
98 + @Deprecated
80 void cancelPackets(TrafficSelector selector, PacketPriority priority, 99 void cancelPackets(TrafficSelector selector, PacketPriority priority,
81 ApplicationId appId); 100 ApplicationId appId);
82 101
83 /** 102 /**
103 + * Cancels previous packet requests for packets matching the given
104 + * selector to be punted from the dataplane to the controller. If a
105 + * deviceId is specified then the packet request is only withdrawn from
106 + * the device represented by that deviceId.
107 + *
108 + * @param selector the traffic selector used to match packets
109 + * @param priority the priority of the rule
110 + * @param appId the application ID of the requester
111 + * @param deviceId an optional deviceId
112 + */
113 + void cancelPackets(TrafficSelector selector, PacketPriority priority,
114 + ApplicationId appId, Optional<DeviceId> deviceId);
115 +
116 +
117 + /**
84 * Returns list of all existing requests ordered by priority. 118 * Returns list of all existing requests ordered by priority.
85 * 119 *
86 * @return list of existing packet requests 120 * @return list of existing packet requests
......
...@@ -24,6 +24,8 @@ import org.onosproject.net.flow.TrafficSelector; ...@@ -24,6 +24,8 @@ import org.onosproject.net.flow.TrafficSelector;
24 24
25 import com.google.common.testing.EqualsTester; 25 import com.google.common.testing.EqualsTester;
26 26
27 +import java.util.Optional;
28 +
27 import static org.hamcrest.MatcherAssert.assertThat; 29 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.is; 30 import static org.hamcrest.Matchers.is;
29 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; 31 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
...@@ -42,27 +44,27 @@ public class DefaultPacketRequestTest { ...@@ -42,27 +44,27 @@ public class DefaultPacketRequestTest {
42 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), 44 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
43 PacketPriority.CONTROL, 45 PacketPriority.CONTROL,
44 NetTestTools.APP_ID, 46 NetTestTools.APP_ID,
45 - NetTestTools.NODE_ID); 47 + NetTestTools.NODE_ID, Optional.empty());
46 private final DefaultPacketRequest sameAsacketRequest1 = 48 private final DefaultPacketRequest sameAsacketRequest1 =
47 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), 49 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
48 PacketPriority.CONTROL, 50 PacketPriority.CONTROL,
49 NetTestTools.APP_ID, 51 NetTestTools.APP_ID,
50 - NetTestTools.NODE_ID); 52 + NetTestTools.NODE_ID, Optional.empty());
51 private final DefaultPacketRequest packetRequest2 = 53 private final DefaultPacketRequest packetRequest2 =
52 new DefaultPacketRequest(selector, 54 new DefaultPacketRequest(selector,
53 PacketPriority.CONTROL, 55 PacketPriority.CONTROL,
54 NetTestTools.APP_ID, 56 NetTestTools.APP_ID,
55 - NetTestTools.NODE_ID); 57 + NetTestTools.NODE_ID, Optional.empty());
56 private final DefaultPacketRequest packetRequest3 = 58 private final DefaultPacketRequest packetRequest3 =
57 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), 59 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
58 PacketPriority.REACTIVE, 60 PacketPriority.REACTIVE,
59 NetTestTools.APP_ID, 61 NetTestTools.APP_ID,
60 - NetTestTools.NODE_ID); 62 + NetTestTools.NODE_ID, Optional.empty());
61 private final DefaultPacketRequest packetRequest4 = 63 private final DefaultPacketRequest packetRequest4 =
62 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), 64 new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(),
63 PacketPriority.CONTROL, 65 PacketPriority.CONTROL,
64 new DefaultApplicationId(1, "foo"), 66 new DefaultApplicationId(1, "foo"),
65 - new NodeId("node1")); 67 + new NodeId("node1"), Optional.empty());
66 68
67 /** 69 /**
68 * Tests the operation of the equals(), toAstring() and hashCode() methods. 70 * Tests the operation of the equals(), toAstring() and hashCode() methods.
......
...@@ -16,9 +16,11 @@ ...@@ -16,9 +16,11 @@
16 package org.onosproject.net.packet; 16 package org.onosproject.net.packet;
17 17
18 import org.onosproject.core.ApplicationId; 18 import org.onosproject.core.ApplicationId;
19 +import org.onosproject.net.DeviceId;
19 import org.onosproject.net.flow.TrafficSelector; 20 import org.onosproject.net.flow.TrafficSelector;
20 21
21 import java.util.List; 22 import java.util.List;
23 +import java.util.Optional;
22 24
23 /** 25 /**
24 * Test adapter for packet service. 26 * Test adapter for packet service.
...@@ -43,11 +45,25 @@ public class PacketServiceAdapter implements PacketService { ...@@ -43,11 +45,25 @@ public class PacketServiceAdapter implements PacketService {
43 } 45 }
44 46
45 @Override 47 @Override
46 - public void requestPackets(TrafficSelector selector, PacketPriority priority, ApplicationId appId) { 48 + public void requestPackets(TrafficSelector selector, PacketPriority priority,
49 + ApplicationId appId) {
47 } 50 }
48 51
49 @Override 52 @Override
50 - public void cancelPackets(TrafficSelector selector, PacketPriority priority, ApplicationId appId) { 53 + public void requestPackets(TrafficSelector selector, PacketPriority priority,
54 + ApplicationId appId, Optional<DeviceId> deviceId) {
55 +
56 + }
57 +
58 + @Override
59 + public void cancelPackets(TrafficSelector selector, PacketPriority priority,
60 + ApplicationId appId) {
61 + }
62 +
63 + @Override
64 + public void cancelPackets(TrafficSelector selector, PacketPriority priority,
65 + ApplicationId appId, Optional<DeviceId> deviceId) {
66 +
51 } 67 }
52 68
53 @Override 69 @Override
......
...@@ -29,6 +29,7 @@ import org.onosproject.cluster.NodeId; ...@@ -29,6 +29,7 @@ import org.onosproject.cluster.NodeId;
29 import org.onosproject.core.ApplicationId; 29 import org.onosproject.core.ApplicationId;
30 import org.onosproject.core.CoreService; 30 import org.onosproject.core.CoreService;
31 import org.onosproject.net.Device; 31 import org.onosproject.net.Device;
32 +import org.onosproject.net.DeviceId;
32 import org.onosproject.net.device.DeviceEvent; 33 import org.onosproject.net.device.DeviceEvent;
33 import org.onosproject.net.device.DeviceListener; 34 import org.onosproject.net.device.DeviceListener;
34 import org.onosproject.net.device.DeviceService; 35 import org.onosproject.net.device.DeviceService;
...@@ -60,6 +61,7 @@ import org.onosproject.net.provider.AbstractProviderService; ...@@ -60,6 +61,7 @@ import org.onosproject.net.provider.AbstractProviderService;
60 import org.slf4j.Logger; 61 import org.slf4j.Logger;
61 62
62 import java.util.List; 63 import java.util.List;
64 +import java.util.Optional;
63 import java.util.concurrent.ExecutorService; 65 import java.util.concurrent.ExecutorService;
64 import java.util.concurrent.Executors; 66 import java.util.concurrent.Executors;
65 67
...@@ -175,18 +177,49 @@ public class PacketManager ...@@ -175,18 +177,49 @@ public class PacketManager
175 checkNotNull(selector, "Selector cannot be null"); 177 checkNotNull(selector, "Selector cannot be null");
176 checkNotNull(appId, "Application ID cannot be null"); 178 checkNotNull(appId, "Application ID cannot be null");
177 179
178 - PacketRequest request = new DefaultPacketRequest(selector, priority, appId, localNodeId); 180 + PacketRequest request = new DefaultPacketRequest(selector, priority, appId,
181 + localNodeId, Optional.empty());
179 store.requestPackets(request); 182 store.requestPackets(request);
180 } 183 }
181 184
182 @Override 185 @Override
186 + public void requestPackets(TrafficSelector selector, PacketPriority priority,
187 + ApplicationId appId, Optional<DeviceId> deviceId) {
188 + checkPermission(PACKET_READ);
189 + checkNotNull(selector, "Selector cannot be null");
190 + checkNotNull(appId, "Application ID cannot be null");
191 +
192 + PacketRequest request =
193 + new DefaultPacketRequest(selector, priority, appId,
194 + localNodeId, deviceId);
195 +
196 + store.requestPackets(request);
197 +
198 + }
199 +
200 + @Override
183 public void cancelPackets(TrafficSelector selector, PacketPriority priority, 201 public void cancelPackets(TrafficSelector selector, PacketPriority priority,
184 ApplicationId appId) { 202 ApplicationId appId) {
185 checkPermission(PACKET_READ); 203 checkPermission(PACKET_READ);
186 checkNotNull(selector, "Selector cannot be null"); 204 checkNotNull(selector, "Selector cannot be null");
187 checkNotNull(appId, "Application ID cannot be null"); 205 checkNotNull(appId, "Application ID cannot be null");
188 206
189 - PacketRequest request = new DefaultPacketRequest(selector, priority, appId, localNodeId); 207 +
208 + PacketRequest request = new DefaultPacketRequest(selector, priority, appId,
209 + localNodeId, Optional.empty());
210 + store.cancelPackets(request);
211 + }
212 +
213 + @Override
214 + public void cancelPackets(TrafficSelector selector, PacketPriority priority,
215 + ApplicationId appId, Optional<DeviceId> deviceId) {
216 + checkPermission(PACKET_READ);
217 + checkNotNull(selector, "Selector cannot be null");
218 + checkNotNull(appId, "Application ID cannot be null");
219 +
220 + PacketRequest request = new DefaultPacketRequest(selector, priority,
221 + appId, localNodeId,
222 + deviceId);
190 store.cancelPackets(request); 223 store.cancelPackets(request);
191 } 224 }
192 225
...@@ -203,7 +236,12 @@ public class PacketManager ...@@ -203,7 +236,12 @@ public class PacketManager
203 private void pushRulesToDevice(Device device) { 236 private void pushRulesToDevice(Device device) {
204 log.debug("Pushing packet requests to device {}", device.id()); 237 log.debug("Pushing packet requests to device {}", device.id());
205 for (PacketRequest request : store.existingRequests()) { 238 for (PacketRequest request : store.existingRequests()) {
206 - pushRule(device, request); 239 + if (!request.deviceId().isPresent()) {
240 + pushRule(device, request);
241 + } else if (request.deviceId().get().equals(device.id())) {
242 + pushRule(device, request);
243 + }
244 +
207 } 245 }
208 } 246 }
209 247
...@@ -332,6 +370,7 @@ public class PacketManager ...@@ -332,6 +370,7 @@ public class PacketManager
332 370
333 } 371 }
334 372
373 +
335 /** 374 /**
336 * Internal callback from the packet store. 375 * Internal callback from the packet store.
337 */ 376 */
...@@ -343,12 +382,24 @@ public class PacketManager ...@@ -343,12 +382,24 @@ public class PacketManager
343 382
344 @Override 383 @Override
345 public void requestPackets(PacketRequest request) { 384 public void requestPackets(PacketRequest request) {
346 - pushToAllDevices(request); 385 + DeviceId deviceid = request.deviceId().orElse(null);
386 +
387 + if (deviceid != null) {
388 + pushRule(deviceService.getDevice(deviceid), request);
389 + } else {
390 + pushToAllDevices(request);
391 + }
347 } 392 }
348 393
349 @Override 394 @Override
350 public void cancelPackets(PacketRequest request) { 395 public void cancelPackets(PacketRequest request) {
351 - removeFromAllDevices(request); 396 + DeviceId deviceid = request.deviceId().orElse(null);
397 +
398 + if (deviceid != null) {
399 + removeRule(deviceService.getDevice(deviceid), request);
400 + } else {
401 + removeFromAllDevices(request);
402 + }
352 } 403 }
353 } 404 }
354 405
......