blob: 88eafd25a99e5d41c47f3039137ebf9776d22a66 [file] [log] [blame]
Jian Li3ff327a2017-03-14 17:02:19 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li3ff327a2017-03-14 17:02:19 +09003 *
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 */
16
17package org.onosproject.drivers.lisp.extensions;
18
Jian Li3ddd5532017-03-18 02:58:23 +090019import com.google.common.collect.Maps;
Jian Li3ff327a2017-03-14 17:02:19 +090020import org.onosproject.mapping.addresses.ExtensionMappingAddress;
21import org.onosproject.mapping.addresses.ExtensionMappingAddressType;
Jian Li3ddd5532017-03-18 02:58:23 +090022import org.onosproject.mapping.addresses.MappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090023import org.onosproject.net.flow.AbstractExtension;
24
Jian Li3ddd5532017-03-18 02:58:23 +090025import java.util.Map;
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
Jian Li299bc1d2017-03-17 19:17:40 +090029import static org.onosproject.mapping.addresses.ExtensionMappingAddressType
Jian Li3ddd5532017-03-18 02:58:23 +090030 .ExtensionMappingAddressTypes.GEO_COORDINATE_ADDRESS;
Jian Li299bc1d2017-03-17 19:17:40 +090031
Jian Li3ff327a2017-03-14 17:02:19 +090032/**
33 * Implementation of LISP Geo Coordinate (GC) address.
Jian Li299bc1d2017-03-17 19:17:40 +090034 * If an ETR desires to send a Map-Reply describing the Geo Coordinates for each
35 * locator in its locator-set, it can use the Geo Coordinate Type to convey
36 * physical location information.
Jian Li3ff327a2017-03-14 17:02:19 +090037 */
38public class LispGcAddress extends AbstractExtension
Jian Li3ddd5532017-03-18 02:58:23 +090039 implements ExtensionMappingAddress {
40
41 private static final String NORTH = "north";
42 private static final String LATITUDE_DEGREE = "latitudeDegree";
43 private static final String LATITUDE_MINUTE = "latitudeMinute";
44 private static final String LATITUDE_SECOND = "latitudeSecond";
45 private static final String EAST = "east";
46 private static final String LONGITUDE_DEGREE = "longitudeDegree";
47 private static final String LONGITUDE_MINUTE = "longitudeMinute";
48 private static final String LONGITUDE_SECOND = "longitudeSecond";
49 private static final String ALTITUDE = "altitude";
50 private static final String ADDRESS = "address";
51
52 private boolean north;
53 private short latitudeDegree;
54 private byte latitudeMinute;
55 private byte latitudeSecond;
56 private boolean east;
57 private short longitudeDegree;
58 private byte longitudeMinute;
59 private byte longitudeSecond;
60 private int altitude;
61 private MappingAddress address;
62
Jian Li3ddd5532017-03-18 02:58:23 +090063 /**
64 * Default constructor.
65 */
66 public LispGcAddress() {
67 }
68
69 /**
70 * Creates an instance with initialized parameters.
71 *
72 * @param north north flag
73 * @param latitudeDegree latitude degree
74 * @param latitudeMinute latitude minute
75 * @param latitudeSecond latitude second
76 * @param east east flag
77 * @param longitudeDegree longitude degree
78 * @param longitudeMinute longitude minute
79 * @param longitudeSecond longitude second
80 * @param altitude altitude
81 * @param address mapping address
82 */
83 private LispGcAddress(boolean north, short latitudeDegree, byte latitudeMinute,
84 byte latitudeSecond, boolean east, short longitudeDegree,
85 byte longitudeMinute, byte longitudeSecond, int altitude,
86 MappingAddress address) {
87 this.north = north;
88 this.latitudeDegree = latitudeDegree;
89 this.latitudeMinute = latitudeMinute;
90 this.latitudeSecond = latitudeSecond;
91 this.east = east;
92 this.longitudeDegree = longitudeDegree;
93 this.longitudeMinute = longitudeMinute;
94 this.longitudeSecond = longitudeSecond;
95 this.altitude = altitude;
96 this.address = address;
97 }
98
99 /**
100 * Obtains north flag value.
101 *
102 * @return north flag value
103 */
104 public boolean isNorth() {
105 return north;
106 }
107
108 /**
109 * Obtains latitude degree.
110 *
111 * @return latitude degree
112 */
113 public short getLatitudeDegree() {
114 return latitudeDegree;
115 }
116
117 /**
118 * Obtains latitude minute.
119 *
120 * @return latitude minute
121 */
122 public byte getLatitudeMinute() {
123 return latitudeMinute;
124 }
125
126 /**
127 * Obtains latitude second.
128 *
129 * @return latitude second
130 */
131 public byte getLatitudeSecond() {
132 return latitudeSecond;
133 }
134
135 /**
136 * Obtains east flag value.
137 *
138 * @return east flag vlaue
139 */
140 public boolean isEast() {
141 return east;
142 }
143
144 /**
145 * Obtains longitude degree.
146 *
147 * @return longitude degree
148 */
149 public short getLongitudeDegree() {
150 return longitudeDegree;
151 }
152
153 /**
154 * Obtains longitude minute.
155 *
156 * @return longitude minute
157 */
158 public byte getLongitudeMinute() {
159 return longitudeMinute;
160 }
161
162 /**
163 * Obtains longitude second.
164 *
165 * @return longitude second
166 */
167 public byte getLongitudeSecond() {
168 return longitudeSecond;
169 }
170
171 /**
172 * Obtains altitude.
173 *
174 * @return altitude
175 */
176 public int getAltitude() {
177 return altitude;
178 }
179
180 /**
181 * Obtains mapping address.
182 *
183 * @return mapping address
184 */
185 public MappingAddress getAddress() {
186 return address;
187 }
188
Jian Li3ff327a2017-03-14 17:02:19 +0900189 @Override
190 public ExtensionMappingAddressType type() {
Jian Li299bc1d2017-03-17 19:17:40 +0900191 return GEO_COORDINATE_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +0900192 }
193
194 @Override
195 public byte[] serialize() {
Jian Li3ddd5532017-03-18 02:58:23 +0900196 Map<String, Object> parameterMap = Maps.newHashMap();
197
198 parameterMap.put(NORTH, north);
199 parameterMap.put(LATITUDE_DEGREE, latitudeDegree);
200 parameterMap.put(LATITUDE_MINUTE, latitudeMinute);
201 parameterMap.put(LATITUDE_SECOND, latitudeSecond);
202 parameterMap.put(EAST, east);
203 parameterMap.put(LONGITUDE_DEGREE, longitudeDegree);
204 parameterMap.put(LONGITUDE_MINUTE, longitudeMinute);
205 parameterMap.put(LONGITUDE_SECOND, longitudeSecond);
206 parameterMap.put(ALTITUDE, altitude);
207 parameterMap.put(ADDRESS, address);
208
Jian Li924995b2017-03-18 12:34:46 +0900209 return APP_KRYO.serialize(parameterMap);
Jian Li3ff327a2017-03-14 17:02:19 +0900210 }
211
212 @Override
213 public void deserialize(byte[] data) {
Jian Li924995b2017-03-18 12:34:46 +0900214 Map<String, Object> parameterMap = APP_KRYO.deserialize(data);
Jian Li3ff327a2017-03-14 17:02:19 +0900215
Jian Li3ddd5532017-03-18 02:58:23 +0900216 this.north = (boolean) parameterMap.get(NORTH);
217 this.latitudeDegree = (short) parameterMap.get(LATITUDE_DEGREE);
218 this.latitudeMinute = (byte) parameterMap.get(LATITUDE_MINUTE);
219 this.latitudeSecond = (byte) parameterMap.get(LATITUDE_SECOND);
220 this.east = (boolean) parameterMap.get(EAST);
221 this.longitudeDegree = (short) parameterMap.get(LONGITUDE_DEGREE);
222 this.longitudeMinute = (byte) parameterMap.get(LONGITUDE_MINUTE);
223 this.longitudeSecond = (byte) parameterMap.get(LONGITUDE_SECOND);
224 this.altitude = (int) parameterMap.get(ALTITUDE);
225 this.address = (MappingAddress) parameterMap.get(ADDRESS);
226 }
227
228 @Override
229 public int hashCode() {
230 return Objects.hash(north, latitudeDegree, latitudeMinute, latitudeSecond,
231 east, longitudeDegree, longitudeMinute, longitudeSecond,
232 altitude, address);
233 }
234
235 @Override
236 public boolean equals(Object obj) {
237 if (this == obj) {
238 return true;
239 }
240
241 if (obj instanceof LispGcAddress) {
242 final LispGcAddress other = (LispGcAddress) obj;
243 return Objects.equals(this.north, other.north) &&
244 Objects.equals(this.latitudeDegree, other.latitudeDegree) &&
245 Objects.equals(this.latitudeMinute, other.latitudeMinute) &&
246 Objects.equals(this.latitudeSecond, other.latitudeSecond) &&
247 Objects.equals(this.east, other.east) &&
248 Objects.equals(this.longitudeDegree, other.longitudeDegree) &&
249 Objects.equals(this.longitudeMinute, other.longitudeMinute) &&
250 Objects.equals(this.longitudeSecond, other.longitudeSecond) &&
251 Objects.equals(this.altitude, other.altitude) &&
252 Objects.equals(this.address, other.address);
253 }
254 return false;
255 }
256
257 @Override
258 public String toString() {
Jian Lifc90a082017-03-31 23:36:14 +0900259 return toStringHelper(type().toString())
Jian Li3ddd5532017-03-18 02:58:23 +0900260 .add("north", north)
261 .add("latitude degree", latitudeDegree)
262 .add("latitude minute", latitudeMinute)
263 .add("latitude second", latitudeSecond)
264 .add("east", east)
265 .add("longitude degree", longitudeDegree)
266 .add("longitude minute", longitudeMinute)
267 .add("longitude second", longitudeSecond)
268 .add("altitude", altitude)
269 .add("address", address)
270 .toString();
271 }
272
273 /**
274 * A builder for building LispGcAddress.
275 */
276 public static final class Builder {
277 private boolean north;
278 private short latitudeDegree;
279 private byte latitudeMinute;
280 private byte latitudeSecond;
281 private boolean east;
282 private short longitudeDegree;
283 private byte longitudeMinute;
284 private byte longitudeSecond;
285 private int altitude;
286 private MappingAddress address;
287
288 /**
289 * Sets north flag value.
290 *
291 * @param north north flag value
292 * @return Builder object
293 */
294 public Builder withIsNorth(boolean north) {
295 this.north = north;
296 return this;
297 }
298
299 /**
300 * Sets latitude degree.
301 *
302 * @param latitudeDegree latitude degree
303 * @return Builder object
304 */
305 public Builder withLatitudeDegree(short latitudeDegree) {
306 this.latitudeDegree = latitudeDegree;
307 return this;
308 }
309
310 /**
311 * Sets latitude minute.
312 *
313 * @param latitudeMinute latitude minute
314 * @return Builder object
315 */
316 public Builder withLatitudeMinute(byte latitudeMinute) {
317 this.latitudeMinute = latitudeMinute;
318 return this;
319 }
320
321 /**
322 * Sets latitude second.
323 *
324 * @param latitudeSecond latitude second
325 * @return Builder object
326 */
327 public Builder withLatitudeSecond(byte latitudeSecond) {
328 this.latitudeSecond = latitudeSecond;
329 return this;
330 }
331
332 /**
333 * Sets east flag value.
334 *
335 * @param east east flag
336 * @return Builder object
337 */
338 public Builder withIsEast(boolean east) {
339 this.east = east;
340 return this;
341 }
342
343 /**
344 * Sets longitude degree.
345 *
346 * @param longitudeDegree longitude degree
347 * @return Builder object
348 */
349 public Builder withLongitudeDegree(short longitudeDegree) {
350 this.longitudeDegree = longitudeDegree;
351 return this;
352 }
353
354 /**
355 * Sets longitude minute.
356 *
357 * @param longitudeMinute longitude minute
358 * @return Builder object
359 */
360 public Builder withLongitudeMinute(byte longitudeMinute) {
361 this.longitudeMinute = longitudeMinute;
362 return this;
363 }
364
365 /**
366 * Sets longitude second.
367 *
368 * @param longitudeSecond longitude second
369 * @return Builder object
370 */
371 public Builder withLongitudeSecond(byte longitudeSecond) {
372 this.longitudeSecond = longitudeSecond;
373 return this;
374 }
375
376 /**
377 * Sets altitude.
378 *
379 * @param altitude altitude
380 * @return Builder object
381 */
382 public Builder withAltitude(int altitude) {
383 this.altitude = altitude;
384 return this;
385 }
386
387 /**
388 * Sets mapping address.
389 *
Frank Wang3a98e0a2017-08-11 11:09:30 +0800390 * @param address mapping address
Jian Li3ddd5532017-03-18 02:58:23 +0900391 * @return Builder object
392 */
393 public Builder withAddress(MappingAddress address) {
394 this.address = address;
395 return this;
396 }
397
398 /**
399 * Builds LispGcAddress instance.
400 *
401 * @return LispGcAddress instance
402 */
403 public LispGcAddress build() {
404
405 return new LispGcAddress(north, latitudeDegree,
406 latitudeMinute, latitudeSecond, east, longitudeDegree,
407 longitudeMinute, longitudeSecond, altitude, address);
408 }
Jian Li3ff327a2017-03-14 17:02:19 +0900409 }
410}