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 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 19 | import org.joda.time.LocalDateTime; |
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 | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 35 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Route event type. |
| 38 | */ |
| 39 | public enum Type { |
| 40 | |
| 41 | /** |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 42 | * Route is new and the next hop is resolved. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 43 | * <p> |
| 44 | * The subject of this event should be the route being added. |
| 45 | * The prevSubject of this event should be null. |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 46 | * </p> |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 47 | */ |
| 48 | ROUTE_ADDED, |
| 49 | |
| 50 | /** |
| 51 | * Route has updated information. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 52 | * <p> |
| 53 | * The subject of this event should be the new route. |
| 54 | * The prevSubject of this event should be the old route. |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 55 | * </p> |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 56 | */ |
| 57 | ROUTE_UPDATED, |
| 58 | |
| 59 | /** |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 60 | * Route was removed or the next hop becomes unresolved. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 61 | * <p> |
| 62 | * The subject of this event should be the route being removed. |
| 63 | * The prevSubject of this event should be null. |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 64 | * </p> |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 65 | */ |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 66 | ROUTE_REMOVED, |
| 67 | |
| 68 | /** |
| 69 | * The set of alternative routes for the subject's prefix has changed, |
| 70 | * but the best route is still the same. |
| 71 | * <p> |
| 72 | * The subject is the best route for the prefix (which has already been |
| 73 | * notified in a previous event). |
| 74 | * The prevSubject of this event is null. |
| 75 | * The alternatives contains the new set of alternative routes. |
| 76 | * </p> |
| 77 | */ |
| 78 | ALTERNATIVE_ROUTES_CHANGED |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 82 | * Creates a new route event without specifying previous subject. |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 83 | * |
| 84 | * @param type event type |
| 85 | * @param subject event subject |
| 86 | */ |
| 87 | public RouteEvent(Type type, ResolvedRoute subject) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 88 | this(type, subject, null, Collections.emptySet()); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates a new route event without specifying previous subject. |
| 93 | * |
| 94 | * @param type event type |
| 95 | * @param subject event subject |
| 96 | * @param alternatives alternative routes for subject's prefix |
| 97 | */ |
| 98 | public RouteEvent(Type type, ResolvedRoute subject, Collection<ResolvedRoute> alternatives) { |
| 99 | this(type, subject, null, alternatives); |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Creates a new route event. |
| 104 | * |
| 105 | * @param type event type |
| 106 | * @param subject event subject |
| 107 | * @param time event time |
| 108 | */ |
| 109 | protected RouteEvent(Type type, ResolvedRoute subject, long time) { |
| 110 | super(type, subject, time); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 111 | this.prevSubject = null; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 112 | |
| 113 | this.alternativeRoutes = Collections.emptySet(); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Creates a new route event with previous subject. |
| 118 | * |
| 119 | * @param type event type |
| 120 | * @param subject event subject |
| 121 | * @param prevSubject previous subject |
| 122 | */ |
| 123 | public RouteEvent(Type type, ResolvedRoute subject, ResolvedRoute prevSubject) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 124 | this(type, subject, prevSubject, Collections.emptySet()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Creates a new route event with a previous subject and alternative routes. |
| 129 | * |
| 130 | * @param type event type |
| 131 | * @param subject event subject |
| 132 | * @param prevSubject previous subject |
| 133 | * @param alternatives alternative routes for subject's prefix |
| 134 | */ |
| 135 | public RouteEvent(Type type, ResolvedRoute subject, ResolvedRoute prevSubject, |
| 136 | Collection<ResolvedRoute> alternatives) { |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 137 | super(type, subject); |
| 138 | this.prevSubject = prevSubject; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 139 | this.alternativeRoutes = alternatives; |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the previous subject of the event. |
| 144 | * |
| 145 | * @return previous subject to which this event pertains |
| 146 | */ |
| 147 | public ResolvedRoute prevSubject() { |
| 148 | return prevSubject; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Returns the set of alternative routes for the subject's prefix. |
| 153 | * |
| 154 | * @return alternative routes |
| 155 | */ |
| 156 | public Collection<ResolvedRoute> alternatives() { |
| 157 | return alternativeRoutes; |
| 158 | } |
| 159 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 160 | @Override |
| 161 | public int hashCode() { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 162 | return Objects.hash(subject(), type(), prevSubject(), alternativeRoutes); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean equals(Object other) { |
| 167 | if (this == other) { |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | if (!(other instanceof RouteEvent)) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | RouteEvent that = (RouteEvent) other; |
| 176 | |
| 177 | return Objects.equals(this.subject(), that.subject()) && |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 178 | Objects.equals(this.type(), that.type()) && |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 179 | Objects.equals(this.prevSubject, that.prevSubject) && |
| 180 | Objects.equals(this.alternativeRoutes, that.alternativeRoutes); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String toString() { |
| 185 | return toStringHelper(this) |
| 186 | .add("time", new LocalDateTime(time())) |
| 187 | .add("type", type()) |
| 188 | .add("subject", subject()) |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 189 | .add("prevSubject", prevSubject) |
| 190 | .add("alternatives", alternativeRoutes) |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 191 | .toString(); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 192 | } |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 193 | } |