blob: 6bddef3a95883b5e14154ac1af633ad8c02849b6 [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
56 /**
Hyunsun Moon1251e192016-06-07 16:57:05 -070057 * Default constructor.
CNlucius74fd4942015-07-20 14:28:04 +080058 *
Hyunsun Moon1251e192016-06-07 16:57:05 -070059 * @param name name of the bridge
60 * @param failMode openflow controller fail mode policy
61 * @param controllers list of openflow controllers
jaegonkim80bee532017-05-15 15:16:38 +090062 * @param datapathType ovs datapath_type
rohitsharana127ba82018-01-16 02:17:30 +053063 * @param controlProtocols list of control protocols
Hyunsun Moon1251e192016-06-07 16:57:05 -070064 * @param otherConfigs other configs
CNlucius74fd4942015-07-20 14:28:04 +080065 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070066 private OvsdbBridge(String name, Optional<FailMode> failMode,
67 List<ControllerInfo> controllers,
jaegonkim80bee532017-05-15 15:16:38 +090068 Optional<String> datapathType,
rohitsharana127ba82018-01-16 02:17:30 +053069 Optional<List<ControlProtocolVersion>> controlProtocols,
Hyunsun Moon1251e192016-06-07 16:57:05 -070070 Map<String, String> otherConfigs) {
71 this.name = checkNotNull(name);
72 this.failMode = failMode;
73 this.controllers = controllers;
jaegonkim80bee532017-05-15 15:16:38 +090074 this.datapathType = datapathType;
rohitsharana127ba82018-01-16 02:17:30 +053075 this.controlProtocols = controlProtocols;
Hyunsun Moon1251e192016-06-07 16:57:05 -070076 this.otherConfigs = otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +080077 }
78
79 /**
BitOhenrycec4eea2015-10-23 17:14:02 +080080 * Gets the bridge name of bridge.
CNlucius74fd4942015-07-20 14:28:04 +080081 *
BitOhenrycec4eea2015-10-23 17:14:02 +080082 * @return the bridge name of bridge
CNlucius74fd4942015-07-20 14:28:04 +080083 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070084 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 /**
jaegonkim80bee532017-05-15 15:16:38 +0900107 * Returns OVSDB datapath Type of the bridge.
108 *
109 * @return datapath type
110 */
111 public Optional<String> datapathType() {
112 return datapathType;
113 }
114
115 /**
rohitsharana127ba82018-01-16 02:17:30 +0530116 * 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 Moon1251e192016-06-07 16:57:05 -0700124 * Returns other configurations of the bridge.
125 *
126 * @return map of configurations
127 */
128 public Map<String, String> otherConfigs() {
129 return otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +0800130 }
131
132 /**
BitOhenrycec4eea2015-10-23 17:14:02 +0800133 * Gets the datapathId of bridge.
CNlucius74fd4942015-07-20 14:28:04 +0800134 *
Hyunsun Moon1251e192016-06-07 16:57:05 -0700135 * @return datapath id; null if not used
CNlucius74fd4942015-07-20 14:28:04 +0800136 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700137 public Optional<String> datapathId() {
138 return Optional.ofNullable(otherConfigs.get(DATAPATH_ID));
CNlucius74fd4942015-07-20 14:28:04 +0800139 }
140
141 @Override
142 public int hashCode() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700143 return Objects.hash(name);
CNlucius74fd4942015-07-20 14:28:04 +0800144 }
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 Moon1251e192016-06-07 16:57:05 -0700153 return Objects.equals(this.name, otherOvsdbBridge.name);
CNlucius74fd4942015-07-20 14:28:04 +0800154 }
155 return false;
156 }
157
158 @Override
159 public String toString() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700160 return toStringHelper(this)
161 .add("bridgeName", name)
162 .add("failMode", failMode)
163 .add("controllers", controllers)
jaegonkim80bee532017-05-15 15:16:38 +0900164 .add("datapathType", datapathType)
rohitsharana127ba82018-01-16 02:17:30 +0530165 .add("controlProtocols", controlProtocols)
Hyunsun Moon1251e192016-06-07 16:57:05 -0700166 .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();
jaegonkim80bee532017-05-15 15:16:38 +0900196 private Optional<String> datapathType = Optional.empty();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700197 private Map<String, String> otherConfigs = Maps.newHashMap();
rohitsharana127ba82018-01-16 02:17:30 +0530198 private Optional<List<ControlProtocolVersion>> controlProtocols = Optional.empty();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700199
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 }
jaegonkim80bee532017-05-15 15:16:38 +0900216 if (bridgeDesc.datapathType().isPresent()) {
217 this.datapathType = bridgeDesc.datapathType();
218 }
rohitsharana127ba82018-01-16 02:17:30 +0530219 if (bridgeDesc.controlProtocols().isPresent()) {
220 this.controlProtocols = bridgeDesc.controlProtocols();
221 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700222 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() {
rohitsharana127ba82018-01-16 02:17:30 +0530233 return new OvsdbBridge(name, failMode, controllers, datapathType, controlProtocols, otherConfigs);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700234 }
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 /**
jaegonkim80bee532017-05-15 15:16:38 +0900303 * 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 /**
rohitsharana127ba82018-01-16 02:17:30 +0530314 * 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 Moon1251e192016-06-07 16:57:05 -0700324 * 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 }
CNlucius74fd4942015-07-20 14:28:04 +0800332 }
333}