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.NumberUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.EidToLocatorRecord;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.EidToLocatorRecord.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.LispAFIAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidtolocatorrecords.EidToLocatorRecordBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.LispAddressContainer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.locatorrecords.LocatorRecord;
25 public class EidToLocatorRecordSerializer {
27 private static final EidToLocatorRecordSerializer INSTANCE = new EidToLocatorRecordSerializer();
29 // Private constructor prevents instantiation from other classes
30 private EidToLocatorRecordSerializer() {
33 public static EidToLocatorRecordSerializer getInstance() {
37 public EidToLocatorRecord deserialize(ByteBuffer buffer) {
38 EidToLocatorRecordBuilder builder = new EidToLocatorRecordBuilder();
39 builder.setRecordTtl(buffer.getInt());
40 byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
41 builder.setMaskLength((short) ByteUtil.getUnsignedByte(buffer));
42 byte actionAndAuthoritative = buffer.get();
43 Action act = Action.forValue(actionAndAuthoritative >> 5);
45 act = Action.NoAction;
47 builder.setAction(act);
48 builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
49 buffer.position(buffer.position() + Length.RESERVED);
50 builder.setMapVersion(buffer.getShort());
52 LispAFIAddress afiAddress = LispAddressSerializer.getInstance().deserialize(buffer);
53 afiAddress = SerializerHelper.fixMask(afiAddress, builder.getMaskLength());
54 LispAddressContainer container = LispAFIConvertor.toContainer(afiAddress);
55 builder.setLispAddressContainer(container);
57 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
58 for (int i = 0; i < locatorCount; i++) {
59 builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
62 return builder.build();
65 public void serialize(ByteBuffer replyBuffer, EidToLocatorRecord record) {
66 replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
67 if (record.getLocatorRecord() != null) {
68 replyBuffer.put((byte) record.getLocatorRecord().size());
70 replyBuffer.put((byte) 0);
72 replyBuffer.put((byte) NumberUtil.asShort(record.getMaskLength()));
73 Action act = Action.NoAction;
74 if (record.getAction() != null) {
75 act = record.getAction();
77 replyBuffer.put((byte) ((act.getIntValue() << 5) | //
78 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
79 replyBuffer.position(replyBuffer.position() + Length.RESERVED);
80 replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
81 if (record.getLispAddressContainer() != null && record.getLispAddressContainer().getAddress() != null) {
82 LispAddressSerializer.getInstance().serialize(replyBuffer, LispAFIConvertor.toAFI(record.getLispAddressContainer()));
85 if (record.getLocatorRecord() != null) {
86 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
87 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
92 public int getSerializationSize(EidToLocatorRecord record) {
93 int size = Length.HEADER_SIZE;
94 if (record.getLispAddressContainer() != null) {
95 size += LispAddressSerializer.getInstance().getAddressSize((LispAFIConvertor.toAFI(record.getLispAddressContainer())));
97 if (record.getLocatorRecord() != null) {
98 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
99 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
105 private interface Flags {
106 int AUTHORITATIVE = 0x10;
109 private interface Length {
110 int HEADER_SIZE = 10;