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.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
14 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
15 import org.opendaylight.lispflowmapping.implementation.util.NumberUtil;
16 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
17 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
18 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
19 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
22 * This class deals with serializing map notify from the java object to udp.
24 public class MapNotifySerializer {
26 private static final MapNotifySerializer INSTANCE = new MapNotifySerializer();
28 // Private constructor prevents instantiation from other classes
29 private MapNotifySerializer() {
32 public static MapNotifySerializer getInstance() {
36 public ByteBuffer serialize(MapNotify mapNotify) {
37 int size = Length.HEADER_SIZE;
38 if (mapNotify.getAuthenticationData() != null) {
39 size += mapNotify.getAuthenticationData().length;
41 for (EidToLocatorRecord eidToLocatorRecord : mapNotify.getEidToLocatorRecord()) {
42 size += EidToLocatorRecordSerializer.getInstance().getSerializationSize(eidToLocatorRecord);
45 ByteBuffer replyBuffer = ByteBuffer.allocate(size);
46 replyBuffer.put((byte) (LispMessageEnum.MapNotify.getValue() << 4));
47 replyBuffer.position(replyBuffer.position() + Length.RES);
48 if (mapNotify.getEidToLocatorRecord() != null) {
49 replyBuffer.put((byte) mapNotify.getEidToLocatorRecord().size());
51 replyBuffer.put((byte) 0);
53 replyBuffer.putLong(NumberUtil.asLong(mapNotify.getNonce()));
54 replyBuffer.putShort(NumberUtil.asShort(mapNotify.getKeyId()));
55 if (mapNotify.getAuthenticationData() != null) {
56 replyBuffer.putShort((short) mapNotify.getAuthenticationData().length);
57 replyBuffer.put(mapNotify.getAuthenticationData());
59 replyBuffer.putShort((short) 0);
62 if (mapNotify.getEidToLocatorRecord() != null) {
63 for (EidToLocatorRecord eidToLocatorRecord : mapNotify.getEidToLocatorRecord()) {
64 EidToLocatorRecordSerializer.getInstance().serialize(replyBuffer, eidToLocatorRecord);
71 public MapNotify deserialize(ByteBuffer notifyBuffer) {
73 MapNotifyBuilder builder = new MapNotifyBuilder();
74 builder.setProxyMapReply(ByteUtil.extractBit(notifyBuffer.get(), Flags.PROXY));
76 notifyBuffer.position(notifyBuffer.position() + Length.RES);
78 byte recordCount = (byte) ByteUtil.getUnsignedByte(notifyBuffer);
79 builder.setNonce(notifyBuffer.getLong());
80 builder.setKeyId(notifyBuffer.getShort());
81 short authenticationLength = notifyBuffer.getShort();
82 byte[] authenticationData = new byte[authenticationLength];
83 notifyBuffer.get(authenticationData);
84 builder.setAuthenticationData(authenticationData);
86 builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
87 for (int i = 0; i < recordCount; i++) {
88 builder.getEidToLocatorRecord().add(
89 new EidToLocatorRecordBuilder(EidToLocatorRecordSerializer.getInstance().deserialize(notifyBuffer)).build());
91 notifyBuffer.limit(notifyBuffer.position());
92 return builder.build();
93 } catch (RuntimeException re) {
94 throw new LispSerializationException("Couldn't deserialize Map-Notify (len=" + notifyBuffer.capacity() + ")", re);
98 private interface Flags {
102 private interface Length {
103 int HEADER_SIZE = 16;