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 java.nio.ByteBuffer;
11 import org.opendaylight.lispflowmapping.lisp.serializer.address.factory.LispAddressSerializerFactory;
12 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
13 import org.opendaylight.lispflowmapping.lisp.util.AddressTypeMap;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
18 * Class to (de)serialize addresses that can be used in an LCAF.
20 * @author Lorand Jakab
23 public class SimpleAddressSerializer {
25 private static final SimpleAddressSerializer INSTANCE = new SimpleAddressSerializer();
27 // Private constructor prevents instantiation from other classes
28 protected SimpleAddressSerializer() {
31 public static SimpleAddressSerializer getInstance() {
35 public int getAddressSize(SimpleAddress address) {
36 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(
37 AddressTypeMap.getSimpleAddressInnerType(address));
38 return Length.AFI + serializer.getAddressSize(address);
41 public void serialize(ByteBuffer buffer, SimpleAddress address) {
42 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(
43 AddressTypeMap.getSimpleAddressInnerType(address));
44 buffer.putShort(serializer.getAfi());
45 serializer.serializeData(buffer, address);
48 @SuppressWarnings("checkstyle:IllegalCatch")
49 public SimpleAddress deserialize(ByteBuffer buffer, LispAddressSerializerContext ctx) {
50 short afi = buffer.getShort();
51 // AddressTypeMap indexes IPv4 and IPv6 prefixes (vs simple addresses) with the negative AFI values -1 and -2
52 if ((afi == 1 || afi == 2) && ctx != null
53 && ctx.getMaskLen() != LispAddressSerializerContext.MASK_LEN_MISSING) {
56 Class<? extends LispAddressFamily> addressType = AddressTypeMap.getAddressType(afi);
57 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
58 if (serializer == null) {
59 throw new LispSerializationException("Unknown AFI: " + afi);
62 return serializer.deserializeSimpleAddressData(buffer, ctx);
63 } catch (RuntimeException e) {
64 throw new LispSerializationException("Problem deserializing AFI " + afi + " in SimpleAddress context", e);
68 private interface Length {