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; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT; |
| 27 | |
| 28 | /** |
| 29 | * Default implementation class of kubevirt network. |
| 30 | */ |
| 31 | public final class DefaultKubevirtNetwork implements KubevirtNetwork { |
| 32 | |
| 33 | private static final String NOT_NULL_MSG = "Network % cannot be null"; |
| 34 | |
| 35 | private final String networkId; |
| 36 | private final Type type; |
| 37 | private final String name; |
| 38 | private final Integer mtu; |
| 39 | private final String segmentId; |
| 40 | private final IpAddress gatewayIp; |
| 41 | private final String cidr; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 42 | private final Set<KubevirtHostRoute> hostRoutes; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 43 | private final KubevirtIpPool ipPool; |
| 44 | |
| 45 | /** |
| 46 | * Default constructor. |
| 47 | * |
| 48 | * @param networkId network identifier |
| 49 | * @param type type of network |
| 50 | * @param name network name |
| 51 | * @param mtu network MTU |
| 52 | * @param segmentId segment identifier |
| 53 | * @param gatewayIp gateway IP address |
| 54 | * @param cidr CIDR of network |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 55 | * @param hostRoutes a set of host routes |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 56 | * @param ipPool IP pool |
| 57 | */ |
| 58 | public DefaultKubevirtNetwork(String networkId, Type type, String name, |
| 59 | Integer mtu, String segmentId, IpAddress gatewayIp, |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 60 | String cidr, Set<KubevirtHostRoute> hostRoutes, |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 61 | KubevirtIpPool ipPool) { |
| 62 | this.networkId = networkId; |
| 63 | this.type = type; |
| 64 | this.name = name; |
| 65 | this.mtu = mtu; |
| 66 | this.segmentId = segmentId; |
| 67 | this.gatewayIp = gatewayIp; |
| 68 | this.cidr = cidr; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 69 | this.hostRoutes = hostRoutes; |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 70 | this.ipPool = ipPool; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String networkId() { |
| 75 | return networkId; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Type type() { |
| 80 | return type; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String name() { |
| 85 | return name; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Integer mtu() { |
| 90 | return mtu; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public String segmentId() { |
| 95 | return segmentId; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public IpAddress gatewayIp() { |
| 100 | return gatewayIp; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String cidr() { |
| 105 | return cidr; |
| 106 | } |
| 107 | |
| 108 | @Override |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 109 | public Set<KubevirtHostRoute> hostRoutes() { |
Jian Li | 034820d | 2021-01-15 16:58:48 +0900 | [diff] [blame] | 110 | if (hostRoutes == null || hostRoutes.size() == 0) { |
| 111 | return ImmutableSet.of(); |
| 112 | } else { |
| 113 | return ImmutableSet.copyOf(hostRoutes); |
| 114 | } |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public KubevirtIpPool ipPool() { |
| 119 | return ipPool; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean equals(Object o) { |
| 124 | if (this == o) { |
| 125 | return true; |
| 126 | } |
| 127 | if (o == null || getClass() != o.getClass()) { |
| 128 | return false; |
| 129 | } |
| 130 | DefaultKubevirtNetwork that = (DefaultKubevirtNetwork) o; |
| 131 | return networkId.equals(that.networkId) && type == that.type && |
| 132 | name.equals(that.name) && mtu.equals(that.mtu) && |
| 133 | gatewayIp.equals(that.gatewayIp) && |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 134 | cidr.equals(that.cidr) && hostRoutes.equals(that.hostRoutes) && |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 135 | ipPool.equals(that.ipPool); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int hashCode() { |
| 140 | return Objects.hash(networkId, type, name, mtu, segmentId, gatewayIp, |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 141 | cidr, hostRoutes, ipPool); |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public String toString() { |
| 146 | return MoreObjects.toStringHelper(this) |
| 147 | .add("networkId", networkId) |
| 148 | .add("type", type) |
| 149 | .add("name", name) |
| 150 | .add("mtu", mtu) |
| 151 | .add("segmentId", segmentId) |
| 152 | .add("gatewayIp", gatewayIp) |
| 153 | .add("cidr", cidr) |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 154 | .add("hostRouts", hostRoutes) |
Jian Li | 8fe714d | 2021-01-11 02:35:06 +0900 | [diff] [blame] | 155 | .add("ipPool", ipPool) |
| 156 | .toString(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns new builder instance. |
| 161 | * |
| 162 | * @return kubevirt port builder |
| 163 | */ |
| 164 | public static Builder builder() { |
| 165 | return new Builder(); |
| 166 | } |
| 167 | |
| 168 | public static final class Builder implements KubevirtNetwork.Builder { |
| 169 | |
| 170 | private String networkId; |
| 171 | private Type type; |
| 172 | private String name; |
| 173 | private Integer mtu; |
| 174 | private String segmentId; |
| 175 | private IpAddress gatewayIp; |
| 176 | private String cidr; |
| 177 | private Set<KubevirtHostRoute> hostRouts; |
| 178 | private KubevirtIpPool ipPool; |
| 179 | |
| 180 | @Override |
| 181 | public KubevirtNetwork build() { |
| 182 | checkArgument(networkId != null, NOT_NULL_MSG, "networkId"); |
| 183 | checkArgument(type != null, NOT_NULL_MSG, "type"); |
| 184 | checkArgument(name != null, NOT_NULL_MSG, "name"); |
| 185 | checkArgument(mtu != null, NOT_NULL_MSG, "mtu"); |
| 186 | checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp"); |
| 187 | checkArgument(cidr != null, NOT_NULL_MSG, "cidr"); |
| 188 | checkArgument(ipPool != null, NOT_NULL_MSG, "ipPool"); |
| 189 | |
| 190 | if (type != FLAT) { |
| 191 | checkArgument(segmentId != null, NOT_NULL_MSG, "segmentId"); |
| 192 | } |
| 193 | |
| 194 | return new DefaultKubevirtNetwork(networkId, type, name, mtu, segmentId, |
| 195 | gatewayIp, cidr, hostRouts, ipPool); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Builder networkId(String networkId) { |
| 200 | this.networkId = networkId; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public Builder name(String name) { |
| 206 | this.name = name; |
| 207 | return this; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Builder type(Type type) { |
| 212 | this.type = type; |
| 213 | return this; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Builder mtu(Integer mtu) { |
| 218 | this.mtu = mtu; |
| 219 | return this; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public Builder segmentId(String segmentId) { |
| 224 | this.segmentId = segmentId; |
| 225 | return this; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Builder gatewayIp(IpAddress ipAddress) { |
| 230 | this.gatewayIp = ipAddress; |
| 231 | return this; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public Builder cidr(String cidr) { |
| 236 | this.cidr = cidr; |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public Builder ipPool(KubevirtIpPool ipPool) { |
| 242 | this.ipPool = ipPool; |
| 243 | return this; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public Builder hostRoutes(Set<KubevirtHostRoute> hostRoutes) { |
| 248 | this.hostRouts = hostRoutes; |
| 249 | return this; |
| 250 | } |
| 251 | } |
| 252 | } |