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 java.nio.ByteBuffer;
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;
21 public final class LocatorRecordSerializer {
23 private static final LocatorRecordSerializer INSTANCE = new LocatorRecordSerializer();
25 // Private constructor prevents instantiation from other classes
26 private LocatorRecordSerializer() {
29 public static LocatorRecordSerializer getInstance() {
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();
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());
61 public int getSerializationSize(LocatorRecord record) {
62 return Length.HEADER_SIZE
63 + LispAddressSerializer.getInstance().getAddressSize(record.getRloc());
66 private interface Flags {
67 int LOCAL_LOCATOR = 0x04;
68 int RLOC_PROBED = 0x02;
72 private interface Length {