blob: 1584a3a3dfc8dc23d2f9fb04021159097cede633 [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();
Jimo Jung14e87bf2018-09-03 16:28:13 +0900168 this.txDeviceIds = description.txDeviceIds();
169 this.rxDeviceIds = description.rxDeviceIds();
170 this.annotations = (SparseAnnotations) description.annotations();
171 }
172
173 /**
174 * Sets mandatory field id.
175 *
176 * @param id to set
177 * @return self
178 */
Jian Li19f25262018-07-03 22:37:12 +0900179 @Override
180 public Builder id(OpenstackVtapId id) {
181 this.id = id;
182 return this;
183 }
184
Jimo Jung14e87bf2018-09-03 16:28:13 +0900185 /**
186 * Sets mandatory field type.
187 *
188 * @param type of the vtap
189 * @return self
190 */
Jian Li19f25262018-07-03 22:37:12 +0900191 @Override
192 public Builder type(Type type) {
193 this.type = type;
194 return this;
195 }
196
Jimo Jung14e87bf2018-09-03 16:28:13 +0900197 /**
198 * Sets mandatory field criterion.
199 *
200 * @param vtapCriterion for the vtap
201 * @return self
202 */
Jian Li19f25262018-07-03 22:37:12 +0900203 @Override
Jimo Jung14e87bf2018-09-03 16:28:13 +0900204 public Builder vtapCriterion(OpenstackVtapCriterion vtapCriterion) {
205 this.vtapCriterion = vtapCriterion;
Jian Li19f25262018-07-03 22:37:12 +0900206 return this;
207 }
208
Jimo Jung14e87bf2018-09-03 16:28:13 +0900209 /**
210 * Sets a tx deviceId set.
211 *
212 * @param txDeviceIds deviceId set for tx
213 * @return builder
214 */
Jian Li19f25262018-07-03 22:37:12 +0900215 @Override
216 public Builder txDeviceIds(Set<DeviceId> txDeviceIds) {
Jimo Jung14e87bf2018-09-03 16:28:13 +0900217 this.txDeviceIds = txDeviceIds;
Jian Li19f25262018-07-03 22:37:12 +0900218 return this;
219 }
220
Jimo Jung14e87bf2018-09-03 16:28:13 +0900221 /**
222 * Sets a rx deviceId set.
223 *
224 * @param rxDeviceIds deviceId set for rx
225 * @return builder
226 */
Jian Li19f25262018-07-03 22:37:12 +0900227 @Override
228 public Builder rxDeviceIds(Set<DeviceId> rxDeviceIds) {
Jimo Jung14e87bf2018-09-03 16:28:13 +0900229 this.rxDeviceIds = rxDeviceIds;
Jian Li19f25262018-07-03 22:37:12 +0900230 return this;
231 }
232
Jimo Jung14e87bf2018-09-03 16:28:13 +0900233 /**
234 * Sets annotations.
235 *
236 * @param annotations of the vtap
237 * @return self
238 */
Jian Li19f25262018-07-03 22:37:12 +0900239 @Override
Jimo Jung14e87bf2018-09-03 16:28:13 +0900240 public Builder annotations(SparseAnnotations annotations) {
241 this.annotations = annotations;
Jian Li19f25262018-07-03 22:37:12 +0900242 return this;
243 }
244
Jimo Jung14e87bf2018-09-03 16:28:13 +0900245 /**
246 * Builds a DefaultOpenstackVtap instance.
247 *
248 * @return DefaultOpenstackVtap
249 */
Jian Li19f25262018-07-03 22:37:12 +0900250 @Override
251 public DefaultOpenstackVtap build() {
Jimo Jung14e87bf2018-09-03 16:28:13 +0900252 return new DefaultOpenstackVtap(checkNotNull(id),
253 checkNotNull(type),
254 checkNotNull(vtapCriterion),
255 txDeviceIds,
256 rxDeviceIds,
257 checkNotNull(annotations));
Jian Li19f25262018-07-03 22:37:12 +0900258 }
259 }
260
261}