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