CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 23 | import org.onosproject.net.behaviour.ControlProtocolVersion; |
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; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | import java.util.Objects; |
| 30 | import java.util.Optional; |
| 31 | |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 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 | |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 50 | /* Add control protocol version property */ |
| 51 | private final Optional<List<ControlProtocolVersion>> controlProtocols; |
| 52 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 53 | /* other optional configs */ |
| 54 | private final Map<String, String> otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 55 | |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 56 | /* multicast snooping flag */ |
| 57 | private final Optional<Boolean> mcastSnoopingEnable; |
| 58 | |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 59 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 60 | * Default constructor. |
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 | * @param name name of the bridge |
| 63 | * @param failMode openflow controller fail mode policy |
| 64 | * @param controllers list of openflow controllers |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 65 | * @param datapathType ovs datapath_type |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 66 | * @param controlProtocols list of control protocols |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 67 | * @param otherConfigs other configs |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 68 | * @param mcastSnoopingEnable multicast snooping enable |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 69 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 70 | private OvsdbBridge(String name, Optional<FailMode> failMode, |
| 71 | List<ControllerInfo> controllers, |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 72 | Optional<String> datapathType, |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 73 | Optional<List<ControlProtocolVersion>> controlProtocols, |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 74 | Map<String, String> otherConfigs, Optional<Boolean> mcastSnoopingEnable) { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 75 | this.name = checkNotNull(name); |
| 76 | this.failMode = failMode; |
| 77 | this.controllers = controllers; |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 78 | this.datapathType = datapathType; |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 79 | this.controlProtocols = controlProtocols; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 80 | this.otherConfigs = otherConfigs; |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 81 | this.mcastSnoopingEnable = mcastSnoopingEnable; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 85 | * Gets the bridge name of bridge. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 86 | * |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 87 | * @return the bridge name of bridge |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 88 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 89 | public String name() { |
| 90 | return name; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the controllers of the bridge. |
| 95 | * |
| 96 | * @return list of controllers |
| 97 | */ |
| 98 | public List<ControllerInfo> controllers() { |
| 99 | return controllers; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns fail mode of the bridge. |
| 104 | * |
| 105 | * @return fail mode |
| 106 | */ |
| 107 | public Optional<FailMode> failMode() { |
| 108 | return failMode; |
| 109 | } |
| 110 | |
| 111 | /** |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 112 | * Returns OVSDB datapath Type of the bridge. |
| 113 | * |
| 114 | * @return datapath type |
| 115 | */ |
| 116 | public Optional<String> datapathType() { |
| 117 | return datapathType; |
| 118 | } |
| 119 | |
| 120 | /** |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 121 | * Returns Control protocol versions of the bridge. |
| 122 | * @return List of Control protocols |
| 123 | */ |
| 124 | public Optional<List<ControlProtocolVersion>> controlProtocols() { |
| 125 | return controlProtocols; |
| 126 | } |
| 127 | |
| 128 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 129 | * Returns other configurations of the bridge. |
| 130 | * |
| 131 | * @return map of configurations |
| 132 | */ |
| 133 | public Map<String, String> otherConfigs() { |
| 134 | return otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 138 | * Returns multicast snooping flag value of the bridge. |
| 139 | * |
| 140 | * @return multicast snooping flag |
| 141 | */ |
| 142 | public Optional<Boolean> mcastSnoopingEnable() { |
| 143 | return mcastSnoopingEnable; |
| 144 | } |
| 145 | |
| 146 | /** |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 147 | * Gets the datapathId of bridge. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 148 | * |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 149 | * @return datapath id; null if not used |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 150 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 151 | public Optional<String> datapathId() { |
| 152 | return Optional.ofNullable(otherConfigs.get(DATAPATH_ID)); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public int hashCode() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 157 | return Objects.hash(name); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean equals(Object obj) { |
| 162 | if (this == obj) { |
| 163 | return true; |
| 164 | } |
| 165 | if (obj instanceof OvsdbBridge) { |
| 166 | final OvsdbBridge otherOvsdbBridge = (OvsdbBridge) obj; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 167 | return Objects.equals(this.name, otherOvsdbBridge.name); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public String toString() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 174 | return toStringHelper(this) |
| 175 | .add("bridgeName", name) |
| 176 | .add("failMode", failMode) |
| 177 | .add("controllers", controllers) |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 178 | .add("datapathType", datapathType) |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 179 | .add("controlProtocols", controlProtocols) |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 180 | .add("otherConfigs", otherConfigs) |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 181 | .add("mcastSnoopingEnable", mcastSnoopingEnable) |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 182 | .toString(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns a new builder instance. |
| 187 | * |
| 188 | * @return ovsdb bridge builder |
| 189 | */ |
| 190 | public static OvsdbBridge.Builder builder() { |
| 191 | return new Builder(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns OVSDB bridge object with a given bridge description. |
| 196 | * |
| 197 | * @param bridgeDesc bridge description |
| 198 | * @return ovsdb bridge |
| 199 | */ |
| 200 | public static OvsdbBridge.Builder builder(BridgeDescription bridgeDesc) { |
| 201 | return new Builder(bridgeDesc); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Builder of OVSDB bridge entities. |
| 206 | */ |
| 207 | public static final class Builder { |
| 208 | private String name; |
| 209 | private Optional<FailMode> failMode = Optional.empty(); |
| 210 | private List<ControllerInfo> controllers = Lists.newArrayList(); |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 211 | private Optional<String> datapathType = Optional.empty(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 212 | private Map<String, String> otherConfigs = Maps.newHashMap(); |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 213 | private Optional<List<ControlProtocolVersion>> controlProtocols = Optional.empty(); |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 214 | private Optional<Boolean> mcastSnoopingEnable = Optional.empty(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 215 | |
| 216 | private Builder() { |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Constructs OVSDB bridge builder with a given bridge description. |
| 221 | * |
| 222 | * @param bridgeDesc bridge description |
| 223 | */ |
| 224 | private Builder(BridgeDescription bridgeDesc) { |
| 225 | if (bridgeDesc.datapathId().isPresent()) { |
| 226 | otherConfigs.put(DATAPATH_ID, bridgeDesc.datapathId().get()); |
| 227 | } |
| 228 | if (bridgeDesc.disableInBand().isPresent()) { |
| 229 | otherConfigs.put(DISABLE_INBAND, |
| 230 | bridgeDesc.disableInBand().get().toString()); |
| 231 | } |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 232 | if (bridgeDesc.datapathType().isPresent()) { |
| 233 | this.datapathType = bridgeDesc.datapathType(); |
| 234 | } |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 235 | if (bridgeDesc.controlProtocols().isPresent()) { |
| 236 | this.controlProtocols = bridgeDesc.controlProtocols(); |
| 237 | } |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 238 | if (bridgeDesc.mcastSnoopingEnable().isPresent()) { |
| 239 | this.mcastSnoopingEnable = bridgeDesc.mcastSnoopingEnable(); |
| 240 | } |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 241 | this.name = bridgeDesc.name(); |
| 242 | this.failMode = bridgeDesc.failMode(); |
| 243 | this.controllers = Lists.newArrayList(bridgeDesc.controllers()); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Builds an immutable OVSDB bridge. |
| 248 | * |
| 249 | * @return ovsdb bridge |
| 250 | */ |
| 251 | public OvsdbBridge build() { |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 252 | return new OvsdbBridge(name, failMode, controllers, datapathType, |
| 253 | controlProtocols, otherConfigs, mcastSnoopingEnable); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns OVSDB bridge builder with a given name. |
| 258 | * |
| 259 | * @param name name of the bridge |
| 260 | * @return ovsdb bridge builder |
| 261 | */ |
| 262 | public Builder name(String name) { |
| 263 | this.name = name; |
| 264 | return this; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns OVSDB bridge builder with a given fail mode. |
| 269 | * |
| 270 | * @param failMode fail mode |
| 271 | * @return ovsdb bridge builder |
| 272 | */ |
| 273 | public Builder failMode(FailMode failMode) { |
| 274 | this.failMode = Optional.ofNullable(failMode); |
| 275 | return this; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns OVSDB bridge builder with given controllers. |
| 280 | * |
| 281 | * @param controllers list of controllers |
| 282 | * @return ovsdb bridge builder |
| 283 | */ |
| 284 | public Builder controllers(List<ControllerInfo> controllers) { |
| 285 | this.controllers = Lists.newArrayList(controllers); |
| 286 | return this; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Returns OVSDB bridge builder with a given controller. |
| 291 | * |
| 292 | * @param controller controller |
| 293 | * @return ovsdb bridge builder |
| 294 | */ |
| 295 | public Builder controller(ControllerInfo controller) { |
| 296 | this.controllers = Lists.newArrayList(controller); |
| 297 | return this; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Returns OVSDB bridge builder with given configs. |
| 302 | * |
| 303 | * @param otherConfigs other configs |
| 304 | * @return ovsdb bridge builder |
| 305 | */ |
| 306 | public Builder otherConfigs(Map<String, String> otherConfigs) { |
| 307 | this.otherConfigs = Maps.newHashMap(otherConfigs); |
| 308 | return this; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Returns OVSDB bridge builder with a given datapath ID. |
| 313 | * |
| 314 | * @param datapathId datapath id |
| 315 | * @return ovsdb bridge builder |
| 316 | */ |
| 317 | public Builder datapathId(String datapathId) { |
| 318 | otherConfigs.put(DATAPATH_ID, datapathId); |
| 319 | return this; |
| 320 | } |
| 321 | |
| 322 | /** |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 323 | * Returns OVSDB bridge builder with a given datapath type. |
| 324 | * |
| 325 | * @param datapathType datapath Type |
| 326 | * @return ovsdb bridge builder |
| 327 | */ |
| 328 | public Builder datapathType(String datapathType) { |
| 329 | this.datapathType = Optional.ofNullable(datapathType); |
| 330 | return this; |
| 331 | } |
| 332 | |
| 333 | /** |
rohitsharan | a127ba8 | 2018-01-16 02:17:30 +0530 | [diff] [blame] | 334 | * Returns OVSDB bridge builder with given control protocol Versions. |
| 335 | * @param controlProtocols list of control protocols |
| 336 | * @return ovsdb bridge builder |
| 337 | */ |
| 338 | public Builder controlProtocols(List<ControlProtocolVersion> controlProtocols) { |
| 339 | this.controlProtocols = Optional.ofNullable(controlProtocols); |
| 340 | return this; |
| 341 | } |
| 342 | |
| 343 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 344 | * Returns OVSDB bridge builder with a given disable in-band config. |
| 345 | * |
| 346 | * @return ovsdb bridge builder |
| 347 | */ |
| 348 | public Builder disableInBand() { |
| 349 | otherConfigs.put(DATAPATH_ID, Boolean.TRUE.toString()); |
| 350 | return this; |
| 351 | } |
Jian Li | 0aed0e6 | 2020-12-12 03:54:54 +0900 | [diff] [blame^] | 352 | |
| 353 | /** |
| 354 | * Returns OVSDB bridge builder with a given mcast snooping enable flag. |
| 355 | * |
| 356 | * @return ovsdb bridge builder |
| 357 | */ |
| 358 | public Builder mcastSnoopingEnable() { |
| 359 | this.mcastSnoopingEnable = Optional.of(Boolean.TRUE); |
| 360 | return this; |
| 361 | } |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 362 | } |
| 363 | } |