391a0ac7defe79b763ac70fc3ee52e3acf868a02
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / LocatorRecordSerializer.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;
9
10 import java.nio.ByteBuffer;
11
12 import org.apache.commons.lang3.BooleanUtils;
13 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
14 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
15 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
16 import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
20
21 public final class LocatorRecordSerializer {
22
23     private static final LocatorRecordSerializer INSTANCE = new LocatorRecordSerializer();
24
25     // Private constructor prevents instantiation from other classes
26     private LocatorRecordSerializer() {
27     }
28
29     public static LocatorRecordSerializer getInstance() {
30         return INSTANCE;
31     }
32
33     protected LocatorRecord deserialize(ByteBuffer buffer) {
34         LocatorRecordBuilder builder = new LocatorRecordBuilder();
35         builder.setPriority((short) ByteUtil.getUnsignedByte(buffer));
36         builder.setWeight((short) ByteUtil.getUnsignedByte(buffer));
37         builder.setMulticastPriority((short) ByteUtil.getUnsignedByte(buffer));
38         builder.setMulticastWeight((short) ByteUtil.getUnsignedByte(buffer));
39         byte flags = (byte) buffer.getShort();
40         builder.setLocalLocator(ByteUtil.extractBit(flags, Flags.LOCAL_LOCATOR));
41         builder.setRlocProbed(ByteUtil.extractBit(flags, Flags.RLOC_PROBED));
42         builder.setRouted(ByteUtil.extractBit(flags, Flags.ROUTED));
43         Rloc rloc = LispAddressSerializer.getInstance().deserializeRloc(buffer);
44         builder.setRloc(rloc);
45         builder.setLocatorId(LispAddressStringifier.getString(rloc));
46         return builder.build();
47     }
48
49     public void serialize(ByteBuffer replyBuffer, LocatorRecord record) {
50         replyBuffer.put((byte) NumberUtil.asShort(record.getPriority()));
51         replyBuffer.put((byte) NumberUtil.asShort(record.getWeight()));
52         replyBuffer.put((byte) NumberUtil.asShort(record.getMulticastPriority()));
53         replyBuffer.put((byte) NumberUtil.asShort(record.getMulticastWeight()));
54         replyBuffer.position(replyBuffer.position() + Length.UNUSED_FLAGS);
55         replyBuffer.put((byte) (ByteUtil.boolToBit(BooleanUtils.isTrue(record.isLocalLocator()), Flags.LOCAL_LOCATOR)
56                 | ByteUtil.boolToBit(BooleanUtils.isTrue(record.isRlocProbed()), Flags.RLOC_PROBED)
57                 | ByteUtil.boolToBit(BooleanUtils.isTrue(record.isRouted()), Flags.ROUTED)));
58         LispAddressSerializer.getInstance().serialize(replyBuffer, record.getRloc());
59     }
60
61     public int getSerializationSize(LocatorRecord record) {
62         return Length.HEADER_SIZE
63                 + LispAddressSerializer.getInstance().getAddressSize(record.getRloc());
64     }
65
66     private interface Flags {
67         int LOCAL_LOCATOR = 0x04;
68         int RLOC_PROBED = 0x02;
69         int ROUTED = 0x01;
70     }
71
72     private interface Length {
73         int HEADER_SIZE = 6;
74         int UNUSED_FLAGS = 1;
75     }
76 }