Move integration tests to mdsal-it-parent
[lispflowmapping.git] / mappingservice / yangmodel / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / address / LispLCAFAddressSerializer.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.LispLCAFAddressSerializerFactory;
13 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
14 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
15 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispLcafAddress;
18
19 public class LispLCAFAddressSerializer extends LispAddressSerializer {
20
21     private static final LispLCAFAddressSerializer INSTANCE = new LispLCAFAddressSerializer();
22
23     // Private constructor prevents instantiation from other classes
24     protected LispLCAFAddressSerializer() {
25     }
26
27     public static LispLCAFAddressSerializer getInstance() {
28         return INSTANCE;
29     }
30
31     @Override
32     protected LispLcafAddress deserializeData(ByteBuffer buffer) {
33         buffer.position(buffer.position() + Length.RES + Length.FLAGS);
34         byte lispCode = (byte) ByteUtil.getUnsignedByte(buffer);
35         LispCanonicalAddressFormatEnum lcafType = LispCanonicalAddressFormatEnum.valueOf(lispCode);
36         byte res2 = buffer.get();
37         short length = buffer.getShort();
38
39         LispLCAFAddressSerializer serializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(lcafType);
40         if (serializer == null) {
41             throw new LispSerializationException("Unknown LispLCAFAddress type=" + lispCode);
42         }
43         return serializer.deserializeData(buffer, res2, length);
44     }
45
46     protected LispLcafAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
47         throw new RuntimeException("Not implemented");
48     }
49
50     @Override
51     public int getAddressSize(LispAFIAddress lispAddress) {
52         return Length.LCAF_HEADER
53                 + LispLCAFAddressSerializerFactory.getLCAFSerializer(
54                         LispCanonicalAddressFormatEnum.valueOf(((LispLcafAddress) lispAddress).getLcafType())).getLcafLength(lispAddress);
55     }
56
57     protected short getLcafLength(LispAFIAddress lispAddress) {
58         throw new RuntimeException("Not implemented");
59     }
60
61     @Override
62     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
63         serializeLCAFAddressHeader(buffer, lispAddress);
64
65         LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(LispCanonicalAddressFormatEnum
66                 .valueOf(((LispLcafAddress) lispAddress).getLcafType()));
67         lcafSerializer.serializeData(buffer, lispAddress);
68     }
69
70     private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAFIAddress lispAddress) {
71         LispLcafAddress lispLcafAddress = (LispLcafAddress) lispAddress;
72         buffer.putShort((short) 0); // RES + Flags.
73         buffer.put(lispLcafAddress.getLcafType().byteValue());
74         buffer.put((byte) 0);
75         LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(LispCanonicalAddressFormatEnum
76                 .valueOf(lispLcafAddress.getLcafType()));
77         buffer.putShort(lcafSerializer.getLcafLength(lispAddress));
78     }
79
80     private interface Length {
81         int RES = 1;
82         int FLAGS = 1;
83
84         int LCAF_HEADER = 6;
85     }
86 }