Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -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 | */ |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 19 | import org.onosproject.event.AbstractEvent; |
| 20 | |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 21 | import java.util.Optional; |
| 22 | |
| 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 24 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Describes network configuration event. |
| 27 | */ |
| 28 | public class NetworkConfigEvent extends AbstractEvent<NetworkConfigEvent.Type, Object> { |
| 29 | |
| 30 | private final Class configClass; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 31 | private final Config config; |
| 32 | private final Config prevConfig; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Type of network configuration events. |
| 36 | */ |
| 37 | public enum Type { |
| 38 | /** |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 39 | * 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 49 | * Signifies that network configuration was added. |
| 50 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 51 | CONFIG_ADDED, |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 52 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 53 | /** |
| 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 Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 75 | this.config = null; |
| 76 | this.prevConfig = null; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 77 | } |
| 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 Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 90 | 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 110 | } |
| 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 Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 121 | /** |
| 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 HIGUCHI | 0c47d53 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 144 | .add("time", Tools.defaultOffsetDataTime(time())) |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 145 | .add("type", type()) |
| 146 | .add("config", config()) |
| 147 | .add("prevConfig", prevConfig()) |
| 148 | .add("configClass", configClass()) |
| 149 | .toString(); |
| 150 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 151 | } |