Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [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.onosproject.vtnrsc; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 21 | import java.util.Iterator; |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 22 | import java.util.List; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 23 | import java.util.Map; |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 24 | import java.util.Objects; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 27 | |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableList; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableSet; |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Implementation of port chain. |
| 35 | */ |
| 36 | public final class DefaultPortChain implements PortChain { |
| 37 | |
| 38 | private final PortChainId portChainId; |
| 39 | private final TenantId tenantId; |
| 40 | private final String name; |
| 41 | private final String description; |
| 42 | private final List<PortPairGroupId> portPairGroupList; |
| 43 | private final List<FlowClassifierId> flowClassifierList; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 44 | private final PortChain oldPortChain; |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 45 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 46 | private final Map<FiveTuple, LoadBalanceId> sfcLoadBalanceIdMap = new ConcurrentHashMap<>(); |
| 47 | private final Map<LoadBalanceId, List<PortPairId>> sfcLoadBalancePathMap = new ConcurrentHashMap<>(); |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 48 | private final Map<LoadBalanceId, List<DeviceId>> sfcClassifiersMap = new ConcurrentHashMap<>(); |
| 49 | private final Map<LoadBalanceId, List<DeviceId>> sfcForwardersMap = new ConcurrentHashMap<>(); |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 50 | |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 51 | /** |
| 52 | * Default constructor to create port chain. |
| 53 | * |
| 54 | * @param portChainId port chain id |
| 55 | * @param tenantId tenant id |
| 56 | * @param name name of port chain |
| 57 | * @param description description of port chain |
| 58 | * @param portPairGroupList port pair group list |
| 59 | * @param flowClassifierList flow classifier list |
| 60 | */ |
| 61 | private DefaultPortChain(PortChainId portChainId, TenantId tenantId, |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 62 | String name, String description, |
| 63 | List<PortPairGroupId> portPairGroupList, |
| 64 | List<FlowClassifierId> flowClassifierList, |
| 65 | PortChain portChain) { |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 66 | |
| 67 | this.portChainId = portChainId; |
| 68 | this.tenantId = tenantId; |
| 69 | this.name = name; |
| 70 | this.description = description; |
| 71 | this.portPairGroupList = portPairGroupList; |
| 72 | this.flowClassifierList = flowClassifierList; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 73 | this.oldPortChain = portChain; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * To create port chain for update with old port chain. |
| 78 | * |
| 79 | * @param newPortChain updated port chain |
| 80 | * @param oldPortChain old port chain |
| 81 | * @return port chain |
| 82 | */ |
| 83 | public static PortChain create(PortChain newPortChain, PortChain oldPortChain) { |
| 84 | return new DefaultPortChain(newPortChain.portChainId(), newPortChain.tenantId(), |
| 85 | newPortChain.name(), newPortChain.description(), |
| 86 | newPortChain.portPairGroups(), newPortChain.flowClassifiers(), oldPortChain); |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 87 | } |
| 88 | |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 89 | /** |
| 90 | * Match for two given paths. |
| 91 | * |
| 92 | * @param path1 path of sfc |
| 93 | * @param path2 path of sfc |
| 94 | * @return true if the given path are same false otherwise |
| 95 | */ |
| 96 | private boolean comparePath(List<PortPairId> path1, List<PortPairId> path2) { |
| 97 | Iterator it = path1.iterator(); |
| 98 | for (PortPairId portPairId: path2) { |
| 99 | if (!portPairId.equals(it.next())) { |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 106 | @Override |
| 107 | public PortChainId portChainId() { |
| 108 | return portChainId; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public TenantId tenantId() { |
| 113 | return tenantId; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public String name() { |
| 118 | return name; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public String description() { |
| 123 | return description; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public List<PortPairGroupId> portPairGroups() { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 128 | return ImmutableList.copyOf(portPairGroupList); |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public List<FlowClassifierId> flowClassifiers() { |
| 133 | return ImmutableList.copyOf(flowClassifierList); |
| 134 | } |
| 135 | |
| 136 | @Override |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 137 | public PortChain oldPortChain() { |
| 138 | return oldPortChain; |
| 139 | } |
| 140 | |
| 141 | @Override |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 142 | public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, |
| 143 | List<PortPairId> path) { |
| 144 | this.sfcLoadBalanceIdMap.put(fiveTuple, id); |
| 145 | this.sfcLoadBalancePathMap.put(id, path); |
| 146 | } |
| 147 | |
| 148 | @Override |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 149 | public void addSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) { |
| 150 | this.sfcClassifiersMap.put(id, classifierList); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void addSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) { |
| 155 | this.sfcForwardersMap.put(id, forwarderList); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void removeSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 160 | List<DeviceId> list = sfcClassifiersMap.get(id); |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 161 | list.removeAll(classifierList); |
| 162 | this.sfcForwardersMap.put(id, list); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removeSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 167 | List<DeviceId> list = sfcForwardersMap.get(id); |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 168 | list.removeAll(forwarderList); |
| 169 | this.sfcForwardersMap.put(id, list); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public List<DeviceId> getSfcClassifiers(LoadBalanceId id) { |
| 174 | return ImmutableList.copyOf(this.sfcClassifiersMap.get(id)); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public List<DeviceId> getSfcForwarders(LoadBalanceId id) { |
| 179 | return ImmutableList.copyOf(this.sfcForwardersMap.get(id)); |
| 180 | } |
| 181 | |
| 182 | @Override |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 183 | public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) { |
| 184 | return this.sfcLoadBalanceIdMap.get(fiveTuple); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Set<FiveTuple> getLoadBalanceIdMapKeys() { |
| 189 | return ImmutableSet.copyOf(sfcLoadBalanceIdMap.keySet()); |
| 190 | } |
| 191 | |
| 192 | @Override |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 193 | public Set<LoadBalanceId> getLoadBalancePathMapKeys() { |
| 194 | return ImmutableSet.copyOf(sfcLoadBalancePathMap.keySet()); |
| 195 | } |
| 196 | |
| 197 | @Override |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 198 | public List<PortPairId> getLoadBalancePath(LoadBalanceId id) { |
| 199 | return ImmutableList.copyOf(this.sfcLoadBalancePathMap.get(id)); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) { |
| 204 | return ImmutableList.copyOf(this.sfcLoadBalancePathMap.get(this.sfcLoadBalanceIdMap.get(fiveTuple))); |
| 205 | } |
| 206 | |
| 207 | @Override |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 208 | public int getLoadBalancePathSize() { |
| 209 | if (sfcLoadBalanceIdMap.isEmpty()) { |
| 210 | return 0; |
| 211 | } |
| 212 | return sfcLoadBalanceIdMap.size(); |
| 213 | } |
| 214 | |
| 215 | @Override |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 216 | public LoadBalanceId matchPath(List<PortPairId> path) { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 217 | |
| 218 | LoadBalanceId id = null; |
| 219 | for (Map.Entry<LoadBalanceId, List<PortPairId>> entry : sfcLoadBalancePathMap.entrySet()) { |
| 220 | List<PortPairId> tempPath = entry.getValue(); |
| 221 | if (comparePath(path, tempPath)) { |
| 222 | id = entry.getKey(); |
| 223 | break; |
| 224 | } |
| 225 | } |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 226 | return id; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 230 | public int hashCode() { |
| 231 | return Objects.hash(portChainId, tenantId, name, description, |
| 232 | portPairGroupList, flowClassifierList); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean equals(Object obj) { |
| 237 | if (this == obj) { |
| 238 | return true; |
| 239 | } |
| 240 | if (obj instanceof DefaultPortChain) { |
| 241 | DefaultPortChain that = (DefaultPortChain) obj; |
| 242 | return Objects.equals(portChainId, that.portChainId) && |
| 243 | Objects.equals(tenantId, that.tenantId) && |
| 244 | Objects.equals(name, that.name) && |
| 245 | Objects.equals(description, that.description) && |
| 246 | Objects.equals(portPairGroupList, that.portPairGroupList) && |
| 247 | Objects.equals(flowClassifierList, that.flowClassifierList); |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public boolean exactMatch(PortChain portChain) { |
| 254 | return this.equals(portChain) && |
| 255 | Objects.equals(this.portChainId, portChain.portChainId()) && |
| 256 | Objects.equals(this.tenantId, portChain.tenantId()); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public String toString() { |
| 261 | return toStringHelper(this) |
| 262 | .add("id", portChainId.toString()) |
| 263 | .add("tenantId", tenantId.toString()) |
| 264 | .add("name", name) |
| 265 | .add("description", description) |
| 266 | .add("portPairGroupList", portPairGroupList) |
| 267 | .add("flowClassifier", flowClassifierList) |
| 268 | .toString(); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * To create an instance of the builder. |
| 273 | * |
| 274 | * @return instance of builder |
| 275 | */ |
| 276 | public static Builder builder() { |
| 277 | return new Builder(); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Builder class for Port chain. |
| 282 | */ |
| 283 | public static final class Builder implements PortChain.Builder { |
| 284 | |
| 285 | private PortChainId portChainId; |
| 286 | private TenantId tenantId; |
| 287 | private String name; |
| 288 | private String description; |
| 289 | private List<PortPairGroupId> portPairGroupList; |
| 290 | private List<FlowClassifierId> flowClassifierList; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 291 | private PortChain portChain; |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 292 | |
| 293 | @Override |
| 294 | public Builder setId(PortChainId portChainId) { |
| 295 | this.portChainId = portChainId; |
| 296 | return this; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public Builder setTenantId(TenantId tenantId) { |
| 301 | this.tenantId = tenantId; |
| 302 | return this; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public Builder setName(String name) { |
| 307 | this.name = name; |
| 308 | return this; |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public Builder setDescription(String description) { |
| 313 | this.description = description; |
| 314 | return this; |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public Builder setPortPairGroups(List<PortPairGroupId> portPairGroups) { |
| 319 | this.portPairGroupList = portPairGroups; |
| 320 | return this; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public Builder setFlowClassifiers(List<FlowClassifierId> flowClassifiers) { |
| 325 | this.flowClassifierList = flowClassifiers; |
| 326 | return this; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public PortChain build() { |
| 331 | |
| 332 | checkNotNull(portChainId, "Port chain id cannot be null"); |
| 333 | checkNotNull(tenantId, "Tenant id cannot be null"); |
| 334 | checkNotNull(portPairGroupList, "Port pair groups cannot be null"); |
| 335 | |
| 336 | return new DefaultPortChain(portChainId, tenantId, name, description, |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 337 | portPairGroupList, flowClassifierList, portChain); |
Phaneendra Manda | 805f82a | 2015-10-26 22:35:44 +0530 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |