blob: cc258c041e25b20db9bc67f07db4038c22fe32ab [file] [log] [blame]
Jian Li19f25262018-07-03 22:37:12 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstackvtap.impl;
17
Jian Li19f25262018-07-03 22:37:12 +090018import com.google.common.collect.ImmutableSet;
Jian Li19f25262018-07-03 22:37:12 +090019import org.onosproject.net.AbstractDescription;
20import org.onosproject.net.DefaultAnnotations;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.SparseAnnotations;
23import org.onosproject.openstackvtap.api.OpenstackVtap;
24import org.onosproject.openstackvtap.api.OpenstackVtapCriterion;
25import org.onosproject.openstackvtap.api.OpenstackVtapId;
26
Jimo Jung14e87bf2018-09-03 16:28:13 +090027import java.util.Objects;
Jian Li19f25262018-07-03 22:37:12 +090028import java.util.Set;
29
30import static com.google.common.base.MoreObjects.toStringHelper;
Jimo Jung14e87bf2018-09-03 16:28:13 +090031import static com.google.common.base.Preconditions.checkNotNull;
Jian Li19f25262018-07-03 22:37:12 +090032
33/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090034 * Default implementation of an immutable OpenstackVtap.
Jian Li19f25262018-07-03 22:37:12 +090035 */
36public final class DefaultOpenstackVtap extends AbstractDescription implements OpenstackVtap {
37
38 private final OpenstackVtapId id;
39 private final Type type;
Jimo Jung14e87bf2018-09-03 16:28:13 +090040 private final OpenstackVtapCriterion vtapCriterion;
Jian Li19f25262018-07-03 22:37:12 +090041 private final Set<DeviceId> txDeviceIds;
42 private final Set<DeviceId> rxDeviceIds;
43
Jimo Jung14e87bf2018-09-03 16:28:13 +090044 /**
45 * Creates an DefaultOpenstackVtap using the supplied information.
46 *
47 * @param id vtap identifier
48 * @param type type of vtap (all,rx,tx)
49 * @param vtapCriterion criterion of vtap
50 * @param txDeviceIds device identifiers applied by vtap tx
51 * @param rxDeviceIds device identifiers applied by vtap rx
52 * @param annotations optional key/value annotations
53 */
54 private DefaultOpenstackVtap(OpenstackVtapId id,
55 Type type,
56 OpenstackVtapCriterion vtapCriterion,
57 Set<DeviceId> txDeviceIds,
58 Set<DeviceId> rxDeviceIds,
Jian Li19f25262018-07-03 22:37:12 +090059 SparseAnnotations... annotations) {
60 super(annotations);
Jimo Jung14e87bf2018-09-03 16:28:13 +090061 this.id = checkNotNull(id);
62 this.type = checkNotNull(type);
63 this.vtapCriterion = checkNotNull(vtapCriterion);
64 this.txDeviceIds = Objects.nonNull(txDeviceIds) ?
65 ImmutableSet.copyOf(txDeviceIds) : ImmutableSet.of();
66 this.rxDeviceIds = Objects.nonNull(rxDeviceIds) ?
67 ImmutableSet.copyOf(rxDeviceIds) : ImmutableSet.of();
Jian Li19f25262018-07-03 22:37:12 +090068 }
69
70 @Override
71 public OpenstackVtapId id() {
72 return id;
73 }
74
75 @Override
76 public Type type() {
77 return type;
78 }
79
80 @Override
Jimo Jung14e87bf2018-09-03 16:28:13 +090081 public OpenstackVtapCriterion vtapCriterion() {
82 return vtapCriterion;
Jian Li19f25262018-07-03 22:37:12 +090083 }
84
85 @Override
86 public Set<DeviceId> txDeviceIds() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090087 return txDeviceIds;
Jian Li19f25262018-07-03 22:37:12 +090088 }
89
90 @Override
91 public Set<DeviceId> rxDeviceIds() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090092 return rxDeviceIds;
93 }
94
95 @Override
96 public int hashCode() {
97 return Objects.hash(id, type, vtapCriterion, txDeviceIds, rxDeviceIds);
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (obj instanceof DefaultOpenstackVtap) {
106 final DefaultOpenstackVtap other = (DefaultOpenstackVtap) obj;
107 return Objects.equals(this.id, other.id) &&
108 Objects.equals(this.type, other.type) &&
109 Objects.equals(this.vtapCriterion, other.vtapCriterion) &&
110 Objects.equals(this.txDeviceIds(), other.txDeviceIds()) &&
111 Objects.equals(this.rxDeviceIds(), other.rxDeviceIds()) &&
112 Objects.equals(this.annotations(), other.annotations());
113 }
114 return false;
Jian Li19f25262018-07-03 22:37:12 +0900115 }
116
117 @Override
118 public String toString() {
119 return toStringHelper(this)
Jimo Jung14e87bf2018-09-03 16:28:13 +0900120 .add("id", id())
121 .add("type", type())
122 .add("vtapCriterion", vtapCriterion())
123 .add("txDeviceIds", txDeviceIds())
124 .add("rxDeviceIds", rxDeviceIds())
125 .add("annotations", annotations())
Jian Li19f25262018-07-03 22:37:12 +0900126 .toString();
127 }
128
Jian Li19f25262018-07-03 22:37:12 +0900129 /**
Jimo Jung14e87bf2018-09-03 16:28:13 +0900130 * Creates OpenstackVtap builder with default parameters.
Jian Li19f25262018-07-03 22:37:12 +0900131 *
Jimo Jung14e87bf2018-09-03 16:28:13 +0900132 * @return builder
Jian Li19f25262018-07-03 22:37:12 +0900133 */
134 public static Builder builder() {
135 return new Builder();
136 }
137
138 /**
Jimo Jung14e87bf2018-09-03 16:28:13 +0900139 * Creates OpenstackVtap builder inheriting with default parameters,
140 * from specified OpenstackVtap.
141 *
142 * @param vtap to inherit default from
143 * @return builder
144 */
145 public static Builder builder(OpenstackVtap vtap) {
146 return new Builder(vtap);
147 }
148
149 /**
Jian Li19f25262018-07-03 22:37:12 +0900150 * Builder for DefaultOpenstackVtap object.
151 */
152 public static class Builder implements OpenstackVtap.Builder {
Jian Li19f25262018-07-03 22:37:12 +0900153 private OpenstackVtapId id;
Jimo Jung14e87bf2018-09-03 16:28:13 +0900154 private Type type = Type.VTAP_ALL;
155 private OpenstackVtapCriterion vtapCriterion;
Jian Li19f25262018-07-03 22:37:12 +0900156 private Set<DeviceId> txDeviceIds;
157 private Set<DeviceId> rxDeviceIds;
Jimo Jung14e87bf2018-09-03 16:28:13 +0900158 private SparseAnnotations annotations = DefaultAnnotations.EMPTY;
Jian Li19f25262018-07-03 22:37:12 +0900159
Jimo Jung14e87bf2018-09-03 16:28:13 +0900160 // Private constructor not intended to use from external
Jian Li19f25262018-07-03 22:37:12 +0900161 Builder() {
162 }
163
Jimo Jung14e87bf2018-09-03 16:28:13 +0900164 Builder(OpenstackVtap description) {
165 this.id = description.id();
166 this.type = description.type();
167 this.vtapCriterion = description.vtapCriterion();
168 this.type = description.type();
169 this.txDeviceIds = description.txDeviceIds();
170 this.rxDeviceIds = description.rxDeviceIds();
171 this.annotations = (SparseAnnotations) description.annotations();
172 }
173
174 /**
175 * Sets mandatory field id.
176 *
177 * @param id to set
178 * @return self
179 */
Jian Li19f25262018-07-03 22:37:12 +0900180 @Override
181 public Builder id(OpenstackVtapId id) {
182 this.id = id;
183 return this;
184 }
185
Jimo Jung14e87bf2018-09-03 16:28:13 +0900186 /**
187 * Sets mandatory field type.
188 *
189 * @param type of the vtap
190 * @return self
191 */
Jian Li19f25262018-07-03 22:37:12 +0900192 @Override
193 public Builder type(Type type) {
194 this.type = type;
195 return this;
196 }
197
Jimo Jung14e87bf2018-09-03 16:28:13 +0900198 /**
199 * Sets mandatory field criterion.
200 *
201 * @param vtapCriterion for the vtap
202 * @return self
203 */
Jian Li19f25262018-07-03 22:37:12 +0900204 @Override
Jimo Jung14e87bf2018-09-03 16:28:13 +0900205 public Builder vtapCriterion(OpenstackVtapCriterion vtapCriterion) {
206 this.vtapCriterion = vtapCriterion;
Jian Li19f25262018-07-03 22:37:12 +0900207 return this;
208 }
209
Jimo Jung14e87bf2018-09-03 16:28:13 +0900210 /**
211 * Sets a tx deviceId set.
212 *
213 * @param txDeviceIds deviceId set for tx
214 * @return builder
215 */
Jian Li19f25262018-07-03 22:37:12 +0900216 @Override
217 public Builder txDeviceIds(Set<DeviceId> txDeviceIds) {
Jimo Jung14e87bf2018-09-03 16:28:13 +0900218 this.txDeviceIds = txDeviceIds;
Jian Li19f25262018-07-03 22:37:12 +0900219 return this;
220 }
221
Jimo Jung14e87bf2018-09-03 16:28:13 +0900222 /**
223 * Sets a rx deviceId set.
224 *
225 * @param rxDeviceIds deviceId set for rx
226 * @return builder
227 */
Jian Li19f25262018-07-03 22:37:12 +0900228 @Override
229 public Builder rxDeviceIds(Set<DeviceId> rxDeviceIds) {
Jimo Jung14e87bf2018-09-03 16:28:13 +0900230 this.rxDeviceIds = rxDeviceIds;
Jian Li19f25262018-07-03 22:37:12 +0900231 return this;
232 }
233
Jimo Jung14e87bf2018-09-03 16:28:13 +0900234 /**
235 * Sets annotations.
236 *
237 * @param annotations of the vtap
238 * @return self
239 */
Jian Li19f25262018-07-03 22:37:12 +0900240 @Override
Jimo Jung14e87bf2018-09-03 16:28:13 +0900241 public Builder annotations(SparseAnnotations annotations) {
242 this.annotations = annotations;
Jian Li19f25262018-07-03 22:37:12 +0900243 return this;
244 }
245
Jimo Jung14e87bf2018-09-03 16:28:13 +0900246 /**
247 * Builds a DefaultOpenstackVtap instance.
248 *
249 * @return DefaultOpenstackVtap
250 */
Jian Li19f25262018-07-03 22:37:12 +0900251 @Override
252 public DefaultOpenstackVtap build() {
Jimo Jung14e87bf2018-09-03 16:28:13 +0900253 return new DefaultOpenstackVtap(checkNotNull(id),
254 checkNotNull(type),
255 checkNotNull(vtapCriterion),
256 txDeviceIds,
257 rxDeviceIds,
258 checkNotNull(annotations));
Jian Li19f25262018-07-03 22:37:12 +0900259 }
260 }
261
262}