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.implementation.serializer;
10 import java.nio.ByteBuffer;
11 import java.util.ArrayList;
13 import org.apache.commons.lang3.BooleanUtils;
14 import org.opendaylight.lispflowmapping.implementation.serializer.address.LispAddressSerializer;
15 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
16 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
17 import org.opendaylight.lispflowmapping.implementation.util.NumberUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.EidToLocatorRecord;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.EidToLocatorRecord.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecordBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.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 LispAddressContainer container = LispAFIConvertor.toContainer(afiAddress);
54 builder.setLispAddressContainer(container);
56 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
57 for (int i = 0; i < locatorCount; i++) {
58 builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
61 return builder.build();
64 public void serialize(ByteBuffer replyBuffer, EidToLocatorRecord record) {
65 replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
66 if (record.getLocatorRecord() != null) {
67 replyBuffer.put((byte) record.getLocatorRecord().size());
69 replyBuffer.put((byte) 0);
71 replyBuffer.put((byte) NumberUtil.asShort(record.getMaskLength()));
72 Action act = Action.NoAction;
73 if (record.getAction() != null) {
74 act = record.getAction();
76 replyBuffer.put((byte) ((act.getIntValue() << 5) | //
77 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
78 replyBuffer.position(replyBuffer.position() + Length.RESERVED);
79 replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
80 if (record.getLispAddressContainer() != null && record.getLispAddressContainer().getAddress() != null) {
81 LispAddressSerializer.getInstance().serialize(replyBuffer, LispAFIConvertor.toAFI(record.getLispAddressContainer()));
84 if (record.getLocatorRecord() != null) {
85 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
86 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
91 public int getSerializationSize(EidToLocatorRecord record) {
92 int size = Length.HEADER_SIZE;
93 if (record.getLispAddressContainer() != null) {
94 size += LispAddressSerializer.getInstance().getAddressSize((LispAFIConvertor.toAFI(record.getLispAddressContainer())));
96 if (record.getLocatorRecord() != null) {
97 for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
98 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
104 private interface Flags {
105 int AUTHORITATIVE = 0x10;
108 private interface Length {
109 int HEADER_SIZE = 10;