blob: 20465613f8ee558fd603acc993453d565db5fe5a [file] [log] [blame]
Pier Ventref5d72362016-07-17 12:02:14 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventref5d72362016-07-17 12:02:14 +02003 *
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
17package org.onosproject.ovsdb.controller;
18
19import com.google.common.collect.Maps;
kdarapufce5abb2018-05-10 19:37:53 +053020import org.onlab.packet.VlanId;
Pier Ventref5d72362016-07-17 12:02:14 +020021import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.behaviour.MirroringDescription;
23import org.onosproject.ovsdb.rfc.notation.Uuid;
24
25import java.util.Map;
26import java.util.Objects;
27import java.util.Optional;
28import java.util.Set;
29import java.util.stream.Collectors;
30
31import static com.google.common.base.MoreObjects.toStringHelper;
32
33/**
34 * The class representing an OVSDB mirror.
35 * This class is immutable.
36 */
37public final class OvsdbMirror {
38
39 private final String mirroringName;
40 private boolean selectAll;
41 private final Set<Uuid> monitorSrcPorts;
42 private final Set<Uuid> monitorDstPorts;
43 private final Set<Short> monitorVlans;
44 private final Optional<Uuid> mirrorPort;
45 private final Optional<Short> mirrorVlan;
46 private Map<String, String> externalIds;
47
48 /**
49 * Creates an OvsdbMirror using the given inputs.
50 *
51 * @param mirroringName the name of the mirroring
52 * @param selectAll mirrors all ports
53 * @param monitorSrcPorts the monitored src ports
54 * @param monitorDstPorts the monitored dst ports
55 * @param monitorVlans the monitored vlans
56 * @param mirrorPort the mirror port
57 * @param mirrorVlan the mirror vlan
58 * @param externalIds optional key/value options
59 */
60 private OvsdbMirror(String mirroringName, boolean selectAll, Set<Uuid> monitorSrcPorts, Set<Uuid> monitorDstPorts,
61 Set<Short> monitorVlans, Optional<Uuid> mirrorPort, Optional<Short> mirrorVlan,
62 Map<String, String> externalIds) {
63
64 this.mirroringName = mirroringName;
65 this.selectAll = selectAll;
66 this.monitorSrcPorts = monitorSrcPorts;
67 this.monitorDstPorts = monitorDstPorts;
68 this.monitorVlans = monitorVlans;
69 this.mirrorPort = mirrorPort;
70 this.mirrorVlan = mirrorVlan;
71 this.externalIds = externalIds;
72
73 }
74
75 /**
76 * Returns the name of the mirroring.
77 *
78 * @return the string representing the name
79 */
80 public String mirroringName() {
81 return mirroringName;
82 }
83
84 /**
85 * Returns selectAll value.
86 *
87 * @return mirrors all ports if true
88 */
89 public boolean selectAll() {
90 return selectAll;
91 }
92
93 /**
94 * Returns the monitored src ports.
95 *
96 * @return the uuids set of the ports
97 */
98 public Set<Uuid> monitorSrcPorts() {
99 return monitorSrcPorts;
100 }
101
102 /**
103 * Returns the monitored dst ports.
104 *
105 * @return the uuids set of the ports
106 */
107 public Set<Uuid> monitorDstPorts() {
108 return monitorDstPorts;
109 }
110
111 /**
112 * Returns the monitored vlans.
113 *
114 * @return the vlans set
115 */
116 public Set<Short> monitorVlans() {
117 return monitorVlans;
118 }
119
120 /**
121 * Returns the mirror port.
122 *
123 * @return the uuid port if present, otherwise null
124 */
125 public Uuid mirrorPort() {
126 return mirrorPort.orElse(null);
127 }
128
129 /**
130 * Returns the mirror vlan.
131 *
132 * @return the vlan id if present, otherwise null
133 */
134 public Short mirrorVlan() {
135 return mirrorVlan.orElse(null);
136 }
137
138 /**
139 * Returns the optional external ids.
140 *
141 * @return the external ids.
142 */
143 public Map<String, String> externalIds() {
144 return externalIds;
145 }
146
147 @Override
148 public int hashCode() {
149 return Objects.hash(mirroringName, selectAll, monitorSrcPorts, monitorDstPorts,
150 monitorVlans, mirrorPort, mirrorVlan, externalIds);
151 }
152
153 @Override
154 public boolean equals(Object obj) {
155 if (this == obj) {
156 return true;
157 }
158 if (obj instanceof OvsdbMirror) {
159 final OvsdbMirror other = (OvsdbMirror) obj;
160 return Objects.equals(this.mirroringName, other.mirroringName) &&
161 Objects.equals(this.selectAll, other.selectAll) &&
162 Objects.equals(this.monitorSrcPorts, other.monitorSrcPorts) &&
163 Objects.equals(this.monitorDstPorts, other.monitorDstPorts) &&
164 Objects.equals(this.monitorVlans, other.monitorVlans) &&
165 Objects.equals(this.mirrorPort, other.mirrorPort) &&
166 Objects.equals(this.mirrorVlan, other.mirrorVlan) &&
167 Objects.equals(this.externalIds, other.externalIds);
168 }
169 return false;
170 }
171
172 @Override
173 public String toString() {
174 return toStringHelper(this)
175 .add("mirroringName", mirroringName())
176 .add("selectAll", selectAll())
177 .add("monitorSrcPorts", monitorSrcPorts())
178 .add("monitorDstPorts", monitorDstPorts())
179 .add("monitorVlans", monitorVlans())
180 .add("mirrorPort", mirrorPort())
181 .add("mirrorVlan", mirrorVlan())
182 .add("externalIds", externalIds())
183 .toString();
184 }
185
186 /**
187 * Returns new OVSDB mirror builder.
188 *
189 * @return ovsdb mirror builder
190 */
191 public static OvsdbMirror.Builder builder() {
192 return new OvsdbMirror.Builder();
193 }
194
195 /**
196 * Returns new OVSDB mirror builder with mirror description.
197 *
198 * @param mirrorDesc mirror description
199 * @return ovsdb mirror builder
200 */
201 public static OvsdbMirror.Builder builder(MirroringDescription mirrorDesc) {
202 return new OvsdbMirror.Builder(mirrorDesc);
203 }
204
205 /**
206 * Builder of OVSDB mirror entities.
207 */
208 public static final class Builder {
209
210 private String mirroringName;
211 private boolean selectAll;
212 private Set<Uuid> monitorSrcPorts;
213 private Set<Uuid> monitorDstPorts;
214 private Set<Short> monitorVlans;
215 private Optional<Uuid> mirrorPort;
216 private Optional<Short> mirrorVlan;
217 private Map<String, String> externalIds = Maps.newHashMap();
218
219 /**
220 * Constructs an empty builder.
221 */
222 private Builder() {
223
224 }
225
226 /**
227 * Constructs a builder with a given mirror description.
228 *
229 * @param mirrorDesc mirror description
230 */
231 private Builder(MirroringDescription mirrorDesc) {
232
233 mirroringName = mirrorDesc.name().name();
234 selectAll = false;
235 monitorSrcPorts = mirrorDesc.monitorSrcPorts().parallelStream()
kdarapufce5abb2018-05-10 19:37:53 +0530236 .map(Uuid::uuid)
Pier Ventref5d72362016-07-17 12:02:14 +0200237 .collect(Collectors.toSet());
238 monitorDstPorts = mirrorDesc.monitorDstPorts().parallelStream()
kdarapufce5abb2018-05-10 19:37:53 +0530239 .map(Uuid::uuid)
Pier Ventref5d72362016-07-17 12:02:14 +0200240 .collect(Collectors.toSet());
241 monitorVlans = mirrorDesc.monitorVlans().parallelStream()
kdarapufce5abb2018-05-10 19:37:53 +0530242 .map(VlanId::toShort)
Pier Ventref5d72362016-07-17 12:02:14 +0200243 .collect(Collectors.toSet());
244
245 if (mirrorDesc.mirrorPort().isPresent()) {
246 mirrorPort = Optional.of(Uuid.uuid(mirrorDesc.mirrorPort().get()));
247 } else {
248 mirrorPort = Optional.empty();
249 }
250
251 if (mirrorDesc.mirrorVlan().isPresent()) {
252 mirrorVlan = Optional.of(mirrorDesc.mirrorVlan().get().toShort());
253 } else {
254 mirrorVlan = Optional.empty();
255 }
256
257 externalIds.putAll(((DefaultAnnotations) mirrorDesc.annotations()).asMap());
258
259 }
260
261 /**
262 * Returns new OVSDB mirror.
263 *
264 * @return ovsdb mirror
265 */
266 public OvsdbMirror build() {
267 return new OvsdbMirror(mirroringName, selectAll, monitorSrcPorts, monitorDstPorts, monitorVlans,
268 mirrorPort, mirrorVlan, externalIds);
269 }
270
271 /**
272 * Returns OVSDB mirror builder with a given name.
273 *
274 * @param name name of the mirror
275 * @return ovsdb interface builder
276 */
277 public OvsdbMirror.Builder mirroringName(String name) {
278 this.mirroringName = name;
279 return this;
280 }
281
282 /**
283 * Returns OVSDB mirror builder with select all.
284 *
285 * @param all mirrors all ports
286 * @return ovsdb interface builder
287 */
288 public OvsdbMirror.Builder selectAll(boolean all) {
289 this.selectAll = all;
290 return this;
291 }
292
293 /**
294 * Returns OVSDB mirror builder with a given set
295 * of monitored src ports.
296 *
297 * @param monitorPorts ports to be monitored
298 * @return ovsdb mirror builder
299 */
300 public OvsdbMirror.Builder monitorSrcPorts(Set<Uuid> monitorPorts) {
301 this.monitorSrcPorts = monitorPorts;
302 return this;
303 }
304
305 /**
306 * Returns OVSDB mirror builder with a given set
307 * of monitored dst ports.
308 *
309 * @param monitorPorts ports to be monitored
310 * @return ovsdb mirror builder
311 */
312 public OvsdbMirror.Builder monitorDstPorts(Set<Uuid> monitorPorts) {
313 this.monitorDstPorts = monitorPorts;
314 return this;
315 }
316
317 /**
318 * Returns OVSDB mirror builder with a given set
319 * of monitored vlans.
320 *
321 * @param vlans vlans to be monitored
322 * @return ovsdb mirror builder
323 */
324 public OvsdbMirror.Builder monitorVlans(Set<Short> vlans) {
325 this.monitorVlans = vlans;
326 return this;
327 }
328
329 /**
330 * Returns OVSDB mirror builder with a given mirror port.
331 *
332 * @param port the mirror port
333 * @return ovsdb mirror builder
334 */
335 public OvsdbMirror.Builder mirrorPort(Uuid port) {
336 this.mirrorPort = Optional.ofNullable(port);
337 return this;
338 }
339
340 /**
341 * Returns OVSDB mirror builder with a given mirror vlan.
342 *
343 * @param vlan the mirror vlan
344 * @return ovsdb mirror builder
345 */
346 public OvsdbMirror.Builder mirrorVlan(Short vlan) {
347 this.mirrorVlan = Optional.ofNullable(vlan);
348 return this;
349 }
350
351 /**
352 * Returns OVSDB mirror builder with given external ids.
353 *
354 * @param ids the external ids
355 * @return ovsdb mirror builder
356 */
357 public OvsdbMirror.Builder externalIds(Map<String, String> ids) {
358 this.externalIds = ids;
359 return this;
360 }
361
362 }
363
364}