CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [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 | */ |
| 16 | package org.onosproject.ovsdb.controller; |
| 17 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onosproject.net.behaviour.BridgeDescription; |
| 21 | import org.onosproject.net.behaviour.BridgeDescription.FailMode; |
| 22 | import org.onosproject.net.behaviour.ControllerInfo; |
| 23 | |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 25 | import static org.onosproject.ovsdb.controller.OvsdbConstant.DATAPATH_ID; |
| 26 | import static org.onosproject.ovsdb.controller.OvsdbConstant.DISABLE_INBAND; |
| 27 | |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Objects; |
| 31 | import java.util.Optional; |
| 32 | |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 35 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 36 | * The class representing an OVSDB bridge. |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 37 | * This class is immutable. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 38 | */ |
| 39 | public final class OvsdbBridge { |
| 40 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 41 | private final String name; |
| 42 | |
| 43 | /* OpenFlow properties */ |
| 44 | private final Optional<FailMode> failMode; |
| 45 | private final List<ControllerInfo> controllers; |
| 46 | |
| 47 | /* Adds more properties */ |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 48 | private final Optional<String> datapathType; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 49 | |
| 50 | /* other optional configs */ |
| 51 | private final Map<String, String> otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 52 | |
| 53 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 54 | * Default constructor. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 55 | * |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 56 | * @param name name of the bridge |
| 57 | * @param failMode openflow controller fail mode policy |
| 58 | * @param controllers list of openflow controllers |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 59 | * @param datapathType ovs datapath_type |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 60 | * @param otherConfigs other configs |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 61 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 62 | private OvsdbBridge(String name, Optional<FailMode> failMode, |
| 63 | List<ControllerInfo> controllers, |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 64 | Optional<String> datapathType, |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 65 | Map<String, String> otherConfigs) { |
| 66 | this.name = checkNotNull(name); |
| 67 | this.failMode = failMode; |
| 68 | this.controllers = controllers; |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 69 | this.datapathType = datapathType; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 70 | this.otherConfigs = otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 74 | * Gets the bridge name of bridge. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 75 | * |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 76 | * @return the bridge name of bridge |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 77 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 78 | public String name() { |
| 79 | return name; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the controllers of the bridge. |
| 84 | * |
| 85 | * @return list of controllers |
| 86 | */ |
| 87 | public List<ControllerInfo> controllers() { |
| 88 | return controllers; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns fail mode of the bridge. |
| 93 | * |
| 94 | * @return fail mode |
| 95 | */ |
| 96 | public Optional<FailMode> failMode() { |
| 97 | return failMode; |
| 98 | } |
| 99 | |
| 100 | /** |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 101 | * Returns OVSDB datapath Type of the bridge. |
| 102 | * |
| 103 | * @return datapath type |
| 104 | */ |
| 105 | public Optional<String> datapathType() { |
| 106 | return datapathType; |
| 107 | } |
| 108 | |
| 109 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 110 | * Returns other configurations of the bridge. |
| 111 | * |
| 112 | * @return map of configurations |
| 113 | */ |
| 114 | public Map<String, String> otherConfigs() { |
| 115 | return otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 119 | * Gets the datapathId of bridge. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 120 | * |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 121 | * @return datapath id; null if not used |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 122 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 123 | public Optional<String> datapathId() { |
| 124 | return Optional.ofNullable(otherConfigs.get(DATAPATH_ID)); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int hashCode() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 129 | return Objects.hash(name); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object obj) { |
| 134 | if (this == obj) { |
| 135 | return true; |
| 136 | } |
| 137 | if (obj instanceof OvsdbBridge) { |
| 138 | final OvsdbBridge otherOvsdbBridge = (OvsdbBridge) obj; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 139 | return Objects.equals(this.name, otherOvsdbBridge.name); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public String toString() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 146 | return toStringHelper(this) |
| 147 | .add("bridgeName", name) |
| 148 | .add("failMode", failMode) |
| 149 | .add("controllers", controllers) |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 150 | .add("datapathType", datapathType) |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 151 | .add("otherConfigs", otherConfigs) |
| 152 | .toString(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns a new builder instance. |
| 157 | * |
| 158 | * @return ovsdb bridge builder |
| 159 | */ |
| 160 | public static OvsdbBridge.Builder builder() { |
| 161 | return new Builder(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns OVSDB bridge object with a given bridge description. |
| 166 | * |
| 167 | * @param bridgeDesc bridge description |
| 168 | * @return ovsdb bridge |
| 169 | */ |
| 170 | public static OvsdbBridge.Builder builder(BridgeDescription bridgeDesc) { |
| 171 | return new Builder(bridgeDesc); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Builder of OVSDB bridge entities. |
| 176 | */ |
| 177 | public static final class Builder { |
| 178 | private String name; |
| 179 | private Optional<FailMode> failMode = Optional.empty(); |
| 180 | private List<ControllerInfo> controllers = Lists.newArrayList(); |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 181 | private Optional<String> datapathType = Optional.empty(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 182 | private Map<String, String> otherConfigs = Maps.newHashMap(); |
| 183 | |
| 184 | private Builder() { |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Constructs OVSDB bridge builder with a given bridge description. |
| 189 | * |
| 190 | * @param bridgeDesc bridge description |
| 191 | */ |
| 192 | private Builder(BridgeDescription bridgeDesc) { |
| 193 | if (bridgeDesc.datapathId().isPresent()) { |
| 194 | otherConfigs.put(DATAPATH_ID, bridgeDesc.datapathId().get()); |
| 195 | } |
| 196 | if (bridgeDesc.disableInBand().isPresent()) { |
| 197 | otherConfigs.put(DISABLE_INBAND, |
| 198 | bridgeDesc.disableInBand().get().toString()); |
| 199 | } |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 200 | if (bridgeDesc.datapathType().isPresent()) { |
| 201 | this.datapathType = bridgeDesc.datapathType(); |
| 202 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 203 | this.name = bridgeDesc.name(); |
| 204 | this.failMode = bridgeDesc.failMode(); |
| 205 | this.controllers = Lists.newArrayList(bridgeDesc.controllers()); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Builds an immutable OVSDB bridge. |
| 210 | * |
| 211 | * @return ovsdb bridge |
| 212 | */ |
| 213 | public OvsdbBridge build() { |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 214 | return new OvsdbBridge(name, failMode, controllers, datapathType, otherConfigs); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns OVSDB bridge builder with a given name. |
| 219 | * |
| 220 | * @param name name of the bridge |
| 221 | * @return ovsdb bridge builder |
| 222 | */ |
| 223 | public Builder name(String name) { |
| 224 | this.name = name; |
| 225 | return this; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns OVSDB bridge builder with a given fail mode. |
| 230 | * |
| 231 | * @param failMode fail mode |
| 232 | * @return ovsdb bridge builder |
| 233 | */ |
| 234 | public Builder failMode(FailMode failMode) { |
| 235 | this.failMode = Optional.ofNullable(failMode); |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns OVSDB bridge builder with given controllers. |
| 241 | * |
| 242 | * @param controllers list of controllers |
| 243 | * @return ovsdb bridge builder |
| 244 | */ |
| 245 | public Builder controllers(List<ControllerInfo> controllers) { |
| 246 | this.controllers = Lists.newArrayList(controllers); |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns OVSDB bridge builder with a given controller. |
| 252 | * |
| 253 | * @param controller controller |
| 254 | * @return ovsdb bridge builder |
| 255 | */ |
| 256 | public Builder controller(ControllerInfo controller) { |
| 257 | this.controllers = Lists.newArrayList(controller); |
| 258 | return this; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns OVSDB bridge builder with given configs. |
| 263 | * |
| 264 | * @param otherConfigs other configs |
| 265 | * @return ovsdb bridge builder |
| 266 | */ |
| 267 | public Builder otherConfigs(Map<String, String> otherConfigs) { |
| 268 | this.otherConfigs = Maps.newHashMap(otherConfigs); |
| 269 | return this; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns OVSDB bridge builder with a given datapath ID. |
| 274 | * |
| 275 | * @param datapathId datapath id |
| 276 | * @return ovsdb bridge builder |
| 277 | */ |
| 278 | public Builder datapathId(String datapathId) { |
| 279 | otherConfigs.put(DATAPATH_ID, datapathId); |
| 280 | return this; |
| 281 | } |
| 282 | |
| 283 | /** |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 284 | * Returns OVSDB bridge builder with a given datapath type. |
| 285 | * |
| 286 | * @param datapathType datapath Type |
| 287 | * @return ovsdb bridge builder |
| 288 | */ |
| 289 | public Builder datapathType(String datapathType) { |
| 290 | this.datapathType = Optional.ofNullable(datapathType); |
| 291 | return this; |
| 292 | } |
| 293 | |
| 294 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 295 | * Returns OVSDB bridge builder with a given disable in-band config. |
| 296 | * |
| 297 | * @return ovsdb bridge builder |
| 298 | */ |
| 299 | public Builder disableInBand() { |
| 300 | otherConfigs.put(DATAPATH_ID, Boolean.TRUE.toString()); |
| 301 | return this; |
| 302 | } |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 303 | } |
| 304 | } |