blob: b661b40ef8ed87a9f4a537a693096292224e73b1 [file] [log] [blame]
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07003 *
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 Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070018
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070019import org.onlab.util.Tools;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070020import org.onosproject.event.AbstractEvent;
21
Jonathan Hartf7021682017-03-22 18:17:21 -070022import java.util.Collection;
23import java.util.Collections;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070024import java.util.Objects;
25
Charles Chane4d13102016-11-08 15:38:44 -080026import static com.google.common.base.MoreObjects.toStringHelper;
27
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070028/**
29 * Describes an event about a route.
30 */
31public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> {
32
Charles Chane4d13102016-11-08 15:38:44 -080033 private final ResolvedRoute prevSubject;
Jonathan Hartf7021682017-03-22 18:17:21 -070034 private final Collection<ResolvedRoute> alternativeRoutes;
Charles Chanbbe17f72017-08-23 16:33:06 -070035 private final Collection<ResolvedRoute> prevAlternativeRoutes;
Charles Chane4d13102016-11-08 15:38:44 -080036
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070037 /**
38 * Route event type.
39 */
40 public enum Type {
41
42 /**
Charles Chanb21d69a2016-11-11 17:46:14 -080043 * Route is new and the next hop is resolved.
Charles Chane4d13102016-11-08 15:38:44 -080044 * <p>
45 * The subject of this event should be the route being added.
46 * The prevSubject of this event should be null.
Jonathan Hartf7021682017-03-22 18:17:21 -070047 * </p>
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070048 */
49 ROUTE_ADDED,
50
51 /**
52 * Route has updated information.
Charles Chane4d13102016-11-08 15:38:44 -080053 * <p>
54 * The subject of this event should be the new route.
55 * The prevSubject of this event should be the old route.
Jonathan Hartf7021682017-03-22 18:17:21 -070056 * </p>
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070057 */
58 ROUTE_UPDATED,
59
60 /**
Charles Chanb21d69a2016-11-11 17:46:14 -080061 * Route was removed or the next hop becomes unresolved.
Charles Chane4d13102016-11-08 15:38:44 -080062 * <p>
63 * The subject of this event should be the route being removed.
64 * The prevSubject of this event should be null.
Jonathan Hartf7021682017-03-22 18:17:21 -070065 * </p>
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070066 */
Jonathan Hartf7021682017-03-22 18:17:21 -070067 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 Hart7d7e2f52016-03-29 16:22:49 -070080 }
81
82 /**
Charles Chane4d13102016-11-08 15:38:44 -080083 * Creates a new route event without specifying previous subject.
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070084 *
85 * @param type event type
86 * @param subject event subject
87 */
88 public RouteEvent(Type type, ResolvedRoute subject) {
Jonathan Hartf7021682017-03-22 18:17:21 -070089 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 Hart7d7e2f52016-03-29 16:22:49 -0700101 }
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 Chane4d13102016-11-08 15:38:44 -0800112 this.prevSubject = null;
Jonathan Hartf7021682017-03-22 18:17:21 -0700113 this.alternativeRoutes = Collections.emptySet();
Charles Chanbbe17f72017-08-23 16:33:06 -0700114 this.prevAlternativeRoutes = null;
Charles Chane4d13102016-11-08 15:38:44 -0800115 }
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 Hartf7021682017-03-22 18:17:21 -0700125 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 Chanbbe17f72017-08-23 16:33:06 -0700138 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 Chane4d13102016-11-08 15:38:44 -0800153 super(type, subject);
154 this.prevSubject = prevSubject;
Jonathan Hartf7021682017-03-22 18:17:21 -0700155 this.alternativeRoutes = alternatives;
Charles Chanbbe17f72017-08-23 16:33:06 -0700156 this.prevAlternativeRoutes = prevAlternatives;
Charles Chane4d13102016-11-08 15:38:44 -0800157 }
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 Hart7d7e2f52016-03-29 16:22:49 -0700166 }
167
Jonathan Hartf7021682017-03-22 18:17:21 -0700168 /**
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 Chanbbe17f72017-08-23 16:33:06 -0700177 /**
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 Hart6c2e7962016-04-11 13:54:09 -0700186 @Override
187 public int hashCode() {
Charles Chanbbe17f72017-08-23 16:33:06 -0700188 return Objects.hash(subject(), type(), prevSubject(), alternativeRoutes, prevAlternativeRoutes);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700189 }
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 Chane4d13102016-11-08 15:38:44 -0800204 Objects.equals(this.type(), that.type()) &&
Jonathan Hartf7021682017-03-22 18:17:21 -0700205 Objects.equals(this.prevSubject, that.prevSubject) &&
Charles Chanbbe17f72017-08-23 16:33:06 -0700206 Objects.equals(this.alternativeRoutes, that.alternativeRoutes) &&
207 Objects.equals(this.prevAlternativeRoutes, that.prevAlternativeRoutes);
Charles Chane4d13102016-11-08 15:38:44 -0800208 }
209
210 @Override
211 public String toString() {
212 return toStringHelper(this)
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700213 .add("time", Tools.defaultOffsetDataTime(time()))
Charles Chane4d13102016-11-08 15:38:44 -0800214 .add("type", type())
215 .add("subject", subject())
Jonathan Hartf7021682017-03-22 18:17:21 -0700216 .add("prevSubject", prevSubject)
217 .add("alternatives", alternativeRoutes)
Charles Chanbbe17f72017-08-23 16:33:06 -0700218 .add("prevAlternatives", prevAlternativeRoutes)
Charles Chane4d13102016-11-08 15:38:44 -0800219 .toString();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700220 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700221}