Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -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 | |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 17 | package org.onosproject.net.intf; |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 18 | |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 19 | import org.joda.time.LocalDateTime; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 20 | import org.onosproject.event.AbstractEvent; |
| 21 | |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Describes an interface event. |
| 26 | */ |
| 27 | public class InterfaceEvent extends AbstractEvent<InterfaceEvent.Type, Interface> { |
| 28 | |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 29 | private final Interface prevSubject; |
| 30 | |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 31 | public enum Type { |
| 32 | /** |
| 33 | * Indicates a new interface has been added. |
| 34 | */ |
| 35 | INTERFACE_ADDED, |
| 36 | |
| 37 | /** |
| 38 | * Indicates an interface has been updated. |
| 39 | */ |
| 40 | INTERFACE_UPDATED, |
| 41 | |
| 42 | /** |
| 43 | * Indicates an interface has been removed. |
| 44 | */ |
| 45 | INTERFACE_REMOVED |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates an interface event with type and subject. |
| 50 | * |
| 51 | * @param type event type |
| 52 | * @param subject subject interface |
| 53 | */ |
| 54 | public InterfaceEvent(Type type, Interface subject) { |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 55 | this(type, subject, null); |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 59 | * Creates an interface event with type, subject and time of event. |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 60 | * |
| 61 | * @param type event type |
| 62 | * @param subject subject interface |
| 63 | * @param time time of event |
| 64 | */ |
| 65 | public InterfaceEvent(Type type, Interface subject, long time) { |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 66 | this(type, subject, null, time); |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 67 | } |
| 68 | |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 69 | /** |
| 70 | * Creates an interface event with type, subject and previous subject. |
| 71 | * |
| 72 | * @param type event type |
| 73 | * @param subject subject interface |
Charles Chan | ab69c2c | 2016-06-13 12:06:31 -0700 | [diff] [blame] | 74 | * @param prevSubject previous interface subject |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 75 | */ |
| 76 | public InterfaceEvent(Type type, Interface subject, Interface prevSubject) { |
| 77 | super(type, subject); |
| 78 | this.prevSubject = prevSubject; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Creates an interface event with type, subject, previous subject and time. |
| 83 | * |
| 84 | * @param type event type |
| 85 | * @param subject subject interface |
Charles Chan | ab69c2c | 2016-06-13 12:06:31 -0700 | [diff] [blame] | 86 | * @param prevSubject previous interface subject |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 87 | * @param time time of event |
| 88 | */ |
| 89 | public InterfaceEvent(Type type, Interface subject, Interface prevSubject, long time) { |
| 90 | super(type, subject, time); |
| 91 | this.prevSubject = prevSubject; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the previous interface subject. |
| 96 | * |
| 97 | * @return previous subject of interface or null if the event is not interface specific. |
| 98 | */ |
| 99 | public Interface prevSubject() { |
| 100 | return prevSubject; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() { |
| 105 | if (prevSubject == null) { |
| 106 | return super.toString(); |
| 107 | } |
| 108 | return toStringHelper(this) |
| 109 | .add("time", new LocalDateTime(time())) |
| 110 | .add("type", type()) |
| 111 | .add("subject", subject()) |
| 112 | .add("prevSubject", prevSubject) |
| 113 | .toString(); |
| 114 | } |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 115 | } |