Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 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.opencord.cordvtn.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.opencord.cordvtn.api.net.AddressPair; |
| 23 | import org.opencord.cordvtn.api.net.NetworkId; |
| 24 | import org.opencord.cordvtn.api.net.PortId; |
| 25 | import org.opencord.cordvtn.api.net.ServicePort; |
| 26 | |
| 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 | import static java.util.Collections.EMPTY_SET; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of {@link ServicePort}. |
| 36 | */ |
| 37 | public final class DefaultServicePort implements ServicePort { |
| 38 | |
| 39 | private static final String ERR_ID_MISSING = "Service port ID is missing"; |
| 40 | |
| 41 | private final PortId id; |
| 42 | private final String name; |
| 43 | private final NetworkId networkId; |
| 44 | private final MacAddress mac; |
| 45 | private final IpAddress ip; |
| 46 | private final VlanId vlanId; |
| 47 | private final Set<AddressPair> addressPairs; |
| 48 | |
| 49 | private DefaultServicePort(PortId id, |
| 50 | String name, |
| 51 | NetworkId networkId, |
| 52 | MacAddress mac, |
| 53 | IpAddress ip, |
| 54 | VlanId vlanId, |
| 55 | Set<AddressPair> addressPairs) { |
| 56 | this.id = id; |
| 57 | this.name = name; |
| 58 | this.networkId = networkId; |
| 59 | this.mac = mac; |
| 60 | this.ip = ip; |
| 61 | this.vlanId = vlanId; |
| 62 | this.addressPairs = addressPairs; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public PortId id() { |
| 67 | return id; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String name() { |
| 72 | return name; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public NetworkId networkId() { |
| 77 | return networkId; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public MacAddress mac() { |
| 82 | return mac; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public IpAddress ip() { |
| 87 | return ip; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public VlanId vlanId() { |
| 92 | return vlanId; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Set<AddressPair> addressPairs() { |
| 97 | return addressPairs; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean equals(Object obj) { |
| 102 | if (this == obj) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | if (obj instanceof DefaultServicePort) { |
| 107 | DefaultServicePort that = (DefaultServicePort) obj; |
| 108 | if (Objects.equals(id, that.id) && |
| 109 | Objects.equals(name, that.name) && |
| 110 | Objects.equals(networkId, that.networkId) && |
| 111 | Objects.equals(mac, that.mac) && |
| 112 | Objects.equals(ip, that.ip) && |
| 113 | Objects.equals(vlanId, that.vlanId) && |
| 114 | Objects.equals(addressPairs, that.addressPairs)) { |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
| 123 | return Objects.hash(id, name, networkId, mac, ip, vlanId, addressPairs); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String toString() { |
| 128 | return MoreObjects.toStringHelper(getClass()) |
| 129 | .add("id", id) |
| 130 | .add("name", name) |
| 131 | .add("networkId", networkId) |
| 132 | .add("mac", mac) |
| 133 | .add("ip", ip) |
| 134 | .add("vlanId", vlanId) |
| 135 | .add("addressPairs", addressPairs) |
| 136 | .toString(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns new service port builder instance. |
| 141 | * |
| 142 | * @return vtn port builder |
| 143 | */ |
| 144 | public static Builder builder() { |
| 145 | return new Builder(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns new builder instance with copy of the supplied service port. |
| 150 | * |
| 151 | * @param sport vtn port |
| 152 | * @return vtn port builder |
| 153 | */ |
| 154 | public static Builder builder(ServicePort sport) { |
| 155 | return new Builder() |
| 156 | .id(sport.id()) |
| 157 | .name(sport.name()) |
| 158 | .networkId(sport.networkId()) |
| 159 | .mac(sport.mac()) |
| 160 | .ip(sport.ip()) |
| 161 | .vlanId(sport.vlanId()) |
| 162 | .addressPairs(sport.addressPairs()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns service port builder instance with updated values. Any value |
| 167 | * not specified in the updated but in the existing remains in the updated |
| 168 | * builder. |
| 169 | * |
| 170 | * @param existing existing service port |
| 171 | * @param updated updated service port |
| 172 | * @return service network builder |
| 173 | */ |
| 174 | public static Builder builder(ServicePort existing, ServicePort updated) { |
| 175 | checkArgument(Objects.equals(existing.id(), updated.id())); |
| 176 | // FIXME allow removing existing values |
| 177 | return new Builder() |
| 178 | .id(existing.id()) |
| 179 | .name(updated.name() != null ? updated.name() : existing.name()) |
| 180 | .networkId(updated.networkId() != null ? |
| 181 | updated.networkId() : existing.networkId()) |
| 182 | .mac(updated.mac() != null ? updated.mac() : existing.mac()) |
| 183 | .ip(updated.ip() != null ? updated.ip() : existing.ip()) |
| 184 | .vlanId(updated.vlanId() != null ? updated.vlanId() : existing.vlanId()) |
| 185 | .addressPairs(updated.addressPairs() != EMPTY_SET ? |
| 186 | updated.addressPairs() : existing.addressPairs()); |
| 187 | } |
| 188 | |
| 189 | public static final class Builder implements ServicePort.Builder { |
| 190 | |
| 191 | private PortId id; |
| 192 | private String name; |
| 193 | private NetworkId networkId; |
| 194 | private MacAddress mac; |
| 195 | private IpAddress ip; |
| 196 | private VlanId vlanId; |
| 197 | private Set<AddressPair> addressPairs; |
| 198 | |
| 199 | private Builder() { |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public ServicePort build() { |
| 204 | checkNotNull(id, ERR_ID_MISSING); |
| 205 | addressPairs = addressPairs != null ? addressPairs : EMPTY_SET; |
| 206 | return new DefaultServicePort( |
| 207 | id, name, |
| 208 | networkId, mac, ip, vlanId, |
| 209 | addressPairs); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public Builder id(PortId id) { |
| 214 | this.id = id; |
| 215 | return this; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public Builder name(String name) { |
| 220 | this.name = name; |
| 221 | return this; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public Builder networkId(NetworkId networkId) { |
| 226 | this.networkId = networkId; |
| 227 | return this; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Builder mac(MacAddress mac) { |
| 232 | this.mac = mac; |
| 233 | return this; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Builder ip(IpAddress ip) { |
| 238 | this.ip = ip; |
| 239 | return this; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Builder vlanId(VlanId vlanId) { |
| 244 | this.vlanId = vlanId; |
| 245 | return this; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public Builder addressPairs(Set<AddressPair> addressPairs) { |
| 250 | this.addressPairs = addressPairs; |
| 251 | return this; |
| 252 | } |
| 253 | } |
| 254 | } |