2 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.lispflowmapping.lisp.serializer.address;
10 import com.google.common.net.InetAddresses;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.nio.ByteBuffer;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Class to (de)serialize IPv6 addresses from/to String representation.
32 * @author Lorand Jakab
34 public final class Ipv6Serializer extends LispAddressSerializer {
36 protected static final Logger LOG = LoggerFactory.getLogger(Ipv6Serializer.class);
38 private static final Ipv6Serializer INSTANCE = new Ipv6Serializer();
40 // Private constructor prevents instantiation from other classes
41 private Ipv6Serializer() {
44 public static Ipv6Serializer getInstance() {
49 public int getAddressSize(LispAddress lispAddress) {
54 public int getAddressSize(SimpleAddress simpleAddress) {
59 protected short getAfi() {
60 return (short) AddressFamily.IpV6.getIntValue();
64 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
65 Ipv6 address = (Ipv6) lispAddress.getAddress();
66 buffer.put(InetAddresses.forString(address.getIpv6().getValue()).getAddress());
70 protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
71 buffer.put(InetAddresses.forString(address.getIpAddress().getIpv6Address().getValue()).getAddress());
75 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
76 EidBuilder eb = new EidBuilder();
77 eb.setAddressType(Ipv6Afi.VALUE);
78 eb.setVirtualNetworkId(getVni(ctx));
79 eb.setAddress(new Ipv6Builder().setIpv6(deserializeData(buffer)).build());
84 protected Rloc deserializeRlocData(ByteBuffer buffer) {
85 RlocBuilder rb = new RlocBuilder();
86 rb.setAddressType(Ipv6Afi.VALUE);
87 rb.setVirtualNetworkId(null);
88 rb.setAddress(new Ipv6Builder().setIpv6(deserializeData(buffer)).build());
93 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
94 return new SimpleAddress(new IpAddress(deserializeData(buffer)));
97 private static Ipv6Address deserializeData(ByteBuffer buffer) {
98 byte[] ipBuffer = new byte[16];
99 InetAddress address = null;
100 buffer.get(ipBuffer);
102 address = InetAddress.getByAddress(ipBuffer);
103 } catch (UnknownHostException e) {
104 LOG.debug("Unknown host {}", ipBuffer, e);
106 return new Ipv6Address(address.getHostAddress());
109 protected interface Length {