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 */ |
| 48 | |
| 49 | /* other optional configs */ |
| 50 | private final Map<String, String> otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 51 | |
| 52 | /** |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 53 | * Default constructor. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 54 | * |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 55 | * @param name name of the bridge |
| 56 | * @param failMode openflow controller fail mode policy |
| 57 | * @param controllers list of openflow controllers |
| 58 | * @param otherConfigs other configs |
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 | private OvsdbBridge(String name, Optional<FailMode> failMode, |
| 61 | List<ControllerInfo> controllers, |
| 62 | Map<String, String> otherConfigs) { |
| 63 | this.name = checkNotNull(name); |
| 64 | this.failMode = failMode; |
| 65 | this.controllers = controllers; |
| 66 | this.otherConfigs = otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 70 | * Gets the bridge name of bridge. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 71 | * |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 72 | * @return the bridge name of bridge |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 73 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 74 | public String name() { |
| 75 | return name; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the controllers of the bridge. |
| 80 | * |
| 81 | * @return list of controllers |
| 82 | */ |
| 83 | public List<ControllerInfo> controllers() { |
| 84 | return controllers; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns fail mode of the bridge. |
| 89 | * |
| 90 | * @return fail mode |
| 91 | */ |
| 92 | public Optional<FailMode> failMode() { |
| 93 | return failMode; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns other configurations of the bridge. |
| 98 | * |
| 99 | * @return map of configurations |
| 100 | */ |
| 101 | public Map<String, String> otherConfigs() { |
| 102 | return otherConfigs; |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
BitOhenry | cec4eea | 2015-10-23 17:14:02 +0800 | [diff] [blame] | 106 | * Gets the datapathId of bridge. |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 107 | * |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 108 | * @return datapath id; null if not used |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 109 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 110 | public Optional<String> datapathId() { |
| 111 | return Optional.ofNullable(otherConfigs.get(DATAPATH_ID)); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 116 | return Objects.hash(name); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
| 121 | if (this == obj) { |
| 122 | return true; |
| 123 | } |
| 124 | if (obj instanceof OvsdbBridge) { |
| 125 | final OvsdbBridge otherOvsdbBridge = (OvsdbBridge) obj; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 126 | return Objects.equals(this.name, otherOvsdbBridge.name); |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String toString() { |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame^] | 133 | return toStringHelper(this) |
| 134 | .add("bridgeName", name) |
| 135 | .add("failMode", failMode) |
| 136 | .add("controllers", controllers) |
| 137 | .add("otherConfigs", otherConfigs) |
| 138 | .toString(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns a new builder instance. |
| 143 | * |
| 144 | * @return ovsdb bridge builder |
| 145 | */ |
| 146 | public static OvsdbBridge.Builder builder() { |
| 147 | return new Builder(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns OVSDB bridge object with a given bridge description. |
| 152 | * |
| 153 | * @param bridgeDesc bridge description |
| 154 | * @return ovsdb bridge |
| 155 | */ |
| 156 | public static OvsdbBridge.Builder builder(BridgeDescription bridgeDesc) { |
| 157 | return new Builder(bridgeDesc); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Builder of OVSDB bridge entities. |
| 162 | */ |
| 163 | public static final class Builder { |
| 164 | private String name; |
| 165 | private Optional<FailMode> failMode = Optional.empty(); |
| 166 | private List<ControllerInfo> controllers = Lists.newArrayList(); |
| 167 | private Map<String, String> otherConfigs = Maps.newHashMap(); |
| 168 | |
| 169 | private Builder() { |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Constructs OVSDB bridge builder with a given bridge description. |
| 174 | * |
| 175 | * @param bridgeDesc bridge description |
| 176 | */ |
| 177 | private Builder(BridgeDescription bridgeDesc) { |
| 178 | if (bridgeDesc.datapathId().isPresent()) { |
| 179 | otherConfigs.put(DATAPATH_ID, bridgeDesc.datapathId().get()); |
| 180 | } |
| 181 | if (bridgeDesc.disableInBand().isPresent()) { |
| 182 | otherConfigs.put(DISABLE_INBAND, |
| 183 | bridgeDesc.disableInBand().get().toString()); |
| 184 | } |
| 185 | |
| 186 | this.name = bridgeDesc.name(); |
| 187 | this.failMode = bridgeDesc.failMode(); |
| 188 | this.controllers = Lists.newArrayList(bridgeDesc.controllers()); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Builds an immutable OVSDB bridge. |
| 193 | * |
| 194 | * @return ovsdb bridge |
| 195 | */ |
| 196 | public OvsdbBridge build() { |
| 197 | return new OvsdbBridge(name, failMode, controllers, otherConfigs); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns OVSDB bridge builder with a given name. |
| 202 | * |
| 203 | * @param name name of the bridge |
| 204 | * @return ovsdb bridge builder |
| 205 | */ |
| 206 | public Builder name(String name) { |
| 207 | this.name = name; |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns OVSDB bridge builder with a given fail mode. |
| 213 | * |
| 214 | * @param failMode fail mode |
| 215 | * @return ovsdb bridge builder |
| 216 | */ |
| 217 | public Builder failMode(FailMode failMode) { |
| 218 | this.failMode = Optional.ofNullable(failMode); |
| 219 | return this; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Returns OVSDB bridge builder with given controllers. |
| 224 | * |
| 225 | * @param controllers list of controllers |
| 226 | * @return ovsdb bridge builder |
| 227 | */ |
| 228 | public Builder controllers(List<ControllerInfo> controllers) { |
| 229 | this.controllers = Lists.newArrayList(controllers); |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns OVSDB bridge builder with a given controller. |
| 235 | * |
| 236 | * @param controller controller |
| 237 | * @return ovsdb bridge builder |
| 238 | */ |
| 239 | public Builder controller(ControllerInfo controller) { |
| 240 | this.controllers = Lists.newArrayList(controller); |
| 241 | return this; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Returns OVSDB bridge builder with given configs. |
| 246 | * |
| 247 | * @param otherConfigs other configs |
| 248 | * @return ovsdb bridge builder |
| 249 | */ |
| 250 | public Builder otherConfigs(Map<String, String> otherConfigs) { |
| 251 | this.otherConfigs = Maps.newHashMap(otherConfigs); |
| 252 | return this; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Returns OVSDB bridge builder with a given datapath ID. |
| 257 | * |
| 258 | * @param datapathId datapath id |
| 259 | * @return ovsdb bridge builder |
| 260 | */ |
| 261 | public Builder datapathId(String datapathId) { |
| 262 | otherConfigs.put(DATAPATH_ID, datapathId); |
| 263 | return this; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns OVSDB bridge builder with a given disable in-band config. |
| 268 | * |
| 269 | * @return ovsdb bridge builder |
| 270 | */ |
| 271 | public Builder disableInBand() { |
| 272 | otherConfigs.put(DATAPATH_ID, Boolean.TRUE.toString()); |
| 273 | return this; |
| 274 | } |
CNlucius | 74fd494 | 2015-07-20 14:28:04 +0800 | [diff] [blame] | 275 | } |
| 276 | } |