blob: b1946957bbb2f52a4d4e74c3c04d032bb8d2bec3 [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
18import static com.google.common.base.MoreObjects.toStringHelper;
jaegonkim256f2c12017-05-26 00:03:42 +090019import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070020import static org.onosproject.ovsdb.controller.OvsdbConstant.*;
21
22import java.util.Map;
23import java.util.Objects;
jaegonkim256f2c12017-05-26 00:03:42 +090024import java.util.Optional;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070025
26import com.google.common.collect.Maps;
27import org.onosproject.net.DefaultAnnotations;
Hyunsun Moon89478662016-06-09 17:52:34 -070028import org.onosproject.net.behaviour.PatchDescription;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070029import org.onosproject.net.behaviour.TunnelDescription;
30
31/**
32 * The class representing an OVSDB interface.
33 * This class is immutable.
34 */
35public final class OvsdbInterface {
36
37 public enum Type {
38 /**
39 * An ordinary network device, e.g. eth0 on Linux.
40 */
41 SYSTEM,
42 /**
43 * A simulated network device that sends and receives traffic.
44 */
45 INTERNAL,
46 /**
47 * A TUN/TAP device managed by Open vSwitch.
48 */
49 TAP,
50 /**
51 * An Ethernet over RFC 2890 Generic Routing Encapsulation over IPv4 IPsec tunnel.
52 */
53 GRE,
54 /**
Jian Li654204c2018-12-16 01:56:31 +090055 * An Ethernet over draft-ietf-nvo3-geneve-08 Generic Network Virtualization Encapsulation tunnel.
56 */
57 GENEVE,
58 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070059 * An Ethernet tunnel over the experimental, UDP-based VXLAN protocol.
60 */
61 VXLAN,
62 /**
63 * A pair of virtual devices that act as a patch cable.
64 */
jaegonkim256f2c12017-05-26 00:03:42 +090065 PATCH,
66 /**
67 * A DPDK net device.
68 */
69 DPDK
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070070 }
71
72 private final String name;
73 private final Type type;
jaegonkim256f2c12017-05-26 00:03:42 +090074 private final Optional<Long> mtu;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070075
76 /* Adds more configs */
77
78 /* Fields start with "options:" prefix defined in the OVSDB */
79 private final Map<String, String> options;
80
jaegonkim256f2c12017-05-26 00:03:42 +090081 private OvsdbInterface(String name, Type type, Optional<Long> mtu, Map<String, String> options) {
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070082 this.name = name;
jaegonkim256f2c12017-05-26 00:03:42 +090083 this.type = checkNotNull(type, "the type of interface can not be null");
84 this.mtu = mtu;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070085 this.options = Maps.newHashMap(options);
86 }
87
88 /**
89 * Returns name of the interface.
90 *
91 * @return interface name
92 */
93 public String name() {
94 return name;
95 }
96
97 /**
98 * Returns type of the interface.
99 *
100 * @return interface type
101 */
102 public Type type() {
103 return type;
104 }
105
106 /**
107 * Returns type of the interface with lowercase string.
108 *
109 * @return interface type string
110 */
111 public String typeToString() {
112 return type.name().toLowerCase();
113 }
114
115 /**
jaegonkim256f2c12017-05-26 00:03:42 +0900116 * Returns mtu of the interface.
117 *
118 * @return interface mtu
119 */
120 public Optional<Long> mtu() {
121 return mtu;
122 }
123
124 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700125 * Returns optional configs of the interface.
126 *
127 * @return interface options
128 */
129 public Map<String, String> options() {
130 return options;
131 }
132
133 @Override
134 public int hashCode() {
135 return Objects.hash(name);
136 }
137
138 @Override
139 public boolean equals(Object obj) {
140 if (this == obj) {
141 return true;
142 }
143 if (obj instanceof OvsdbInterface) {
144 final OvsdbInterface otherOvsdbInterface = (OvsdbInterface) obj;
145 return Objects.equals(this.name, otherOvsdbInterface.name);
146 }
147 return false;
148 }
149
150 @Override
151 public String toString() {
152 return toStringHelper(this)
153 .add("name", name)
154 .add("type", type)
jaegonkim256f2c12017-05-26 00:03:42 +0900155 .add("mtu", mtu)
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700156 .add("options", options)
157 .toString();
158 }
159
160 /**
161 * Returns new OVSDB interface builder.
162 *
163 * @return ovsdb interface builder
164 */
165 public static OvsdbInterface.Builder builder() {
166 return new Builder();
167 }
168
169 /**
170 * Returns new OVSDB interface builder with tunnel interface description.
171 *
172 * @param tunnelDesc tunnel interface description
173 * @return ovsdb interface builder
174 */
175 public static OvsdbInterface.Builder builder(TunnelDescription tunnelDesc) {
176 return new Builder(tunnelDesc);
177 }
178
179 /**
Hyunsun Moon89478662016-06-09 17:52:34 -0700180 * Returns new OVSDB interface builder with patch interface description.
181 *
182 * @param patchDesc patch interface description
183 * @return ovsdb interface builder
184 */
185 public static OvsdbInterface.Builder builder(PatchDescription patchDesc) {
186 return new Builder(patchDesc);
187 }
188
189 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700190 * Builder of OVSDB interface entities.
191 */
192 public static final class Builder {
193 private String name;
194 private Type type;
jaegonkim256f2c12017-05-26 00:03:42 +0900195 private Optional<Long> mtu = Optional.empty();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700196 private Map<String, String> options = Maps.newHashMap();
197
198 private Builder() {
199 }
200
201 /**
202 * Constructs a builder with a given tunnel interface description.
203 *
204 * @param tunnelDesc tunnel interface description
205 */
206 private Builder(TunnelDescription tunnelDesc) {
207 this.name = tunnelDesc.ifaceName();
208 this.type = Type.valueOf(tunnelDesc.type().name());
209
Hyunsun Moon89478662016-06-09 17:52:34 -0700210 Map<String, String> tunOptions = Maps.newHashMap();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700211 if (tunnelDesc.local().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700212 tunOptions.put(TUNNEL_LOCAL_IP, tunnelDesc.local().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700213 }
214 if (tunnelDesc.remote().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700215 tunOptions.put(TUNNEL_REMOTE_IP, tunnelDesc.remote().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700216 }
217 if (tunnelDesc.key().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700218 tunOptions.put(TUNNEL_KEY, tunnelDesc.key().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700219 }
220
221 // set other configurations if there are any
Hyunsun Moon89478662016-06-09 17:52:34 -0700222 tunOptions.putAll(((DefaultAnnotations) tunnelDesc.annotations()).asMap());
223 options = tunOptions;
224 }
225
226 /**
227 * Constructs a builder with a given patch interface description.
228 *
229 * @param patchDesc patch interface description
230 */
231 private Builder(PatchDescription patchDesc) {
232 this.name = patchDesc.ifaceName();
233 this.type = Type.PATCH;
234
235 Map<String, String> patchOptions = Maps.newHashMap();
236 patchOptions.put(PATCH_PEER, patchDesc.peer());
237 options = patchOptions;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700238 }
239
240 /**
241 * Returns new OVSDB interface.
242 *
243 * @return ovsdb interface
244 */
245 public OvsdbInterface build() {
jaegonkim256f2c12017-05-26 00:03:42 +0900246 return new OvsdbInterface(name, type, mtu, options);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700247 }
248
249 /**
250 * Returns OVSDB interface builder with a given name.
251 *
252 * @param name name of the interface
253 * @return ovsdb interface builder
254 */
255 public Builder name(String name) {
256 this.name = name;
257 return this;
258 }
259
260 /**
261 * Returns OVSDB interface builder with a given interface type.
262 *
263 * @param type type of the interface
264 * @return ovsdb interface builder
265 */
266 public Builder type(Type type) {
267 this.type = type;
268 return this;
269 }
270
271 /**
jaegonkim256f2c12017-05-26 00:03:42 +0900272 * Returns OVSDB interface builder with a given interface mtu.
273 *
274 * @param mtu mtu of the interface
275 * @return ovsdb interface builder
276 */
277 public Builder mtu(Long mtu) {
278 this.mtu = Optional.ofNullable(mtu);
279 return this;
280 }
281
282 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700283 * Returns OVSDB interface builder with given options.
284 *
285 * @param options map of options
286 * @return ovsdb interface builder
287 */
288 public Builder options(Map<String, String> options) {
289 this.options = Maps.newHashMap(options);
290 return this;
291 }
292 }
293}