blob: 9d66de3f872c77705f7bccce406acca37e9ad790 [file] [log] [blame]
Phaneendra Manda805f82a2015-10-26 22:35:44 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Phaneendra Manda805f82a2015-10-26 22:35:44 +05303 *
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 */
16package org.onosproject.vtnrsc;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
Phaneendra Manda329a1272016-02-10 22:57:00 +053021import java.util.Iterator;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053022import java.util.List;
Phaneendra Manda329a1272016-02-10 22:57:00 +053023import java.util.Map;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053024import java.util.Objects;
Phaneendra Manda329a1272016-02-10 22:57:00 +053025import java.util.Set;
26import java.util.concurrent.ConcurrentHashMap;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053027
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +053028import org.onosproject.net.DeviceId;
29
Phaneendra Manda805f82a2015-10-26 22:35:44 +053030import com.google.common.collect.ImmutableList;
Phaneendra Manda329a1272016-02-10 22:57:00 +053031import com.google.common.collect.ImmutableSet;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053032
33/**
34 * Implementation of port chain.
35 */
36public 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 Manda8db7d092016-06-04 00:17:24 +053044 private final PortChain oldPortChain;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053045
Phaneendra Manda329a1272016-02-10 22:57:00 +053046 private final Map<FiveTuple, LoadBalanceId> sfcLoadBalanceIdMap = new ConcurrentHashMap<>();
47 private final Map<LoadBalanceId, List<PortPairId>> sfcLoadBalancePathMap = new ConcurrentHashMap<>();
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +053048 private final Map<LoadBalanceId, List<DeviceId>> sfcClassifiersMap = new ConcurrentHashMap<>();
49 private final Map<LoadBalanceId, List<DeviceId>> sfcForwardersMap = new ConcurrentHashMap<>();
Phaneendra Manda8db7d092016-06-04 00:17:24 +053050
Phaneendra Manda805f82a2015-10-26 22:35:44 +053051 /**
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 Manda8db7d092016-06-04 00:17:24 +053062 String name, String description,
63 List<PortPairGroupId> portPairGroupList,
64 List<FlowClassifierId> flowClassifierList,
65 PortChain portChain) {
Phaneendra Manda805f82a2015-10-26 22:35:44 +053066
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 Manda8db7d092016-06-04 00:17:24 +053073 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 Manda805f82a2015-10-26 22:35:44 +053087 }
88
Phaneendra Manda329a1272016-02-10 22:57:00 +053089 /**
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 Manda805f82a2015-10-26 22:35:44 +0530106 @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 Manda8db7d092016-06-04 00:17:24 +0530128 return ImmutableList.copyOf(portPairGroupList);
Phaneendra Manda805f82a2015-10-26 22:35:44 +0530129 }
130
131 @Override
132 public List<FlowClassifierId> flowClassifiers() {
133 return ImmutableList.copyOf(flowClassifierList);
134 }
135
136 @Override
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530137 public PortChain oldPortChain() {
138 return oldPortChain;
139 }
140
141 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +0530142 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 Mandaab7fdfa2016-04-19 01:24:09 +0530149 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 Manda8db7d092016-06-04 00:17:24 +0530160 List<DeviceId> list = sfcClassifiersMap.get(id);
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +0530161 list.removeAll(classifierList);
162 this.sfcForwardersMap.put(id, list);
163 }
164
165 @Override
166 public void removeSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) {
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530167 List<DeviceId> list = sfcForwardersMap.get(id);
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +0530168 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 Manda329a1272016-02-10 22:57:00 +0530183 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 Mandaab7fdfa2016-04-19 01:24:09 +0530193 public Set<LoadBalanceId> getLoadBalancePathMapKeys() {
194 return ImmutableSet.copyOf(sfcLoadBalancePathMap.keySet());
195 }
196
197 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +0530198 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 Manda275ff0c2016-02-25 11:50:24 +0530208 public int getLoadBalancePathSize() {
209 if (sfcLoadBalanceIdMap.isEmpty()) {
210 return 0;
211 }
212 return sfcLoadBalanceIdMap.size();
213 }
214
215 @Override
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530216 public LoadBalanceId matchPath(List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530217
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 Manda8db7d092016-06-04 00:17:24 +0530226 return id;
Phaneendra Manda329a1272016-02-10 22:57:00 +0530227 }
228
229 @Override
Phaneendra Manda805f82a2015-10-26 22:35:44 +0530230 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 Manda8db7d092016-06-04 00:17:24 +0530291 private PortChain portChain;
Phaneendra Manda805f82a2015-10-26 22:35:44 +0530292
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 Manda8db7d092016-06-04 00:17:24 +0530337 portPairGroupList, flowClassifierList, portChain);
Phaneendra Manda805f82a2015-10-26 22:35:44 +0530338 }
339 }
340}