Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 18 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 19 | import org.onlab.util.Tools; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 20 | import org.onosproject.event.AbstractEvent; |
| 21 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 24 | import java.util.Objects; |
| 25 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Describes an event about a route. |
| 30 | */ |
| 31 | public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> { |
| 32 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 33 | private final ResolvedRoute prevSubject; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 34 | private final Collection<ResolvedRoute> alternativeRoutes; |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 35 | private final Collection<ResolvedRoute> prevAlternativeRoutes; |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 36 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Route event type. |
| 39 | */ |
| 40 | public enum Type { |
| 41 | |
| 42 | /** |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 43 | * Route is new and the next hop is resolved. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 44 | * <p> |
| 45 | * The subject of this event should be the route being added. |
| 46 | * The prevSubject of this event should be null. |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 47 | * </p> |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 48 | */ |
| 49 | ROUTE_ADDED, |
| 50 | |
| 51 | /** |
| 52 | * Route has updated information. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 53 | * <p> |
| 54 | * The subject of this event should be the new route. |
| 55 | * The prevSubject of this event should be the old route. |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 56 | * </p> |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 57 | */ |
| 58 | ROUTE_UPDATED, |
| 59 | |
| 60 | /** |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 61 | * Route was removed or the next hop becomes unresolved. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 62 | * <p> |
| 63 | * The subject of this event should be the route being removed. |
| 64 | * The prevSubject of this event should be null. |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 65 | * </p> |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 66 | */ |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 67 | ROUTE_REMOVED, |
| 68 | |
| 69 | /** |
| 70 | * The set of alternative routes for the subject's prefix has changed, |
| 71 | * but the best route is still the same. |
| 72 | * <p> |
| 73 | * The subject is the best route for the prefix (which has already been |
| 74 | * notified in a previous event). |
| 75 | * The prevSubject of this event is null. |
| 76 | * The alternatives contains the new set of alternative routes. |
| 77 | * </p> |
| 78 | */ |
| 79 | ALTERNATIVE_ROUTES_CHANGED |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 83 | * Creates a new route event without specifying previous subject. |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 84 | * |
| 85 | * @param type event type |
| 86 | * @param subject event subject |
| 87 | */ |
| 88 | public RouteEvent(Type type, ResolvedRoute subject) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 89 | this(type, subject, null, Collections.emptySet()); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a new route event without specifying previous subject. |
| 94 | * |
| 95 | * @param type event type |
| 96 | * @param subject event subject |
| 97 | * @param alternatives alternative routes for subject's prefix |
| 98 | */ |
| 99 | public RouteEvent(Type type, ResolvedRoute subject, Collection<ResolvedRoute> alternatives) { |
| 100 | this(type, subject, null, alternatives); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Creates a new route event. |
| 105 | * |
| 106 | * @param type event type |
| 107 | * @param subject event subject |
| 108 | * @param time event time |
| 109 | */ |
| 110 | protected RouteEvent(Type type, ResolvedRoute subject, long time) { |
| 111 | super(type, subject, time); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 112 | this.prevSubject = null; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 113 | this.alternativeRoutes = Collections.emptySet(); |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 114 | this.prevAlternativeRoutes = null; |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates a new route event with previous subject. |
| 119 | * |
| 120 | * @param type event type |
| 121 | * @param subject event subject |
| 122 | * @param prevSubject previous subject |
| 123 | */ |
| 124 | public RouteEvent(Type type, ResolvedRoute subject, ResolvedRoute prevSubject) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 125 | this(type, subject, prevSubject, Collections.emptySet()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates a new route event with a previous subject and alternative routes. |
| 130 | * |
| 131 | * @param type event type |
| 132 | * @param subject event subject |
| 133 | * @param prevSubject previous subject |
| 134 | * @param alternatives alternative routes for subject's prefix |
| 135 | */ |
| 136 | public RouteEvent(Type type, ResolvedRoute subject, ResolvedRoute prevSubject, |
| 137 | Collection<ResolvedRoute> alternatives) { |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 138 | this(type, subject, prevSubject, alternatives, null); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Creates a new route event with a previous subject, alternative routes and previous alternative |
| 143 | * routes. |
| 144 | * |
| 145 | * @param type event type |
| 146 | * @param subject event subject |
| 147 | * @param prevSubject previous subject |
| 148 | * @param alternatives alternative routes for subject's prefix |
| 149 | * @param prevAlternatives previous alternative routes for subject's prefix |
| 150 | */ |
| 151 | public RouteEvent(Type type, ResolvedRoute subject, ResolvedRoute prevSubject, |
| 152 | Collection<ResolvedRoute> alternatives, Collection<ResolvedRoute> prevAlternatives) { |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 153 | super(type, subject); |
| 154 | this.prevSubject = prevSubject; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 155 | this.alternativeRoutes = alternatives; |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 156 | this.prevAlternativeRoutes = prevAlternatives; |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the previous subject of the event. |
| 161 | * |
| 162 | * @return previous subject to which this event pertains |
| 163 | */ |
| 164 | public ResolvedRoute prevSubject() { |
| 165 | return prevSubject; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Returns the set of alternative routes for the subject's prefix. |
| 170 | * |
| 171 | * @return alternative routes |
| 172 | */ |
| 173 | public Collection<ResolvedRoute> alternatives() { |
| 174 | return alternativeRoutes; |
| 175 | } |
| 176 | |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Returns the set of previous alternative routes for the subject's prefix. |
| 179 | * |
| 180 | * @return previous alternative routes |
| 181 | */ |
| 182 | public Collection<ResolvedRoute> prevAlternatives() { |
| 183 | return prevAlternativeRoutes; |
| 184 | } |
| 185 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 186 | @Override |
| 187 | public int hashCode() { |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 188 | return Objects.hash(subject(), type(), prevSubject(), alternativeRoutes, prevAlternativeRoutes); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean equals(Object other) { |
| 193 | if (this == other) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | if (!(other instanceof RouteEvent)) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | RouteEvent that = (RouteEvent) other; |
| 202 | |
| 203 | return Objects.equals(this.subject(), that.subject()) && |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 204 | Objects.equals(this.type(), that.type()) && |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 205 | Objects.equals(this.prevSubject, that.prevSubject) && |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 206 | Objects.equals(this.alternativeRoutes, that.alternativeRoutes) && |
| 207 | Objects.equals(this.prevAlternativeRoutes, that.prevAlternativeRoutes); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public String toString() { |
| 212 | return toStringHelper(this) |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 213 | .add("time", Tools.defaultOffsetDataTime(time())) |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 214 | .add("type", type()) |
| 215 | .add("subject", subject()) |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 216 | .add("prevSubject", prevSubject) |
| 217 | .add("alternatives", alternativeRoutes) |
Charles Chan | bbe17f7 | 2017-08-23 16:33:06 -0700 | [diff] [blame] | 218 | .add("prevAlternatives", prevAlternativeRoutes) |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 219 | .toString(); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 220 | } |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 221 | } |