blob: 5e7f83a421ae4a5faa88758c6e4e2c3afee5b25e [file] [log] [blame]
Mohammad Shahidaa7c1232017-08-09 11:13:15 +05301/*
2 * Copyright 2017-present Open Networking Foundation
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 Milkeya95193c2017-08-10 15:35:36 -070017package org.onosproject.evpnrouteservice;
Mohammad Shahidaa7c1232017-08-09 11:13:15 +053018
19import java.util.Collection;
20import java.util.Collections;
21import java.util.Objects;
22
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070023import org.onlab.util.Tools;
Ray Milkeya95193c2017-08-10 15:35:36 -070024import org.onosproject.event.AbstractEvent;
25
Mohammad Shahidaa7c1232017-08-09 11:13:15 +053026import static com.google.common.base.MoreObjects.toStringHelper;
27
28/**
29 * Describes an event about a route.
30 */
31public class EvpnRouteEvent extends AbstractEvent<EvpnRouteEvent.Type,
32 EvpnRoute> {
33
34 private final EvpnRoute prevSubject;
35 private final Collection<EvpnRoute> alternativeRoutes;
36
37 /**
38 * Route event type.
39 */
40 public enum Type {
41
42 /**
43 * Route is new and the next hop is resolved.
44 * <p>
45 * The subject of this event should be the route being added.
46 * The prevSubject of this event should be null.
47 * </p>
48 */
49 ROUTE_ADDED,
50
51 /**
52 * Route has updated information.
53 * <p>
54 * The subject of this event should be the new route.
55 * The prevSubject of this event should be the old route.
56 * </p>
57 */
58 ROUTE_UPDATED,
59
60 /**
61 * Route was removed or the next hop becomes unresolved.
62 * <p>
63 * The subject of this event should be the route being removed.
64 * The prevSubject of this event should be null.
65 * </p>
66 */
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
80 }
81
82 /**
83 * Creates a new route event without specifying previous subject.
84 *
85 * @param type event type
86 * @param subject event subject
87 */
88 public EvpnRouteEvent(Type type, EvpnRoute subject) {
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 EvpnRouteEvent(Type type, EvpnRoute subject,
100 Collection<EvpnRoute> alternatives) {
101 this(type, subject, null, alternatives);
102 }
103
104 /**
105 * Creates a new route event.
106 *
107 * @param type event type
108 * @param subject event subject
109 * @param time event time
110 */
111 protected EvpnRouteEvent(Type type, EvpnRoute subject, long time) {
112 super(type, subject, time);
113 this.prevSubject = null;
114
115 this.alternativeRoutes = Collections.emptySet();
116 }
117
118 /**
119 * Creates a new route event with previous subject.
120 *
121 * @param type event type
122 * @param subject event subject
123 * @param prevSubject previous subject
124 */
125 public EvpnRouteEvent(Type type, EvpnRoute subject, EvpnRoute prevSubject) {
126 this(type, subject, prevSubject, Collections.emptySet());
127 }
128
129 /**
130 * Creates a new route event with a previous subject and alternative routes.
131 *
132 * @param type event type
133 * @param subject event subject
134 * @param prevSubject previous subject
135 * @param alternatives alternative routes for subject's prefix
136 */
137 public EvpnRouteEvent(Type type, EvpnRoute subject, EvpnRoute prevSubject,
138 Collection<EvpnRoute> alternatives) {
139 super(type, subject);
140 this.prevSubject = prevSubject;
141 this.alternativeRoutes = alternatives;
142 }
143
144 /**
145 * Returns the previous subject of the event.
146 *
147 * @return previous subject to which this event pertains
148 */
149 public EvpnRoute prevSubject() {
150 return prevSubject;
151 }
152
153 /**
154 * Returns the set of alternative routes for the subject's prefix.
155 *
156 * @return alternative routes
157 */
158 public Collection<EvpnRoute> alternatives() {
159 return alternativeRoutes;
160 }
161
162 @Override
163 public int hashCode() {
164 return Objects.hash(subject(), type(), prevSubject(), alternativeRoutes);
165 }
166
167 @Override
168 public boolean equals(Object other) {
169 if (this == other) {
170 return true;
171 }
172
173 if (!(other instanceof EvpnRouteEvent)) {
174 return false;
175 }
176
177 EvpnRouteEvent that = (EvpnRouteEvent) other;
178
179 return Objects.equals(this.subject(), that.subject()) &&
180 Objects.equals(this.type(), that.type()) &&
181 Objects.equals(this.prevSubject, that.prevSubject) &&
182 Objects.equals(this.alternativeRoutes, that.alternativeRoutes);
183 }
184
185 @Override
186 public String toString() {
187 return toStringHelper(this)
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700188 .add("time", Tools.defaultOffsetDataTime(time()))
Mohammad Shahidaa7c1232017-08-09 11:13:15 +0530189 .add("type", type())
190 .add("subject", subject())
191 .add("prevSubject", prevSubject)
192 .add("alternatives", alternativeRoutes)
193 .toString();
194 }
195}