blob: be8afe4613129d37fafaaed7f738c1a0834b289e [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 /**
55 * An Ethernet tunnel over the experimental, UDP-based VXLAN protocol.
56 */
57 VXLAN,
58 /**
59 * A pair of virtual devices that act as a patch cable.
60 */
jaegonkim256f2c12017-05-26 00:03:42 +090061 PATCH,
62 /**
63 * A DPDK net device.
64 */
65 DPDK
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070066 }
67
68 private final String name;
69 private final Type type;
jaegonkim256f2c12017-05-26 00:03:42 +090070 private final Optional<Long> mtu;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070071
72 /* Adds more configs */
73
74 /* Fields start with "options:" prefix defined in the OVSDB */
75 private final Map<String, String> options;
76
jaegonkim256f2c12017-05-26 00:03:42 +090077 private OvsdbInterface(String name, Type type, Optional<Long> mtu, Map<String, String> options) {
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070078 this.name = name;
jaegonkim256f2c12017-05-26 00:03:42 +090079 this.type = checkNotNull(type, "the type of interface can not be null");
80 this.mtu = mtu;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -070081 this.options = Maps.newHashMap(options);
82 }
83
84 /**
85 * Returns name of the interface.
86 *
87 * @return interface name
88 */
89 public String name() {
90 return name;
91 }
92
93 /**
94 * Returns type of the interface.
95 *
96 * @return interface type
97 */
98 public Type type() {
99 return type;
100 }
101
102 /**
103 * Returns type of the interface with lowercase string.
104 *
105 * @return interface type string
106 */
107 public String typeToString() {
108 return type.name().toLowerCase();
109 }
110
111 /**
jaegonkim256f2c12017-05-26 00:03:42 +0900112 * Returns mtu of the interface.
113 *
114 * @return interface mtu
115 */
116 public Optional<Long> mtu() {
117 return mtu;
118 }
119
120 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700121 * Returns optional configs of the interface.
122 *
123 * @return interface options
124 */
125 public Map<String, String> options() {
126 return options;
127 }
128
129 @Override
130 public int hashCode() {
131 return Objects.hash(name);
132 }
133
134 @Override
135 public boolean equals(Object obj) {
136 if (this == obj) {
137 return true;
138 }
139 if (obj instanceof OvsdbInterface) {
140 final OvsdbInterface otherOvsdbInterface = (OvsdbInterface) obj;
141 return Objects.equals(this.name, otherOvsdbInterface.name);
142 }
143 return false;
144 }
145
146 @Override
147 public String toString() {
148 return toStringHelper(this)
149 .add("name", name)
150 .add("type", type)
jaegonkim256f2c12017-05-26 00:03:42 +0900151 .add("mtu", mtu)
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700152 .add("options", options)
153 .toString();
154 }
155
156 /**
157 * Returns new OVSDB interface builder.
158 *
159 * @return ovsdb interface builder
160 */
161 public static OvsdbInterface.Builder builder() {
162 return new Builder();
163 }
164
165 /**
166 * Returns new OVSDB interface builder with tunnel interface description.
167 *
168 * @param tunnelDesc tunnel interface description
169 * @return ovsdb interface builder
170 */
171 public static OvsdbInterface.Builder builder(TunnelDescription tunnelDesc) {
172 return new Builder(tunnelDesc);
173 }
174
175 /**
Hyunsun Moon89478662016-06-09 17:52:34 -0700176 * Returns new OVSDB interface builder with patch interface description.
177 *
178 * @param patchDesc patch interface description
179 * @return ovsdb interface builder
180 */
181 public static OvsdbInterface.Builder builder(PatchDescription patchDesc) {
182 return new Builder(patchDesc);
183 }
184
185 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700186 * Builder of OVSDB interface entities.
187 */
188 public static final class Builder {
189 private String name;
190 private Type type;
jaegonkim256f2c12017-05-26 00:03:42 +0900191 private Optional<Long> mtu = Optional.empty();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700192 private Map<String, String> options = Maps.newHashMap();
193
194 private Builder() {
195 }
196
197 /**
198 * Constructs a builder with a given tunnel interface description.
199 *
200 * @param tunnelDesc tunnel interface description
201 */
202 private Builder(TunnelDescription tunnelDesc) {
203 this.name = tunnelDesc.ifaceName();
204 this.type = Type.valueOf(tunnelDesc.type().name());
205
Hyunsun Moon89478662016-06-09 17:52:34 -0700206 Map<String, String> tunOptions = Maps.newHashMap();
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700207 if (tunnelDesc.local().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700208 tunOptions.put(TUNNEL_LOCAL_IP, tunnelDesc.local().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700209 }
210 if (tunnelDesc.remote().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700211 tunOptions.put(TUNNEL_REMOTE_IP, tunnelDesc.remote().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700212 }
213 if (tunnelDesc.key().isPresent()) {
Hyunsun Moon89478662016-06-09 17:52:34 -0700214 tunOptions.put(TUNNEL_KEY, tunnelDesc.key().get().strValue());
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700215 }
216
217 // set other configurations if there are any
Hyunsun Moon89478662016-06-09 17:52:34 -0700218 tunOptions.putAll(((DefaultAnnotations) tunnelDesc.annotations()).asMap());
219 options = tunOptions;
220 }
221
222 /**
223 * Constructs a builder with a given patch interface description.
224 *
225 * @param patchDesc patch interface description
226 */
227 private Builder(PatchDescription patchDesc) {
228 this.name = patchDesc.ifaceName();
229 this.type = Type.PATCH;
230
231 Map<String, String> patchOptions = Maps.newHashMap();
232 patchOptions.put(PATCH_PEER, patchDesc.peer());
233 options = patchOptions;
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700234 }
235
236 /**
237 * Returns new OVSDB interface.
238 *
239 * @return ovsdb interface
240 */
241 public OvsdbInterface build() {
jaegonkim256f2c12017-05-26 00:03:42 +0900242 return new OvsdbInterface(name, type, mtu, options);
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700243 }
244
245 /**
246 * Returns OVSDB interface builder with a given name.
247 *
248 * @param name name of the interface
249 * @return ovsdb interface builder
250 */
251 public Builder name(String name) {
252 this.name = name;
253 return this;
254 }
255
256 /**
257 * Returns OVSDB interface builder with a given interface type.
258 *
259 * @param type type of the interface
260 * @return ovsdb interface builder
261 */
262 public Builder type(Type type) {
263 this.type = type;
264 return this;
265 }
266
267 /**
jaegonkim256f2c12017-05-26 00:03:42 +0900268 * Returns OVSDB interface builder with a given interface mtu.
269 *
270 * @param mtu mtu of the interface
271 * @return ovsdb interface builder
272 */
273 public Builder mtu(Long mtu) {
274 this.mtu = Optional.ofNullable(mtu);
275 return this;
276 }
277
278 /**
Hyunsun Moondd14e8e2016-06-09 16:17:32 -0700279 * Returns OVSDB interface builder with given options.
280 *
281 * @param options map of options
282 * @return ovsdb interface builder
283 */
284 public Builder options(Map<String, String> options) {
285 this.options = Maps.newHashMap(options);
286 return this;
287 }
288 }
289}