fixed map request yang model, changed embeded map reply to be an eid to locator record
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / serializer / MapNotifySerializer.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.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;
20
21 /**
22  * This class deals with serializing map notify from the java object to udp.
23  */
24 public class MapNotifySerializer {
25
26     private static final MapNotifySerializer INSTANCE = new MapNotifySerializer();
27
28     // Private constructor prevents instantiation from other classes
29     private MapNotifySerializer() {
30     }
31
32     public static MapNotifySerializer getInstance() {
33         return INSTANCE;
34     }
35
36     public ByteBuffer serialize(MapNotify mapNotify) {
37         int size = Length.HEADER_SIZE;
38         if (mapNotify.getAuthenticationData() != null) {
39             size += mapNotify.getAuthenticationData().length;
40         }
41         for (EidToLocatorRecord eidToLocatorRecord : mapNotify.getEidToLocatorRecord()) {
42             size += EidToLocatorRecordSerializer.getInstance().getSerializationSize(eidToLocatorRecord);
43         }
44
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());
50         } else {
51             replyBuffer.put((byte) 0);
52         }
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());
58         } else {
59             replyBuffer.putShort((short) 0);
60         }
61
62         if (mapNotify.getEidToLocatorRecord() != null) {
63             for (EidToLocatorRecord eidToLocatorRecord : mapNotify.getEidToLocatorRecord()) {
64                 EidToLocatorRecordSerializer.getInstance().serialize(replyBuffer, eidToLocatorRecord);
65             }
66         }
67         replyBuffer.clear();
68         return replyBuffer;
69     }
70
71     public MapNotify deserialize(ByteBuffer notifyBuffer) {
72         try {
73             MapNotifyBuilder builder = new MapNotifyBuilder();
74             builder.setProxyMapReply(ByteUtil.extractBit(notifyBuffer.get(), Flags.PROXY));
75
76             notifyBuffer.position(notifyBuffer.position() + Length.RES);
77
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);
85
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());
90             }
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);
95         }
96     }
97
98     private interface Flags {
99         byte PROXY = 0x08;
100     }
101
102     private interface Length {
103         int HEADER_SIZE = 16;
104         int RES = 2;
105     }
106
107 }