blob: fc5199d3f9127cb8282f11fbd9cb3ab26c28594e [file] [log] [blame]
Jonathan Hart7d7e2f52016-03-29 16:22:49 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.incubator.net.routing;
18
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 Hart6c2e7962016-04-11 13:54:09 -070022import java.util.Objects;
23
Charles Chane4d13102016-11-08 15:38:44 -080024import static com.google.common.base.MoreObjects.toStringHelper;
25
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070026/**
27 * Describes an event about a route.
28 */
29public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> {
30
Charles Chane4d13102016-11-08 15:38:44 -080031 private final ResolvedRoute prevSubject;
32
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070033 /**
34 * Route event type.
35 */
36 public enum Type {
37
38 /**
39 * Route is new.
Charles Chane4d13102016-11-08 15:38:44 -080040 * <p>
41 * The subject of this event should be the route being added.
42 * The prevSubject of this event should be null.
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070043 */
44 ROUTE_ADDED,
45
46 /**
47 * Route has updated information.
Charles Chane4d13102016-11-08 15:38:44 -080048 * <p>
49 * The subject of this event should be the new route.
50 * The prevSubject of this event should be the old route.
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070051 */
52 ROUTE_UPDATED,
53
54 /**
55 * Route was removed.
Charles Chane4d13102016-11-08 15:38:44 -080056 * <p>
57 * The subject of this event should be the route being removed.
58 * The prevSubject of this event should be null.
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070059 */
60 ROUTE_REMOVED
61 }
62
63 /**
Charles Chane4d13102016-11-08 15:38:44 -080064 * Creates a new route event without specifying previous subject.
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070065 *
66 * @param type event type
67 * @param subject event subject
68 */
69 public RouteEvent(Type type, ResolvedRoute subject) {
70 super(type, subject);
Charles Chane4d13102016-11-08 15:38:44 -080071 this.prevSubject = null;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -070072 }
73
74 /**
75 * Creates a new route event.
76 *
77 * @param type event type
78 * @param subject event subject
79 * @param time event time
80 */
81 protected RouteEvent(Type type, ResolvedRoute subject, long time) {
82 super(type, subject, time);
Charles Chane4d13102016-11-08 15:38:44 -080083 this.prevSubject = null;
84 }
85
86 /**
87 * Creates a new route event with previous subject.
88 *
89 * @param type event type
90 * @param subject event subject
91 * @param prevSubject previous subject
92 */
93 public RouteEvent(Type type, ResolvedRoute subject, ResolvedRoute prevSubject) {
94 super(type, subject);
95 this.prevSubject = prevSubject;
96 }
97
98 /**
99 * Returns the previous subject of the event.
100 *
101 * @return previous subject to which this event pertains
102 */
103 public ResolvedRoute prevSubject() {
104 return prevSubject;
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700105 }
106
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700107 @Override
108 public int hashCode() {
Charles Chane4d13102016-11-08 15:38:44 -0800109 return Objects.hash(subject(), type(), prevSubject());
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700110 }
111
112 @Override
113 public boolean equals(Object other) {
114 if (this == other) {
115 return true;
116 }
117
118 if (!(other instanceof RouteEvent)) {
119 return false;
120 }
121
122 RouteEvent that = (RouteEvent) other;
123
124 return Objects.equals(this.subject(), that.subject()) &&
Charles Chane4d13102016-11-08 15:38:44 -0800125 Objects.equals(this.type(), that.type()) &&
126 Objects.equals(this.prevSubject(), that.prevSubject());
127 }
128
129 @Override
130 public String toString() {
131 return toStringHelper(this)
132 .add("time", new LocalDateTime(time()))
133 .add("type", type())
134 .add("subject", subject())
135 .add("prevSubject", prevSubject())
136 .toString();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700137 }
Jonathan Hart7d7e2f52016-03-29 16:22:49 -0700138}