support lcaf traffic engineering TELSDN-178: #close
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispLCAFAddressSerializer.java
1 /*
2  * Copyright (c) 2013 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.implementation.serializer.address;
9
10 import java.nio.ByteBuffer;
11
12 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
13 import org.opendaylight.lispflowmapping.implementation.serializer.address.factory.LispLCAFAddressSerializerFactory;
14 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
15 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
16 import org.opendaylight.lispflowmapping.type.lisp.address.LispLCAFAddress;
17
18 public class LispLCAFAddressSerializer extends LispAddressSerializer {
19
20     private static final LispLCAFAddressSerializer INSTANCE = new LispLCAFAddressSerializer();
21
22     // Private constructor prevents instantiation from other classes
23     protected LispLCAFAddressSerializer() {
24     }
25
26     public static LispLCAFAddressSerializer getInstance() {
27         return INSTANCE;
28     }
29
30     @Override
31     protected LispLCAFAddress deserializeData(ByteBuffer buffer) {
32         buffer.position(buffer.position() + Length.RES + Length.FLAGS);
33         byte lispCode = buffer.get();
34         LispCanonicalAddressFormatEnum lcafType = LispCanonicalAddressFormatEnum.valueOf(lispCode);
35         byte res2 = buffer.get();
36         short length = buffer.getShort();
37
38         LispLCAFAddressSerializer serializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(lcafType);
39         if (serializer == null) {
40             throw new LispSerializationException("Unknown LispLCAFAddress type=" + lispCode);
41         }
42         return serializer.deserializeData(buffer, res2, length);
43     }
44
45     protected LispLCAFAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
46         throw new RuntimeException("Not implemented");
47     }
48
49     @Override
50     public int getAddressSize(LispAddress lispAddress) {
51         return Length.LCAF_HEADER
52                 + LispLCAFAddressSerializerFactory.getLCAFSerializer(((LispLCAFAddress) lispAddress).getType()).getLcafLength(lispAddress);
53     }
54
55     protected short getLcafLength(LispAddress lispAddress) {
56         throw new RuntimeException("Not implemented");
57     }
58
59     @Override
60     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
61         serializeLCAFAddressHeader(buffer, lispAddress);
62         
63         LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(((LispLCAFAddress) lispAddress).getType());
64         lcafSerializer.serializeData(buffer, lispAddress);
65     }
66
67     private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress) {
68         LispLCAFAddress lispLCAFAddress = (LispLCAFAddress) lispAddress;
69         buffer.putShort((short) 0); // RES + Flags.
70         buffer.put(lispLCAFAddress.getType().getLispCode());
71         buffer.put(lispLCAFAddress.getRes2());
72         LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(lispLCAFAddress.getType());
73         buffer.putShort(lcafSerializer.getLcafLength(lispAddress));
74     }
75
76     private interface Length {
77         int RES = 1;
78         int FLAGS = 1;
79
80         int LCAF_HEADER = 6;
81     }
82 }