24a2c709c4c00b1308dfe8a15d15ebfb257515ab
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / 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.lisp.serializer;
9
10 import java.nio.ByteBuffer;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.apache.commons.lang3.BooleanUtils;
14 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
15 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
16 import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
25
26 /**
27  * This class deals with serializing map notify from the java object to udp.
28  */
29 public final class MapNotifySerializer {
30
31     private static final MapNotifySerializer INSTANCE = new MapNotifySerializer();
32
33     // Private constructor prevents instantiation from other classes
34     private MapNotifySerializer() {
35     }
36
37     public static MapNotifySerializer getInstance() {
38         return INSTANCE;
39     }
40
41     public ByteBuffer serialize(MapNotify mapNotify) {
42         int size = Length.HEADER_SIZE;
43         if (mapNotify.getAuthenticationData() != null) {
44             size += mapNotify.getAuthenticationData().length;
45         }
46         if (mapNotify.isXtrSiteIdPresent() != null && mapNotify.isXtrSiteIdPresent()) {
47             size += org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer.Length.XTRID_SIZE
48                   + org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer.Length.SITEID_SIZE;
49         }
50         for (MappingRecordItem mappingRecord : mapNotify.getMappingRecordItem()) {
51             size += MappingRecordSerializer.getInstance().getSerializationSize(mappingRecord.getMappingRecord());
52         }
53
54         ByteBuffer replyBuffer = ByteBuffer.allocate(size);
55         replyBuffer.put((byte) ((byte) (MessageType.MapNotify.getIntValue() << 4)
56                 | ByteUtil.boolToBit(BooleanUtils.isTrue(mapNotify.isXtrSiteIdPresent()), Flags.XTRSITEID)));
57         replyBuffer.position(replyBuffer.position() + Length.RES);
58         replyBuffer.put(ByteUtil.boolToBit(BooleanUtils.isTrue(mapNotify.isMergeEnabled()), Flags.MERGE_ENABLED));
59         if (mapNotify.getMappingRecordItem() != null) {
60             replyBuffer.put((byte) mapNotify.getMappingRecordItem().size());
61         } else {
62             replyBuffer.put((byte) 0);
63         }
64         replyBuffer.putLong(NumberUtil.asLong(mapNotify.getNonce()));
65         replyBuffer.putShort(NumberUtil.asShort(mapNotify.getKeyId()));
66         if (mapNotify.getAuthenticationData() != null) {
67             replyBuffer.putShort((short) mapNotify.getAuthenticationData().length);
68             replyBuffer.put(mapNotify.getAuthenticationData());
69         } else {
70             replyBuffer.putShort((short) 0);
71         }
72
73         if (mapNotify.getMappingRecordItem() != null) {
74             for (MappingRecordItem mappingRecord : mapNotify.getMappingRecordItem()) {
75                 MappingRecordSerializer.getInstance().serialize(replyBuffer, mappingRecord.getMappingRecord());
76             }
77         }
78
79         if (mapNotify.isXtrSiteIdPresent() != null && mapNotify.isXtrSiteIdPresent()) {
80             replyBuffer.put(mapNotify.getXtrId().getValue());
81             replyBuffer.put(mapNotify.getSiteId().getValue());
82         }
83         replyBuffer.clear();
84         return replyBuffer;
85     }
86
87     @SuppressWarnings("checkstyle:IllegalCatch")
88     public MapNotify deserialize(ByteBuffer notifyBuffer) {
89         try {
90             final byte typeAndFlags = notifyBuffer.get();
91             final int type = typeAndFlags >> 4;
92             if (MessageType.forValue(type) != MessageType.MapNotify) {
93                 throw new LispSerializationException("Expected Map-Notify packet (type 4), but was type " + type);
94             }
95
96             MapNotifyBuilder builder = new MapNotifyBuilder();
97             builder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
98
99             boolean xtrSiteIdPresent = ByteUtil.extractBit(typeAndFlags, Flags.XTRSITEID);
100             builder.setXtrSiteIdPresent(xtrSiteIdPresent);
101
102             notifyBuffer.position(notifyBuffer.position() + Length.RES);
103             builder.setMergeEnabled(ByteUtil.extractBit(notifyBuffer.get(), Flags.MERGE_ENABLED));
104
105             byte recordCount = (byte) ByteUtil.getUnsignedByte(notifyBuffer);
106             builder.setNonce(notifyBuffer.getLong());
107             builder.setKeyId(notifyBuffer.getShort());
108             short authenticationLength = notifyBuffer.getShort();
109             byte[] authenticationData = new byte[authenticationLength];
110             notifyBuffer.get(authenticationData);
111             builder.setAuthenticationData(authenticationData);
112
113             if (xtrSiteIdPresent) {
114                 List<MappingRecordBuilder> mrbs = new ArrayList<MappingRecordBuilder>();
115                 for (int i = 0; i < recordCount; i++) {
116                     mrbs.add(MappingRecordSerializer.getInstance().deserializeToBuilder(notifyBuffer));
117                 }
118                 byte[] xtrIdBuf  = new byte[MapRegisterSerializer.Length.XTRID_SIZE];
119                 notifyBuffer.get(xtrIdBuf);
120                 XtrId xtrId = new XtrId(xtrIdBuf);
121                 byte[] siteIdBuf = new byte[MapRegisterSerializer.Length.SITEID_SIZE];
122                 notifyBuffer.get(siteIdBuf);
123                 SiteId siteId = new SiteId(siteIdBuf);
124                 builder.setXtrId(xtrId);
125                 builder.setSiteId(siteId);
126                 for (MappingRecordBuilder mrb : mrbs) {
127                     mrb.setXtrId(xtrId);
128                     mrb.setSiteId(siteId);
129                     builder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(
130                             mrb.build()).build());
131                 }
132             } else {
133                 for (int i = 0; i < recordCount; i++) {
134                     builder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(
135                             MappingRecordSerializer.getInstance().deserialize(notifyBuffer)).build());
136                 }
137             }
138
139             notifyBuffer.limit(notifyBuffer.position());
140             return builder.build();
141         } catch (RuntimeException re) {
142             throw new LispSerializationException("Couldn't deserialize Map-Notify (len="
143                     + notifyBuffer.capacity() + ")", re);
144         }
145     }
146
147     private interface Flags {
148         byte XTRSITEID = 0x08;
149         byte MERGE_ENABLED = 0x04;
150     }
151
152     private interface Length {
153         int HEADER_SIZE = 16;
154         int RES = 1;
155     }
156
157 }