blob: 9a6037249952988d4bd25f2dba29fec697872adb [file] [log] [blame]
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moondd14e8e2016-06-09 16:17:32 -07003 *
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
Jian Li83a5e8d2019-05-17 19:50:28 +090018import com.google.common.collect.Maps;
19import org.onosproject.net.DefaultAnnotations;
20import org.onosproject.net.behaviour.PatchDescription;
21import org.onosproject.net.behaviour.TunnelDescription;
22import org.onosproject.ovsdb.rfc.table.Interface.InterfaceColumn;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070023
24import java.util.Map;
25import java.util.Objects;
jaegonkim256f2c12017-05-26 00:03:42 +090026import java.util.Optional;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070027
Jian Li83a5e8d2019-05-17 19:50:28 +090028import static com.google.common.base.MoreObjects.toStringHelper;
29import static org.onosproject.ovsdb.controller.OvsdbConstant.PATCH_PEER;
30import static org.onosproject.ovsdb.controller.OvsdbConstant.TUNNEL_KEY;
31import static org.onosproject.ovsdb.controller.OvsdbConstant.TUNNEL_LOCAL_IP;
32import static org.onosproject.ovsdb.controller.OvsdbConstant.TUNNEL_REMOTE_IP;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070033
34/**
35 * The class representing an OVSDB interface.
36 * This class is immutable.
37 */
38public final class OvsdbInterface {
39
40 public enum Type {
41 /**
42 * An ordinary network device, e.g. eth0 on Linux.
43 */
44 SYSTEM,
45 /**
46 * A simulated network device that sends and receives traffic.
47 */
48 INTERNAL,
49 /**
50 * A TUN/TAP device managed by Open vSwitch.
51 */
52 TAP,
53 /**
54 * An Ethernet over RFC 2890 Generic Routing Encapsulation over IPv4 IPsec tunnel.
55 */
56 GRE,
57 /**
Jian Li6e666802018-12-15 03:06:04 +090058 * An Ethernet over draft-ietf-nvo3-geneve-08 Generic Network Virtualization Encapsulation tunnel.
59 */
60 GENEVE,
61 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070062 * An Ethernet tunnel over the experimental, UDP-based VXLAN protocol.
63 */
64 VXLAN,
65 /**
66 * A pair of virtual devices that act as a patch cable.
67 */
jaegonkim256f2c12017-05-26 00:03:42 +090068 PATCH,
69 /**
70 * A DPDK net device.
71 */
72 DPDK
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070073 }
74
75 private final String name;
76 private final Type type;
jaegonkim256f2c12017-05-26 00:03:42 +090077 private final Optional<Long> mtu;
Jian Li83a5e8d2019-05-17 19:50:28 +090078 private final Map<InterfaceColumn, Map<String, String>> data;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070079
80 /* Adds more configs */
81
82 /* Fields start with "options:" prefix defined in the OVSDB */
83 private final Map<String, String> options;
84
Jian Li83a5e8d2019-05-17 19:50:28 +090085 private OvsdbInterface(String name, Type type, Optional<Long> mtu,
86 Map<String, String> options, Map<InterfaceColumn,
87 Map<String, String>> data) {
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070088 this.name = name;
Jian Li83a5e8d2019-05-17 19:50:28 +090089 this.type = type;
jaegonkim256f2c12017-05-26 00:03:42 +090090 this.mtu = mtu;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070091 this.options = Maps.newHashMap(options);
Jian Li83a5e8d2019-05-17 19:50:28 +090092 this.data = data;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070093 }
94
95 /**
96 * Returns name of the interface.
97 *
98 * @return interface name
99 */
100 public String name() {
101 return name;
102 }
103
104 /**
105 * Returns type of the interface.
106 *
107 * @return interface type
108 */
109 public Type type() {
110 return type;
111 }
112
113 /**
114 * Returns type of the interface with lowercase string.
115 *
116 * @return interface type string
117 */
118 public String typeToString() {
119 return type.name().toLowerCase();
120 }
121
122 /**
jaegonkim256f2c12017-05-26 00:03:42 +0900123 * Returns mtu of the interface.
124 *
125 * @return interface mtu
126 */
127 public Optional<Long> mtu() {
128 return mtu;
129 }
130
131 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700132 * Returns optional configs of the interface.
133 *
134 * @return interface options
135 */
136 public Map<String, String> options() {
137 return options;
138 }
139
Jian Li83a5e8d2019-05-17 19:50:28 +0900140 /**
141 * Returns the data of the interface.
142 *
143 * @return interface data
144 */
145 public Map<InterfaceColumn, Map<String, String>> data() {
146 return data;
147 }
148
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700149 @Override
150 public int hashCode() {
151 return Objects.hash(name);
152 }
153
154 @Override
155 public boolean equals(Object obj) {
156 if (this == obj) {
157 return true;
158 }
159 if (obj instanceof OvsdbInterface) {
160 final OvsdbInterface otherOvsdbInterface = (OvsdbInterface) obj;
161 return Objects.equals(this.name, otherOvsdbInterface.name);
162 }
163 return false;
164 }
165
166 @Override
167 public String toString() {
168 return toStringHelper(this)
169 .add("name", name)
170 .add("type", type)
jaegonkim256f2c12017-05-26 00:03:42 +0900171 .add("mtu", mtu)
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700172 .add("options", options)
Jian Li83a5e8d2019-05-17 19:50:28 +0900173 .add("data", data)
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700174 .toString();
175 }
176
177 /**
178 * Returns new OVSDB interface builder.
179 *
180 * @return ovsdb interface builder
181 */
182 public static OvsdbInterface.Builder builder() {
183 return new Builder();
184 }
185
186 /**
187 * Returns new OVSDB interface builder with tunnel interface description.
188 *
189 * @param tunnelDesc tunnel interface description
190 * @return ovsdb interface builder
191 */
192 public static OvsdbInterface.Builder builder(TunnelDescription tunnelDesc) {
193 return new Builder(tunnelDesc);
194 }
195
196 /**
Hyunsun Moon89478662016-06-09 17:52:34 -0700197 * Returns new OVSDB interface builder with patch interface description.
198 *
199 * @param patchDesc patch interface description
200 * @return ovsdb interface builder
201 */
202 public static OvsdbInterface.Builder builder(PatchDescription patchDesc) {
203 return new Builder(patchDesc);
204 }
205
206 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700207 * Builder of OVSDB interface entities.
208 */
209 public static final class Builder {
210 private String name;
211 private Type type;
jaegonkim256f2c12017-05-26 00:03:42 +0900212 private Optional<Long> mtu = Optional.empty();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700213 private Map<String, String> options = Maps.newHashMap();
Jian Li83a5e8d2019-05-17 19:50:28 +0900214 private Map<InterfaceColumn, Map<String, String>> data = Maps.newHashMap();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700215
216 private Builder() {
217 }
218
219 /**
220 * Constructs a builder with a given tunnel interface description.
221 *
222 * @param tunnelDesc tunnel interface description
223 */
224 private Builder(TunnelDescription tunnelDesc) {
225 this.name = tunnelDesc.ifaceName();
226 this.type = Type.valueOf(tunnelDesc.type().name());
227
Hyunsun Moon89478662016-06-09 17:52:34 -0700228 Map<String, String> tunOptions = Maps.newHashMap();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700229 if (tunnelDesc.local().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700230 tunOptions.put(TUNNEL_LOCAL_IP, tunnelDesc.local().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700231 }
232 if (tunnelDesc.remote().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700233 tunOptions.put(TUNNEL_REMOTE_IP, tunnelDesc.remote().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700234 }
235 if (tunnelDesc.key().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700236 tunOptions.put(TUNNEL_KEY, tunnelDesc.key().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700237 }
238
239 // set other configurations if there are any
Hyunsun Moon89478662016-06-09 17:52:34 -0700240 tunOptions.putAll(((DefaultAnnotations) tunnelDesc.annotations()).asMap());
241 options = tunOptions;
242 }
243
244 /**
245 * Constructs a builder with a given patch interface description.
246 *
247 * @param patchDesc patch interface description
248 */
249 private Builder(PatchDescription patchDesc) {
250 this.name = patchDesc.ifaceName();
251 this.type = Type.PATCH;
252
253 Map<String, String> patchOptions = Maps.newHashMap();
254 patchOptions.put(PATCH_PEER, patchDesc.peer());
255 options = patchOptions;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700256 }
257
258 /**
259 * Returns new OVSDB interface.
260 *
261 * @return ovsdb interface
262 */
263 public OvsdbInterface build() {
Jian Li83a5e8d2019-05-17 19:50:28 +0900264 return new OvsdbInterface(name, type, mtu, options, data);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700265 }
266
267 /**
268 * Returns OVSDB interface builder with a given name.
269 *
270 * @param name name of the interface
271 * @return ovsdb interface builder
272 */
273 public Builder name(String name) {
274 this.name = name;
275 return this;
276 }
277
278 /**
279 * Returns OVSDB interface builder with a given interface type.
280 *
281 * @param type type of the interface
282 * @return ovsdb interface builder
283 */
284 public Builder type(Type type) {
285 this.type = type;
286 return this;
287 }
288
289 /**
jaegonkim256f2c12017-05-26 00:03:42 +0900290 * Returns OVSDB interface builder with a given interface mtu.
291 *
292 * @param mtu mtu of the interface
293 * @return ovsdb interface builder
294 */
295 public Builder mtu(Long mtu) {
296 this.mtu = Optional.ofNullable(mtu);
297 return this;
298 }
299
300 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700301 * Returns OVSDB interface builder with given options.
302 *
303 * @param options map of options
304 * @return ovsdb interface builder
305 */
306 public Builder options(Map<String, String> options) {
307 this.options = Maps.newHashMap(options);
308 return this;
309 }
Jian Li83a5e8d2019-05-17 19:50:28 +0900310
311 /**
312 * Returns OVSDB interface builder with given data.
313 *
314 * @param data map of data
315 * @return ovsdb interface builder
316 */
317 public Builder data(Map<InterfaceColumn, Map<String, String>> data) {
318 this.data = data;
319 return this;
320 }
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700321 }
322}