2 * Copyright (c) 2016 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.Ipv4Serializer.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.inet.types.rev130715.Ipv4Prefix;
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.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinaryBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Class to (de)serialize IPv4 prefixes from/to byte[] representation.
36 * @author Lorand Jakab
38 public final class Ipv4PrefixBinarySerializer extends LispAddressSerializer {
40 protected static final Logger LOG = LoggerFactory.getLogger(Ipv4PrefixBinarySerializer.class);
42 private static final Ipv4PrefixBinarySerializer INSTANCE = new Ipv4PrefixBinarySerializer();
44 // Private constructor prevents instantiation from other classes
45 private Ipv4PrefixBinarySerializer() {
48 public static Ipv4PrefixBinarySerializer getInstance() {
53 public int getAddressSize(LispAddress lispAddress) {
54 // XXX does this need to worry about the mask too?
59 public int getAddressSize(SimpleAddress simpleAddress) {
60 // XXX does this need to worry about the mask too?
65 protected short getAfi() {
66 return (short) AddressFamily.IpV4.getIntValue();
70 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
71 Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) lispAddress.getAddress();
72 buffer.put(prefix.getIpv4AddressBinary().getValue());
76 protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
77 buffer.put(InetAddresses.forString(MaskUtil.getAddressStringForIpPrefix(address.getIpPrefix())).getAddress());
81 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
82 byte[] ipBuffer = new byte[4];
84 Ipv4PrefixBinary prefix = new Ipv4PrefixBinaryBuilder()
85 .setIpv4AddressBinary(new Ipv4AddressBinary(ipBuffer))
86 .setIpv4MaskLength(Uint8.valueOf(ctx.getMaskLen()))
88 EidBuilder eb = new EidBuilder();
89 eb.setAddressType(Ipv4PrefixBinaryAfi.class);
90 eb.setVirtualNetworkId(getVni(ctx));
91 eb.setAddress(prefix);
96 protected Rloc deserializeRlocData(ByteBuffer buffer) {
97 throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
101 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
102 byte[] ipBuffer = new byte[4];
103 InetAddress address = null;
104 buffer.get(ipBuffer);
106 address = InetAddress.getByAddress(ipBuffer);
107 } catch (UnknownHostException e) {
108 LOG.debug("Unknown host {}", ipBuffer, e);
110 return new SimpleAddress(new IpPrefix(new Ipv4Prefix(address.getHostAddress() + "/" + ctx.getMaskLen())));