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