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;
15 import org.opendaylight.lispflowmapping.lisp.serializer.address.Ipv6Serializer.Length;
16 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
17 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6PrefixBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Class to (de)serialize IPv6 prefixes from/to String representation.
34 * @author Lorand Jakab
37 public final class Ipv6PrefixSerializer extends LispAddressSerializer {
39 protected static final Logger LOG = LoggerFactory.getLogger(Ipv6PrefixSerializer.class);
41 private static final Ipv6PrefixSerializer INSTANCE = new Ipv6PrefixSerializer();
43 // Private constructor prevents instantiation from other classes
44 private Ipv6PrefixSerializer() {
47 public static Ipv6PrefixSerializer getInstance() {
52 public int getAddressSize(LispAddress lispAddress) {
53 return Length.IPV6; // XXX does this need to worry about the mask too?
57 public int getAddressSize(SimpleAddress simpleAddress) {
58 return Length.IPV6; // XXX does this need to worry about the mask too?
62 protected short getAfi() {
63 return (short) AddressFamily.IpV6.getIntValue();
67 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
68 Ipv6Prefix prefix = (Ipv6Prefix) lispAddress.getAddress();
69 String address = MaskUtil.getAddressStringForIpv6Prefix(prefix);
70 buffer.put(InetAddresses.forString(address).getAddress());
74 protected void serializeData(ByteBuffer buffer, IpPrefix prefix) {
75 buffer.put(InetAddresses.forString(prefix.getIpv6Prefix().getValue()).getAddress());
79 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
80 EidBuilder eb = new EidBuilder();
81 eb.setAddressType(Ipv6PrefixAfi.class);
82 eb.setVirtualNetworkId(getVni(ctx));
83 eb.setAddress(new Ipv6PrefixBuilder().setIpv6Prefix(deserializeData(buffer, ctx)).build());
88 protected Rloc deserializeRlocData(ByteBuffer buffer) {
89 throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
93 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
94 return new SimpleAddress(new IpPrefix(deserializeData(buffer, ctx)));
97 private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix
98 deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
99 byte[] ipBuffer = new byte[16];
100 InetAddress address = null;
101 buffer.get(ipBuffer);
103 address = InetAddress.getByAddress(ipBuffer);
104 } catch (UnknownHostException e) {
105 LOG.debug("Unknown host {}", ipBuffer, e);
107 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix(
108 address.getHostAddress() + "/" + ctx.getMaskLen());