Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -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 | */ |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.api.net; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.openstack4j.model.network.Port; |
| 25 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 26 | import java.util.Comparator; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 27 | import java.util.Objects; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 34 | * Representation of a port holding the basic virtual port and additional service |
| 35 | * port specific information. |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 36 | * All the services making use of CordVtnService are intended to interface |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 37 | * with this VtnPort, and not allowed to directly access {@link Port} or |
| 38 | * {@link ServicePort}. |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 39 | */ |
| 40 | public final class VtnPort extends ServicePort { |
| 41 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 42 | private static final String ERR_IP_MISSING = "VTN port IP address is missing"; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 43 | |
| 44 | private final NetworkId netId; |
| 45 | private final MacAddress mac; |
| 46 | private final IpAddress ip; |
| 47 | |
| 48 | private VtnPort(PortId id, |
| 49 | NetworkId netId, |
| 50 | MacAddress mac, |
| 51 | IpAddress ip, |
| 52 | VlanId vlanId, |
| 53 | Set<AddressPair> addressPairs) { |
| 54 | super(id, vlanId, addressPairs); |
| 55 | this.netId = netId; |
| 56 | this.mac = mac; |
| 57 | this.ip = ip; |
| 58 | } |
| 59 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 60 | public static final Comparator<VtnPort> VTN_PORT_COMPARATOR = |
| 61 | (port1, port2) -> port1.netId().id().compareTo(port2.netId().id()); |
| 62 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Returns the network ID of this port. |
| 65 | * |
| 66 | * @return network id |
| 67 | */ |
| 68 | public NetworkId netId() { |
| 69 | return netId; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the MAC address of this port. |
| 74 | * |
| 75 | * @return mac address |
| 76 | */ |
| 77 | public MacAddress mac() { |
| 78 | return mac; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the IP address of this port. |
| 83 | * |
| 84 | * @return ip address |
| 85 | */ |
| 86 | public IpAddress ip() { |
| 87 | return ip; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean equals(Object obj) { |
| 92 | if (this == obj) { |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | if (obj instanceof VtnPort) { |
| 97 | VtnPort that = (VtnPort) obj; |
| 98 | if (Objects.equals(id, that.id) && |
| 99 | Objects.equals(netId, that.netId) && |
| 100 | Objects.equals(mac, that.mac) && |
| 101 | Objects.equals(ip, that.ip) && |
| 102 | Objects.equals(vlanId, that.vlanId) && |
| 103 | Objects.equals(addressPairs, that.addressPairs)) { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int hashCode() { |
| 112 | return Objects.hash(id, netId, mac, ip, vlanId, addressPairs); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return MoreObjects.toStringHelper(getClass()) |
| 118 | .add("id", id) |
| 119 | .add("netId", netId) |
| 120 | .add("mac", mac) |
| 121 | .add("ip", ip) |
| 122 | .add("vlanId", vlanId) |
| 123 | .add("addressPairs", addressPairs) |
| 124 | .toString(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the immutable VTN port with the supplied Neutron port with additional |
| 129 | * vtn port information. |
| 130 | * |
| 131 | * @param port neutron port |
| 132 | * @param servicePort vtn port |
| 133 | * @return vtn port |
| 134 | */ |
| 135 | public static VtnPort of(Port port, ServicePort servicePort) { |
| 136 | validateNeutronPort(port); |
| 137 | if (servicePort != null) { |
| 138 | checkArgument(Objects.equals(port.getId(), servicePort.id().id())); |
| 139 | } |
| 140 | |
| 141 | return builder().id(PortId.of(port.getId())) |
| 142 | .netId(NetworkId.of(port.getNetworkId())) |
| 143 | .mac(MacAddress.valueOf(port.getMacAddress())) |
| 144 | .ip(IpAddress.valueOf(port.getFixedIps().iterator().next().getIpAddress())) |
| 145 | .vlanId(servicePort == null ? null : servicePort.vlanId().orElse(null)) |
| 146 | .addressPairs(servicePort == null ? ImmutableSet.of() : servicePort.addressPairs()) |
| 147 | .build(); |
| 148 | } |
| 149 | |
| 150 | private static void validateNeutronPort(Port port) { |
| 151 | checkNotNull(port); |
| 152 | checkArgument(port.getFixedIps().size() > 0, ERR_IP_MISSING); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the immutable VTN port with the supplied VTN port with additional |
| 157 | * vtn port information. |
| 158 | * |
| 159 | * @param vtnPort vtn port |
| 160 | * @param servicePort vtn port |
| 161 | * @return vtn port |
| 162 | */ |
| 163 | public static VtnPort of(VtnPort vtnPort, ServicePort servicePort) { |
| 164 | return builder(vtnPort) |
| 165 | .vlanId(servicePort.vlanId().orElse(null)) |
| 166 | .addressPairs(servicePort.addressPairs()) |
| 167 | .build(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns new vtn port builder instance. |
| 172 | * |
| 173 | * @return vtn port builder |
| 174 | */ |
| 175 | public static Builder builder() { |
| 176 | return new Builder(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns new vtn port builder instance with copy of the supplied vtn port. |
| 181 | * |
| 182 | * @param vtnPort vtn port |
| 183 | * @return vtn port builder |
| 184 | */ |
| 185 | public static Builder builder(VtnPort vtnPort) { |
| 186 | return new Builder().id(vtnPort.id()) |
| 187 | .netId(vtnPort.netId()) |
| 188 | .mac(vtnPort.mac()) |
| 189 | .ip(vtnPort.ip()) |
| 190 | .vlanId(vtnPort.vlanId().orElse(null)) |
| 191 | .addressPairs(vtnPort.addressPairs()); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Builder of the vtn port entities. |
| 196 | */ |
| 197 | public static final class Builder { |
| 198 | private PortId id; |
| 199 | private NetworkId netId; |
| 200 | private MacAddress mac; |
| 201 | private IpAddress ip; |
| 202 | private VlanId vlanId; |
| 203 | private Set<AddressPair> addressPairs = ImmutableSet.of(); |
| 204 | |
| 205 | private Builder() { |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Builds an immutable vtn port. |
| 210 | * |
| 211 | * @return vtn port instance |
| 212 | */ |
| 213 | public VtnPort build() { |
| 214 | checkNotNull(id, "VtnPort port id cannot be null"); |
| 215 | checkNotNull(netId, "VtnPort network id cannot be null"); |
| 216 | checkNotNull(mac, "VtnPort mac address cannot be null"); |
| 217 | checkNotNull(ip, "VtnPort ip address cannot be null"); |
| 218 | addressPairs = addressPairs == null ? ImmutableSet.of() : addressPairs; |
| 219 | |
| 220 | return new VtnPort(id, netId, mac, ip, vlanId, addressPairs); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns vtn port builder with the supplied port id. |
| 225 | * |
| 226 | * @param id port id |
| 227 | * @return vtn port builder |
| 228 | */ |
| 229 | public Builder id(PortId id) { |
| 230 | this.id = id; |
| 231 | return this; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Returns vtn port builder with the supplied network id. |
| 236 | * |
| 237 | * @param netId network id |
| 238 | * @return vtn port builder |
| 239 | */ |
| 240 | public Builder netId(NetworkId netId) { |
| 241 | this.netId = netId; |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns vtn port builder with the supplied mac address. |
| 247 | * |
| 248 | * @param mac mac address |
| 249 | * @return vtn port builder |
| 250 | */ |
| 251 | public Builder mac(MacAddress mac) { |
| 252 | this.mac = mac; |
| 253 | return this; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns vtn port builder with the supplied ip address. |
| 258 | * |
| 259 | * @param ip ip address |
| 260 | * @return vtn port builder |
| 261 | */ |
| 262 | public Builder ip(IpAddress ip) { |
| 263 | this.ip = ip; |
| 264 | return this; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns vtn port builder with the supplied VLAN ID. |
| 269 | * |
| 270 | * @param vlanId vlan id |
| 271 | * @return vtn port builder |
| 272 | */ |
| 273 | public Builder vlanId(VlanId vlanId) { |
| 274 | this.vlanId = vlanId; |
| 275 | return this; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns vtn port builder with the supplied address pairs. |
| 280 | * |
| 281 | * @param addressPairs set of address pairs |
| 282 | * @return vtn port builder |
| 283 | */ |
| 284 | public Builder addressPairs(Set<AddressPair> addressPairs) { |
| 285 | this.addressPairs = addressPairs; |
| 286 | return this; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Returns vtn port builder with the given additional address pair. |
| 291 | * |
| 292 | * @param addressPair address pair to add |
| 293 | * @return vtn port builder |
| 294 | */ |
| 295 | public Builder addAddressPair(AddressPair addressPair) { |
| 296 | checkNotNull(addressPair, "VtnPort address pair cannot be null"); |
| 297 | |
| 298 | Set<AddressPair> updated = Sets.newHashSet(this.addressPairs); |
| 299 | updated.add(addressPair); |
| 300 | this.addressPairs = ImmutableSet.copyOf(updated); |
| 301 | return this; |
| 302 | } |
| 303 | } |
| 304 | } |