blob: c4546356d3cb1d9e8b5a82adcf46940d2294ae85 [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.Optional;
26import java.util.Set;
27import java.util.concurrent.ConcurrentHashMap;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053028
29import com.google.common.collect.ImmutableList;
Phaneendra Manda329a1272016-02-10 22:57:00 +053030import com.google.common.collect.ImmutableSet;
Phaneendra Manda805f82a2015-10-26 22:35:44 +053031
32/**
33 * Implementation of port chain.
34 */
35public final class DefaultPortChain implements PortChain {
36
37 private final PortChainId portChainId;
38 private final TenantId tenantId;
39 private final String name;
40 private final String description;
41 private final List<PortPairGroupId> portPairGroupList;
42 private final List<FlowClassifierId> flowClassifierList;
43
Phaneendra Manda329a1272016-02-10 22:57:00 +053044 private final Map<FiveTuple, LoadBalanceId> sfcLoadBalanceIdMap = new ConcurrentHashMap<>();
45 private final Map<LoadBalanceId, List<PortPairId>> sfcLoadBalancePathMap = new ConcurrentHashMap<>();
46
Phaneendra Manda805f82a2015-10-26 22:35:44 +053047 /**
48 * Default constructor to create port chain.
49 *
50 * @param portChainId port chain id
51 * @param tenantId tenant id
52 * @param name name of port chain
53 * @param description description of port chain
54 * @param portPairGroupList port pair group list
55 * @param flowClassifierList flow classifier list
56 */
57 private DefaultPortChain(PortChainId portChainId, TenantId tenantId,
58 String name, String description,
59 List<PortPairGroupId> portPairGroupList,
60 List<FlowClassifierId> flowClassifierList) {
61
62 this.portChainId = portChainId;
63 this.tenantId = tenantId;
64 this.name = name;
65 this.description = description;
66 this.portPairGroupList = portPairGroupList;
67 this.flowClassifierList = flowClassifierList;
68 }
69
Phaneendra Manda329a1272016-02-10 22:57:00 +053070 /**
71 * Match for two given paths.
72 *
73 * @param path1 path of sfc
74 * @param path2 path of sfc
75 * @return true if the given path are same false otherwise
76 */
77 private boolean comparePath(List<PortPairId> path1, List<PortPairId> path2) {
78 Iterator it = path1.iterator();
79 for (PortPairId portPairId: path2) {
80 if (!portPairId.equals(it.next())) {
81 return false;
82 }
83 }
84 return true;
85 }
86
Phaneendra Manda805f82a2015-10-26 22:35:44 +053087 @Override
88 public PortChainId portChainId() {
89 return portChainId;
90 }
91
92 @Override
93 public TenantId tenantId() {
94 return tenantId;
95 }
96
97 @Override
98 public String name() {
99 return name;
100 }
101
102 @Override
103 public String description() {
104 return description;
105 }
106
107 @Override
108 public List<PortPairGroupId> portPairGroups() {
109 return ImmutableList.copyOf(portPairGroupList);
110 }
111
112 @Override
113 public List<FlowClassifierId> flowClassifiers() {
114 return ImmutableList.copyOf(flowClassifierList);
115 }
116
117 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +0530118 public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id,
119 List<PortPairId> path) {
120 this.sfcLoadBalanceIdMap.put(fiveTuple, id);
121 this.sfcLoadBalancePathMap.put(id, path);
122 }
123
124 @Override
125 public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) {
126 return this.sfcLoadBalanceIdMap.get(fiveTuple);
127 }
128
129 @Override
130 public Set<FiveTuple> getLoadBalanceIdMapKeys() {
131 return ImmutableSet.copyOf(sfcLoadBalanceIdMap.keySet());
132 }
133
134 @Override
135 public List<PortPairId> getLoadBalancePath(LoadBalanceId id) {
136 return ImmutableList.copyOf(this.sfcLoadBalancePathMap.get(id));
137 }
138
139 @Override
140 public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) {
141 return ImmutableList.copyOf(this.sfcLoadBalancePathMap.get(this.sfcLoadBalanceIdMap.get(fiveTuple)));
142 }
143
144 @Override
Phaneendra Manda275ff0c2016-02-25 11:50:24 +0530145 public int getLoadBalancePathSize() {
146 if (sfcLoadBalanceIdMap.isEmpty()) {
147 return 0;
148 }
149 return sfcLoadBalanceIdMap.size();
150 }
151
152 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +0530153 public Optional<LoadBalanceId> matchPath(List<PortPairId> path) {
154
155 LoadBalanceId id = null;
156 for (Map.Entry<LoadBalanceId, List<PortPairId>> entry : sfcLoadBalancePathMap.entrySet()) {
157 List<PortPairId> tempPath = entry.getValue();
158 if (comparePath(path, tempPath)) {
159 id = entry.getKey();
160 break;
161 }
162 }
163 return Optional.of(id);
164 }
165
166 @Override
Phaneendra Manda805f82a2015-10-26 22:35:44 +0530167 public int hashCode() {
168 return Objects.hash(portChainId, tenantId, name, description,
169 portPairGroupList, flowClassifierList);
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (this == obj) {
175 return true;
176 }
177 if (obj instanceof DefaultPortChain) {
178 DefaultPortChain that = (DefaultPortChain) obj;
179 return Objects.equals(portChainId, that.portChainId) &&
180 Objects.equals(tenantId, that.tenantId) &&
181 Objects.equals(name, that.name) &&
182 Objects.equals(description, that.description) &&
183 Objects.equals(portPairGroupList, that.portPairGroupList) &&
184 Objects.equals(flowClassifierList, that.flowClassifierList);
185 }
186 return false;
187 }
188
189 @Override
190 public boolean exactMatch(PortChain portChain) {
191 return this.equals(portChain) &&
192 Objects.equals(this.portChainId, portChain.portChainId()) &&
193 Objects.equals(this.tenantId, portChain.tenantId());
194 }
195
196 @Override
197 public String toString() {
198 return toStringHelper(this)
199 .add("id", portChainId.toString())
200 .add("tenantId", tenantId.toString())
201 .add("name", name)
202 .add("description", description)
203 .add("portPairGroupList", portPairGroupList)
204 .add("flowClassifier", flowClassifierList)
205 .toString();
206 }
207
208 /**
209 * To create an instance of the builder.
210 *
211 * @return instance of builder
212 */
213 public static Builder builder() {
214 return new Builder();
215 }
216
217 /**
218 * Builder class for Port chain.
219 */
220 public static final class Builder implements PortChain.Builder {
221
222 private PortChainId portChainId;
223 private TenantId tenantId;
224 private String name;
225 private String description;
226 private List<PortPairGroupId> portPairGroupList;
227 private List<FlowClassifierId> flowClassifierList;
228
229 @Override
230 public Builder setId(PortChainId portChainId) {
231 this.portChainId = portChainId;
232 return this;
233 }
234
235 @Override
236 public Builder setTenantId(TenantId tenantId) {
237 this.tenantId = tenantId;
238 return this;
239 }
240
241 @Override
242 public Builder setName(String name) {
243 this.name = name;
244 return this;
245 }
246
247 @Override
248 public Builder setDescription(String description) {
249 this.description = description;
250 return this;
251 }
252
253 @Override
254 public Builder setPortPairGroups(List<PortPairGroupId> portPairGroups) {
255 this.portPairGroupList = portPairGroups;
256 return this;
257 }
258
259 @Override
260 public Builder setFlowClassifiers(List<FlowClassifierId> flowClassifiers) {
261 this.flowClassifierList = flowClassifiers;
262 return this;
263 }
264
265 @Override
266 public PortChain build() {
267
268 checkNotNull(portChainId, "Port chain id cannot be null");
269 checkNotNull(tenantId, "Tenant id cannot be null");
270 checkNotNull(portPairGroupList, "Port pair groups cannot be null");
271
272 return new DefaultPortChain(portChainId, tenantId, name, description,
273 portPairGroupList, flowClassifierList);
274 }
275 }
276}