Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 19 | import static org.onosproject.ovsdb.controller.OvsdbConstant.*; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import com.google.common.collect.Maps; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.PatchDescription; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.TunnelDescription; |
| 28 | |
| 29 | /** |
| 30 | * The class representing an OVSDB interface. |
| 31 | * This class is immutable. |
| 32 | */ |
| 33 | public final class OvsdbInterface { |
| 34 | |
| 35 | public enum Type { |
| 36 | /** |
| 37 | * An ordinary network device, e.g. eth0 on Linux. |
| 38 | */ |
| 39 | SYSTEM, |
| 40 | /** |
| 41 | * A simulated network device that sends and receives traffic. |
| 42 | */ |
| 43 | INTERNAL, |
| 44 | /** |
| 45 | * A TUN/TAP device managed by Open vSwitch. |
| 46 | */ |
| 47 | TAP, |
| 48 | /** |
| 49 | * An Ethernet over RFC 2890 Generic Routing Encapsulation over IPv4 IPsec tunnel. |
| 50 | */ |
| 51 | GRE, |
| 52 | /** |
| 53 | * An Ethernet tunnel over the experimental, UDP-based VXLAN protocol. |
| 54 | */ |
| 55 | VXLAN, |
| 56 | /** |
| 57 | * A pair of virtual devices that act as a patch cable. |
| 58 | */ |
| 59 | PATCH |
| 60 | } |
| 61 | |
| 62 | private final String name; |
| 63 | private final Type type; |
| 64 | |
| 65 | /* Adds more configs */ |
| 66 | |
| 67 | /* Fields start with "options:" prefix defined in the OVSDB */ |
| 68 | private final Map<String, String> options; |
| 69 | |
| 70 | private OvsdbInterface(String name, Type type, Map<String, String> options) { |
| 71 | this.name = name; |
| 72 | this.type = type; |
| 73 | this.options = Maps.newHashMap(options); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns name of the interface. |
| 78 | * |
| 79 | * @return interface name |
| 80 | */ |
| 81 | public String name() { |
| 82 | return name; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns type of the interface. |
| 87 | * |
| 88 | * @return interface type |
| 89 | */ |
| 90 | public Type type() { |
| 91 | return type; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns type of the interface with lowercase string. |
| 96 | * |
| 97 | * @return interface type string |
| 98 | */ |
| 99 | public String typeToString() { |
| 100 | return type.name().toLowerCase(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns optional configs of the interface. |
| 105 | * |
| 106 | * @return interface options |
| 107 | */ |
| 108 | public Map<String, String> options() { |
| 109 | return options; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int hashCode() { |
| 114 | return Objects.hash(name); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean equals(Object obj) { |
| 119 | if (this == obj) { |
| 120 | return true; |
| 121 | } |
| 122 | if (obj instanceof OvsdbInterface) { |
| 123 | final OvsdbInterface otherOvsdbInterface = (OvsdbInterface) obj; |
| 124 | return Objects.equals(this.name, otherOvsdbInterface.name); |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | return toStringHelper(this) |
| 132 | .add("name", name) |
| 133 | .add("type", type) |
| 134 | .add("options", options) |
| 135 | .toString(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns new OVSDB interface builder. |
| 140 | * |
| 141 | * @return ovsdb interface builder |
| 142 | */ |
| 143 | public static OvsdbInterface.Builder builder() { |
| 144 | return new Builder(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns new OVSDB interface builder with tunnel interface description. |
| 149 | * |
| 150 | * @param tunnelDesc tunnel interface description |
| 151 | * @return ovsdb interface builder |
| 152 | */ |
| 153 | public static OvsdbInterface.Builder builder(TunnelDescription tunnelDesc) { |
| 154 | return new Builder(tunnelDesc); |
| 155 | } |
| 156 | |
| 157 | /** |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 158 | * Returns new OVSDB interface builder with patch interface description. |
| 159 | * |
| 160 | * @param patchDesc patch interface description |
| 161 | * @return ovsdb interface builder |
| 162 | */ |
| 163 | public static OvsdbInterface.Builder builder(PatchDescription patchDesc) { |
| 164 | return new Builder(patchDesc); |
| 165 | } |
| 166 | |
| 167 | /** |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 168 | * Builder of OVSDB interface entities. |
| 169 | */ |
| 170 | public static final class Builder { |
| 171 | private String name; |
| 172 | private Type type; |
| 173 | private Map<String, String> options = Maps.newHashMap(); |
| 174 | |
| 175 | private Builder() { |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Constructs a builder with a given tunnel interface description. |
| 180 | * |
| 181 | * @param tunnelDesc tunnel interface description |
| 182 | */ |
| 183 | private Builder(TunnelDescription tunnelDesc) { |
| 184 | this.name = tunnelDesc.ifaceName(); |
| 185 | this.type = Type.valueOf(tunnelDesc.type().name()); |
| 186 | |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 187 | Map<String, String> tunOptions = Maps.newHashMap(); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 188 | if (tunnelDesc.local().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 189 | tunOptions.put(TUNNEL_LOCAL_IP, tunnelDesc.local().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 190 | } |
| 191 | if (tunnelDesc.remote().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 192 | tunOptions.put(TUNNEL_REMOTE_IP, tunnelDesc.remote().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 193 | } |
| 194 | if (tunnelDesc.key().isPresent()) { |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 195 | tunOptions.put(TUNNEL_KEY, tunnelDesc.key().get().strValue()); |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // set other configurations if there are any |
Hyunsun Moon | 8947866 | 2016-06-09 17:52:34 -0700 | [diff] [blame] | 199 | tunOptions.putAll(((DefaultAnnotations) tunnelDesc.annotations()).asMap()); |
| 200 | options = tunOptions; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Constructs a builder with a given patch interface description. |
| 205 | * |
| 206 | * @param patchDesc patch interface description |
| 207 | */ |
| 208 | private Builder(PatchDescription patchDesc) { |
| 209 | this.name = patchDesc.ifaceName(); |
| 210 | this.type = Type.PATCH; |
| 211 | |
| 212 | Map<String, String> patchOptions = Maps.newHashMap(); |
| 213 | patchOptions.put(PATCH_PEER, patchDesc.peer()); |
| 214 | options = patchOptions; |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns new OVSDB interface. |
| 219 | * |
| 220 | * @return ovsdb interface |
| 221 | */ |
| 222 | public OvsdbInterface build() { |
| 223 | return new OvsdbInterface(name, type, options); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns OVSDB interface builder with a given name. |
| 228 | * |
| 229 | * @param name name of the interface |
| 230 | * @return ovsdb interface builder |
| 231 | */ |
| 232 | public Builder name(String name) { |
| 233 | this.name = name; |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns OVSDB interface builder with a given interface type. |
| 239 | * |
| 240 | * @param type type of the interface |
| 241 | * @return ovsdb interface builder |
| 242 | */ |
| 243 | public Builder type(Type type) { |
| 244 | this.type = type; |
| 245 | return this; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns OVSDB interface builder with given options. |
| 250 | * |
| 251 | * @param options map of options |
| 252 | * @return ovsdb interface builder |
| 253 | */ |
| 254 | public Builder options(Map<String, String> options) { |
| 255 | this.options = Maps.newHashMap(options); |
| 256 | return this; |
| 257 | } |
| 258 | } |
| 259 | } |