Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.xosclient.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | 91ba41a | 2016-06-10 16:52:39 -0700 | [diff] [blame] | 23 | import org.onosproject.xosclient.api.VtnServiceApi.NetworkType; |
| 24 | import org.onosproject.xosclient.api.VtnServiceApi.ServiceType; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkArgument; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Representation of CORD VTN controlled network service. |
| 34 | */ |
| 35 | public final class VtnService { |
| 36 | |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 37 | private final VtnServiceId id; |
| 38 | private final String name; |
| 39 | private final ServiceType serviceType; |
| 40 | private final NetworkType networkType; |
| 41 | private final long vni; |
| 42 | private final IpPrefix subnet; |
| 43 | private final IpAddress serviceIp; |
| 44 | private final Set<VtnServiceId> providerServices; |
| 45 | private final Set<VtnServiceId> tenantServices; |
| 46 | |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 47 | private VtnService(VtnServiceId id, |
| 48 | String name, |
| 49 | ServiceType serviceType, |
| 50 | NetworkType networkType, |
| 51 | long vni, |
| 52 | IpPrefix subnet, |
| 53 | IpAddress serviceIp, |
| 54 | Set<VtnServiceId> providerServices, |
| 55 | Set<VtnServiceId> tenantServices) { |
| 56 | this.id = id; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 57 | this.name = name; |
| 58 | this.serviceType = serviceType; |
| 59 | this.networkType = networkType; |
| 60 | this.vni = vni; |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 61 | this.subnet = subnet; |
| 62 | this.serviceIp = serviceIp; |
| 63 | this.providerServices = providerServices; |
| 64 | this.tenantServices = tenantServices; |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns service ID. |
| 69 | * |
| 70 | * @return service id |
| 71 | */ |
| 72 | public VtnServiceId id() { |
| 73 | return id; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns service name. |
| 78 | * |
| 79 | * @return name |
| 80 | */ |
| 81 | public String name() { |
| 82 | return name; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns service type. |
| 87 | * |
| 88 | * @return service type |
| 89 | */ |
| 90 | public ServiceType serviceType() { |
| 91 | return serviceType; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns segmentation ID of this service. |
| 96 | * |
| 97 | * @return segmentation id |
| 98 | */ |
| 99 | public long vni() { |
| 100 | return vni; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns network type. |
| 105 | * |
| 106 | * @return network type |
| 107 | */ |
| 108 | public NetworkType networkType() { |
| 109 | return networkType; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns service IP range. |
| 114 | * |
| 115 | * @return subnet cidr |
| 116 | */ |
| 117 | public IpPrefix subnet() { |
| 118 | return subnet; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns service IP address. |
| 123 | * |
| 124 | * @return ip address |
| 125 | */ |
| 126 | public IpAddress serviceIp() { |
| 127 | return serviceIp; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns provider service IDs. |
| 132 | * |
| 133 | * @return list of provider service id |
| 134 | */ |
| 135 | public Set<VtnServiceId> providerServices() { |
| 136 | return providerServices; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns tenant service IDs. |
| 141 | * |
| 142 | * @return list of tenant service id |
| 143 | */ |
| 144 | public Set<VtnServiceId> tenantServices() { |
| 145 | return tenantServices; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public int hashCode() { |
| 150 | return Objects.hash(id); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean equals(Object obj) { |
| 155 | if (this == obj) { |
| 156 | return true; |
| 157 | } |
| 158 | if (!(obj instanceof VtnService)) { |
| 159 | return false; |
| 160 | } |
| 161 | final VtnService other = (VtnService) obj; |
| 162 | return Objects.equals(this.id, other.id); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public String toString() { |
| 167 | return MoreObjects.toStringHelper(this) |
| 168 | .add("id", id) |
| 169 | .add("name", name) |
| 170 | .add("serviceType", serviceType) |
| 171 | .add("networkType", networkType) |
| 172 | .add("vni", vni) |
| 173 | .add("subnet", subnet) |
| 174 | .add("serviceIp", serviceIp) |
| 175 | .add("providerServices", providerServices) |
| 176 | .add("tenantServices", tenantServices) |
| 177 | .toString(); |
| 178 | } |
Hyunsun Moon | 6504099 | 2016-07-27 18:41:34 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Returns a new builder instance. |
| 182 | * |
| 183 | * @return new builder |
| 184 | */ |
| 185 | public static final Builder build() { |
| 186 | return new Builder(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Builder of VTN service entities. |
| 191 | */ |
| 192 | public static final class Builder { |
| 193 | |
| 194 | private VtnServiceId id; |
| 195 | private String name; |
| 196 | private ServiceType serviceType; |
| 197 | private NetworkType networkType; |
| 198 | private long vni = -1; |
| 199 | private IpPrefix subnet; |
| 200 | private IpAddress serviceIp; |
| 201 | private Set<VtnServiceId> providerServices; |
| 202 | private Set<VtnServiceId> tenantServices; |
| 203 | |
| 204 | private Builder() { |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Builds an immutable VTN service. |
| 209 | * |
| 210 | * @return vtn service instance |
| 211 | */ |
| 212 | public VtnService build() { |
| 213 | checkNotNull(id, "VTN service ID cannot be null"); |
| 214 | checkArgument(!Strings.isNullOrEmpty(name), "VTN service name cannot be null"); |
| 215 | checkNotNull(serviceType, "VTN service type cannot be null"); |
| 216 | checkNotNull(networkType, "VTN network type cannot be null"); |
| 217 | checkArgument(vni > 0, "VTN network VNI is not set"); |
| 218 | checkNotNull(subnet, "VTN subnet cannot be null"); |
| 219 | checkNotNull(serviceIp, "VTN service IP cannot be null"); |
| 220 | |
| 221 | providerServices = providerServices == null ? ImmutableSet.of() : providerServices; |
| 222 | tenantServices = tenantServices == null ? ImmutableSet.of() : tenantServices; |
| 223 | |
| 224 | return new VtnService(id, |
| 225 | name, |
| 226 | serviceType, |
| 227 | networkType, |
| 228 | vni, |
| 229 | subnet, |
| 230 | serviceIp, |
| 231 | providerServices, |
| 232 | tenantServices); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Returns VTN service builder with the supplied service ID. |
| 237 | * |
| 238 | * @param id service identifier |
| 239 | * @return vtn service builder |
| 240 | */ |
| 241 | public Builder id(VtnServiceId id) { |
| 242 | this.id = id; |
| 243 | return this; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Returns VTN service builder with the supplied service name. |
| 248 | * |
| 249 | * @param name service name |
| 250 | * @return vtn service builder |
| 251 | */ |
| 252 | public Builder name(String name) { |
| 253 | if (Strings.isNullOrEmpty(name)) { |
| 254 | final String msg = "VTN service name cannot be null"; |
| 255 | throw new IllegalArgumentException(msg); |
| 256 | } |
| 257 | this.name = name; |
| 258 | return this; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns VTN service builder with the supplied service type. |
| 263 | * |
| 264 | * @param serviceType service type |
| 265 | * @return vtn service builder |
| 266 | */ |
| 267 | public Builder serviceType(ServiceType serviceType) { |
| 268 | this.serviceType = serviceType; |
| 269 | return this; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns VTN service builder with the supplied network type. |
| 274 | * |
| 275 | * @param networkType network type |
| 276 | * @return vtn service builder |
| 277 | */ |
| 278 | public Builder networkType(NetworkType networkType) { |
| 279 | this.networkType = networkType; |
| 280 | return this; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Returns VTN service builder with the supplied VNI. |
| 285 | * |
| 286 | * @param vni vni of the service network |
| 287 | * @return vtn service builder |
| 288 | */ |
| 289 | public Builder vni(long vni) { |
| 290 | if (vni < 0 || vni > 16777215) { |
| 291 | final String msg = "VNI " + vni + " is out of range"; |
| 292 | throw new IllegalArgumentException(msg); |
| 293 | } |
| 294 | this.vni = vni; |
| 295 | return this; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Returns VTN service builder with the supplied VNI. |
| 300 | * |
| 301 | * @param vni vni of the service network as a string |
| 302 | * @return vtn service builder |
| 303 | */ |
| 304 | public Builder vni(String vni) { |
| 305 | try { |
| 306 | return vni(Long.parseLong(vni)); |
| 307 | } catch (NumberFormatException | NullPointerException e) { |
| 308 | final String msg = "Malformed number string " + vni + |
| 309 | " for VTN network VNI"; |
| 310 | throw new IllegalArgumentException(msg); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Returns VTN service builder with the supplied subnet. |
| 316 | * |
| 317 | * @param subnet subnet of the service network |
| 318 | * @return vtn service builder |
| 319 | */ |
| 320 | public Builder subnet(IpPrefix subnet) { |
| 321 | if (subnet == null) { |
| 322 | final String msg = "VTN service subnet is null"; |
| 323 | throw new IllegalArgumentException(msg); |
| 324 | } |
| 325 | this.subnet = subnet; |
| 326 | return this; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Returns VTN service builder with the supplied subnet. |
| 331 | * |
| 332 | * @param subnet subnet of the service network as a string |
| 333 | * @return vtn service builder |
| 334 | */ |
| 335 | public Builder subnet(String subnet) { |
| 336 | try { |
| 337 | return subnet(IpPrefix.valueOf(subnet)); |
| 338 | } catch (IllegalArgumentException | NullPointerException e) { |
| 339 | final String msg = "Malformed IP prefix string " + subnet + |
| 340 | " for VTN service subnet"; |
| 341 | throw new IllegalArgumentException(msg); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Returns VTN service builder with the supplied service IP address. |
| 347 | * |
| 348 | * @param serviceIp service ip address |
| 349 | * @return vtn service builder |
| 350 | */ |
| 351 | public Builder serviceIp(IpAddress serviceIp) { |
| 352 | if (serviceIp == null) { |
| 353 | final String msg = "VTN service IP cannot be null"; |
| 354 | throw new IllegalArgumentException(msg); |
| 355 | } |
| 356 | this.serviceIp = serviceIp; |
| 357 | return this; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Returns VTN service builder with the supplied service IP address. |
| 362 | * |
| 363 | * @param serviceIp service ip address as a string |
| 364 | * @return vtn service builder |
| 365 | */ |
| 366 | public Builder serviceIp(String serviceIp) { |
| 367 | try { |
| 368 | return serviceIp(IpAddress.valueOf(serviceIp)); |
| 369 | } catch (IllegalArgumentException | NullPointerException e) { |
| 370 | final String msg = "Malformed IP address string " + serviceIp + |
| 371 | " for VTN service IP address"; |
| 372 | throw new IllegalArgumentException(msg); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Returns VTN service builder with the supplied provider services. |
| 378 | * |
| 379 | * @param pServices provider services |
| 380 | * @return vtn service builder |
| 381 | */ |
| 382 | public Builder providerServices(Set<VtnServiceId> pServices) { |
| 383 | if (pServices == null) { |
| 384 | final String msg = "Provider services cannot be null"; |
| 385 | throw new IllegalArgumentException(msg); |
| 386 | } |
| 387 | this.providerServices = pServices; |
| 388 | return this; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Returns VTN service builder with the supplied tenant services. |
| 393 | * |
| 394 | * @param tServices tenant services |
| 395 | * @return vtn service builder |
| 396 | */ |
| 397 | public Builder tenantServices(Set<VtnServiceId> tServices) { |
| 398 | if (tServices == null) { |
| 399 | final String msg = "Tenant services cannot be null"; |
| 400 | throw new IllegalArgumentException(msg); |
| 401 | } |
| 402 | this.tenantServices = tServices; |
| 403 | return this; |
| 404 | } |
| 405 | } |
Hyunsun Moon | 4c39663 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 406 | } |