blob: b38d1d7414a7c1998c59dbbe92609263d09dd783 [file] [log] [blame]
CNlucius74fd4942015-07-20 14:28:04 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
23
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;
27
28import java.util.List;
29import java.util.Map;
30import java.util.Objects;
31import java.util.Optional;
32
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
50 /* other optional configs */
51 private final Map<String, String> otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +080052
53 /**
Hyunsun Moon1251e192016-06-07 16:57:05 -070054 * Default constructor.
CNlucius74fd4942015-07-20 14:28:04 +080055 *
Hyunsun Moon1251e192016-06-07 16:57:05 -070056 * @param name name of the bridge
57 * @param failMode openflow controller fail mode policy
58 * @param controllers list of openflow controllers
jaegonkim80bee532017-05-15 15:16:38 +090059 * @param datapathType ovs datapath_type
Hyunsun Moon1251e192016-06-07 16:57:05 -070060 * @param otherConfigs other configs
CNlucius74fd4942015-07-20 14:28:04 +080061 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070062 private OvsdbBridge(String name, Optional<FailMode> failMode,
63 List<ControllerInfo> controllers,
jaegonkim80bee532017-05-15 15:16:38 +090064 Optional<String> datapathType,
Hyunsun Moon1251e192016-06-07 16:57:05 -070065 Map<String, String> otherConfigs) {
66 this.name = checkNotNull(name);
67 this.failMode = failMode;
68 this.controllers = controllers;
jaegonkim80bee532017-05-15 15:16:38 +090069 this.datapathType = datapathType;
Hyunsun Moon1251e192016-06-07 16:57:05 -070070 this.otherConfigs = otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +080071 }
72
73 /**
BitOhenrycec4eea2015-10-23 17:14:02 +080074 * Gets the bridge name of bridge.
CNlucius74fd4942015-07-20 14:28:04 +080075 *
BitOhenrycec4eea2015-10-23 17:14:02 +080076 * @return the bridge name of bridge
CNlucius74fd4942015-07-20 14:28:04 +080077 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070078 public String name() {
79 return name;
80 }
81
82 /**
83 * Returns the controllers of the bridge.
84 *
85 * @return list of controllers
86 */
87 public List<ControllerInfo> controllers() {
88 return controllers;
89 }
90
91 /**
92 * Returns fail mode of the bridge.
93 *
94 * @return fail mode
95 */
96 public Optional<FailMode> failMode() {
97 return failMode;
98 }
99
100 /**
jaegonkim80bee532017-05-15 15:16:38 +0900101 * Returns OVSDB datapath Type of the bridge.
102 *
103 * @return datapath type
104 */
105 public Optional<String> datapathType() {
106 return datapathType;
107 }
108
109 /**
Hyunsun Moon1251e192016-06-07 16:57:05 -0700110 * Returns other configurations of the bridge.
111 *
112 * @return map of configurations
113 */
114 public Map<String, String> otherConfigs() {
115 return otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +0800116 }
117
118 /**
BitOhenrycec4eea2015-10-23 17:14:02 +0800119 * Gets the datapathId of bridge.
CNlucius74fd4942015-07-20 14:28:04 +0800120 *
Hyunsun Moon1251e192016-06-07 16:57:05 -0700121 * @return datapath id; null if not used
CNlucius74fd4942015-07-20 14:28:04 +0800122 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700123 public Optional<String> datapathId() {
124 return Optional.ofNullable(otherConfigs.get(DATAPATH_ID));
CNlucius74fd4942015-07-20 14:28:04 +0800125 }
126
127 @Override
128 public int hashCode() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700129 return Objects.hash(name);
CNlucius74fd4942015-07-20 14:28:04 +0800130 }
131
132 @Override
133 public boolean equals(Object obj) {
134 if (this == obj) {
135 return true;
136 }
137 if (obj instanceof OvsdbBridge) {
138 final OvsdbBridge otherOvsdbBridge = (OvsdbBridge) obj;
Hyunsun Moon1251e192016-06-07 16:57:05 -0700139 return Objects.equals(this.name, otherOvsdbBridge.name);
CNlucius74fd4942015-07-20 14:28:04 +0800140 }
141 return false;
142 }
143
144 @Override
145 public String toString() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700146 return toStringHelper(this)
147 .add("bridgeName", name)
148 .add("failMode", failMode)
149 .add("controllers", controllers)
jaegonkim80bee532017-05-15 15:16:38 +0900150 .add("datapathType", datapathType)
Hyunsun Moon1251e192016-06-07 16:57:05 -0700151 .add("otherConfigs", otherConfigs)
152 .toString();
153 }
154
155 /**
156 * Returns a new builder instance.
157 *
158 * @return ovsdb bridge builder
159 */
160 public static OvsdbBridge.Builder builder() {
161 return new Builder();
162 }
163
164 /**
165 * Returns OVSDB bridge object with a given bridge description.
166 *
167 * @param bridgeDesc bridge description
168 * @return ovsdb bridge
169 */
170 public static OvsdbBridge.Builder builder(BridgeDescription bridgeDesc) {
171 return new Builder(bridgeDesc);
172 }
173
174 /**
175 * Builder of OVSDB bridge entities.
176 */
177 public static final class Builder {
178 private String name;
179 private Optional<FailMode> failMode = Optional.empty();
180 private List<ControllerInfo> controllers = Lists.newArrayList();
jaegonkim80bee532017-05-15 15:16:38 +0900181 private Optional<String> datapathType = Optional.empty();
Hyunsun Moon1251e192016-06-07 16:57:05 -0700182 private Map<String, String> otherConfigs = Maps.newHashMap();
183
184 private Builder() {
185 }
186
187 /**
188 * Constructs OVSDB bridge builder with a given bridge description.
189 *
190 * @param bridgeDesc bridge description
191 */
192 private Builder(BridgeDescription bridgeDesc) {
193 if (bridgeDesc.datapathId().isPresent()) {
194 otherConfigs.put(DATAPATH_ID, bridgeDesc.datapathId().get());
195 }
196 if (bridgeDesc.disableInBand().isPresent()) {
197 otherConfigs.put(DISABLE_INBAND,
198 bridgeDesc.disableInBand().get().toString());
199 }
jaegonkim80bee532017-05-15 15:16:38 +0900200 if (bridgeDesc.datapathType().isPresent()) {
201 this.datapathType = bridgeDesc.datapathType();
202 }
Hyunsun Moon1251e192016-06-07 16:57:05 -0700203 this.name = bridgeDesc.name();
204 this.failMode = bridgeDesc.failMode();
205 this.controllers = Lists.newArrayList(bridgeDesc.controllers());
206 }
207
208 /**
209 * Builds an immutable OVSDB bridge.
210 *
211 * @return ovsdb bridge
212 */
213 public OvsdbBridge build() {
jaegonkim80bee532017-05-15 15:16:38 +0900214 return new OvsdbBridge(name, failMode, controllers, datapathType, otherConfigs);
Hyunsun Moon1251e192016-06-07 16:57:05 -0700215 }
216
217 /**
218 * Returns OVSDB bridge builder with a given name.
219 *
220 * @param name name of the bridge
221 * @return ovsdb bridge builder
222 */
223 public Builder name(String name) {
224 this.name = name;
225 return this;
226 }
227
228 /**
229 * Returns OVSDB bridge builder with a given fail mode.
230 *
231 * @param failMode fail mode
232 * @return ovsdb bridge builder
233 */
234 public Builder failMode(FailMode failMode) {
235 this.failMode = Optional.ofNullable(failMode);
236 return this;
237 }
238
239 /**
240 * Returns OVSDB bridge builder with given controllers.
241 *
242 * @param controllers list of controllers
243 * @return ovsdb bridge builder
244 */
245 public Builder controllers(List<ControllerInfo> controllers) {
246 this.controllers = Lists.newArrayList(controllers);
247 return this;
248 }
249
250 /**
251 * Returns OVSDB bridge builder with a given controller.
252 *
253 * @param controller controller
254 * @return ovsdb bridge builder
255 */
256 public Builder controller(ControllerInfo controller) {
257 this.controllers = Lists.newArrayList(controller);
258 return this;
259 }
260
261 /**
262 * Returns OVSDB bridge builder with given configs.
263 *
264 * @param otherConfigs other configs
265 * @return ovsdb bridge builder
266 */
267 public Builder otherConfigs(Map<String, String> otherConfigs) {
268 this.otherConfigs = Maps.newHashMap(otherConfigs);
269 return this;
270 }
271
272 /**
273 * Returns OVSDB bridge builder with a given datapath ID.
274 *
275 * @param datapathId datapath id
276 * @return ovsdb bridge builder
277 */
278 public Builder datapathId(String datapathId) {
279 otherConfigs.put(DATAPATH_ID, datapathId);
280 return this;
281 }
282
283 /**
jaegonkim80bee532017-05-15 15:16:38 +0900284 * Returns OVSDB bridge builder with a given datapath type.
285 *
286 * @param datapathType datapath Type
287 * @return ovsdb bridge builder
288 */
289 public Builder datapathType(String datapathType) {
290 this.datapathType = Optional.ofNullable(datapathType);
291 return this;
292 }
293
294 /**
Hyunsun Moon1251e192016-06-07 16:57:05 -0700295 * Returns OVSDB bridge builder with a given disable in-band config.
296 *
297 * @return ovsdb bridge builder
298 */
299 public Builder disableInBand() {
300 otherConfigs.put(DATAPATH_ID, Boolean.TRUE.toString());
301 return this;
302 }
CNlucius74fd4942015-07-20 14:28:04 +0800303 }
304}