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