blob: 90b7bd82bf4eafcf8bbd5241b216b80ac507acc8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host;
tom64b7aac2014-08-26 00:18:21 -070017
Charles Chanec0425c2015-11-11 12:09:38 -080018import org.joda.time.LocalDateTime;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.event.AbstractEvent;
20import org.onosproject.net.Host;
Charles Chanec0425c2015-11-11 12:09:38 -080021
22import static com.google.common.base.MoreObjects.toStringHelper;
tom64b7aac2014-08-26 00:18:21 -070023
24/**
25 * Describes end-station host event.
26 */
27public class HostEvent extends AbstractEvent<HostEvent.Type, Host> {
28
29 /**
30 * Type of host events.
31 */
32 public enum Type {
33 /**
34 * Signifies that a new host has been detected.
35 */
36 HOST_ADDED,
37
38 /**
39 * Signifies that a host has been removed.
40 */
41 HOST_REMOVED,
42
43 /**
tom7869ad92014-09-09 14:32:08 -070044 * Signifies that host data changed, e.g. IP address
45 */
46 HOST_UPDATED,
47
48 /**
tom64b7aac2014-08-26 00:18:21 -070049 * Signifies that a host location has changed.
50 */
51 HOST_MOVED
52 }
53
Charles Chan33f28a92015-11-13 13:12:38 -080054 private Host prevSubject;
Charles Chanec0425c2015-11-11 12:09:38 -080055
tom64b7aac2014-08-26 00:18:21 -070056 /**
57 * Creates an event of a given type and for the specified host and the
58 * current time.
59 *
60 * @param type host event type
61 * @param host event host subject
62 */
63 public HostEvent(Type type, Host host) {
64 super(type, host);
65 }
66
67 /**
68 * Creates an event of a given type and for the specified host and time.
69 *
70 * @param type host event type
71 * @param host event host subject
72 * @param time occurrence time
73 */
74 public HostEvent(Type type, Host host, long time) {
75 super(type, host, time);
76 }
77
Charles Chanec0425c2015-11-11 12:09:38 -080078 /**
Charles Chan33f28a92015-11-13 13:12:38 -080079 * Creates an event with previous subject.
Charles Chanec0425c2015-11-11 12:09:38 -080080 *
Charles Chan33f28a92015-11-13 13:12:38 -080081 * The previous subject is ignored if the type is not moved or updated
82 *
83 * @param type host event type
Charles Chanec0425c2015-11-11 12:09:38 -080084 * @param host event host subject
Charles Chan33f28a92015-11-13 13:12:38 -080085 * @param prevSubject previous host subject
Charles Chanec0425c2015-11-11 12:09:38 -080086 */
Charles Chan33f28a92015-11-13 13:12:38 -080087 public HostEvent(Type type, Host host, Host prevSubject) {
88 super(type, host);
89 if (type == Type.HOST_MOVED || type == Type.HOST_UPDATED) {
90 this.prevSubject = prevSubject;
91 }
Charles Chanec0425c2015-11-11 12:09:38 -080092 }
93
94 /**
Charles Chan33f28a92015-11-13 13:12:38 -080095 * Gets the previous subject in this host event.
Charles Chanec0425c2015-11-11 12:09:38 -080096 *
Charles Chan33f28a92015-11-13 13:12:38 -080097 * @return the previous subject, or null if previous subject is not
Charles Chanec0425c2015-11-11 12:09:38 -080098 * specified.
99 */
Charles Chan33f28a92015-11-13 13:12:38 -0800100 public Host prevSubject() {
101 return this.prevSubject;
Charles Chanec0425c2015-11-11 12:09:38 -0800102 }
103
104 @Override
105 public String toString() {
106 return toStringHelper(this)
107 .add("time", new LocalDateTime(time()))
108 .add("type", type())
109 .add("subject", subject())
Charles Chan33f28a92015-11-13 13:12:38 -0800110 .add("prevSubject", prevSubject())
Charles Chanec0425c2015-11-11 12:09:38 -0800111 .toString();
112 }
tom64b7aac2014-08-26 00:18:21 -0700113}