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.exception.LispSerializationException;
15 import org.opendaylight.lispflowmapping.lisp.type.LispMessageEnum;
16 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
17 import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eidtolocatorrecords.EidToLocatorRecord;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eidtolocatorrecords.EidToLocatorRecordBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregisternotification.MapRegisterBuilder;
24 * This class deals with deserializing map register from udp to the java object.
26 public class MapRegisterSerializer {
28 private static final MapRegisterSerializer INSTANCE = new MapRegisterSerializer();
30 // Private constructor prevents instantiation from other classes
31 private MapRegisterSerializer() {
34 public static MapRegisterSerializer getInstance() {
38 public ByteBuffer serialize(MapRegister mapRegister) {
39 int size = Length.HEADER_SIZE;
40 if (mapRegister.getAuthenticationData() != null) {
41 size += mapRegister.getAuthenticationData().length;
43 if (mapRegister.isXtrSiteIdPresent() != null && mapRegister.isXtrSiteIdPresent()) {
44 size += Length.XTRID_SIZE + Length.SITEID_SIZE;
46 for (EidToLocatorRecord eidToLocatorRecord : mapRegister.getEidToLocatorRecord()) {
47 size += EidToLocatorRecordSerializer.getInstance().getSerializationSize(eidToLocatorRecord);
50 ByteBuffer registerBuffer = ByteBuffer.allocate(size);
51 registerBuffer.put((byte) ((byte) (LispMessageEnum.MapRegister.getValue() << 4) |
52 ByteUtil.boolToBit(BooleanUtils.isTrue(mapRegister.isProxyMapReply()), Flags.PROXY) |
53 ByteUtil.boolToBit(BooleanUtils.isTrue(mapRegister.isXtrSiteIdPresent()), Flags.XTRSITEID)));
54 registerBuffer.position(registerBuffer.position() + Length.RES);
55 registerBuffer.put(ByteUtil.boolToBit(BooleanUtils.isTrue(mapRegister.isWantMapNotify()), Flags.WANT_MAP_REPLY));
56 registerBuffer.put((byte) mapRegister.getEidToLocatorRecord().size());
57 registerBuffer.putLong(NumberUtil.asLong(mapRegister.getNonce()));
58 registerBuffer.putShort(NumberUtil.asShort(mapRegister.getKeyId()));
60 if (mapRegister.getAuthenticationData() != null) {
61 registerBuffer.putShort((short) mapRegister.getAuthenticationData().length);
62 registerBuffer.put(mapRegister.getAuthenticationData());
64 registerBuffer.putShort((short) 0);
66 for (EidToLocatorRecord eidToLocatorRecord : mapRegister.getEidToLocatorRecord()) {
67 EidToLocatorRecordSerializer.getInstance().serialize(registerBuffer, eidToLocatorRecord);
70 if (mapRegister.isXtrSiteIdPresent() != null && mapRegister.isXtrSiteIdPresent()) {
71 registerBuffer.put(mapRegister.getXtrId());
72 registerBuffer.put(mapRegister.getSiteId());
74 registerBuffer.clear();
75 return registerBuffer;
78 public MapRegister deserialize(ByteBuffer registerBuffer) {
80 MapRegisterBuilder builder = new MapRegisterBuilder();
81 builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
83 byte typeAndFlags = registerBuffer.get();
84 boolean xtrSiteIdPresent = ByteUtil.extractBit(typeAndFlags, Flags.XTRSITEID);
85 builder.setProxyMapReply(ByteUtil.extractBit(typeAndFlags, Flags.PROXY));
86 builder.setXtrSiteIdPresent(xtrSiteIdPresent);
88 registerBuffer.position(registerBuffer.position() + Length.RES);
89 builder.setWantMapNotify(ByteUtil.extractBit(registerBuffer.get(), Flags.WANT_MAP_REPLY));
90 byte recordCount = (byte) ByteUtil.getUnsignedByte(registerBuffer);
91 builder.setNonce(registerBuffer.getLong());
92 builder.setKeyId(registerBuffer.getShort());
93 short authenticationLength = registerBuffer.getShort();
94 byte[] authenticationData = new byte[authenticationLength];
95 registerBuffer.get(authenticationData);
96 builder.setAuthenticationData(authenticationData);
98 for (int i = 0; i < recordCount; i++) {
99 builder.getEidToLocatorRecord().add(
100 new EidToLocatorRecordBuilder(EidToLocatorRecordSerializer.getInstance().deserialize(registerBuffer)).build());
103 if (xtrSiteIdPresent) {
104 byte[] xtrId = new byte[Length.XTRID_SIZE];
105 registerBuffer.get(xtrId);
106 byte[] siteId = new byte[Length.SITEID_SIZE];
107 registerBuffer.get(siteId);
108 builder.setXtrId(xtrId);
109 builder.setSiteId(siteId);
111 registerBuffer.limit(registerBuffer.position());
112 byte[] mapRegisterBytes = new byte[registerBuffer.position()];
113 registerBuffer.position(0);
114 registerBuffer.get(mapRegisterBytes);
115 return builder.build();
116 } catch (RuntimeException re) {
117 throw new LispSerializationException("Couldn't deserialize Map-Register (len=" + registerBuffer.capacity() + ")", re);
122 private interface Flags {
124 byte XTRSITEID = 0x02;
125 byte WANT_MAP_REPLY = 0x01;
128 public interface Length {
129 int HEADER_SIZE = 16;