Merge "Remove BindingAwareBroker"
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / LcafSerializer.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.lisp.serializer.address;
9
10 import java.nio.ByteBuffer;
11
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;
22
23 public class LcafSerializer extends LispAddressSerializer {
24
25     private static final LcafSerializer INSTANCE = new LcafSerializer();
26     private static final byte DEFAULT_IID_MASK_LENGTH = (byte) 32;
27
28     // Private constructor prevents instantiation from other classes
29     protected LcafSerializer() {
30     }
31
32     public static LcafSerializer getInstance() {
33         return INSTANCE;
34     }
35
36     @Override
37     public int getAddressSize(LispAddress lispAddress) {
38         return Length.LCAF_HEADER
39                 + LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType()).getLcafLength(lispAddress);
40     }
41
42     protected int getLcafHeaderSize() {
43         return Length.LCAF_HEADER;
44     }
45
46     protected short getLcafLength(LispAddress lispAddress) {
47         throw new LispSerializationException("Unimplemented method");
48     }
49
50     @Override
51     protected short getAfi() {
52         return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
53     }
54
55     @Override
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);
61     }
62
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()));
69     }
70
71     private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress,
72             LispAddressSerializer serializer) {
73         buffer.putShort((short) 0); // RES + Flags.
74         buffer.put(serializer.getLcafType());
75         buffer.put((byte) 0);
76     }
77
78     @Override
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();
86
87         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
88         if (serializer == null) {
89             throw new LispSerializationException("Unknown LCAF type: " + lcafType);
90         }
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);
95         }
96         return serializer.deserializeLcafEidData(buffer, res2, length, ctx);
97     }
98
99     @Override
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();
106
107         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
108         if (serializer == null) {
109             throw new LispSerializationException("Unknown LCAF type: " + lcafType);
110         }
111         return serializer.deserializeLcafRlocData(buffer, res2, length, null);
112     }
113
114     private interface Length {
115         int RES = 1;
116         int FLAGS = 1;
117
118         int LCAF_HEADER = 6;
119     }
120 }