blob: ef90abb50dd92cb60c34e38b95eed5895fc3d1bd [file] [log] [blame]
Jian Lifc90a082017-03-31 23:36:14 +09001/*
2 * Copyright 2017-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.drivers.lisp.extensions;
17
Jian Li3fbc9502017-04-11 10:41:25 +090018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lifc90a082017-03-31 23:36:14 +090020import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
Jian Li3fbc9502017-04-11 10:41:25 +090025import org.onosproject.codec.CodecContext;
Jian Lifc90a082017-03-31 23:36:14 +090026import org.onosproject.lisp.ctl.ExtensionMappingAddressInterpreter;
27import org.onosproject.lisp.msg.types.LispAfiAddress;
28import org.onosproject.lisp.msg.types.LispDistinguishedNameAddress;
29import org.onosproject.lisp.msg.types.LispIpv4Address;
30import org.onosproject.lisp.msg.types.LispIpv6Address;
31import org.onosproject.lisp.msg.types.LispMacAddress;
32import org.onosproject.lisp.msg.types.lcaf.LispAppDataLcafAddress;
33import org.onosproject.lisp.msg.types.lcaf.LispAsLcafAddress;
34import org.onosproject.lisp.msg.types.lcaf.LispGeoCoordinateLcafAddress;
35import org.onosproject.lisp.msg.types.lcaf.LispLcafAddress;
36import org.onosproject.lisp.msg.types.lcaf.LispListLcafAddress;
37import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress;
38import org.onosproject.lisp.msg.types.lcaf.LispNatLcafAddress;
39import org.onosproject.lisp.msg.types.lcaf.LispNonceLcafAddress;
40import org.onosproject.lisp.msg.types.lcaf.LispSegmentLcafAddress;
41import org.onosproject.lisp.msg.types.lcaf.LispSourceDestLcafAddress;
42import org.onosproject.lisp.msg.types.lcaf.LispTeLcafAddress;
43import org.onosproject.lisp.msg.types.lcaf.LispTeRecord;
44import org.onosproject.mapping.addresses.ExtensionMappingAddress;
45import org.onosproject.mapping.addresses.ExtensionMappingAddressResolver;
46import org.onosproject.mapping.addresses.ExtensionMappingAddressType;
47import org.onosproject.mapping.addresses.IPMappingAddress;
48import org.onosproject.mapping.addresses.MappingAddress;
49import org.onosproject.mapping.addresses.MappingAddresses;
50import org.onosproject.net.driver.AbstractHandlerBehaviour;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
54import java.util.List;
55
Jian Li3fbc9502017-04-11 10:41:25 +090056import static com.google.common.base.Preconditions.checkNotNull;
57import static org.onlab.util.Tools.nullIsIllegal;
Jian Lifc90a082017-03-31 23:36:14 +090058import static org.onosproject.mapping.addresses.ExtensionMappingAddressType.ExtensionMappingAddressTypes.*;
59/**
60 * Interpreter for mapping address extension.
61 */
62public class LispExtensionMappingAddressInterpreter extends AbstractHandlerBehaviour
63 implements ExtensionMappingAddressInterpreter, ExtensionMappingAddressResolver {
64
65 private static final Logger log = LoggerFactory.getLogger(
66 LispExtensionMappingAddressInterpreter.class);
67
68 private static final int IPV4_PREFIX_LENGTH = 32;
69 private static final int IPV6_PREFIX_LENGTH = 128;
70
Jian Li3fbc9502017-04-11 10:41:25 +090071 protected static final String LISP_LIST_ADDRESS = "listAddress";
72 protected static final String LISP_SEGMENT_ADDRESS = "segmentAddress";
73 protected static final String LISP_AS_ADDRESS = "asAddress";
74 protected static final String LISP_APPLICATION_DATA_ADDRESS = "applicationDataAddress";
75 protected static final String LISP_GEO_COORDINATE_ADDRESS = "geoCoordinateAddress";
76 protected static final String LISP_NAT_ADDRESS = "natAddress";
77 protected static final String LISP_NONCE_ADDRESS = "nonceAddress";
78 protected static final String LISP_MULTICAST_ADDRESS = "multicastAddress";
79 protected static final String LISP_TRAFFIC_ENGINEERING_ADDRESS = "trafficEngineeringAddress";
80 protected static final String LISP_SOURCE_DEST_ADDRESS = "sourceDestAddress";
81
82 private static final String TYPE = "type";
83
84 private static final String MISSING_MEMBER_MESSAGE =
85 " member is required in LispExtensionMappingAddressInterpreter";
86
Jian Lifc90a082017-03-31 23:36:14 +090087 @Override
88 public boolean supported(ExtensionMappingAddressType type) {
89
90 if (type.equals(LIST_ADDRESS.type())) {
91 return true;
92 }
93 if (type.equals(SEGMENT_ADDRESS.type())) {
94 return true;
95 }
96 if (type.equals(AS_ADDRESS.type())) {
97 return true;
98 }
99 if (type.equals(APPLICATION_DATA_ADDRESS.type())) {
100 return true;
101 }
102 if (type.equals(GEO_COORDINATE_ADDRESS.type())) {
103 return true;
104 }
105 if (type.equals(NAT_ADDRESS.type())) {
106 return true;
107 }
108 if (type.equals(NONCE_ADDRESS.type())) {
109 return true;
110 }
111 if (type.equals(MULTICAST_ADDRESS.type())) {
112 return true;
113 }
114 if (type.equals(TRAFFIC_ENGINEERING_ADDRESS.type())) {
115 return true;
116 }
117 if (type.equals(SOURCE_DEST_ADDRESS.type())) {
118 return true;
119 }
120
121 return false;
122 }
123
124 @Override
125 public LispLcafAddress mapMappingAddress(ExtensionMappingAddress mappingAddress) {
126 ExtensionMappingAddressType type = mappingAddress.type();
127
128 if (type.equals(LIST_ADDRESS.type())) {
129
130 LispListAddress listAddress = (LispListAddress) mappingAddress;
131 LispAfiAddress ipv4 = mapping2afi(listAddress.getIpv4());
132 LispAfiAddress ipv6 = mapping2afi(listAddress.getIpv6());
133
Jian Lie5aa5df2017-04-02 22:40:56 +0900134 if (ipv4 != null && ipv6 != null) {
135 return new LispListLcafAddress(ImmutableList.of(ipv4, ipv6));
136 } else {
137 return new LispListLcafAddress(ImmutableList.of());
138 }
Jian Lifc90a082017-03-31 23:36:14 +0900139 }
140
141 if (type.equals(SEGMENT_ADDRESS.type())) {
142
143 LispSegmentAddress segmentAddress = (LispSegmentAddress) mappingAddress;
144
145 return new LispSegmentLcafAddress.SegmentAddressBuilder()
146 .withInstanceId(segmentAddress.getInstanceId())
147 .withAddress(getAfiAddress(segmentAddress.getAddress()))
148 .build();
149 }
150
151 if (type.equals(AS_ADDRESS.type())) {
152
153 LispAsAddress asAddress = (LispAsAddress) mappingAddress;
154
155 return new LispAsLcafAddress.AsAddressBuilder()
156 .withAsNumber(asAddress.getAsNumber())
157 .withAddress(getAfiAddress(asAddress.getAddress()))
158 .build();
159 }
160
161 if (type.equals(APPLICATION_DATA_ADDRESS.type())) {
162
163 LispAppDataAddress appDataAddress = (LispAppDataAddress) mappingAddress;
164
165 return new LispAppDataLcafAddress.AppDataAddressBuilder()
166 .withProtocol(appDataAddress.getProtocol())
167 .withIpTos(appDataAddress.getIpTos())
168 .withLocalPortLow(appDataAddress.getLocalPortLow())
169 .withLocalPortHigh(appDataAddress.getLocalPortHigh())
170 .withRemotePortLow(appDataAddress.getRemotePortLow())
171 .withRemotePortHigh(appDataAddress.getRemotePortHigh())
172 .withAddress(getAfiAddress(appDataAddress.getAddress()))
173 .build();
174 }
175
176 if (type.equals(GEO_COORDINATE_ADDRESS.type())) {
177
178 LispGcAddress gcAddress = (LispGcAddress) mappingAddress;
179
180 return new LispGeoCoordinateLcafAddress.GeoCoordinateAddressBuilder()
181 .withIsNorth(gcAddress.isNorth())
182 .withLatitudeDegree(gcAddress.getLatitudeDegree())
183 .withLatitudeMinute(gcAddress.getLatitudeMinute())
184 .withLatitudeSecond(gcAddress.getLatitudeSecond())
185 .withIsEast(gcAddress.isEast())
186 .withLongitudeDegree(gcAddress.getLongitudeDegree())
187 .withLongitudeMinute(gcAddress.getLongitudeMinute())
188 .withLongitudeSecond(gcAddress.getLongitudeSecond())
189 .withAltitude(gcAddress.getAltitude())
190 .withAddress(getAfiAddress(gcAddress.getAddress()))
191 .build();
192 }
193
194 if (type.equals(NAT_ADDRESS.type())) {
195
196 LispNatAddress natAddress = (LispNatAddress) mappingAddress;
197
198 List<LispAfiAddress> aas = Lists.newArrayList();
199
200 natAddress.getRtrRlocAddresses()
201 .forEach(rtr -> aas.add(getAfiAddress(rtr)));
202
203 return new LispNatLcafAddress.NatAddressBuilder()
204 .withMsUdpPortNumber(natAddress.getMsUdpPortNumber())
205 .withEtrUdpPortNumber(natAddress.getEtrUdpPortNumber())
206 .withMsRlocAddress(getAfiAddress(natAddress.getMsRlocAddress()))
207 .withGlobalEtrRlocAddress(
208 getAfiAddress(natAddress.getGlobalEtrRlocAddress()))
209 .withPrivateEtrRlocAddress(
210 getAfiAddress(natAddress.getPrivateEtrRlocAddress()))
211 .withRtrRlocAddresses(aas)
212 .build();
213 }
214
215 if (type.equals(NONCE_ADDRESS.type())) {
216
217 LispNonceAddress nonceAddress = (LispNonceAddress) mappingAddress;
218
219 return new LispNonceLcafAddress.NonceAddressBuilder()
220 .withNonce(nonceAddress.getNonce())
221 .withAddress(getAfiAddress(nonceAddress.getAddress()))
222 .build();
223 }
224
225 if (type.equals(MULTICAST_ADDRESS.type())) {
226
227 LispMulticastAddress multicastAddress = (LispMulticastAddress) mappingAddress;
228
229 return new LispMulticastLcafAddress.MulticastAddressBuilder()
230 .withInstanceId(multicastAddress.getInstanceId())
231 .withSrcAddress(getAfiAddress(multicastAddress.getSrcAddress()))
232 .withSrcMaskLength(multicastAddress.getSrcMaskLength())
233 .withGrpAddress(getAfiAddress(multicastAddress.getGrpAddress()))
234 .withGrpMaskLength(multicastAddress.getGrpMaskLength())
235 .build();
236 }
237
238 if (type.equals(TRAFFIC_ENGINEERING_ADDRESS.type())) {
239
240 LispTeAddress teAddress = (LispTeAddress) mappingAddress;
241
242 List<LispTeRecord> records = Lists.newArrayList();
243
244 teAddress.getTeRecords().forEach(record -> {
245 LispTeRecord teRecord =
246 new LispTeRecord.TeRecordBuilder()
247 .withIsLookup(record.isLookup())
248 .withIsRlocProbe(record.isRlocProbe())
249 .withIsStrict(record.isStrict())
250 .withRtrRlocAddress(getAfiAddress(
251 record.getAddress()))
252 .build();
253 records.add(teRecord);
254 });
255
256 return new LispTeLcafAddress.TeAddressBuilder()
257 .withTeRecords(records)
258 .build();
259 }
260
261 if (type.equals(SOURCE_DEST_ADDRESS.type())) {
262
263 LispSrcDstAddress srcDstAddress = (LispSrcDstAddress) mappingAddress;
264
265 return new LispSourceDestLcafAddress.SourceDestAddressBuilder()
266 .withSrcPrefix(getAfiAddress(srcDstAddress.getSrcPrefix()))
267 .withSrcMaskLength(srcDstAddress.getSrcMaskLength())
268 .withDstPrefix(getAfiAddress(srcDstAddress.getDstPrefix()))
269 .withDstMaskLength(srcDstAddress.getDstMaskLength())
270 .build();
271 }
272
273 log.error("Unsupported extension mapping address type {}", mappingAddress.type());
274
275 return null;
276 }
277
278 @Override
279 public ExtensionMappingAddress mapLcafAddress(LispLcafAddress lcafAddress) {
280
281 switch (lcafAddress.getType()) {
282 case LIST:
283 LispListLcafAddress lcafListAddress = (LispListLcafAddress) lcafAddress;
284 MappingAddress ipv4Ma =
285 afi2mapping(lcafListAddress.getAddresses().get(0));
286 MappingAddress ipv6Ma =
287 afi2mapping(lcafListAddress.getAddresses().get(1));
288
289 return new LispListAddress.Builder()
290 .withIpv4(ipv4Ma)
291 .withIpv6(ipv6Ma)
292 .build();
293
294 case SEGMENT:
295 LispSegmentLcafAddress segmentLcafAddress =
296 (LispSegmentLcafAddress) lcafAddress;
297
298 return new LispSegmentAddress.Builder()
299 .withInstanceId(segmentLcafAddress.getInstanceId())
300 .withAddress(getMappingAddress(segmentLcafAddress.getAddress()))
301 .build();
302
303 case AS:
304 LispAsLcafAddress asLcafAddress = (LispAsLcafAddress) lcafAddress;
305
306 return new org.onosproject.drivers.lisp.extensions.LispAsAddress.Builder()
307 .withAsNumber(asLcafAddress.getAsNumber())
308 .withAddress(getMappingAddress(asLcafAddress.getAddress()))
309 .build();
310
311 case APPLICATION_DATA:
312
313 LispAppDataLcafAddress appLcafAddress = (LispAppDataLcafAddress) lcafAddress;
314
315 return new LispAppDataAddress.Builder()
316 .withProtocol(appLcafAddress.getProtocol())
317 .withIpTos(appLcafAddress.getIpTos())
318 .withLocalPortLow(appLcafAddress.getLocalPortLow())
319 .withLocalPortHigh(appLcafAddress.getLocalPortHigh())
320 .withRemotePortLow(appLcafAddress.getRemotePortLow())
321 .withRemotePortHigh(appLcafAddress.getRemotePortHigh())
322 .withAddress(getMappingAddress(appLcafAddress.getAddress()))
323 .build();
324
325 case GEO_COORDINATE:
326
327 LispGeoCoordinateLcafAddress gcLcafAddress =
328 (LispGeoCoordinateLcafAddress) lcafAddress;
329
330 return new LispGcAddress.Builder()
331 .withIsNorth(gcLcafAddress.isNorth())
332 .withLatitudeDegree(gcLcafAddress.getLatitudeDegree())
333 .withLatitudeMinute(gcLcafAddress.getLatitudeMinute())
334 .withLatitudeSecond(gcLcafAddress.getLatitudeSecond())
335 .withIsEast(gcLcafAddress.isEast())
336 .withLongitudeDegree(gcLcafAddress.getLongitudeDegree())
337 .withLongitudeMinute(gcLcafAddress.getLongitudeMinute())
338 .withLongitudeSecond(gcLcafAddress.getLongitudeSecond())
339 .withAltitude(gcLcafAddress.getAltitude())
340 .withAddress(getMappingAddress(gcLcafAddress.getAddress()))
341 .build();
342
343 case NAT:
344
345 LispNatLcafAddress natLcafAddress = (LispNatLcafAddress) lcafAddress;
346
347 List<MappingAddress> mas = Lists.newArrayList();
348
349 natLcafAddress.getRtrRlocAddresses()
350 .forEach(rtr -> mas.add(getMappingAddress(rtr)));
351
352 return new LispNatAddress.Builder()
353 .withMsUdpPortNumber(natLcafAddress.getMsUdpPortNumber())
354 .withEtrUdpPortNumber(natLcafAddress.getEtrUdpPortNumber())
355 .withMsRlocAddress(getMappingAddress(natLcafAddress.getMsRlocAddress()))
356 .withGlobalEtrRlocAddress(
357 getMappingAddress(natLcafAddress.getGlobalEtrRlocAddress()))
358 .withPrivateEtrRlocAddress(
359 getMappingAddress(natLcafAddress.getPrivateEtrRlocAddress()))
360 .withRtrRlocAddresses(mas)
361 .build();
362
363 case NONCE:
364
365 LispNonceLcafAddress nonceLcafAddress = (LispNonceLcafAddress) lcafAddress;
366
367 return new LispNonceAddress.Builder()
368 .withNonce(nonceLcafAddress.getNonce())
369 .withAddress(getMappingAddress(nonceLcafAddress.getAddress()))
370 .build();
371
372 case MULTICAST:
373
374 LispMulticastLcafAddress multiLcafAddress =
375 (LispMulticastLcafAddress) lcafAddress;
376
377 return new LispMulticastAddress.Builder()
378 .withInstanceId(multiLcafAddress.getInstanceId())
379 .withSrcAddress(getMappingAddress(multiLcafAddress.getSrcAddress()))
380 .withSrcMaskLength(multiLcafAddress.getSrcMaskLength())
381 .withGrpAddress(getMappingAddress(multiLcafAddress.getGrpAddress()))
382 .withGrpMaskLength(multiLcafAddress.getGrpMaskLength())
383 .build();
384
385 case TRAFFIC_ENGINEERING:
386
387 LispTeLcafAddress teLcafAddress = (LispTeLcafAddress) lcafAddress;
388
389 List<LispTeAddress.TeRecord> records = Lists.newArrayList();
390
391 teLcafAddress.getTeRecords().forEach(record -> {
392 LispTeAddress.TeRecord teRecord =
393 new LispTeAddress.TeRecord.Builder()
394 .withIsLookup(record.isLookup())
395 .withIsRlocProbe(record.isRlocProbe())
396 .withIsStrict(record.isStrict())
397 .withRtrRlocAddress(getMappingAddress(
398 record.getRtrRlocAddress()))
399 .build();
400 records.add(teRecord);
401 });
402
403 return new LispTeAddress.Builder()
404 .withTeRecords(records)
405 .build();
406
407 case SECURITY:
408
409 // TODO: need to implement security type later
410 log.warn("security type will be implemented later");
411
412 return null;
413
414 case SOURCE_DEST:
415
416 LispSourceDestLcafAddress srcDstLcafAddress =
417 (LispSourceDestLcafAddress) lcafAddress;
418
419
420 return new LispSrcDstAddress.Builder()
421 .withSrcPrefix(getMappingAddress(srcDstLcafAddress.getSrcPrefix()))
422 .withSrcMaskLength(srcDstLcafAddress.getSrcMaskLength())
423 .withDstPrefix(getMappingAddress(srcDstLcafAddress.getDstPrefix()))
424 .withDstMaskLength(srcDstLcafAddress.getDstMaskLength())
425 .build();
426
427 case UNSPECIFIED:
428 case UNKNOWN:
429 default:
430 log.error("Unsupported LCAF type {}", lcafAddress.getType());
431 return null;
432 }
433 }
434
435 @Override
436 public ExtensionMappingAddress getExtensionMappingAddress(
437 ExtensionMappingAddressType type) {
438
439 if (type.equals(LIST_ADDRESS.type())) {
440 return new LispListAddress();
441 }
442 if (type.equals(SEGMENT_ADDRESS.type())) {
443 return new LispSegmentAddress();
444 }
445 if (type.equals(AS_ADDRESS.type())) {
446 return new LispAsAddress();
447 }
448 if (type.equals(APPLICATION_DATA_ADDRESS.type())) {
449 return new LispAppDataAddress();
450 }
451 if (type.equals(GEO_COORDINATE_ADDRESS.type())) {
452 return new LispGcAddress();
453 }
454 if (type.equals(NAT_ADDRESS.type())) {
455 return new LispNatAddress();
456 }
457 if (type.equals(NONCE_ADDRESS.type())) {
458 return new LispNonceAddress();
459 }
460 if (type.equals(MULTICAST_ADDRESS.type())) {
461 return new LispMulticastAddress();
462 }
463 if (type.equals(TRAFFIC_ENGINEERING_ADDRESS.type())) {
464 return new LispTeAddress();
465 }
466 if (type.equals(SOURCE_DEST_ADDRESS.type())) {
467 return new LispSrcDstAddress();
468 }
469
470 return null;
471 }
472
473 /**
474 * Converts AFI address to generalized mapping address.
475 *
476 * @param afi IP typed AFI address
477 * @return generalized mapping address
478 */
479 private MappingAddress afi2mapping(LispAfiAddress afi) {
480 switch (afi.getAfi()) {
481 case IP4:
482 IpAddress ipv4Address = ((LispIpv4Address) afi).getAddress();
483 IpPrefix ipv4Prefix = IpPrefix.valueOf(ipv4Address, IPV4_PREFIX_LENGTH);
484 return MappingAddresses.ipv4MappingAddress(ipv4Prefix);
485 case IP6:
486 IpAddress ipv6Address = ((LispIpv6Address) afi).getAddress();
487 IpPrefix ipv6Prefix = IpPrefix.valueOf(ipv6Address, IPV6_PREFIX_LENGTH);
488 return MappingAddresses.ipv6MappingAddress(ipv6Prefix);
489 default:
490 log.warn("Only support to convert IP address type");
491 break;
492 }
493 return null;
494 }
495
496 /**
497 * Converts mapping address to AFI address.
498 *
499 * @param address generalized mapping address
500 * @return IP typed AFI address
501 */
502 private LispAfiAddress mapping2afi(MappingAddress address) {
503 switch (address.type()) {
504 case IPV4:
505 IpPrefix ipv4Prefix = ((IPMappingAddress) address).ip();
506 return new LispIpv4Address(ipv4Prefix.address());
507 case IPV6:
508 IpPrefix ipv6Prefix = ((IPMappingAddress) address).ip();
509 return new LispIpv6Address(ipv6Prefix.address());
510 default:
511 log.warn("Only support to convert IP address type");
512 break;
513 }
514 return null;
515 }
516
517 /**
518 * Converts LispAfiAddress into abstracted mapping address.
519 *
520 * @param address LispAfiAddress
521 * @return abstracted mapping address
522 */
523 private MappingAddress getMappingAddress(LispAfiAddress address) {
524
525 if (address == null) {
526 log.warn("Address is not specified.");
527 return null;
528 }
529
530 switch (address.getAfi()) {
531 case IP4:
532 return afi2mapping(address);
533 case IP6:
534 return afi2mapping(address);
535 case AS:
536 int asNum = ((org.onosproject.lisp.msg.types.LispAsAddress) address).getASNum();
537 return MappingAddresses.asMappingAddress(String.valueOf(asNum));
538 case DISTINGUISHED_NAME:
539 String dn = ((LispDistinguishedNameAddress)
540 address).getDistinguishedName();
541 return MappingAddresses.dnMappingAddress(dn);
542 case MAC:
543 MacAddress macAddress = ((LispMacAddress) address).getAddress();
544 return MappingAddresses.ethMappingAddress(macAddress);
545 case LCAF:
546 LispLcafAddress lcafAddress = (LispLcafAddress) address;
547 return MappingAddresses.extensionMappingAddressWrapper(mapLcafAddress(lcafAddress));
548 default:
549 log.warn("Unsupported address AFI type {}", address.getAfi());
550 break;
551 }
552
553 return null;
554 }
555
556 /**
557 * Converts mapping address into afi address.
558 *
559 * @param address mapping address
560 * @return afi address
561 */
562 private LispAfiAddress getAfiAddress(MappingAddress address) {
563
564 if (address == null) {
565 log.warn("Address is not specified.");
566 return null;
567 }
568
569 switch (address.type()) {
570 case IPV4:
571 return mapping2afi(address);
572 case IPV6:
573 return mapping2afi(address);
574 case AS:
575 int asNum = ((org.onosproject.lisp.msg.types.LispAsAddress) address).getASNum();
576 return new org.onosproject.lisp.msg.types.LispAsAddress(asNum);
577 case DN:
578 String dn = ((LispDistinguishedNameAddress) address).getDistinguishedName();
579 return new LispDistinguishedNameAddress(dn);
580 case ETH:
581 MacAddress macAddress = ((LispMacAddress) address).getAddress();
582 return new LispMacAddress(macAddress);
583 case EXTENSION:
584 ExtensionMappingAddress extAddress = (ExtensionMappingAddress) address;
585 return mapMappingAddress(extAddress);
586 default:
587 log.warn("Unsupported address type {}", address.type());
588 break;
589 }
590
591 return null;
592 }
Jian Li3fbc9502017-04-11 10:41:25 +0900593
594 @Override
595 public ObjectNode encode(ExtensionMappingAddress mappingAddress, CodecContext context) {
596 checkNotNull(mappingAddress, "Extension mapping address cannot be null");
597 ExtensionMappingAddressType type = mappingAddress.type();
598 ObjectNode root = context.mapper().createObjectNode();
599
600 if (type.equals(LIST_ADDRESS.type())) {
601 LispListAddress listAddress = (LispListAddress) mappingAddress;
602 root.set(LISP_LIST_ADDRESS,
603 context.codec(LispListAddress.class).encode(listAddress, context));
604 }
605 if (type.equals(SEGMENT_ADDRESS.type())) {
606 LispSegmentAddress segmentAddress = (LispSegmentAddress) mappingAddress;
607 root.set(LISP_SEGMENT_ADDRESS,
608 context.codec(LispSegmentAddress.class).encode(segmentAddress, context));
609 }
610 if (type.equals(AS_ADDRESS.type())) {
611 LispAsAddress asAddress = (LispAsAddress) mappingAddress;
612 root.set(LISP_AS_ADDRESS,
613 context.codec(LispAsAddress.class).encode(asAddress, context));
614 }
615 if (type.equals(APPLICATION_DATA_ADDRESS.type())) {
616 LispAppDataAddress appDataAddress = (LispAppDataAddress) mappingAddress;
617 root.set(LISP_APPLICATION_DATA_ADDRESS,
618 context.codec(LispAppDataAddress.class).encode(appDataAddress, context));
619 }
620 if (type.equals(GEO_COORDINATE_ADDRESS.type())) {
621 LispGcAddress gcAddress = (LispGcAddress) mappingAddress;
622 root.set(LISP_GEO_COORDINATE_ADDRESS,
623 context.codec(LispGcAddress.class).encode(gcAddress, context));
624 }
625 if (type.equals(NAT_ADDRESS.type())) {
626 LispNatAddress natAddress = (LispNatAddress) mappingAddress;
627 root.set(LISP_NAT_ADDRESS,
628 context.codec(LispNatAddress.class).encode(natAddress, context));
629 }
630 if (type.equals(NONCE_ADDRESS.type())) {
631 LispNonceAddress nonceAddress = (LispNonceAddress) mappingAddress;
632 root.set(LISP_NONCE_ADDRESS, context.codec(LispNonceAddress.class).encode(nonceAddress, context));
633 }
634 if (type.equals(MULTICAST_ADDRESS.type())) {
635 LispMulticastAddress multicastAddress = (LispMulticastAddress) mappingAddress;
636 root.set(LISP_MULTICAST_ADDRESS,
637 context.codec(LispMulticastAddress.class).encode(multicastAddress, context));
638 }
639 if (type.equals(TRAFFIC_ENGINEERING_ADDRESS.type())) {
640 LispTeAddress teAddress = (LispTeAddress) mappingAddress;
641 root.set(LISP_TRAFFIC_ENGINEERING_ADDRESS,
642 context.codec(LispTeAddress.class).encode(teAddress, context));
643 }
644 if (type.equals(SOURCE_DEST_ADDRESS.type())) {
645 LispSrcDstAddress srcDstAddress = (LispSrcDstAddress) mappingAddress;
646 root.set(LISP_SOURCE_DEST_ADDRESS,
647 context.codec(LispSrcDstAddress.class).encode(srcDstAddress, context));
648 }
649
650 return root;
651 }
652
653 @Override
654 public ExtensionMappingAddress decode(ObjectNode json, CodecContext context) {
655 if (json == null || !json.isObject()) {
656 return null;
657 }
658
659 // parse extension type
660 String typeString = nullIsIllegal(json.get(TYPE),
661 TYPE + MISSING_MEMBER_MESSAGE).asText();
662
663 if (typeString.equals(LIST_ADDRESS.name())) {
664 return context.codec(LispListAddress.class)
665 .decode(get(json, LISP_LIST_ADDRESS), context);
666 }
667 if (typeString.equals(SEGMENT_ADDRESS.name())) {
668 return context.codec(LispSegmentAddress.class)
669 .decode(get(json, LISP_SEGMENT_ADDRESS), context);
670 }
671 if (typeString.equals(AS_ADDRESS.name())) {
672 return context.codec(LispAsAddress.class)
673 .decode(get(json, LISP_AS_ADDRESS), context);
674 }
675 if (typeString.equals(APPLICATION_DATA_ADDRESS.name())) {
676 return context.codec(LispAppDataAddress.class)
677 .decode(get(json, LISP_APPLICATION_DATA_ADDRESS), context);
678 }
679 if (typeString.equals(GEO_COORDINATE_ADDRESS.name())) {
680 return context.codec(LispGcAddress.class)
681 .decode(get(json, LISP_GEO_COORDINATE_ADDRESS), context);
682 }
683 if (typeString.equals(NAT_ADDRESS.name())) {
684 return context.codec(LispNatAddress.class)
685 .decode(get(json, LISP_NAT_ADDRESS), context);
686 }
687 if (typeString.equals(NONCE_ADDRESS.name())) {
688 return context.codec(LispNonceAddress.class)
689 .decode(get(json, LISP_NONCE_ADDRESS), context);
690 }
691 if (typeString.equals(MULTICAST_ADDRESS.name())) {
692 return context.codec(LispMulticastAddress.class)
693 .decode(get(json, LISP_MULTICAST_ADDRESS), context);
694 }
695 if (typeString.equals(TRAFFIC_ENGINEERING_ADDRESS.name())) {
696 return context.codec(LispTeAddress.class)
697 .decode(get(json, LISP_TRAFFIC_ENGINEERING_ADDRESS), context);
698 }
699 if (typeString.equals(SOURCE_DEST_ADDRESS.name())) {
700 return context.codec(LispSrcDstAddress.class)
701 .decode(get(json, LISP_SOURCE_DEST_ADDRESS), context);
702 }
703
704 throw new UnsupportedOperationException(
705 "Driver does not support extension type " + typeString);
706 }
707
708 /**
709 * Gets a child Object Node from a parent by name. If the child is not found
710 * or does nor represent an object, null is returned.
711 *
712 * @param parent parent object
713 * @param childName name of child to query
714 * @return child object if found, null if not found or if not an object
715 */
716 private static ObjectNode get(ObjectNode parent, String childName) {
717 JsonNode node = parent.path(childName);
718 return node.isObject() && !node.isNull() ? (ObjectNode) node : null;
719 }
Jian Lifc90a082017-03-31 23:36:14 +0900720}