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.Ipv4Serializer.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.Ipv4PrefixAfi;
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.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
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 IPv4 prefixes from/to String representation.
34 * @author Lorand Jakab
37 public final class Ipv4PrefixSerializer extends LispAddressSerializer {
39 protected static final Logger LOG = LoggerFactory.getLogger(Ipv4PrefixSerializer.class);
41 private static final Ipv4PrefixSerializer INSTANCE = new Ipv4PrefixSerializer();
43 // Private constructor prevents instantiation from other classes
44 private Ipv4PrefixSerializer() {
47 public static Ipv4PrefixSerializer getInstance() {
52 public int getAddressSize(LispAddress lispAddress) {
53 // XXX does this need to worry about the mask too?
58 public int getAddressSize(SimpleAddress simpleAddress) {
59 // XXX does this need to worry about the mask too?
64 protected short getAfi() {
65 return (short) AddressFamily.IpV4.getIntValue();
69 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
70 Ipv4Prefix prefix = (Ipv4Prefix) lispAddress.getAddress();
71 String address = MaskUtil.getAddressStringForIpv4Prefix(prefix);
72 buffer.put(InetAddresses.forString(address).getAddress());
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 EidBuilder eb = new EidBuilder();
83 eb.setAddressType(Ipv4PrefixAfi.class);
84 eb.setVirtualNetworkId(getVni(ctx));
85 eb.setAddress(new Ipv4PrefixBuilder().setIpv4Prefix(deserializeData(buffer, ctx)).build());
90 protected Rloc deserializeRlocData(ByteBuffer buffer) {
91 throw new LispSerializationException("RLOCs should not be deserialized into prefixes!");
95 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
96 return new SimpleAddress(new IpPrefix(deserializeData(buffer, ctx)));
99 private static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix
100 deserializeData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
101 byte[] ipBuffer = new byte[4];
102 InetAddress address = null;
103 buffer.get(ipBuffer);
105 address = InetAddress.getByAddress(ipBuffer);
106 } catch (UnknownHostException e) {
107 LOG.debug("Unknown host {}", ipBuffer, e);
109 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(
110 address.getHostAddress() + "/" + ctx.getMaskLen());