Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
Jian Li | 2778ffa | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 20 | import org.apache.commons.net.util.SubnetUtils; |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | |
| 25 | /** |
| 26 | * Default implementation of kubernetes network. |
| 27 | */ |
| 28 | public final class DefaultK8sNetwork implements K8sNetwork { |
| 29 | |
| 30 | private static final int DEFAULT_MTU = 1500; |
Jian Li | 2778ffa | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 31 | private static final Type DEFAULT_TYPE = Type.VXLAN; |
| 32 | private static final String DEFAULT_SEGMENT_ID = String.valueOf(100); |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 33 | |
| 34 | private final String networkId; |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 35 | private final String name; |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 36 | private final Type type; |
| 37 | private final Integer mtu; |
| 38 | private final String segmentId; |
| 39 | private final IpAddress gatewayIp; |
| 40 | private final String cidr; |
| 41 | |
| 42 | private static final String NOT_NULL_MSG = "Network % cannot be null"; |
| 43 | |
| 44 | // private constructor not intended for external invocation |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 45 | private DefaultK8sNetwork(String networkId, String name, Type type, Integer mtu, |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 46 | String segmentId, IpAddress gatewayIp, String cidr) { |
| 47 | this.networkId = networkId; |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 48 | this.name = name; |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 49 | this.type = type; |
| 50 | this.mtu = mtu; |
| 51 | this.segmentId = segmentId; |
| 52 | this.gatewayIp = gatewayIp; |
| 53 | this.cidr = cidr; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public String networkId() { |
| 58 | return networkId; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Type type() { |
| 63 | return type; |
| 64 | } |
| 65 | |
| 66 | @Override |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 67 | public String name() { |
| 68 | return name; |
| 69 | } |
| 70 | |
| 71 | @Override |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 72 | public Integer mtu() { |
| 73 | return mtu; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String segmentId() { |
| 78 | return segmentId; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public IpAddress gatewayIp() { |
| 83 | return gatewayIp; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String cidr() { |
| 88 | return cidr; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean equals(Object o) { |
| 93 | if (this == o) { |
| 94 | return true; |
| 95 | } |
| 96 | if (o == null || getClass() != o.getClass()) { |
| 97 | return false; |
| 98 | } |
| 99 | DefaultK8sNetwork that = (DefaultK8sNetwork) o; |
| 100 | return Objects.equal(networkId, that.networkId) && |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 101 | Objects.equal(name, that.name) && |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 102 | type == that.type && |
| 103 | Objects.equal(mtu, that.mtu) && |
| 104 | Objects.equal(segmentId, that.segmentId) && |
| 105 | Objects.equal(gatewayIp, that.gatewayIp) && |
| 106 | Objects.equal(cidr, that.cidr); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public int hashCode() { |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 111 | return Objects.hashCode(networkId, name, type, mtu, segmentId, gatewayIp, cidr); |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String toString() { |
| 116 | return MoreObjects.toStringHelper(this) |
| 117 | .add("networkId", networkId) |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 118 | .add("name", name) |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 119 | .add("type", type) |
| 120 | .add("mtu", mtu) |
| 121 | .add("segmentId", segmentId) |
| 122 | .add("gatewayIp", gatewayIp) |
| 123 | .add("cidr", cidr) |
| 124 | .toString(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns new builder instance. |
| 129 | * |
| 130 | * @return kubernetes network builder |
| 131 | */ |
| 132 | public static Builder builder() { |
| 133 | return new Builder(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Default implementation of kubernetes network builder. |
| 138 | */ |
| 139 | public static final class Builder implements K8sNetwork.Builder { |
| 140 | |
| 141 | private String networkId; |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 142 | private String name; |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 143 | private Type type; |
| 144 | private Integer mtu; |
| 145 | private String segmentId; |
| 146 | private IpAddress gatewayIp; |
| 147 | private String cidr; |
| 148 | |
| 149 | @Override |
| 150 | public K8sNetwork build() { |
| 151 | checkArgument(networkId != null, NOT_NULL_MSG, "networkId"); |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 152 | checkArgument(name != null, NOT_NULL_MSG, "name"); |
Jian Li | 2778ffa | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 153 | |
| 154 | // TODO: CIDR can be retrieve from k8s node info, therefore, such |
| 155 | // value injection should be purged sooner |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 156 | checkArgument(cidr != null, NOT_NULL_MSG, "cidr"); |
| 157 | |
Jian Li | 2778ffa | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 158 | // gateway IP address is derived from subnet CIDR |
| 159 | gatewayIp = getGatewayIp(cidr); |
| 160 | |
| 161 | if (segmentId == null) { |
| 162 | segmentId = DEFAULT_SEGMENT_ID; |
| 163 | } |
| 164 | |
| 165 | // VXLAN as the default tunneling protocol if not specified |
| 166 | if (type == null) { |
| 167 | type = DEFAULT_TYPE; |
| 168 | } |
| 169 | |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 170 | if (mtu == null) { |
| 171 | mtu = DEFAULT_MTU; |
| 172 | } |
| 173 | |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 174 | return new DefaultK8sNetwork(networkId, name, type, mtu, segmentId, gatewayIp, cidr); |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Builder networkId(String networkId) { |
| 179 | this.networkId = networkId; |
| 180 | return this; |
| 181 | } |
| 182 | |
| 183 | @Override |
Jian Li | 7e8f57e | 2019-01-24 18:31:03 +0900 | [diff] [blame] | 184 | public Builder name(String name) { |
| 185 | this.name = name; |
| 186 | return this; |
| 187 | } |
| 188 | |
| 189 | @Override |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 190 | public Builder type(Type type) { |
| 191 | this.type = type; |
| 192 | return this; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Builder mtu(Integer mtu) { |
| 197 | this.mtu = mtu; |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Builder segmentId(String segmentId) { |
| 203 | this.segmentId = segmentId; |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Builder gatewayIp(IpAddress ipAddress) { |
| 209 | this.gatewayIp = ipAddress; |
| 210 | return this; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public Builder cidr(String cidr) { |
| 215 | this.cidr = cidr; |
| 216 | return this; |
| 217 | } |
Jian Li | 2778ffa | 2019-05-07 13:21:52 +0900 | [diff] [blame] | 218 | |
| 219 | private IpAddress getGatewayIp(String cidr) { |
| 220 | SubnetUtils utils = new SubnetUtils(cidr); |
| 221 | utils.setInclusiveHostCount(false); |
| 222 | SubnetUtils.SubnetInfo info = utils.getInfo(); |
| 223 | |
| 224 | return IpAddress.valueOf(info.getLowAddress()); |
| 225 | } |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 226 | } |
| 227 | } |