Committed by
Gerrit Code Review
Unit tests for route store.
Change-Id: Ia711c497bb7d0751d692c2461c884ddc5287a2ef
Showing
6 changed files
with
107 additions
and
17 deletions
... | @@ -20,6 +20,8 @@ import org.onlab.packet.IpAddress; | ... | @@ -20,6 +20,8 @@ import org.onlab.packet.IpAddress; |
20 | import org.onlab.packet.IpPrefix; | 20 | import org.onlab.packet.IpPrefix; |
21 | import org.onlab.packet.MacAddress; | 21 | import org.onlab.packet.MacAddress; |
22 | 22 | ||
23 | +import java.util.Objects; | ||
24 | + | ||
23 | import static com.google.common.base.MoreObjects.toStringHelper; | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
24 | 26 | ||
25 | /** | 27 | /** |
... | @@ -84,6 +86,28 @@ public class ResolvedRoute { | ... | @@ -84,6 +86,28 @@ public class ResolvedRoute { |
84 | } | 86 | } |
85 | 87 | ||
86 | @Override | 88 | @Override |
89 | + public int hashCode() { | ||
90 | + return Objects.hash(prefix, nextHop, nextHopMac); | ||
91 | + } | ||
92 | + | ||
93 | + @Override | ||
94 | + public boolean equals(Object other) { | ||
95 | + if (this == other) { | ||
96 | + return true; | ||
97 | + } | ||
98 | + | ||
99 | + if (!(other instanceof ResolvedRoute)) { | ||
100 | + return false; | ||
101 | + } | ||
102 | + | ||
103 | + ResolvedRoute that = (ResolvedRoute) other; | ||
104 | + | ||
105 | + return Objects.equals(this.prefix, that.prefix) && | ||
106 | + Objects.equals(this.nextHop, that.nextHop) && | ||
107 | + Objects.equals(this.nextHopMac, that.nextHopMac); | ||
108 | + } | ||
109 | + | ||
110 | + @Override | ||
87 | public String toString() { | 111 | public String toString() { |
88 | return toStringHelper(this) | 112 | return toStringHelper(this) |
89 | .add("prefix", prefix) | 113 | .add("prefix", prefix) | ... | ... |
... | @@ -18,6 +18,8 @@ package org.onosproject.incubator.net.routing; | ... | @@ -18,6 +18,8 @@ package org.onosproject.incubator.net.routing; |
18 | 18 | ||
19 | import org.onosproject.event.AbstractEvent; | 19 | import org.onosproject.event.AbstractEvent; |
20 | 20 | ||
21 | +import java.util.Objects; | ||
22 | + | ||
21 | /** | 23 | /** |
22 | * Describes an event about a route. | 24 | * Describes an event about a route. |
23 | */ | 25 | */ |
... | @@ -65,4 +67,24 @@ public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> { | ... | @@ -65,4 +67,24 @@ public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> { |
65 | super(type, subject, time); | 67 | super(type, subject, time); |
66 | } | 68 | } |
67 | 69 | ||
70 | + @Override | ||
71 | + public int hashCode() { | ||
72 | + return Objects.hash(subject(), type()); | ||
73 | + } | ||
74 | + | ||
75 | + @Override | ||
76 | + public boolean equals(Object other) { | ||
77 | + if (this == other) { | ||
78 | + return true; | ||
79 | + } | ||
80 | + | ||
81 | + if (!(other instanceof RouteEvent)) { | ||
82 | + return false; | ||
83 | + } | ||
84 | + | ||
85 | + RouteEvent that = (RouteEvent) other; | ||
86 | + | ||
87 | + return Objects.equals(this.subject(), that.subject()) && | ||
88 | + Objects.equals(this.type(), that.type()); | ||
89 | + } | ||
68 | } | 90 | } | ... | ... |
incubator/net/src/main/java/org/onosproject/incubator/net/routing/impl/ListenerQueue.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2016-present Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.routing.impl; | ||
18 | + | ||
19 | +import org.onosproject.incubator.net.routing.RouteEvent; | ||
20 | + | ||
21 | +/** | ||
22 | + * Queues updates for a route listener to ensure they are received in the | ||
23 | + * correct order. | ||
24 | + */ | ||
25 | +interface ListenerQueue { | ||
26 | + | ||
27 | + /** | ||
28 | + * Posts an event to the listener. | ||
29 | + * | ||
30 | + * @param event event | ||
31 | + */ | ||
32 | + void post(RouteEvent event); | ||
33 | + | ||
34 | + /** | ||
35 | + * Initiates event delivery to the listener. | ||
36 | + */ | ||
37 | + void start(); | ||
38 | + | ||
39 | + /** | ||
40 | + * Halts event delivery to the listener. | ||
41 | + */ | ||
42 | + void stop(); | ||
43 | +} |
... | @@ -114,7 +114,7 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>, | ... | @@ -114,7 +114,7 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>, |
114 | public void addListener(RouteListener listener) { | 114 | public void addListener(RouteListener listener) { |
115 | synchronized (this) { | 115 | synchronized (this) { |
116 | log.debug("Synchronizing current routes to new listener"); | 116 | log.debug("Synchronizing current routes to new listener"); |
117 | - ListenerQueue l = new ListenerQueue(listener); | 117 | + ListenerQueue l = createListenerQueue(listener); |
118 | routeStore.getRouteTables().forEach(table -> { | 118 | routeStore.getRouteTables().forEach(table -> { |
119 | Collection<Route> routes = routeStore.getRoutes(table); | 119 | Collection<Route> routes = routeStore.getRoutes(table); |
120 | if (routes != null) { | 120 | if (routes != null) { |
... | @@ -234,10 +234,19 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>, | ... | @@ -234,10 +234,19 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>, |
234 | } | 234 | } |
235 | 235 | ||
236 | /** | 236 | /** |
237 | - * Queues updates for a route listener to ensure they are received in the | 237 | + * Creates a new listener queue. |
238 | - * correct order. | 238 | + * |
239 | + * @param listener route listener | ||
240 | + * @return listener queue | ||
239 | */ | 241 | */ |
240 | - private class ListenerQueue { | 242 | + ListenerQueue createListenerQueue(RouteListener listener) { |
243 | + return new DefaultListenerQueue(listener); | ||
244 | + } | ||
245 | + | ||
246 | + /** | ||
247 | + * Default route listener queue. | ||
248 | + */ | ||
249 | + private class DefaultListenerQueue implements ListenerQueue { | ||
241 | 250 | ||
242 | private final ExecutorService executorService; | 251 | private final ExecutorService executorService; |
243 | private final BlockingQueue<RouteEvent> queue; | 252 | private final BlockingQueue<RouteEvent> queue; |
... | @@ -248,31 +257,23 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>, | ... | @@ -248,31 +257,23 @@ public class RouteManager implements ListenerService<RouteEvent, RouteListener>, |
248 | * | 257 | * |
249 | * @param listener route listener to queue updates for | 258 | * @param listener route listener to queue updates for |
250 | */ | 259 | */ |
251 | - public ListenerQueue(RouteListener listener) { | 260 | + public DefaultListenerQueue(RouteListener listener) { |
252 | this.listener = listener; | 261 | this.listener = listener; |
253 | queue = new LinkedBlockingQueue<>(); | 262 | queue = new LinkedBlockingQueue<>(); |
254 | executorService = newSingleThreadExecutor(threadFactory); | 263 | executorService = newSingleThreadExecutor(threadFactory); |
255 | } | 264 | } |
256 | 265 | ||
257 | - /** | 266 | + @Override |
258 | - * Posts and event to the listener. | ||
259 | - * | ||
260 | - * @param event event | ||
261 | - */ | ||
262 | public void post(RouteEvent event) { | 267 | public void post(RouteEvent event) { |
263 | queue.add(event); | 268 | queue.add(event); |
264 | } | 269 | } |
265 | 270 | ||
266 | - /** | 271 | + @Override |
267 | - * Initiates event delivery to the listener. | ||
268 | - */ | ||
269 | public void start() { | 272 | public void start() { |
270 | executorService.execute(this::poll); | 273 | executorService.execute(this::poll); |
271 | } | 274 | } |
272 | 275 | ||
273 | - /** | 276 | + @Override |
274 | - * Halts event delivery to the listener. | ||
275 | - */ | ||
276 | public void stop() { | 277 | public void stop() { |
277 | executorService.shutdown(); | 278 | executorService.shutdown(); |
278 | } | 279 | } | ... | ... |
incubator/net/src/test/java/org/onosproject/incubator/net/routing/impl/RouteManagerTest.java
0 → 100644
This diff is collapsed. Click to expand it.
... | @@ -66,7 +66,7 @@ public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegat | ... | @@ -66,7 +66,7 @@ public class LocalRouteStore extends AbstractStore<RouteEvent, RouteStoreDelegat |
66 | private Map<IpAddress, MacAddress> nextHops = new ConcurrentHashMap<>(); | 66 | private Map<IpAddress, MacAddress> nextHops = new ConcurrentHashMap<>(); |
67 | 67 | ||
68 | @Activate | 68 | @Activate |
69 | - protected void activate() { | 69 | + public void activate() { |
70 | routeTables = new ConcurrentHashMap<>(); | 70 | routeTables = new ConcurrentHashMap<>(); |
71 | 71 | ||
72 | routeTables.put(IPV4, new RouteTable()); | 72 | routeTables.put(IPV4, new RouteTable()); | ... | ... |
-
Please register or login to post a comment