Migrate lisp-proto implementation to IETF YANG model
[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
27     // Private constructor prevents instantiation from other classes
28     protected LcafSerializer() {
29     }
30
31     public static LcafSerializer getInstance() {
32         return INSTANCE;
33     }
34
35     @Override
36     public int getAddressSize(LispAddress lispAddress) {
37         return Length.LCAF_HEADER
38                 + LispAddressSerializerFactory.getSerializer(lispAddress.getAddressType()).getLcafLength(lispAddress);
39     }
40
41     protected int getLcafHeaderSize() {
42         return Length.LCAF_HEADER;
43     }
44
45     protected short getLcafLength(LispAddress lispAddress) {
46         throw new RuntimeException("Not implemented");
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.putShort((short) (lcafSerializer.getLcafLength(lispAddress) -
66                 LispAddressSerializer.getInstance().getInstanceIdExtraSize()));
67     }
68
69     private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress,
70             LispAddressSerializer serializer) {
71         buffer.putShort((short) 0); // RES + Flags.
72         buffer.put(serializer.getLcafType());
73         buffer.put((byte) 0);
74     }
75
76     @Override
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();
84
85         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
86         if (serializer == null) {
87             throw new LispSerializationException("Unknown LCAF type: " + lcafType);
88         }
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);
93         }
94         return serializer.deserializeLcafEidData(buffer, res2, length, ctx);
95     }
96
97     @Override
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();
104
105         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(addressType);
106         if (serializer == null) {
107             throw new LispSerializationException("Unknown LCAF type: " + lcafType);
108         }
109         return serializer.deserializeLcafRlocData(buffer, res2, length, null);
110     }
111
112     private interface Length {
113         int RES = 1;
114         int FLAGS = 1;
115
116         int LCAF_HEADER = 6;
117     }
118 }