blob: b568eb07df5e6ae64e505ee58ea3bbf0020ef920 [file] [log] [blame]
CNlucius74fd4942015-07-20 14:28:04 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
CNlucius74fd4942015-07-20 14:28:04 +08003 *
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 */
16package org.onosproject.ovsdb.controller;
17
Hyunsun Moon1251e192016-06-07 16:57:05 -070018import com.google.common.collect.Lists;
19import com.google.common.collect.Maps;
20import org.onosproject.net.behaviour.BridgeDescription;
21import org.onosproject.net.behaviour.BridgeDescription.FailMode;
22import org.onosproject.net.behaviour.ControllerInfo;
rohitsharana127ba82018-01-16 02:17:30 +053023import org.onosproject.net.behaviour.ControlProtocolVersion;
CNlucius74fd4942015-07-20 14:28:04 +080024import static com.google.common.base.MoreObjects.toStringHelper;
Hyunsun Moon1251e192016-06-07 16:57:05 -070025import static org.onosproject.ovsdb.controller.OvsdbConstant.DATAPATH_ID;
26import static org.onosproject.ovsdb.controller.OvsdbConstant.DISABLE_INBAND;
Hyunsun Moon1251e192016-06-07 16:57:05 -070027import java.util.List;
28import java.util.Map;
29import java.util.Objects;
30import java.util.Optional;
31
rohitsharana127ba82018-01-16 02:17:30 +053032
CNlucius74fd4942015-07-20 14:28:04 +080033import static com.google.common.base.Preconditions.checkNotNull;
34
CNlucius74fd4942015-07-20 14:28:04 +080035/**
Hyunsun Moon1251e192016-06-07 16:57:05 -070036 * The class representing an OVSDB bridge.
BitOhenrycec4eea2015-10-23 17:14:02 +080037 * This class is immutable.
CNlucius74fd4942015-07-20 14:28:04 +080038 */
39public final class OvsdbBridge {
40
Hyunsun Moon1251e192016-06-07 16:57:05 -070041 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 */
jaegonkim80bee532017-05-15 15:16:38 +090048 private final Optional<String> datapathType;
Hyunsun Moon1251e192016-06-07 16:57:05 -070049
rohitsharana127ba82018-01-16 02:17:30 +053050 /* Add control protocol version property */
51 private final Optional<List<ControlProtocolVersion>> controlProtocols;
52
Hyunsun Moon1251e192016-06-07 16:57:05 -070053 /* other optional configs */
54 private final Map<String, String> otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +080055
Jian Li0aed0e62020-12-12 03:54:54 +090056 /* multicast snooping flag */
57 private final Optional<Boolean> mcastSnoopingEnable;
58
CNlucius74fd4942015-07-20 14:28:04 +080059 /**
Hyunsun Moon1251e192016-06-07 16:57:05 -070060 * Default constructor.
CNlucius74fd4942015-07-20 14:28:04 +080061 *
Hyunsun Moon1251e192016-06-07 16:57:05 -070062 * @param name name of the bridge
63 * @param failMode openflow controller fail mode policy
64 * @param controllers list of openflow controllers
jaegonkim80bee532017-05-15 15:16:38 +090065 * @param datapathType ovs datapath_type
rohitsharana127ba82018-01-16 02:17:30 +053066 * @param controlProtocols list of control protocols
Hyunsun Moon1251e192016-06-07 16:57:05 -070067 * @param otherConfigs other configs
Jian Li0aed0e62020-12-12 03:54:54 +090068 * @param mcastSnoopingEnable multicast snooping enable
CNlucius74fd4942015-07-20 14:28:04 +080069 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070070 private OvsdbBridge(String name, Optional<FailMode> failMode,
71 List<ControllerInfo> controllers,
jaegonkim80bee532017-05-15 15:16:38 +090072 Optional<String> datapathType,
rohitsharana127ba82018-01-16 02:17:30 +053073 Optional<List<ControlProtocolVersion>> controlProtocols,
Jian Li0aed0e62020-12-12 03:54:54 +090074 Map<String, String> otherConfigs, Optional<Boolean> mcastSnoopingEnable) {
Hyunsun Moon1251e192016-06-07 16:57:05 -070075 this.name = checkNotNull(name);
76 this.failMode = failMode;
77 this.controllers = controllers;
jaegonkim80bee532017-05-15 15:16:38 +090078 this.datapathType = datapathType;
rohitsharana127ba82018-01-16 02:17:30 +053079 this.controlProtocols = controlProtocols;
Hyunsun Moon1251e192016-06-07 16:57:05 -070080 this.otherConfigs = otherConfigs;
Jian Li0aed0e62020-12-12 03:54:54 +090081 this.mcastSnoopingEnable = mcastSnoopingEnable;
CNlucius74fd4942015-07-20 14:28:04 +080082 }
83
84 /**
BitOhenrycec4eea2015-10-23 17:14:02 +080085 * Gets the bridge name of bridge.
CNlucius74fd4942015-07-20 14:28:04 +080086 *
BitOhenrycec4eea2015-10-23 17:14:02 +080087 * @return the bridge name of bridge
CNlucius74fd4942015-07-20 14:28:04 +080088 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070089 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 /**
jaegonkim80bee532017-05-15 15:16:38 +0900112 * Returns OVSDB datapath Type of the bridge.
113 *
114 * @return datapath type
115 */
116 public Optional<String> datapathType() {
117 return datapathType;
118 }
119
120 /**
rohitsharana127ba82018-01-16 02:17:30 +0530121 * 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 Moon1251e192016-06-07 16:57:05 -0700129 * Returns other configurations of the bridge.
130 *
131 * @return map of configurations
132 */
133 public Map<String, String> otherConfigs() {
134 return otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +0800135 }
136
137 /**
Jian Li0aed0e62020-12-12 03:54:54 +0900138 * 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 /**
BitOhenrycec4eea2015-10-23 17:14:02 +0800147 * Gets the datapathId of bridge.
CNlucius74fd4942015-07-20 14:28:04 +0800148 *
Hyunsun Moon1251e192016-06-07 16:57:05 -0700149 * @return datapath id; null if not used
CNlucius74fd4942015-07-20 14:28:04 +0800150 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700151 public Optional<String> datapathId() {
152 return Optional.ofNullable(otherConfigs.get(DATAPATH_ID));
CNlucius74fd4942015-07-20 14:28:04 +0800153 }
154
155 @Override
156 public int hashCode() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700157 return Objects.hash(name);
CNlucius74fd4942015-07-20 14:28:04 +0800158 }
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 Moon1251e192016-06-07 16:57:05 -0700167 return Objects.equals(this.name, otherOvsdbBridge.name);
CNlucius74fd4942015-07-20 14:28:04 +0800168 }
169 return false;
170 }
171
172 @Override
173 public String toString() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700174 return toStringHelper(this)
175 .add("bridgeName", name)
176 .add("failMode", failMode)
177 .add("controllers", controllers)
jaegonkim80bee532017-05-15 15:16:38 +0900178 .add("datapathType", datapathType)
rohitsharana127ba82018-01-16 02:17:30 +0530179 .add("controlProtocols", controlProtocols)
Hyunsun Moon1251e192016-06-07 16:57:05 -0700180 .add("otherConfigs", otherConfigs)
Jian Li0aed0e62020-12-12 03:54:54 +0900181 .add("mcastSnoopingEnable", mcastSnoopingEnable)
Hyunsun Moon1251e192016-06-07 16:57:05 -0700182 .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();
jaegonkim80bee532017-05-15 15:16:38 +0900211 private Optional<String> datapathType = Optional.empty();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700212 private Map<String, String> otherConfigs = Maps.newHashMap();
rohitsharana127ba82018-01-16 02:17:30 +0530213 private Optional<List<ControlProtocolVersion>> controlProtocols = Optional.empty();
Jian Li0aed0e62020-12-12 03:54:54 +0900214 private Optional<Boolean> mcastSnoopingEnable = Optional.empty();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700215
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 }
jaegonkim80bee532017-05-15 15:16:38 +0900232 if (bridgeDesc.datapathType().isPresent()) {
233 this.datapathType = bridgeDesc.datapathType();
234 }
rohitsharana127ba82018-01-16 02:17:30 +0530235 if (bridgeDesc.controlProtocols().isPresent()) {
236 this.controlProtocols = bridgeDesc.controlProtocols();
237 }
Jian Li0aed0e62020-12-12 03:54:54 +0900238 if (bridgeDesc.mcastSnoopingEnable().isPresent()) {
239 this.mcastSnoopingEnable = bridgeDesc.mcastSnoopingEnable();
240 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700241 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 Li0aed0e62020-12-12 03:54:54 +0900252 return new OvsdbBridge(name, failMode, controllers, datapathType,
253 controlProtocols, otherConfigs, mcastSnoopingEnable);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700254 }
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 /**
jaegonkim80bee532017-05-15 15:16:38 +0900323 * 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 /**
rohitsharana127ba82018-01-16 02:17:30 +0530334 * 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 Moon1251e192016-06-07 16:57:05 -0700344 * 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 Li0aed0e62020-12-12 03:54:54 +0900352
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 }
CNlucius74fd4942015-07-20 14:28:04 +0800362 }
363}