2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.lisp.serializer;
10 import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava;
12 import java.nio.ByteBuffer;
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;
23 public final class LocatorRecordSerializer {
25 private static final LocatorRecordSerializer INSTANCE = new LocatorRecordSerializer();
27 // Private constructor prevents instantiation from other classes
28 private LocatorRecordSerializer() {
31 public static LocatorRecordSerializer getInstance() {
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();
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());
63 public int getSerializationSize(LocatorRecord record) {
64 return Length.HEADER_SIZE
65 + LispAddressSerializer.getInstance().getAddressSize(record.getRloc());
68 private interface Flags {
69 int LOCAL_LOCATOR = 0x04;
70 int RLOC_PROBED = 0x02;
74 private interface Length {