Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 17 | package org.onosproject.incubator.net.routing; |
| 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 | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Describes an event about a route. |
| 28 | */ |
| 29 | public class RouteEvent extends AbstractEvent<RouteEvent.Type, ResolvedRoute> { |
| 30 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 31 | private final ResolvedRoute prevSubject; |
| 32 | |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Route event type. |
| 35 | */ |
| 36 | public enum Type { |
| 37 | |
| 38 | /** |
| 39 | * Route is new. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 40 | * <p> |
| 41 | * The subject of this event should be the route being added. |
| 42 | * The prevSubject of this event should be null. |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 43 | */ |
| 44 | ROUTE_ADDED, |
| 45 | |
| 46 | /** |
| 47 | * Route has updated information. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 48 | * <p> |
| 49 | * The subject of this event should be the new route. |
| 50 | * The prevSubject of this event should be the old route. |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 51 | */ |
| 52 | ROUTE_UPDATED, |
| 53 | |
| 54 | /** |
| 55 | * Route was removed. |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 56 | * <p> |
| 57 | * The subject of this event should be the route being removed. |
| 58 | * The prevSubject of this event should be null. |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 59 | */ |
| 60 | ROUTE_REMOVED |
| 61 | } |
| 62 | |
| 63 | /** |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 64 | * Creates a new route event without specifying previous subject. |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 65 | * |
| 66 | * @param type event type |
| 67 | * @param subject event subject |
| 68 | */ |
| 69 | public RouteEvent(Type type, ResolvedRoute subject) { |
| 70 | super(type, subject); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 71 | this.prevSubject = null; |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 72 | } |
| 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 Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 83 | 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 Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public int hashCode() { |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 109 | return Objects.hash(subject(), type(), prevSubject()); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 110 | } |
| 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 Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 125 | 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 Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 137 | } |
Jonathan Hart | 7d7e2f5 | 2016-03-29 16:22:49 -0700 | [diff] [blame] | 138 | } |