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