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