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.lispflowmapping.lisp.serializer.address.Ipv6Serializer.Length;
15 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
16 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6PrefixBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Class to (de)serialize IPv6 prefixes from/to String representation.
33 * @author Lorand Jakab
35 public final class Ipv6PrefixSerializer extends LispAddressSerializer {
37 protected static final Logger LOG = LoggerFactory.getLogger(Ipv6PrefixSerializer.class);
39 private static final Ipv6PrefixSerializer INSTANCE = new Ipv6PrefixSerializer();
41 // Private constructor prevents instantiation from other classes
42 private Ipv6PrefixSerializer() {
45 public static Ipv6PrefixSerializer getInstance() {
50 public int getAddressSize(LispAddress lispAddress) {
51 // XXX does this need to worry about the mask too?
56 public int getAddressSize(SimpleAddress simpleAddress) {
57 // 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 void serializeData(ByteBuffer buffer, SimpleAddress address) {
80 buffer.put(InetAddresses.forString(MaskUtil.getAddressStringForIpPrefix(address.getIpPrefix())).getAddress());
84 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
85 EidBuilder eb = new EidBuilder();
86 eb.setAddressType(Ipv6PrefixAfi.class);
87 eb.setVirtualNetworkId(getVni(ctx));
88 eb.setAddress(new Ipv6PrefixBuilder().setIpv6Prefix(deserializeData(buffer, ctx)).build());
93 protected Rloc deserializeRlocData(ByteBuffer buffer) {
94 throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
98 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
99 return new SimpleAddress(new IpPrefix(deserializeData(buffer, ctx)));
102 private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix
103 deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
104 byte[] ipBuffer = new byte[16];
105 InetAddress address = null;
106 buffer.get(ipBuffer);
108 address = InetAddress.getByAddress(ipBuffer);
109 } catch (UnknownHostException e) {
110 LOG.debug("Unknown host {}", ipBuffer, e);
112 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix(
113 address.getHostAddress() + "/" + ctx.getMaskLen());