blob: 7e51be59cc5fbe4492c3fb453483b0f8c76e3af6 [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
18import com.google.common.base.Objects;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
21import org.onosproject.net.AbstractDescription;
22import org.onosproject.net.DefaultAnnotations;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.SparseAnnotations;
25import org.onosproject.openstackvtap.api.OpenstackVtap;
26import org.onosproject.openstackvtap.api.OpenstackVtapCriterion;
27import org.onosproject.openstackvtap.api.OpenstackVtapId;
28
29import java.util.Set;
30
31import static com.google.common.base.MoreObjects.toStringHelper;
32import static com.google.common.base.Preconditions.checkArgument;
33
34/**
35 * Default implementation of an immutable openstack vTap.
36 */
37public final class DefaultOpenstackVtap extends AbstractDescription implements OpenstackVtap {
38
39 private final OpenstackVtapId id;
40 private final Type type;
41 private final OpenstackVtapCriterion vTapCriterion;
42 private final Set<DeviceId> txDeviceIds;
43 private final Set<DeviceId> rxDeviceIds;
44
45 // private constructor not intended to use from external
46 private DefaultOpenstackVtap(OpenstackVtapId id, Type type,
47 OpenstackVtapCriterion vTapCriterion,
48 Set<DeviceId> txDeviceIds, Set<DeviceId> rxDeviceIds,
49 SparseAnnotations... annotations) {
50 super(annotations);
51 this.id = id;
52 this.type = type;
53 this.vTapCriterion = vTapCriterion;
54 this.txDeviceIds = txDeviceIds;
55 this.rxDeviceIds = rxDeviceIds;
56 }
57
58 @Override
59 public OpenstackVtapId id() {
60 return id;
61 }
62
63 @Override
64 public Type type() {
65 return type;
66 }
67
68 @Override
69 public OpenstackVtapCriterion vTapCriterion() {
70 return vTapCriterion;
71 }
72
73 @Override
74 public Set<DeviceId> txDeviceIds() {
75 return ImmutableSet.copyOf(txDeviceIds);
76 }
77
78 @Override
79 public Set<DeviceId> rxDeviceIds() {
80 return ImmutableSet.copyOf(rxDeviceIds);
81 }
82
83 @Override
84 public String toString() {
85 return toStringHelper(this)
86 .add("id", id)
87 .add("type", type)
88 .add("vTapCriterion", vTapCriterion)
89 .add("txDeviceIds", txDeviceIds)
90 .add("rxDeviceIds", rxDeviceIds)
91 .toString();
92 }
93
94 @Override
95 public int hashCode() {
96 return Objects.hashCode(super.hashCode(),
97 id,
98 type,
99 vTapCriterion,
100 txDeviceIds,
101 rxDeviceIds);
102 }
103
104 @Override
105 public boolean equals(Object object) {
106 if (object != null && getClass() == object.getClass()) {
107 if (!super.equals(object)) {
108 return false;
109 }
110 DefaultOpenstackVtap that = (DefaultOpenstackVtap) object;
111 return Objects.equal(this.id, that.id)
112 && Objects.equal(this.type, that.type)
113 && Objects.equal(this.vTapCriterion, that.vTapCriterion)
114 && Objects.equal(this.txDeviceIds, that.txDeviceIds)
115 && Objects.equal(this.rxDeviceIds, that.rxDeviceIds);
116 }
117 return false;
118 }
119
120 /**
121 * Creates a new default openstack vTap builder.
122 *
123 * @return default openstack vTap builder
124 */
125 public static Builder builder() {
126 return new Builder();
127 }
128
129 /**
130 * Builder for DefaultOpenstackVtap object.
131 */
132 public static class Builder implements OpenstackVtap.Builder {
133 private static final SparseAnnotations EMPTY = DefaultAnnotations.builder().build();
134
135 private OpenstackVtapId id;
136 private Type type;
137 private OpenstackVtapCriterion vTapCriterion;
138 private Set<DeviceId> txDeviceIds;
139 private Set<DeviceId> rxDeviceIds;
140 private SparseAnnotations annotations = EMPTY;
141
142 // private constructor not intended to use from external
143 Builder() {
144 }
145
146 @Override
147 public Builder id(OpenstackVtapId id) {
148 this.id = id;
149 return this;
150 }
151
152 @Override
153 public Builder type(Type type) {
154 this.type = type;
155 return this;
156 }
157
158 @Override
159 public Builder vTapCriterion(OpenstackVtapCriterion vTapCriterion) {
160 this.vTapCriterion = vTapCriterion;
161 return this;
162 }
163
164 @Override
165 public Builder txDeviceIds(Set<DeviceId> txDeviceIds) {
166 if (txDeviceIds != null) {
167 this.txDeviceIds = ImmutableSet.copyOf(txDeviceIds);
168 } else {
169 this.txDeviceIds = Sets.newHashSet();
170 }
171 return this;
172 }
173
174 @Override
175 public Builder rxDeviceIds(Set<DeviceId> rxDeviceIds) {
176 if (rxDeviceIds != null) {
177 this.rxDeviceIds = ImmutableSet.copyOf(rxDeviceIds);
178 } else {
179 this.rxDeviceIds = Sets.newHashSet();
180 }
181 return this;
182 }
183
184 @Override
185 public Builder annotations(SparseAnnotations... annotations) {
186 checkArgument(annotations.length <= 1,
187 "Only one set of annotations is expected");
188 this.annotations = annotations.length == 1 ? annotations[0] : EMPTY;
189 return this;
190 }
191
192 @Override
193 public DefaultOpenstackVtap build() {
194 return new DefaultOpenstackVtap(id, type, vTapCriterion,
195 txDeviceIds, rxDeviceIds, annotations);
196 }
197 }
198
199}