blob: d2e59a5836738909f099d760ffe6fae01926d9e6 [file] [log] [blame]
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -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.intf;
18
19import org.onosproject.event.AbstractEvent;
gaurav2c626cc2016-04-26 04:18:33 +053020import org.joda.time.LocalDateTime;
21import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070022
23/**
24 * Describes an interface event.
25 */
26public class InterfaceEvent extends AbstractEvent<InterfaceEvent.Type, Interface> {
27
gaurav2c626cc2016-04-26 04:18:33 +053028 private final Interface prevSubject;
29
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070030 public enum Type {
31 /**
32 * Indicates a new interface has been added.
33 */
34 INTERFACE_ADDED,
35
36 /**
37 * Indicates an interface has been updated.
38 */
39 INTERFACE_UPDATED,
40
41 /**
42 * Indicates an interface has been removed.
43 */
44 INTERFACE_REMOVED
45 }
46
47 /**
48 * Creates an interface event with type and subject.
49 *
50 * @param type event type
51 * @param subject subject interface
52 */
53 public InterfaceEvent(Type type, Interface subject) {
gaurav2c626cc2016-04-26 04:18:33 +053054 this(type, subject, null);
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070055 }
56
57 /**
gaurav2c626cc2016-04-26 04:18:33 +053058 * Creates an interface event with type, subject and time of event.
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070059 *
60 * @param type event type
61 * @param subject subject interface
62 * @param time time of event
63 */
64 public InterfaceEvent(Type type, Interface subject, long time) {
gaurav2c626cc2016-04-26 04:18:33 +053065 this(type, subject, null, time);
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070066 }
67
gaurav2c626cc2016-04-26 04:18:33 +053068 /**
69 * Creates an interface event with type, subject and previous subject.
70 *
71 * @param type event type
72 * @param subject subject interface
Charles Chanab69c2c2016-06-13 12:06:31 -070073 * @param prevSubject previous interface subject
gaurav2c626cc2016-04-26 04:18:33 +053074 */
75 public InterfaceEvent(Type type, Interface subject, Interface prevSubject) {
76 super(type, subject);
77 this.prevSubject = prevSubject;
78 }
79
80 /**
81 * Creates an interface event with type, subject, previous subject and time.
82 *
83 * @param type event type
84 * @param subject subject interface
Charles Chanab69c2c2016-06-13 12:06:31 -070085 * @param prevSubject previous interface subject
gaurav2c626cc2016-04-26 04:18:33 +053086 * @param time time of event
87 */
88 public InterfaceEvent(Type type, Interface subject, Interface prevSubject, long time) {
89 super(type, subject, time);
90 this.prevSubject = prevSubject;
91 }
92
93 /**
94 * Returns the previous interface subject.
95 *
96 * @return previous subject of interface or null if the event is not interface specific.
97 */
98 public Interface prevSubject() {
99 return prevSubject;
100 }
101
102 @Override
103 public String toString() {
104 if (prevSubject == null) {
105 return super.toString();
106 }
107 return toStringHelper(this)
108 .add("time", new LocalDateTime(time()))
109 .add("type", type())
110 .add("subject", subject())
111 .add("prevSubject", prevSubject)
112 .toString();
113 }
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -0700114}