blob: a7d83859a67bf808a97214764afc57276c9acf99 [file] [log] [blame]
Hyunsun Mooneaf75e62016-09-27 16:40:23 -07001/*
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 */
16package org.opencord.cordvtn.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.openstack4j.model.network.Port;
25
26import java.util.Objects;
27import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Representation of the port containing all port information consumed by VTN service.
34 */
35public final class VtnPort extends ServicePort {
36
37 private static final String ERR_IP_MISSING = "VTN port IP adderess is missing";
38
39 private final NetworkId netId;
40 private final MacAddress mac;
41 private final IpAddress ip;
42
43 private VtnPort(PortId id,
44 NetworkId netId,
45 MacAddress mac,
46 IpAddress ip,
47 VlanId vlanId,
48 Set<AddressPair> addressPairs) {
49 super(id, vlanId, addressPairs);
50 this.netId = netId;
51 this.mac = mac;
52 this.ip = ip;
53 }
54
55 /**
56 * Returns the network ID of this port.
57 *
58 * @return network id
59 */
60 public NetworkId netId() {
61 return netId;
62 }
63
64 /**
65 * Returns the MAC address of this port.
66 *
67 * @return mac address
68 */
69 public MacAddress mac() {
70 return mac;
71 }
72
73 /**
74 * Returns the IP address of this port.
75 *
76 * @return ip address
77 */
78 public IpAddress ip() {
79 return ip;
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87
88 if (obj instanceof VtnPort) {
89 VtnPort that = (VtnPort) obj;
90 if (Objects.equals(id, that.id) &&
91 Objects.equals(netId, that.netId) &&
92 Objects.equals(mac, that.mac) &&
93 Objects.equals(ip, that.ip) &&
94 Objects.equals(vlanId, that.vlanId) &&
95 Objects.equals(addressPairs, that.addressPairs)) {
96 return true;
97 }
98 }
99 return false;
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(id, netId, mac, ip, vlanId, addressPairs);
105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(getClass())
110 .add("id", id)
111 .add("netId", netId)
112 .add("mac", mac)
113 .add("ip", ip)
114 .add("vlanId", vlanId)
115 .add("addressPairs", addressPairs)
116 .toString();
117 }
118
119 /**
120 * Returns the immutable VTN port with the supplied Neutron port with additional
121 * vtn port information.
122 *
123 * @param port neutron port
124 * @param servicePort vtn port
125 * @return vtn port
126 */
127 public static VtnPort of(Port port, ServicePort servicePort) {
128 validateNeutronPort(port);
129 if (servicePort != null) {
130 checkArgument(Objects.equals(port.getId(), servicePort.id().id()));
131 }
132
133 return builder().id(PortId.of(port.getId()))
134 .netId(NetworkId.of(port.getNetworkId()))
135 .mac(MacAddress.valueOf(port.getMacAddress()))
136 .ip(IpAddress.valueOf(port.getFixedIps().iterator().next().getIpAddress()))
137 .vlanId(servicePort == null ? null : servicePort.vlanId().orElse(null))
138 .addressPairs(servicePort == null ? ImmutableSet.of() : servicePort.addressPairs())
139 .build();
140 }
141
142 private static void validateNeutronPort(Port port) {
143 checkNotNull(port);
144 checkArgument(port.getFixedIps().size() > 0, ERR_IP_MISSING);
145 }
146
147 /**
148 * Returns the immutable VTN port with the supplied VTN port with additional
149 * vtn port information.
150 *
151 * @param vtnPort vtn port
152 * @param servicePort vtn port
153 * @return vtn port
154 */
155 public static VtnPort of(VtnPort vtnPort, ServicePort servicePort) {
156 return builder(vtnPort)
157 .vlanId(servicePort.vlanId().orElse(null))
158 .addressPairs(servicePort.addressPairs())
159 .build();
160 }
161
162 /**
163 * Returns new vtn port builder instance.
164 *
165 * @return vtn port builder
166 */
167 public static Builder builder() {
168 return new Builder();
169 }
170
171 /**
172 * Returns new vtn port builder instance with copy of the supplied vtn port.
173 *
174 * @param vtnPort vtn port
175 * @return vtn port builder
176 */
177 public static Builder builder(VtnPort vtnPort) {
178 return new Builder().id(vtnPort.id())
179 .netId(vtnPort.netId())
180 .mac(vtnPort.mac())
181 .ip(vtnPort.ip())
182 .vlanId(vtnPort.vlanId().orElse(null))
183 .addressPairs(vtnPort.addressPairs());
184 }
185
186 /**
187 * Builder of the vtn port entities.
188 */
189 public static final class Builder {
190 private PortId id;
191 private NetworkId netId;
192 private MacAddress mac;
193 private IpAddress ip;
194 private VlanId vlanId;
195 private Set<AddressPair> addressPairs = ImmutableSet.of();
196
197 private Builder() {
198 }
199
200 /**
201 * Builds an immutable vtn port.
202 *
203 * @return vtn port instance
204 */
205 public VtnPort build() {
206 checkNotNull(id, "VtnPort port id cannot be null");
207 checkNotNull(netId, "VtnPort network id cannot be null");
208 checkNotNull(mac, "VtnPort mac address cannot be null");
209 checkNotNull(ip, "VtnPort ip address cannot be null");
210 addressPairs = addressPairs == null ? ImmutableSet.of() : addressPairs;
211
212 return new VtnPort(id, netId, mac, ip, vlanId, addressPairs);
213 }
214
215 /**
216 * Returns vtn port builder with the supplied port id.
217 *
218 * @param id port id
219 * @return vtn port builder
220 */
221 public Builder id(PortId id) {
222 this.id = id;
223 return this;
224 }
225
226 /**
227 * Returns vtn port builder with the supplied network id.
228 *
229 * @param netId network id
230 * @return vtn port builder
231 */
232 public Builder netId(NetworkId netId) {
233 this.netId = netId;
234 return this;
235 }
236
237 /**
238 * Returns vtn port builder with the supplied mac address.
239 *
240 * @param mac mac address
241 * @return vtn port builder
242 */
243 public Builder mac(MacAddress mac) {
244 this.mac = mac;
245 return this;
246 }
247
248 /**
249 * Returns vtn port builder with the supplied ip address.
250 *
251 * @param ip ip address
252 * @return vtn port builder
253 */
254 public Builder ip(IpAddress ip) {
255 this.ip = ip;
256 return this;
257 }
258
259 /**
260 * Returns vtn port builder with the supplied VLAN ID.
261 *
262 * @param vlanId vlan id
263 * @return vtn port builder
264 */
265 public Builder vlanId(VlanId vlanId) {
266 this.vlanId = vlanId;
267 return this;
268 }
269
270 /**
271 * Returns vtn port builder with the supplied address pairs.
272 *
273 * @param addressPairs set of address pairs
274 * @return vtn port builder
275 */
276 public Builder addressPairs(Set<AddressPair> addressPairs) {
277 this.addressPairs = addressPairs;
278 return this;
279 }
280
281 /**
282 * Returns vtn port builder with the given additional address pair.
283 *
284 * @param addressPair address pair to add
285 * @return vtn port builder
286 */
287 public Builder addAddressPair(AddressPair addressPair) {
288 checkNotNull(addressPair, "VtnPort address pair cannot be null");
289
290 Set<AddressPair> updated = Sets.newHashSet(this.addressPairs);
291 updated.add(addressPair);
292 this.addressPairs = ImmutableSet.copyOf(updated);
293 return this;
294 }
295 }
296}