blob: ee1f03c10b17f2cea89cd8155e1508bf752feae9 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska96d55b12015-05-11 08:52:03 -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 */
Ray Milkeya4122362015-08-18 15:19:08 -070016package org.onosproject.net.config;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070018import org.onlab.util.Tools;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070019import org.onosproject.event.AbstractEvent;
20
Charles Chan023a8982016-02-04 11:00:41 -080021import java.util.Optional;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24
Thomas Vachuska96d55b12015-05-11 08:52:03 -070025/**
26 * Describes network configuration event.
27 */
28public class NetworkConfigEvent extends AbstractEvent<NetworkConfigEvent.Type, Object> {
29
30 private final Class configClass;
Charles Chan023a8982016-02-04 11:00:41 -080031 private final Config config;
32 private final Config prevConfig;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070033
34 /**
35 * Type of network configuration events.
36 */
37 public enum Type {
38 /**
Thomas Vachuskae6360222015-07-21 10:10:36 -070039 * Signifies that a network configuration was registered.
40 */
41 CONFIG_REGISTERED,
42
43 /**
44 * Signifies that a network configuration was unregistered.
45 */
46 CONFIG_UNREGISTERED,
47
48 /**
Thomas Vachuska96d55b12015-05-11 08:52:03 -070049 * Signifies that network configuration was added.
50 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070051 CONFIG_ADDED,
Thomas Vachuskae6360222015-07-21 10:10:36 -070052
Thomas Vachuska96d55b12015-05-11 08:52:03 -070053 /**
54 * Signifies that network configuration was updated.
55 */
56 CONFIG_UPDATED,
57
58 /**
59 * Signifies that network configuration was removed.
60 */
61 CONFIG_REMOVED
62 }
63
64 /**
65 * Creates an event of a given type and for the specified subject and the
66 * current time.
67 *
68 * @param type event type
69 * @param subject event subject
70 * @param configClass configuration class
71 */
72 public NetworkConfigEvent(Type type, Object subject, Class configClass) {
73 super(type, subject);
74 this.configClass = configClass;
Charles Chan023a8982016-02-04 11:00:41 -080075 this.config = null;
76 this.prevConfig = null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070077 }
78
79 /**
80 * Creates an event of a given type and for the specified subject and time.
81 *
82 * @param type device event type
83 * @param subject event subject
84 * @param configClass configuration class
85 * @param time occurrence time
86 */
87 public NetworkConfigEvent(Type type, Object subject, Class configClass, long time) {
88 super(type, subject, time);
89 this.configClass = configClass;
Charles Chan023a8982016-02-04 11:00:41 -080090 this.config = null;
91 this.prevConfig = null;
92 }
93
94 /**
95 * Creates an event of a given type and for the specified subject,
96 * previous config and time.
97 *
98 * @param type device event type
99 * @param subject event subject
100 * @param configClass configuration class
101 * @param config current config
102 * @param prevConfig previous config
103 */
104 public NetworkConfigEvent(Type type, Object subject, Config config,
105 Config prevConfig, Class configClass) {
106 super(type, subject);
107 this.configClass = configClass;
108 this.config = config;
109 this.prevConfig = prevConfig;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700110 }
111
112 /**
113 * Returns the class of configuration that has been changed.
114 *
115 * @return configuration class
116 */
117 public Class configClass() {
118 return configClass;
119 }
120
Charles Chan023a8982016-02-04 11:00:41 -0800121 /**
122 * Returns current config.
123 *
124 * @return current config; value presents only when the type is
125 * CONFIG_ADDED or CONFIG_UPDATED
126 */
127 public Optional<Config> config() {
128 return (config != null) ? Optional.of(config) : Optional.empty();
129 }
130
131 /**
132 * Returns previous config.
133 *
134 * @return previous config; value presents only when the type is
135 * CONFIG_UPDATED or CONFIG_REMOVED
136 */
137 public Optional<Config> prevConfig() {
138 return (prevConfig != null) ? Optional.of(prevConfig) : Optional.empty();
139 }
140
141 @Override
142 public String toString() {
143 return toStringHelper(this)
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700144 .add("time", Tools.defaultOffsetDataTime(time()))
Charles Chan023a8982016-02-04 11:00:41 -0800145 .add("type", type())
146 .add("config", config())
147 .add("prevConfig", prevConfig())
148 .add("configClass", configClass())
149 .toString();
150 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700151}