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 java.net.Inet4Address;
11 import java.net.UnknownHostException;
12 import java.nio.ByteBuffer;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4BinaryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Class to (de)serialize IPv4 addresses from/to byte[] representation.
33 * @author Lorand Jakab
35 public final class Ipv4BinarySerializer extends LispAddressSerializer {
37 protected static final Logger LOG = LoggerFactory.getLogger(Ipv4Serializer.class);
39 private static final Ipv4BinarySerializer INSTANCE = new Ipv4BinarySerializer();
41 // Private constructor prevents instantiation from other classes
42 private Ipv4BinarySerializer() {
45 public static Ipv4BinarySerializer getInstance() {
50 public int getAddressSize(LispAddress lispAddress) {
55 public int getAddressSize(SimpleAddress simpleAddress) {
60 protected short getAfi() {
61 return (short) AddressFamily.IpV4.getIntValue();
65 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
66 Ipv4Binary address = (Ipv4Binary) lispAddress.getAddress();
67 buffer.put(address.getIpv4Binary().getValue());
71 protected void serializeData(ByteBuffer buffer, SimpleAddress address) {
73 buffer.put(Inet4Address.getByName(address.getIpAddress().getIpv4Address().getValue()).getAddress());
74 } catch (UnknownHostException e) {
75 LOG.debug("Unknown host {}", address.getIpAddress().getIpv4Address().getValue(), e);
80 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
81 EidBuilder eb = new EidBuilder();
82 eb.setAddressType(Ipv4BinaryAfi.VALUE);
83 eb.setVirtualNetworkId(getVni(ctx));
84 eb.setAddress(new Ipv4BinaryBuilder().setIpv4Binary(deserializeData(buffer)).build());
89 protected Rloc deserializeRlocData(ByteBuffer buffer) {
90 RlocBuilder rb = new RlocBuilder();
91 rb.setAddressType(Ipv4BinaryAfi.VALUE);
92 rb.setVirtualNetworkId(null);
93 rb.setAddress(new Ipv4BinaryBuilder().setIpv4Binary(deserializeData(buffer)).build());
98 protected SimpleAddress deserializeSimpleAddressData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
99 return new SimpleAddress(new IpAddress(deserializeDataNonBinary(buffer)));
102 private static Ipv4AddressBinary deserializeData(ByteBuffer buffer) {
103 byte[] ipBuffer = new byte[4];
104 buffer.get(ipBuffer);
105 return new Ipv4AddressBinary(ipBuffer);
108 private static Ipv4Address deserializeDataNonBinary(ByteBuffer buffer) {
109 byte[] ipBuffer = new byte[4];
110 buffer.get(ipBuffer);
111 return IetfInetUtil.ipv4AddressFor(ipBuffer);
114 protected interface Length {