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 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 MappingRecordBuilder builder = new MappingRecordBuilder();
38 builder.setRecordTtl(buffer.getInt());
39 byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
40 builder.setMaskLength((short) ByteUtil.getUnsignedByte(buffer));
41 byte actionAndAuthoritative = buffer.get();
42 Action act = Action.forValue(actionAndAuthoritative >> 5);
44 act = Action.NoAction;
46 builder.setAction(act);
47 builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
48 buffer.position(buffer.position() + Length.RESERVED);
49 builder.setMapVersion(buffer.getShort());
51 LispAddressSerializerContext ctx = new LispAddressSerializerContext(builder.getMaskLength());
52 builder.setEid(LispAddressSerializer.getInstance().deserializeEid(buffer, ctx));
54 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
55 for (int i = 0; i < locatorCount; i++) {
56 builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
59 return builder.build();
62 public void serialize(ByteBuffer replyBuffer, MappingRecord record) {
63 replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
64 if (record.getLocatorRecord() != null) {
65 replyBuffer.put((byte) record.getLocatorRecord().size());
67 replyBuffer.put((byte) 0);
69 if (record.getEid() != null && MaskUtil.getMaskForAddress(record.getEid().getAddress()) != -1) {
70 replyBuffer.put((byte) NumberUtil.asShort(MaskUtil.getMaskForAddress(record.getEid().getAddress())));
72 replyBuffer.put((byte) 0);
74 Action act = Action.NoAction;
75 if (record.getAction() != null) {
76 act = record.getAction();
78 replyBuffer.put((byte) ((act.getIntValue() << 5) | //
79 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
80 replyBuffer.position(replyBuffer.position() + Length.RESERVED);
81 replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
82 if (record.getEid() != null && record.getEid().getAddress() != null) {
83 LispAddressSerializer.getInstance().serialize(replyBuffer, record.getEid());
86 if (record.getLocatorRecord() != null) {
87 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
88 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
93 public int getSerializationSize(MappingRecord record) {
94 int size = Length.HEADER_SIZE;
95 if (record.getEid() != null) {
96 size += LispAddressSerializer.getInstance().getAddressSize(record.getEid());
98 if (record.getLocatorRecord() != null) {
99 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
100 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
106 private interface Flags {
107 int AUTHORITATIVE = 0x10;
110 private interface Length {
111 int HEADER_SIZE = 10;