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;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.nio.ByteBuffer;
16 import org.opendaylight.lispflowmapping.lisp.serializer.address.Ipv4Serializer.Length;
17 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinaryBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * @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 return Length.IPV4; // XXX does this need to worry about the mask too?
58 public int getAddressSize(SimpleAddress simpleAddress) {
59 return Length.IPV4; // XXX does this need to worry about the mask too?
63 protected short getAfi() {
64 return (short) AddressFamily.IpV4.getIntValue();
68 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
69 Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) lispAddress.getAddress();
70 buffer.put(prefix.getIpv4AddressBinary().getValue());
74 protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
75 buffer.put(InetAddresses.forString(MaskUtil.getAddressStringForIpPrefix(address.getIpPrefix())).getAddress());
79 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
80 byte[] ipBuffer = new byte[4];
82 Ipv4PrefixBinary prefix = new Ipv4PrefixBinaryBuilder()
83 .setIpv4AddressBinary(new Ipv4AddressBinary(ipBuffer)).setIpv4MaskLength(ctx.getMaskLen()).build();
84 EidBuilder eb = new EidBuilder();
85 eb.setAddressType(Ipv4PrefixBinaryAfi.class);
86 eb.setVirtualNetworkId(getVni(ctx));
87 eb.setAddress(prefix);
92 protected Rloc deserializeRlocData(ByteBuffer buffer) {
93 throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
97 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
98 byte[] ipBuffer = new byte[4];
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 SimpleAddress(new IpPrefix(new Ipv4Prefix(address.getHostAddress() + "/" + ctx.getMaskLen())));