blob: 9dae6e96d20a904bdba47c0c31bfb5472f43af97 [file] [log] [blame]
Hyunsun Moon4c396632016-05-13 04:17:53 -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.onosproject.xosclient.api;
17
18import com.google.common.base.MoreObjects;
Hyunsun Moon65040992016-07-27 18:41:34 -070019import com.google.common.collect.ImmutableMap;
Hyunsun Moon4c396632016-05-13 04:17:53 -070020import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22
23import java.util.Map;
24import java.util.Objects;
25
Hyunsun Moon65040992016-07-27 18:41:34 -070026import static com.google.common.base.Preconditions.checkNotNull;
27
Hyunsun Moon4c396632016-05-13 04:17:53 -070028/**
29 * Representation of port in a CORD VTN controlled network, it can be for VM
30 * or container.
31 */
32public final class VtnPort {
33
34 private final VtnPortId id;
35 private final String name;
36 private final VtnServiceId serviceId;
37 private final MacAddress mac;
38 private final IpAddress ip;
39 // TODO remove this when XOS provides vSG information
40 private final Map<IpAddress, MacAddress> addressPairs;
41
Hyunsun Moon65040992016-07-27 18:41:34 -070042 private VtnPort(VtnPortId id,
43 String name,
44 VtnServiceId serviceId,
45 MacAddress mac,
46 IpAddress ip,
47 Map<IpAddress, MacAddress> addressPairs) {
Hyunsun Moon4c396632016-05-13 04:17:53 -070048 this.id = id;
49 this.name = name;
50 this.serviceId = serviceId;
51 this.mac = mac;
52 this.ip = ip;
53 this.addressPairs = addressPairs;
54 }
55
56 /**
57 * Returns vtn port ID.
58 *
59 * @return vtn port id
60 */
61 public VtnPortId id() {
62 return id;
63 }
64
65 /**
66 * Returns vtn port name.
67 *
68 * @return vtn port name
69 */
70 public String name() {
71 return name;
72 }
73
74 /**
75 * Returns the ID of the service this port is in.
76 *
77 * @return vtn service id
78 */
79 public VtnServiceId serviceId() {
80 return serviceId;
81 }
82
83 /**
84 * Returns MAC address of this port.
85 *
86 * @return mac address
87 */
88 public MacAddress mac() {
89 return mac;
90 }
91
92 /**
93 * Returns IP address of this port.
94 *
95 * @return ip address
96 */
97 public IpAddress ip() {
98 return ip;
99 }
100
101 /**
102 * Returns address pairs of the nested containers inside.
103 *
104 * @return map of ip and address
105 */
106 public Map<IpAddress, MacAddress> addressPairs() {
107 return addressPairs;
108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(id);
113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (!(obj instanceof VtnPort)) {
121 return false;
122 }
123 final VtnPort other = (VtnPort) obj;
124 return Objects.equals(this.id, other.id);
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(this)
130 .add("id", id)
131 .add("name", name)
132 .add("serviceId", serviceId)
133 .add("mac", mac)
134 .add("ip", ip)
135 .add("addressPairs", addressPairs)
136 .toString();
137 }
Hyunsun Moon65040992016-07-27 18:41:34 -0700138
139 /**
140 * Returns a new vtn port builder instance.
141 *
142 * @return new vtn port builder
143 */
144 public static final Builder builder() {
145 return new Builder();
146 }
147
148 /**
149 * Builder of VTN port entities.
150 */
151 public static final class Builder {
152
153 private VtnPortId id;
154 private String name;
155 private VtnServiceId serviceId;
156 private MacAddress mac;
157 private IpAddress ip;
158 // TODO remove this when XOS provides vSG information
159 private Map<IpAddress, MacAddress> addressPairs;
160
161 private Builder() {
162 }
163
164 /**
165 * Builds an immutable VTN port.
166 *
167 * @return vtn port instance
168 */
169 public VtnPort build() {
170 checkNotNull(id, "VTN port ID cannot be null");
171 checkNotNull(serviceId, "VTN port service ID cannot be null");
172 checkNotNull(mac, "VTN port MAC address cannot be null");
173 checkNotNull(ip, "VTN port IP address cannot be null");
174 addressPairs = addressPairs == null ? ImmutableMap.of() : addressPairs;
175
176 return new VtnPort(id,
177 name,
178 serviceId,
179 mac,
180 ip,
181 addressPairs);
182 }
183
184 /**
185 * Returns VTN port builder with the supplied port ID.
186 *
187 * @param id port identifier
188 * @return vtn port builder
189 */
190 public Builder id(VtnPortId id) {
191 this.id = id;
192 return this;
193 }
194
195 /**
196 * Returns VTN port builder with the supplied port name.
197 * Port name can be null.
198 *
199 * @param name port name
200 * @return vtn port builder
201 */
202 public Builder name(String name) {
203 this.name = name;
204 return this;
205 }
206
207 /**
208 * Returns VTN port builder with the supplied service ID.
209 *
210 * @param serviceId vtn port service id
211 * @return vtn port builder
212 */
213 public Builder serviceId(VtnServiceId serviceId) {
214 this.serviceId = serviceId;
215 return this;
216 }
217
218 /**
219 * Returns VTN port builder with the supplied MAC address.
220 *
221 * @param mac mac address
222 * @return vtn port builder
223 */
224 public Builder mac(MacAddress mac) {
225 if (mac == null) {
226 final String msg = "VTN port MAC address cannot be null";
227 throw new IllegalArgumentException(msg);
228 }
229 this.mac = mac;
230 return this;
231 }
232
233 /**
234 * Returns VTN port builder with the supplied MAC address.
235 *
236 * @param mac mac address as a string
237 * @return vtn port builder
238 */
239 public Builder mac(String mac) {
240 try {
241 return mac(MacAddress.valueOf(mac));
242 } catch (IllegalArgumentException | NullPointerException e) {
243 final String msg = "Malformed MAC address string " + mac +
244 " for VTN port MAC address";
245 throw new IllegalArgumentException(msg);
246 }
247 }
248
249 /**
250 * Returns VTN port builder with the supplied IP address.
251 *
252 * @param ip ip address
253 * @return vtn port builder
254 */
255 public Builder ip(IpAddress ip) {
256 if (ip == null) {
257 final String msg = "VTN port IP address cannot be null";
258 throw new IllegalArgumentException(msg);
259 }
260 this.ip = ip;
261 return this;
262 }
263
264 /**
265 * Returns VTN port builder with the supplied IP address.
266 *
267 * @param ip ip address as a string
268 * @return vtn port builder
269 */
270 public Builder ip(String ip) {
271 try {
272 return ip(IpAddress.valueOf(ip));
273 } catch (IllegalArgumentException | NullPointerException e) {
274 final String msg = "Malformed IP address string " + ip +
275 " for VTN port IP address";
276 throw new IllegalArgumentException(msg);
277 }
278 }
279
280 /**
281 * Returns VTN port builder with the supplied address pairs.
282 *
283 * @param addressPairs address pairs
284 * @return vtn port builder
285 */
286 public Builder addressPairs(Map<IpAddress, MacAddress> addressPairs) {
287 if (addressPairs == null) {
288 final String msg = "VTN address pairs cannot be null";
289 throw new IllegalArgumentException(msg);
290 }
291 this.addressPairs = addressPairs;
292 return this;
293 }
294 }
Hyunsun Moon4c396632016-05-13 04:17:53 -0700295}