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;
12 import org.opendaylight.lispflowmapping.lisp.serializer.address.factory.LispAddressSerializerFactory;
13 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
14 import org.opendaylight.lispflowmapping.lisp.util.AddressTypeMap;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
19 * @author Lorand Jakab
22 public class SimpleAddressSerializer {
24 private static final SimpleAddressSerializer INSTANCE = new SimpleAddressSerializer();
26 // Private constructor prevents instantiation from other classes
27 protected SimpleAddressSerializer() {
30 public static SimpleAddressSerializer getInstance() {
34 public int getAddressSize(SimpleAddress address) {
35 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(
36 AddressTypeMap.getSimpleAddressInnerType(address));
37 return Length.AFI + serializer.getAddressSize(address);
40 public void serialize(ByteBuffer buffer, SimpleAddress address) {
41 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(
42 AddressTypeMap.getSimpleAddressInnerType(address));
43 buffer.putShort(serializer.getAfi());
44 serializer.serializeData(buffer, address);
47 public SimpleAddress deserialize(ByteBuffer buffer, LispAddressSerializerContext ctx) {
48 short afi = buffer.getShort();
49 // AddressTypeMap indexes IPv4 and IPv6 prefixes (vs simple addresses) with the negative AFI values -1 and -2
50 if ((afi == 1 || afi == 2) && ctx != null &&
51 ctx.getMaskLen() != LispAddressSerializerContext.MASK_LEN_MISSING) {
54 Class <? extends LispAddressFamily> addressType = AddressTypeMap.getAddressType(afi);
55 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
56 if (serializer == null) {
57 throw new LispSerializationException("Unknown AFI: " + afi);
60 return serializer.deserializeSimpleAddressData(buffer, ctx);
61 } catch (RuntimeException e) {
62 throw new LispSerializationException("Problem deserializing AFI " + afi + " in SimpleAddress context", e);
66 private interface Length {