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.IpPrefix; |
| 21 | import org.opencord.cordvtn.api.net.NetworkId; |
| 22 | import org.opencord.cordvtn.api.net.SegmentId; |
| 23 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static java.util.Collections.EMPTY_MAP; |
| 31 | |
| 32 | /** |
| 33 | * Implementation of {@link ServiceNetwork}. |
| 34 | */ |
| 35 | public final class DefaultServiceNetwork implements ServiceNetwork { |
| 36 | |
| 37 | private static final String ERR_ID_MISSING = "Service network ID cannot be null"; |
| 38 | |
| 39 | private final NetworkId id; |
| 40 | private final String name; |
| 41 | private final NetworkType type; |
| 42 | private final SegmentId segmentId; |
| 43 | private final IpPrefix subnet; |
| 44 | private final IpAddress serviceIp; |
| 45 | private final Map<NetworkId, DependencyType> providers; |
| 46 | |
| 47 | private DefaultServiceNetwork(NetworkId id, |
| 48 | String name, |
| 49 | NetworkType type, |
| 50 | SegmentId segmentId, |
| 51 | IpPrefix subnet, |
| 52 | IpAddress serviceIp, |
| 53 | Map<NetworkId, DependencyType> providers) { |
| 54 | this.id = id; |
| 55 | this.name = name; |
| 56 | this.type = type; |
| 57 | this.segmentId = segmentId; |
| 58 | this.subnet = subnet; |
| 59 | this.serviceIp = serviceIp; |
| 60 | this.providers = providers; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public NetworkId id() { |
| 65 | return id; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String name() { |
| 70 | return name; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public NetworkType type() { |
| 75 | return type; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public SegmentId segmentId() { |
| 80 | return segmentId; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public IpPrefix subnet() { |
| 85 | return subnet; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public IpAddress serviceIp() { |
| 90 | return serviceIp; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Map<NetworkId, DependencyType> providers() { |
| 95 | return providers; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean equals(Object obj) { |
| 100 | if (this == obj) { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | if (obj instanceof DefaultServiceNetwork) { |
| 105 | DefaultServiceNetwork that = (DefaultServiceNetwork) obj; |
| 106 | if (Objects.equals(id, that.id) && |
| 107 | Objects.equals(name, that.name) && |
| 108 | Objects.equals(type, that.type) && |
| 109 | Objects.equals(segmentId, that.segmentId) && |
| 110 | Objects.equals(subnet, that.subnet) && |
| 111 | Objects.equals(serviceIp, that.serviceIp) && |
| 112 | Objects.equals(providers, that.providers)) { |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public int hashCode() { |
| 121 | return Objects.hash(id, name, segmentId, subnet, serviceIp, type, providers); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | return MoreObjects.toStringHelper(getClass()) |
| 127 | .add("id", id) |
| 128 | .add("name", name) |
| 129 | .add("type", type) |
| 130 | .add("segmentId", segmentId) |
| 131 | .add("subnet", subnet) |
| 132 | .add("serviceIp", serviceIp) |
| 133 | .add("providers", providers) |
| 134 | .toString(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns new service network builder instance. |
| 139 | * |
| 140 | * @return service network builder |
| 141 | */ |
| 142 | public static Builder builder() { |
| 143 | return new Builder(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns new service network builder instance with copy of the given service network. |
| 148 | * |
| 149 | * @param snet service network |
| 150 | * @return service network builder |
| 151 | */ |
| 152 | public static Builder builder(ServiceNetwork snet) { |
| 153 | return new Builder() |
| 154 | .id(snet.id()) |
| 155 | .name(snet.name()) |
| 156 | .type(snet.type()) |
| 157 | .segmentId(snet.segmentId()) |
| 158 | .subnet(snet.subnet()) |
| 159 | .serviceIp(snet.serviceIp()) |
| 160 | .providers(snet.providers()); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns service network builder instance with updated values. Any value |
| 165 | * not specified in the updated but in the existing remains in the updated |
| 166 | * builder. |
| 167 | * |
| 168 | * @param existing existing service network |
| 169 | * @param updated updated service network |
| 170 | * @return service network builder |
| 171 | */ |
| 172 | public static Builder builder(ServiceNetwork existing, ServiceNetwork updated) { |
| 173 | checkArgument(Objects.equals(existing.id(), updated.id())); |
| 174 | // FIXME allow removing existing values |
| 175 | return new Builder() |
| 176 | .id(existing.id()) |
| 177 | .name(updated.name() != null ? updated.name() : existing.name()) |
| 178 | .type(updated.type() != null ? updated.type() : existing.type()) |
| 179 | .segmentId(updated.segmentId() != null ? |
| 180 | updated.segmentId() : existing.segmentId()) |
| 181 | .subnet(updated.subnet() != null ? updated.subnet() : existing.subnet()) |
| 182 | .serviceIp(updated.serviceIp() != null ? |
| 183 | updated.serviceIp() : existing.serviceIp()) |
| 184 | .providers(updated.providers() != EMPTY_MAP ? |
| 185 | updated.providers() : existing.providers()); |
| 186 | } |
| 187 | |
| 188 | public static final class Builder implements ServiceNetwork.Builder { |
| 189 | |
| 190 | private NetworkId id; |
| 191 | private String name; |
| 192 | private NetworkType type; |
| 193 | private SegmentId segmentId; |
| 194 | private IpPrefix subnet; |
| 195 | private IpAddress serviceIp; |
| 196 | private Map<NetworkId, DependencyType> providers; |
| 197 | |
| 198 | private Builder() { |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public ServiceNetwork build() { |
| 203 | checkNotNull(id, ERR_ID_MISSING); |
| 204 | providers = providers != null ? providers : EMPTY_MAP; |
| 205 | return new DefaultServiceNetwork( |
| 206 | id, name, type, |
| 207 | segmentId, |
| 208 | subnet, serviceIp, |
| 209 | providers); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public Builder id(NetworkId 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 type(NetworkType type) { |
| 226 | this.type = type; |
| 227 | return this; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Builder segmentId(SegmentId segmentId) { |
| 232 | this.segmentId = segmentId; |
| 233 | return this; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Builder subnet(IpPrefix subnet) { |
| 238 | this.subnet = subnet; |
| 239 | return this; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Builder serviceIp(IpAddress serviceIp) { |
| 244 | this.serviceIp = serviceIp; |
| 245 | return this; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public Builder providers(Map<NetworkId, DependencyType> providers) { |
| 250 | this.providers = providers; |
| 251 | return this; |
| 252 | } |
| 253 | } |
| 254 | } |