Bump upstreams for 2022.09 Chlorine
[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 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.lispflowmapping.lisp.util.ByteUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdLcaf;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
21
22 public class LcafSerializer extends LispAddressSerializer {
23     private static final LcafSerializer INSTANCE = new LcafSerializer();
24     private static final byte DEFAULT_IID_MASK_LENGTH = (byte) 32;
25
26     // Private constructor prevents instantiation from other classes
27     protected LcafSerializer() {
28     }
29
30     public static LcafSerializer getInstance() {
31         return INSTANCE;
32     }
33
34     @Override
35     public int getAddressSize(LispAddress lispAddress) {
36         return Length.LCAF_HEADER
37                 + LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType()).getLcafLength(lispAddress);
38     }
39
40     protected int getLcafHeaderSize() {
41         return Length.LCAF_HEADER;
42     }
43
44     @Override
45     protected short getLcafLength(LispAddress lispAddress) {
46         throw new LispSerializationException("Unimplemented method");
47     }
48
49     @Override
50     protected short getAfi() {
51         return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
52     }
53
54     @Override
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);
60     }
61
62     protected void serializeLCAFAddressHeaderForInstanceId(ByteBuffer buffer, LispAddress lispAddress) {
63         LispAddressSerializer lcafSerializer = InstanceIdSerializer.getInstance();
64         serializeLCAFAddressHeader(buffer, lispAddress, lcafSerializer);
65         buffer.put(buffer.position() - 1, DEFAULT_IID_MASK_LENGTH);
66         buffer.putShort((short) (lcafSerializer.getLcafLength(lispAddress)
67                 - LispAddressSerializer.getInstance().getInstanceIdExtraSize()));
68     }
69
70     private static void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress,
71             LispAddressSerializer serializer) {
72         // RES + Flags
73         buffer.putShort((short) 0);
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         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 && !InstanceIdLcaf.VALUE.equals(addressType)) {
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         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 }