Move integration tests to mdsal-it-parent
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / serializer / MapRegisterSerializer.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.serializer;
9
10 import java.nio.ByteBuffer;
11 import java.util.ArrayList;
12
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;
21
22 /**
23  * This class deals with deserializing map register from udp to the java object.
24  */
25 public class MapRegisterSerializer {
26
27     private static final MapRegisterSerializer INSTANCE = new MapRegisterSerializer();
28
29     // Private constructor prevents instantiation from other classes
30     private MapRegisterSerializer() {
31     }
32
33     public static MapRegisterSerializer getInstance() {
34         return INSTANCE;
35     }
36
37     public ByteBuffer serialize(MapRegister mapRegister) {
38         int size = Length.HEADER_SIZE;
39         if (mapRegister.getAuthenticationData() != null) {
40             size += mapRegister.getAuthenticationData().length;
41         }
42         if (mapRegister.isXtrSiteIdPresent() != null && mapRegister.isXtrSiteIdPresent()) {
43             size += Length.XTRID_SIZE + Length.SITEID_SIZE;
44         }
45         for (EidToLocatorRecord eidToLocatorRecord : mapRegister.getEidToLocatorRecord()) {
46             size += EidToLocatorRecordSerializer.getInstance().getSerializationSize(eidToLocatorRecord);
47         }
48
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()));
58
59         if (mapRegister.getAuthenticationData() != null) {
60             registerBuffer.putShort((short) mapRegister.getAuthenticationData().length);
61             registerBuffer.put(mapRegister.getAuthenticationData());
62         } else {
63             registerBuffer.putShort((short) 0);
64         }
65         for (EidToLocatorRecord eidToLocatorRecord : mapRegister.getEidToLocatorRecord()) {
66             EidToLocatorRecordSerializer.getInstance().serialize(registerBuffer, eidToLocatorRecord);
67         }
68
69         if (mapRegister.isXtrSiteIdPresent() != null && mapRegister.isXtrSiteIdPresent()) {
70             registerBuffer.put(mapRegister.getXtrId());
71             registerBuffer.put(mapRegister.getSiteId());
72         }
73         registerBuffer.clear();
74         return registerBuffer;
75     }
76
77     public MapRegister deserialize(ByteBuffer registerBuffer) {
78         try {
79             MapRegisterBuilder builder = new MapRegisterBuilder();
80             builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
81
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);
86
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);
96
97             for (int i = 0; i < recordCount; i++) {
98                 builder.getEidToLocatorRecord().add(
99                         new EidToLocatorRecordBuilder(EidToLocatorRecordSerializer.getInstance().deserialize(registerBuffer)).build());
100             }
101
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);
109             }
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);
117         }
118
119     }
120
121     private interface Flags {
122         byte PROXY = 0x08;
123         byte XTRSITEID = 0x02;
124         byte WANT_MAP_REPLY = 0x01;
125     }
126
127     public interface Length {
128         int HEADER_SIZE = 16;
129         int XTRID_SIZE = 16;
130         int SITEID_SIZE = 8;
131         int RES = 1;
132     }
133 }