blob: 00eb375b3507475859cda6dfcaa807d9edfcbb10 [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 */
48
49 /* other optional configs */
50 private final Map<String, String> otherConfigs;
CNlucius74fd4942015-07-20 14:28:04 +080051
52 /**
Hyunsun Moon1251e192016-06-07 16:57:05 -070053 * Default constructor.
CNlucius74fd4942015-07-20 14:28:04 +080054 *
Hyunsun Moon1251e192016-06-07 16:57:05 -070055 * @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
CNlucius74fd4942015-07-20 14:28:04 +080059 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070060 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;
CNlucius74fd4942015-07-20 14:28:04 +080067 }
68
69 /**
BitOhenrycec4eea2015-10-23 17:14:02 +080070 * Gets the bridge name of bridge.
CNlucius74fd4942015-07-20 14:28:04 +080071 *
BitOhenrycec4eea2015-10-23 17:14:02 +080072 * @return the bridge name of bridge
CNlucius74fd4942015-07-20 14:28:04 +080073 */
Hyunsun Moon1251e192016-06-07 16:57:05 -070074 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;
CNlucius74fd4942015-07-20 14:28:04 +0800103 }
104
105 /**
BitOhenrycec4eea2015-10-23 17:14:02 +0800106 * Gets the datapathId of bridge.
CNlucius74fd4942015-07-20 14:28:04 +0800107 *
Hyunsun Moon1251e192016-06-07 16:57:05 -0700108 * @return datapath id; null if not used
CNlucius74fd4942015-07-20 14:28:04 +0800109 */
Hyunsun Moon1251e192016-06-07 16:57:05 -0700110 public Optional<String> datapathId() {
111 return Optional.ofNullable(otherConfigs.get(DATAPATH_ID));
CNlucius74fd4942015-07-20 14:28:04 +0800112 }
113
114 @Override
115 public int hashCode() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700116 return Objects.hash(name);
CNlucius74fd4942015-07-20 14:28:04 +0800117 }
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 Moon1251e192016-06-07 16:57:05 -0700126 return Objects.equals(this.name, otherOvsdbBridge.name);
CNlucius74fd4942015-07-20 14:28:04 +0800127 }
128 return false;
129 }
130
131 @Override
132 public String toString() {
Hyunsun Moon1251e192016-06-07 16:57:05 -0700133 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 }
CNlucius74fd4942015-07-20 14:28:04 +0800275 }
276}