Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ovsdb.controller; |
| 17 | |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.Maps; |
| 19 | import org.onosproject.net.DefaultAnnotations; |
| 20 | import org.onosproject.net.behaviour.PatchDescription; |
| 21 | import org.onosproject.net.behaviour.TunnelDescription; |
| 22 | import org.onosproject.ovsdb.rfc.table.Interface.InterfaceColumn; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 26 | import java.util.Optional; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 27 | |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static org.onosproject.ovsdb.controller.OvsdbConstant.PATCH_PEER; |
| 30 | import static org.onosproject.ovsdb.controller.OvsdbConstant.TUNNEL_KEY; |
| 31 | import static org.onosproject.ovsdb.controller.OvsdbConstant.TUNNEL_LOCAL_IP; |
| 32 | import static org.onosproject.ovsdb.controller.OvsdbConstant.TUNNEL_REMOTE_IP; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * The class representing an OVSDB interface. |
| 36 | * This class is immutable. |
| 37 | */ |
| 38 | public 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 Li | 6e66680 | 2018-12-15 03:06:04 +0900 | [diff] [blame] | 58 | * An Ethernet over draft-ietf-nvo3-geneve-08 Generic Network Virtualization Encapsulation tunnel. |
| 59 | */ |
| 60 | GENEVE, |
| 61 | /** |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 62 | * 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 | */ |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 68 | PATCH, |
| 69 | /** |
| 70 | * A DPDK net device. |
| 71 | */ |
| 72 | DPDK |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private final String name; |
| 76 | private final Type type; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 77 | private final Optional<Long> mtu; |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 78 | private final Map<InterfaceColumn, Map<String, String>> data; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 79 | |
| 80 | /* Adds more configs */ |
| 81 | |
| 82 | /* Fields start with "options:" prefix defined in the OVSDB */ |
| 83 | private final Map<String, String> options; |
| 84 | |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 85 | private OvsdbInterface(String name, Type type, Optional<Long> mtu, |
| 86 | Map<String, String> options, Map<InterfaceColumn, |
| 87 | Map<String, String>> data) { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 88 | this.name = name; |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 89 | this.type = type; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 90 | this.mtu = mtu; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 91 | this.options = Maps.newHashMap(options); |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 92 | this.data = data; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 93 | } |
| 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 | /** |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 123 | * Returns mtu of the interface. |
| 124 | * |
| 125 | * @return interface mtu |
| 126 | */ |
| 127 | public Optional<Long> mtu() { |
| 128 | return mtu; |
| 129 | } |
| 130 | |
| 131 | /** |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 132 | * Returns optional configs of the interface. |
| 133 | * |
| 134 | * @return interface options |
| 135 | */ |
| 136 | public Map<String, String> options() { |
| 137 | return options; |
| 138 | } |
| 139 | |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 140 | /** |
| 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 149 | @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) |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 171 | .add("mtu", mtu) |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 172 | .add("options", options) |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 173 | .add("data", data) |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 174 | .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 Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 197 | * 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 207 | * Builder of OVSDB interface entities. |
| 208 | */ |
| 209 | public static final class Builder { |
| 210 | private String name; |
| 211 | private Type type; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 212 | private Optional<Long> mtu = Optional.empty(); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 213 | private Map<String, String> options = Maps.newHashMap(); |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 214 | private Map<InterfaceColumn, Map<String, String>> data = Maps.newHashMap(); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 215 | |
| 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 Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 228 | Map<String, String> tunOptions = Maps.newHashMap(); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 229 | if (tunnelDesc.local().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 230 | tunOptions.put(TUNNEL_LOCAL_IP, tunnelDesc.local().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 231 | } |
| 232 | if (tunnelDesc.remote().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 233 | tunOptions.put(TUNNEL_REMOTE_IP, tunnelDesc.remote().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 234 | } |
| 235 | if (tunnelDesc.key().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 236 | tunOptions.put(TUNNEL_KEY, tunnelDesc.key().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // set other configurations if there are any |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 240 | 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns new OVSDB interface. |
| 260 | * |
| 261 | * @return ovsdb interface |
| 262 | */ |
| 263 | public OvsdbInterface build() { |
Jian Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 264 | return new OvsdbInterface(name, type, mtu, options, data); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 265 | } |
| 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 | /** |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 290 | * 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 301 | * 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 Li | 83a5e8d | 2019-05-17 19:50:28 +0900 | [diff] [blame^] | 310 | |
| 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 321 | } |
| 322 | } |