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.util.ByteUtil;
16 import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
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.EidToLocatorRecord;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.EidToLocatorRecord.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispAFIAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eidtolocatorrecords.EidToLocatorRecordBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.LispAddressContainer;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
26 public class EidToLocatorRecordSerializer {
28 private static final EidToLocatorRecordSerializer INSTANCE = new EidToLocatorRecordSerializer();
30 // Private constructor prevents instantiation from other classes
31 private EidToLocatorRecordSerializer() {
34 public static EidToLocatorRecordSerializer getInstance() {
38 public EidToLocatorRecord deserialize(ByteBuffer buffer) {
39 EidToLocatorRecordBuilder builder = new EidToLocatorRecordBuilder();
40 builder.setRecordTtl(buffer.getInt());
41 byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
42 builder.setMaskLength((short) ByteUtil.getUnsignedByte(buffer));
43 byte actionAndAuthoritative = buffer.get();
44 Action act = Action.forValue(actionAndAuthoritative >> 5);
46 act = Action.NoAction;
48 builder.setAction(act);
49 builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
50 buffer.position(buffer.position() + Length.RESERVED);
51 builder.setMapVersion(buffer.getShort());
53 LispAFIAddress afiAddress = LispAddressSerializer.getInstance().deserialize(buffer);
54 afiAddress = MaskUtil.fixMask(afiAddress, builder.getMaskLength());
55 LispAddressContainer container = LispAFIConvertor.toContainer(afiAddress);
56 builder.setLispAddressContainer(container);
58 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
59 for (int i = 0; i < locatorCount; i++) {
60 builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
63 return builder.build();
66 public void serialize(ByteBuffer replyBuffer, EidToLocatorRecord record) {
67 replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
68 if (record.getLocatorRecord() != null) {
69 replyBuffer.put((byte) record.getLocatorRecord().size());
71 replyBuffer.put((byte) 0);
73 replyBuffer.put((byte) NumberUtil.asShort(record.getMaskLength()));
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.getLispAddressContainer() != null && record.getLispAddressContainer().getAddress() != null) {
83 LispAddressSerializer.getInstance().serialize(replyBuffer, LispAFIConvertor.toAFI(record.getLispAddressContainer()));
86 if (record.getLocatorRecord() != null) {
87 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
88 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
93 public int getSerializationSize(EidToLocatorRecord record) {
94 int size = Length.HEADER_SIZE;
95 if (record.getLispAddressContainer() != null) {
96 size += LispAddressSerializer.getInstance().getAddressSize((LispAFIConvertor.toAFI(record.getLispAddressContainer())));
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;