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.lisp.exception.LispSerializationException;
15 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
16 import org.opendaylight.lispflowmapping.implementation.util.NumberUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecordBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapregisternotification.MapRegisterBuilder;
23 * This class deals with deserializing map register from udp to the java object.
25 public class MapRegisterSerializer {
27 private static final MapRegisterSerializer INSTANCE = new MapRegisterSerializer();
29 // Private constructor prevents instantiation from other classes
30 private MapRegisterSerializer() {
33 public static MapRegisterSerializer getInstance() {
37 public ByteBuffer serialize(MapRegister mapRegister) {
38 int size = Length.HEADER_SIZE;
39 if (mapRegister.getAuthenticationData() != null) {
40 size += mapRegister.getAuthenticationData().length;
42 if (mapRegister.isXtrSiteIdPresent() != null && mapRegister.isXtrSiteIdPresent()) {
43 size += Length.XTRID_SIZE + Length.SITEID_SIZE;
45 for (EidToLocatorRecord eidToLocatorRecord : mapRegister.getEidToLocatorRecord()) {
46 size += EidToLocatorRecordSerializer.getInstance().getSerializationSize(eidToLocatorRecord);
49 ByteBuffer registerBuffer = ByteBuffer.allocate(size);
50 registerBuffer.put((byte) ((byte) (LispMessageEnum.MapRegister.getValue() << 4) |
51 ByteUtil.boolToBit(BooleanUtils.isTrue(mapRegister.isProxyMapReply()), Flags.PROXY) |
52 ByteUtil.boolToBit(BooleanUtils.isTrue(mapRegister.isXtrSiteIdPresent()), Flags.XTRSITEID)));
53 registerBuffer.position(registerBuffer.position() + Length.RES);
54 registerBuffer.put(ByteUtil.boolToBit(BooleanUtils.isTrue(mapRegister.isWantMapNotify()), Flags.WANT_MAP_REPLY));
55 registerBuffer.put((byte) mapRegister.getEidToLocatorRecord().size());
56 registerBuffer.putLong(NumberUtil.asLong(mapRegister.getNonce()));
57 registerBuffer.putShort(NumberUtil.asShort(mapRegister.getKeyId()));
59 if (mapRegister.getAuthenticationData() != null) {
60 registerBuffer.putShort((short) mapRegister.getAuthenticationData().length);
61 registerBuffer.put(mapRegister.getAuthenticationData());
63 registerBuffer.putShort((short) 0);
65 for (EidToLocatorRecord eidToLocatorRecord : mapRegister.getEidToLocatorRecord()) {
66 EidToLocatorRecordSerializer.getInstance().serialize(registerBuffer, eidToLocatorRecord);
69 if (mapRegister.isXtrSiteIdPresent() != null && mapRegister.isXtrSiteIdPresent()) {
70 registerBuffer.put(mapRegister.getXtrId());
71 registerBuffer.put(mapRegister.getSiteId());
73 registerBuffer.clear();
74 return registerBuffer;
77 public MapRegister deserialize(ByteBuffer registerBuffer) {
79 MapRegisterBuilder builder = new MapRegisterBuilder();
80 builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
82 byte typeAndFlags = registerBuffer.get();
83 boolean xtrSiteIdPresent = ByteUtil.extractBit(typeAndFlags, Flags.XTRSITEID);
84 builder.setProxyMapReply(ByteUtil.extractBit(typeAndFlags, Flags.PROXY));
85 builder.setXtrSiteIdPresent(xtrSiteIdPresent);
87 registerBuffer.position(registerBuffer.position() + Length.RES);
88 builder.setWantMapNotify(ByteUtil.extractBit(registerBuffer.get(), Flags.WANT_MAP_REPLY));
89 byte recordCount = (byte) ByteUtil.getUnsignedByte(registerBuffer);
90 builder.setNonce(registerBuffer.getLong());
91 builder.setKeyId(registerBuffer.getShort());
92 short authenticationLength = registerBuffer.getShort();
93 byte[] authenticationData = new byte[authenticationLength];
94 registerBuffer.get(authenticationData);
95 builder.setAuthenticationData(authenticationData);
97 for (int i = 0; i < recordCount; i++) {
98 builder.getEidToLocatorRecord().add(
99 new EidToLocatorRecordBuilder(EidToLocatorRecordSerializer.getInstance().deserialize(registerBuffer)).build());
102 if (xtrSiteIdPresent) {
103 byte[] xtrId = new byte[Length.XTRID_SIZE];
104 registerBuffer.get(xtrId);
105 byte[] siteId = new byte[Length.SITEID_SIZE];
106 registerBuffer.get(siteId);
107 builder.setXtrId(xtrId);
108 builder.setSiteId(siteId);
110 registerBuffer.limit(registerBuffer.position());
111 byte[] mapRegisterBytes = new byte[registerBuffer.position()];
112 registerBuffer.position(0);
113 registerBuffer.get(mapRegisterBytes);
114 return builder.build();
115 } catch (RuntimeException re) {
116 throw new LispSerializationException("Couldn't deserialize Map-Register (len=" + registerBuffer.capacity() + ")", re);
121 private interface Flags {
123 byte XTRSITEID = 0x02;
124 byte WANT_MAP_REPLY = 0x01;
127 public interface Length {
128 int HEADER_SIZE = 16;