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 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 20 | import static org.onosproject.ovsdb.controller.OvsdbConstant.*; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 24 | import java.util.Optional; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 25 | |
| 26 | import com.google.common.collect.Maps; |
| 27 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.PatchDescription; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 29 | import org.onosproject.net.behaviour.TunnelDescription; |
| 30 | |
| 31 | /** |
| 32 | * The class representing an OVSDB interface. |
| 33 | * This class is immutable. |
| 34 | */ |
| 35 | public 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 | */ |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 61 | PATCH, |
| 62 | /** |
| 63 | * A DPDK net device. |
| 64 | */ |
| 65 | DPDK |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private final String name; |
| 69 | private final Type type; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 70 | private final Optional<Long> mtu; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 71 | |
| 72 | /* Adds more configs */ |
| 73 | |
| 74 | /* Fields start with "options:" prefix defined in the OVSDB */ |
| 75 | private final Map<String, String> options; |
| 76 | |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 77 | private OvsdbInterface(String name, Type type, Optional<Long> mtu, Map<String, String> options) { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 78 | this.name = name; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 79 | this.type = checkNotNull(type, "the type of interface can not be null"); |
| 80 | this.mtu = mtu; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 81 | 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 | /** |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 112 | * Returns mtu of the interface. |
| 113 | * |
| 114 | * @return interface mtu |
| 115 | */ |
| 116 | public Optional<Long> mtu() { |
| 117 | return mtu; |
| 118 | } |
| 119 | |
| 120 | /** |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 121 | * 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) |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 151 | .add("mtu", mtu) |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 152 | .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 Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 176 | * 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 186 | * Builder of OVSDB interface entities. |
| 187 | */ |
| 188 | public static final class Builder { |
| 189 | private String name; |
| 190 | private Type type; |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 191 | private Optional<Long> mtu = Optional.empty(); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 192 | 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 Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 206 | Map<String, String> tunOptions = Maps.newHashMap(); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 207 | if (tunnelDesc.local().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 208 | tunOptions.put(TUNNEL_LOCAL_IP, tunnelDesc.local().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 209 | } |
| 210 | if (tunnelDesc.remote().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 211 | tunOptions.put(TUNNEL_REMOTE_IP, tunnelDesc.remote().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 212 | } |
| 213 | if (tunnelDesc.key().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 214 | tunOptions.put(TUNNEL_KEY, tunnelDesc.key().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // set other configurations if there are any |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 218 | 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns new OVSDB interface. |
| 238 | * |
| 239 | * @return ovsdb interface |
| 240 | */ |
| 241 | public OvsdbInterface build() { |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 242 | return new OvsdbInterface(name, type, mtu, options); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 243 | } |
| 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 | /** |
jaegonkim | 256f2c1 | 2017-05-26 00:03:42 +0900 | [diff] [blame] | 268 | * 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 Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 279 | * 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 | } |