2 * Copyright (c) 2014 Contextream, Inc. and others. 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.lispflowmapping.lisp.util.ByteUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdLcaf;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
23 public class LcafSerializer extends LispAddressSerializer {
25 private static final LcafSerializer INSTANCE = new LcafSerializer();
27 // Private constructor prevents instantiation from other classes
28 protected LcafSerializer() {
31 public static LcafSerializer getInstance() {
36 public int getAddressSize(LispAddress lispAddress) {
37 return Length.LCAF_HEADER
38 + LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType()).getLcafLength(lispAddress);
41 protected int getLcafHeaderSize() {
42 return Length.LCAF_HEADER;
45 protected short getLcafLength(LispAddress lispAddress) {
46 throw new RuntimeException("Not implemented");
50 protected short getAfi() {
51 return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
55 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
56 LispAddressSerializer lcafSerializer = LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType());
57 serializeLCAFAddressHeader(buffer, lispAddress, lcafSerializer);
58 buffer.putShort(lcafSerializer.getLcafLength(lispAddress));
59 lcafSerializer.serializeData(buffer, lispAddress);
62 protected void serializeLCAFAddressHeaderForInstanceId(ByteBuffer buffer, LispAddress lispAddress) {
63 LispAddressSerializer lcafSerializer = InstanceIdSerializer.getInstance();
64 serializeLCAFAddressHeader(buffer, lispAddress, lcafSerializer);
65 buffer.putShort((short) (lcafSerializer.getLcafLength(lispAddress) -
66 LispAddressSerializer.getInstance().getInstanceIdExtraSize()));
69 private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress,
70 LispAddressSerializer serializer) {
71 buffer.putShort((short) 0); // RES + Flags.
72 buffer.put(serializer.getLcafType());
77 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
78 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
79 byte lcafType = (byte) ByteUtil.getUnsignedByte(buffer);
80 Class <? extends LispAddressFamily> addressType = AddressTypeMap.getLcafType(lcafType);
81 // TODO move these to ctx to shorten the list of arguments
82 byte res2 = buffer.get();
83 short length = buffer.getShort();
85 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
86 if (serializer == null) {
87 throw new LispSerializationException("Unknown LCAF type: " + lcafType);
89 // Reset the mask context here, since the general mask length field in mapping records doesn't apply to LCAF
90 // address types; except for Instance ID, since we don't store it as an LCAF
91 if (ctx != null && addressType != InstanceIdLcaf.class) {
92 ctx.setMaskLen(LispAddressSerializerContext.MASK_LEN_MISSING);
94 return serializer.deserializeLcafEidData(buffer, res2, length, ctx);
98 protected Rloc deserializeRlocData(ByteBuffer buffer) {
99 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
100 byte lcafType = (byte) ByteUtil.getUnsignedByte(buffer);
101 Class <? extends LispAddressFamily> addressType = AddressTypeMap.getLcafType(lcafType);
102 byte res2 = buffer.get();
103 short length = buffer.getShort();
105 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
106 if (serializer == null) {
107 throw new LispSerializationException("Unknown LCAF type: " + lcafType);
109 return serializer.deserializeLcafRlocData(buffer, res2, length, null);
112 private interface Length {