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();
26 private static final byte DEFAULT_IID_MASK_LENGTH = (byte) 32;
28 // Private constructor prevents instantiation from other classes
29 protected LcafSerializer() {
32 public static LcafSerializer getInstance() {
37 public int getAddressSize(LispAddress lispAddress) {
38 return Length.LCAF_HEADER
39 + LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType()).getLcafLength(lispAddress);
42 protected int getLcafHeaderSize() {
43 return Length.LCAF_HEADER;
46 protected short getLcafLength(LispAddress lispAddress) {
47 throw new LispSerializationException("Unimplemented method");
51 protected short getAfi() {
52 return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
56 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
57 LispAddressSerializer lcafSerializer = LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType());
58 serializeLCAFAddressHeader(buffer, lispAddress, lcafSerializer);
59 buffer.putShort(lcafSerializer.getLcafLength(lispAddress));
60 lcafSerializer.serializeData(buffer, lispAddress);
63 protected void serializeLCAFAddressHeaderForInstanceId(ByteBuffer buffer, LispAddress lispAddress) {
64 LispAddressSerializer lcafSerializer = InstanceIdSerializer.getInstance();
65 serializeLCAFAddressHeader(buffer, lispAddress, lcafSerializer);
66 buffer.put(buffer.position() - 1, DEFAULT_IID_MASK_LENGTH);
67 buffer.putShort((short) (lcafSerializer.getLcafLength(lispAddress)
68 - LispAddressSerializer.getInstance().getInstanceIdExtraSize()));
71 private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress,
72 LispAddressSerializer serializer) {
73 buffer.putShort((short) 0); // RES + Flags.
74 buffer.put(serializer.getLcafType());
79 protected Eid deserializeEidData(ByteBuffer buffer, LispAddressSerializerContext ctx) {
80 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
81 byte lcafType = (byte) ByteUtil.getUnsignedByte(buffer);
82 Class<? extends LispAddressFamily> addressType = AddressTypeMap.getLcafType(lcafType);
83 // TODO move these to ctx to shorten the list of arguments
84 byte res2 = buffer.get();
85 short length = buffer.getShort();
87 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
88 if (serializer == null) {
89 throw new LispSerializationException("Unknown LCAF type: " + lcafType);
91 // Reset the mask context here, since the general mask length field in mapping records doesn't apply to LCAF
92 // address types; except for Instance ID, since we don't store it as an LCAF
93 if (ctx != null && addressType != InstanceIdLcaf.class) {
94 ctx.setMaskLen(LispAddressSerializerContext.MASK_LEN_MISSING);
96 return serializer.deserializeLcafEidData(buffer, res2, length, ctx);
100 protected Rloc deserializeRlocData(ByteBuffer buffer) {
101 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
102 byte lcafType = (byte) ByteUtil.getUnsignedByte(buffer);
103 Class<? extends LispAddressFamily> addressType = AddressTypeMap.getLcafType(lcafType);
104 byte res2 = buffer.get();
105 short length = buffer.getShort();
107 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
108 if (serializer == null) {
109 throw new LispSerializationException("Unknown LCAF type: " + lcafType);
111 return serializer.deserializeLcafRlocData(buffer, res2, length, null);
114 private interface Length {