blob: 0428beb06370f9aec9484d8f75345710a1aa92ab [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
Charles Chane4d13102016-11-08 15:38:44 -080019import org.joda.time.LocalDateTime;
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 Chane4d13102016-11-08 15:38:44 -080035
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070036 /**
37 * Route event type.
38 */
39 public enum Type {
40
41 /**
Charles Chanb21d69a2016-11-11 17:46:14 -080042 * Route is new and the next hop is resolved.
Charles Chane4d13102016-11-08 15:38:44 -080043 * <p>
44 * The subject of this event should be the route being added.
45 * The prevSubject of this event should be null.
Jonathan Hartf7021682017-03-22 18:17:21 -070046 * </p>
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070047 */
48 ROUTE_ADDED,
49
50 /**
51 * Route has updated information.
Charles Chane4d13102016-11-08 15:38:44 -080052 * <p>
53 * The subject of this event should be the new route.
54 * The prevSubject of this event should be the old route.
Jonathan Hartf7021682017-03-22 18:17:21 -070055 * </p>
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070056 */
57 ROUTE_UPDATED,
58
59 /**
Charles Chanb21d69a2016-11-11 17:46:14 -080060 * Route was removed or the next hop becomes unresolved.
Charles Chane4d13102016-11-08 15:38:44 -080061 * <p>
62 * The subject of this event should be the route being removed.
63 * The prevSubject of this event should be null.
Jonathan Hartf7021682017-03-22 18:17:21 -070064 * </p>
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070065 */
Jonathan Hartf7021682017-03-22 18:17:21 -070066 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 Hart7d7e2f52016-03-29 16:22:49 -070079 }
80
81 /**
Charles Chane4d13102016-11-08 15:38:44 -080082 * Creates a new route event without specifying previous subject.
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070083 *
84 * @param type event type
85 * @param subject event subject
86 */
87 public RouteEvent(Type type, ResolvedRoute subject) {
Jonathan Hartf7021682017-03-22 18:17:21 -070088 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 Hart7d7e2f52016-03-29 16:22:49 -0700100 }
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 Chane4d13102016-11-08 15:38:44 -0800111 this.prevSubject = null;
Jonathan Hartf7021682017-03-22 18:17:21 -0700112
113 this.alternativeRoutes = Collections.emptySet();
Charles Chane4d13102016-11-08 15:38:44 -0800114 }
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 Hartf7021682017-03-22 18:17:21 -0700124 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 Chane4d13102016-11-08 15:38:44 -0800137 super(type, subject);
138 this.prevSubject = prevSubject;
Jonathan Hartf7021682017-03-22 18:17:21 -0700139 this.alternativeRoutes = alternatives;
Charles Chane4d13102016-11-08 15:38:44 -0800140 }
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 Hart7d7e2f52016-03-29 16:22:49 -0700149 }
150
Jonathan Hartf7021682017-03-22 18:17:21 -0700151 /**
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 Hart6c2e7962016-04-11 13:54:09 -0700160 @Override
161 public int hashCode() {
Jonathan Hartf7021682017-03-22 18:17:21 -0700162 return Objects.hash(subject(), type(), prevSubject(), alternativeRoutes);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700163 }
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 Chane4d13102016-11-08 15:38:44 -0800178 Objects.equals(this.type(), that.type()) &&
Jonathan Hartf7021682017-03-22 18:17:21 -0700179 Objects.equals(this.prevSubject, that.prevSubject) &&
180 Objects.equals(this.alternativeRoutes, that.alternativeRoutes);
Charles Chane4d13102016-11-08 15:38:44 -0800181 }
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 Hartf7021682017-03-22 18:17:21 -0700189 .add("prevSubject", prevSubject)
190 .add("alternatives", alternativeRoutes)
Charles Chane4d13102016-11-08 15:38:44 -0800191 .toString();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700192 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700193}