Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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.kubevirtnetworking.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame^] | 19 | import com.google.common.base.Strings; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onlab.packet.IpAddress; |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame^] | 22 | import org.onosproject.net.DeviceId; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 23 | |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 24 | import java.util.HashSet; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 25 | import java.util.Objects; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT; |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame^] | 30 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.GENEVE; |
| 31 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.GRE; |
| 32 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.VXLAN; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Default implementation class of kubevirt network. |
| 36 | */ |
| 37 | public final class DefaultKubevirtNetwork implements KubevirtNetwork { |
| 38 | |
| 39 | private static final String NOT_NULL_MSG = "Network % cannot be null"; |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame^] | 40 | private static final String TENANT_BRIDGE_PREFIX = "br-int-"; |
| 41 | private static final String OF_PREFIX = "of:"; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 42 | |
| 43 | private final String networkId; |
| 44 | private final Type type; |
| 45 | private final String name; |
| 46 | private final Integer mtu; |
| 47 | private final String segmentId; |
| 48 | private final IpAddress gatewayIp; |
| 49 | private final String cidr; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 50 | private final Set<KubevirtHostRoute> hostRoutes; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 51 | private final KubevirtIpPool ipPool; |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 52 | private final Set<IpAddress> dnses; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Default constructor. |
| 56 | * |
| 57 | * @param networkId network identifier |
| 58 | * @param type type of network |
| 59 | * @param name network name |
| 60 | * @param mtu network MTU |
| 61 | * @param segmentId segment identifier |
| 62 | * @param gatewayIp gateway IP address |
| 63 | * @param cidr CIDR of network |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 64 | * @param hostRoutes a set of host routes |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 65 | * @param ipPool IP pool |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 66 | * @param dnses a set of DNSes |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 67 | */ |
| 68 | public DefaultKubevirtNetwork(String networkId, Type type, String name, |
| 69 | Integer mtu, String segmentId, IpAddress gatewayIp, |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 70 | String cidr, Set<KubevirtHostRoute> hostRoutes, |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 71 | KubevirtIpPool ipPool, Set<IpAddress> dnses) { |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 72 | this.networkId = networkId; |
| 73 | this.type = type; |
| 74 | this.name = name; |
| 75 | this.mtu = mtu; |
| 76 | this.segmentId = segmentId; |
| 77 | this.gatewayIp = gatewayIp; |
| 78 | this.cidr = cidr; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 79 | this.hostRoutes = hostRoutes; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 80 | this.ipPool = ipPool; |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 81 | this.dnses = dnses; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String networkId() { |
| 86 | return networkId; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Type type() { |
| 91 | return type; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String name() { |
| 96 | return name; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Integer mtu() { |
| 101 | return mtu; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String segmentId() { |
| 106 | return segmentId; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public IpAddress gatewayIp() { |
| 111 | return gatewayIp; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String cidr() { |
| 116 | return cidr; |
| 117 | } |
| 118 | |
| 119 | @Override |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 120 | public Set<KubevirtHostRoute> hostRoutes() { |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 121 | if (hostRoutes == null || hostRoutes.size() == 0) { |
| 122 | return ImmutableSet.of(); |
| 123 | } else { |
| 124 | return ImmutableSet.copyOf(hostRoutes); |
| 125 | } |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public KubevirtIpPool ipPool() { |
| 130 | return ipPool; |
| 131 | } |
| 132 | |
| 133 | @Override |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 134 | public Set<IpAddress> dnses() { |
| 135 | if (dnses == null || dnses.size() == 0) { |
| 136 | return ImmutableSet.of(); |
| 137 | } else { |
| 138 | return ImmutableSet.copyOf(dnses); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame^] | 143 | public String tenantBridgeName() { |
| 144 | if (type == VXLAN || type == GRE || type == GENEVE) { |
| 145 | return TENANT_BRIDGE_PREFIX + segmentIdHex(segmentId); |
| 146 | } |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public DeviceId tenantDeviceId(String hostname) { |
| 152 | if (type == VXLAN || type == GRE || type == GENEVE) { |
| 153 | String dpid = genDpidFromName(tenantBridgeName() + "-" + hostname); |
| 154 | if (dpid != null) { |
| 155 | return DeviceId.deviceId(dpid); |
| 156 | } |
| 157 | } |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | @Override |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 162 | public boolean equals(Object o) { |
| 163 | if (this == o) { |
| 164 | return true; |
| 165 | } |
| 166 | if (o == null || getClass() != o.getClass()) { |
| 167 | return false; |
| 168 | } |
| 169 | DefaultKubevirtNetwork that = (DefaultKubevirtNetwork) o; |
| 170 | return networkId.equals(that.networkId) && type == that.type && |
| 171 | name.equals(that.name) && mtu.equals(that.mtu) && |
| 172 | gatewayIp.equals(that.gatewayIp) && |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 173 | cidr.equals(that.cidr) && hostRoutes.equals(that.hostRoutes) && |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 174 | ipPool.equals(that.ipPool) && |
| 175 | dnses.equals(that.dnses); |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public int hashCode() { |
| 180 | return Objects.hash(networkId, type, name, mtu, segmentId, gatewayIp, |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 181 | cidr, hostRoutes, ipPool, dnses); |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public String toString() { |
| 186 | return MoreObjects.toStringHelper(this) |
| 187 | .add("networkId", networkId) |
| 188 | .add("type", type) |
| 189 | .add("name", name) |
| 190 | .add("mtu", mtu) |
| 191 | .add("segmentId", segmentId) |
| 192 | .add("gatewayIp", gatewayIp) |
| 193 | .add("cidr", cidr) |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 194 | .add("hostRouts", hostRoutes) |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 195 | .add("ipPool", ipPool) |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 196 | .add("dnses", dnses) |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 197 | .toString(); |
| 198 | } |
| 199 | |
Jian Li | 556709c | 2021-02-03 17:54:28 +0900 | [diff] [blame^] | 200 | private String segmentIdHex(String segIdStr) { |
| 201 | int segId = Integer.parseInt(segIdStr); |
| 202 | return String.format("%06x", segId).toLowerCase(); |
| 203 | } |
| 204 | |
| 205 | private String genDpidFromName(String name) { |
| 206 | if (name != null) { |
| 207 | String hexString = Integer.toHexString(name.hashCode()); |
| 208 | return OF_PREFIX + Strings.padStart(hexString, 16, '0'); |
| 209 | } |
| 210 | |
| 211 | return null; |
| 212 | } |
| 213 | |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 214 | /** |
| 215 | * Returns new builder instance. |
| 216 | * |
| 217 | * @return kubevirt port builder |
| 218 | */ |
| 219 | public static Builder builder() { |
| 220 | return new Builder(); |
| 221 | } |
| 222 | |
| 223 | public static final class Builder implements KubevirtNetwork.Builder { |
| 224 | |
| 225 | private String networkId; |
| 226 | private Type type; |
| 227 | private String name; |
| 228 | private Integer mtu; |
| 229 | private String segmentId; |
| 230 | private IpAddress gatewayIp; |
| 231 | private String cidr; |
| 232 | private Set<KubevirtHostRoute> hostRouts; |
| 233 | private KubevirtIpPool ipPool; |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 234 | private Set<IpAddress> dnses; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 235 | |
| 236 | @Override |
| 237 | public KubevirtNetwork build() { |
| 238 | checkArgument(networkId != null, NOT_NULL_MSG, "networkId"); |
| 239 | checkArgument(type != null, NOT_NULL_MSG, "type"); |
| 240 | checkArgument(name != null, NOT_NULL_MSG, "name"); |
| 241 | checkArgument(mtu != null, NOT_NULL_MSG, "mtu"); |
| 242 | checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp"); |
| 243 | checkArgument(cidr != null, NOT_NULL_MSG, "cidr"); |
| 244 | checkArgument(ipPool != null, NOT_NULL_MSG, "ipPool"); |
| 245 | |
| 246 | if (type != FLAT) { |
| 247 | checkArgument(segmentId != null, NOT_NULL_MSG, "segmentId"); |
| 248 | } |
| 249 | |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 250 | if (dnses == null) { |
| 251 | dnses = new HashSet<>(); |
| 252 | } |
| 253 | |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 254 | return new DefaultKubevirtNetwork(networkId, type, name, mtu, segmentId, |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 255 | gatewayIp, cidr, hostRouts, ipPool, dnses); |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public Builder networkId(String networkId) { |
| 260 | this.networkId = networkId; |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public Builder name(String name) { |
| 266 | this.name = name; |
| 267 | return this; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public Builder type(Type type) { |
| 272 | this.type = type; |
| 273 | return this; |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public Builder mtu(Integer mtu) { |
| 278 | this.mtu = mtu; |
| 279 | return this; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public Builder segmentId(String segmentId) { |
| 284 | this.segmentId = segmentId; |
| 285 | return this; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Builder gatewayIp(IpAddress ipAddress) { |
| 290 | this.gatewayIp = ipAddress; |
| 291 | return this; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public Builder cidr(String cidr) { |
| 296 | this.cidr = cidr; |
| 297 | return this; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public Builder ipPool(KubevirtIpPool ipPool) { |
| 302 | this.ipPool = ipPool; |
| 303 | return this; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public Builder hostRoutes(Set<KubevirtHostRoute> hostRoutes) { |
| 308 | this.hostRouts = hostRoutes; |
| 309 | return this; |
| 310 | } |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 311 | |
| 312 | @Override |
| 313 | public KubevirtNetwork.Builder dnses(Set<IpAddress> dnses) { |
| 314 | this.dnses = dnses; |
| 315 | return this; |
| 316 | } |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 317 | } |
| 318 | } |