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;
11 import java.util.ArrayList;
13 import org.apache.commons.lang3.BooleanUtils;
14 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
15 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializerContext;
16 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
17 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
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.mapping.record.container.MappingRecord;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
24 public final class MappingRecordSerializer {
26 private static final MappingRecordSerializer INSTANCE = new MappingRecordSerializer();
28 // Private constructor prevents instantiation from other classes
29 private MappingRecordSerializer() {
32 public static MappingRecordSerializer getInstance() {
36 public MappingRecord deserialize(ByteBuffer buffer) {
37 return deserializeToBuilder(buffer).build();
40 public MappingRecordBuilder deserializeToBuilder(ByteBuffer buffer) {
41 MappingRecordBuilder builder = new MappingRecordBuilder();
42 builder.setRecordTtl(buffer.getInt());
43 final byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
44 final short maskLength = ((short) ByteUtil.getUnsignedByte(buffer));
45 final byte actionAndAuthoritative = buffer.get();
46 Action act = Action.forValue(actionAndAuthoritative >> 5);
48 act = Action.NoAction;
50 builder.setAction(act);
51 builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
52 buffer.position(buffer.position() + Length.RESERVED);
53 builder.setMapVersion(buffer.getShort());
55 LispAddressSerializerContext ctx = new LispAddressSerializerContext(maskLength);
56 builder.setEid(LispAddressSerializer.getInstance().deserializeEid(buffer, ctx));
58 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
59 for (int i = 0; i < locatorCount; i++) {
60 builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
63 builder.setTimestamp(System.currentTimeMillis());
68 public void serialize(ByteBuffer replyBuffer, MappingRecord record) {
69 replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
70 if (record.getLocatorRecord() != null) {
71 replyBuffer.put((byte) record.getLocatorRecord().size());
73 replyBuffer.put((byte) 0);
75 if (record.getEid() != null && MaskUtil.getMaskForAddress(record.getEid().getAddress()) != -1) {
76 replyBuffer.put((byte) NumberUtil.asShort(MaskUtil.getMaskForAddress(record.getEid().getAddress())));
78 replyBuffer.put((byte) 0);
80 Action act = Action.NoAction;
81 if (record.getAction() != null) {
82 act = record.getAction();
84 replyBuffer.put((byte) ((act.getIntValue() << 5) | //
85 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
86 replyBuffer.position(replyBuffer.position() + Length.RESERVED);
87 replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
88 if (record.getEid() != null && record.getEid().getAddress() != null) {
89 LispAddressSerializer.getInstance().serialize(replyBuffer, record.getEid());
92 if (record.getLocatorRecord() != null) {
93 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
94 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
99 public int getSerializationSize(MappingRecord record) {
100 int size = Length.HEADER_SIZE;
101 if (record.getEid() != null) {
102 size += LispAddressSerializer.getInstance().getAddressSize(record.getEid());
104 if (record.getLocatorRecord() != null) {
105 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
106 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
112 private interface Flags {
113 int AUTHORITATIVE = 0x10;
116 private interface Length {
117 int HEADER_SIZE = 10;