blob: 98ea5cb53527a25bba9812236fddff26b123be77 [file] [log] [blame]
Andrea Campanella1e573442018-05-17 17:07:13 +02001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.net.device;
17
18import org.onlab.util.Tools;
19import org.onosproject.event.AbstractEvent;
20import org.onosproject.net.DeviceId;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23
24/**
25 * Describes event related to a channel established by ONOS with a device.
26 */
27public class ChannelEvent extends AbstractEvent<ChannelEvent.Type, DeviceId> {
28
29 private final Throwable throwable;
30
31 /**
32 * Type of device events.
33 */
34 public enum Type {
35 /**
36 * Signifies that the channel has properly connected.
37 */
38 CHANNEL_CONNECTED,
39
40 /**
41 * Signifies that the channel has disconnected.
42 */
43 CHANNEL_DISCONNECTED,
44
45 /**
46 * Signifies that an error happened on the channel with the given device.
47 */
48 CHANNEL_ERROR
49
50 }
51
52 /**
53 * Creates an event of a given type and for the specified device.
54 *
55 * @param type device event type
56 * @param deviceId event device subject
57 */
58 public ChannelEvent(Type type, DeviceId deviceId) {
59 this(type, deviceId, null);
60 }
61
62 /**
63 * Creates an event of a given type and for the specified device, given a certain throwable.
64 *
65 * @param type device event type
66 * @param deviceId event device subject
67 * @param throwable exception happened on the channel
68 */
69 public ChannelEvent(Type type, DeviceId deviceId, Throwable throwable) {
70 super(type, deviceId);
71 this.throwable = throwable;
72 }
73
74 /**
75 * Creates an event of a given type and for the specified device and the current time.
76 *
77 * @param type device event type
78 * @param deviceId event device subject
79 * @param throwable exception happened on the channel
80 * @param time occurrence time
81 */
82 public ChannelEvent(Type type, DeviceId deviceId, Throwable throwable, long time) {
83 super(type, deviceId, time);
84 this.throwable = throwable;
85 }
86
87 /**
88 * Returns the exception that happened on the channel.
89 *
90 * @return a throwable if associated to the event, otherwise null.
91 */
92 public Throwable throwable() {
93 return throwable;
94 }
95
96 @Override
97 public String toString() {
98 if (throwable == null) {
99 return super.toString();
100 }
101 return toStringHelper(this)
102 .add("time", Tools.defaultOffsetDataTime(time()))
103 .add("type", type())
104 .add("subject", subject())
105 .add("throwable", throwable)
106 .toString();
107 }
108}