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 java.net.Inet6Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.nio.ByteBuffer;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
29 * @author Lorand Jakab
32 public class Ipv6Serializer extends LispAddressSerializer {
34 private static final Ipv6Serializer INSTANCE = new Ipv6Serializer();
36 // Private constructor prevents instantiation from other classes
37 private Ipv6Serializer() {
40 public static Ipv6Serializer getInstance() {
45 public int getAddressSize(LispAddress lispAddress) {
50 public int getAddressSize(SimpleAddress simpleAddress) {
55 protected short getAfi() {
56 return (short) AddressFamily.IpV6.getIntValue();
60 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
61 Ipv6 address = (Ipv6) lispAddress.getAddress();
63 buffer.put(Inet6Address.getByName(address.getIpv6().getValue()).getAddress());
64 } catch (UnknownHostException e) {
69 protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
71 buffer.put(Inet6Address.getByName(address.getIpAddress().getIpv6Address().getValue()).getAddress());
72 } catch (UnknownHostException e) {
77 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
78 EidBuilder eb = new EidBuilder();
79 eb.setAddressType(Ipv6Afi.class);
80 eb.setVirtualNetworkId(getVni(ctx));
81 eb.setAddress(new Ipv6Builder().setIpv6(deserializeData(buffer)).build());
86 protected Rloc deserializeRlocData(ByteBuffer buffer) {
87 RlocBuilder rb = new RlocBuilder();
88 rb.setAddressType(Ipv6Afi.class);
89 rb.setVirtualNetworkId(null);
90 rb.setAddress(new Ipv6Builder().setIpv6(deserializeData(buffer)).build());
95 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
96 return new SimpleAddress(new IpAddress(deserializeData(buffer)));
99 private Ipv6Address deserializeData(ByteBuffer buffer) {
100 byte[] ipBuffer = new byte[16];
101 InetAddress address = null;
102 buffer.get(ipBuffer);
104 address = InetAddress.getByAddress(ipBuffer);
105 } catch (UnknownHostException e) {
107 return new Ipv6Address(address.getHostAddress());
110 protected interface Length {