View Javadoc

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  
13  import org.apache.commons.lang3.BooleanUtils;
14  import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
15  import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
16  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
17  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
18  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
19  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
20  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
21  
22  /**
23   * This class deals with serializing map reply from the java object to udp.
24   */
25  public final class MapReplySerializer {
26  
27      private static final MapReplySerializer INSTANCE = new MapReplySerializer();
28  
29      // Private constructor prevents instantiation from other classes
30      private MapReplySerializer() {
31      }
32  
33      public static MapReplySerializer getInstance() {
34          return INSTANCE;
35      }
36  
37      public ByteBuffer serialize(MapReply mapReply) {
38          int size = Length.HEADER_SIZE;
39          for (MappingRecordItem eidToLocatorRecord : mapReply.getMappingRecordItem()) {
40              size += MappingRecordSerializer.getInstance().getSerializationSize(eidToLocatorRecord.getMappingRecord());
41          }
42  
43          ByteBuffer replyBuffer = ByteBuffer.allocate(size);
44  
45          replyBuffer.put((byte) ((MessageType.MapReply.getIntValue() << 4) | //
46                  (BooleanUtils.isTrue(mapReply.isProbe()) ? Flags.PROBE : 0x00) | //
47                  (BooleanUtils.isTrue(mapReply.isEchoNonceEnabled()) ? Flags.ECHO_NONCE_ENABLED : 0x00)));
48  
49          replyBuffer.position(replyBuffer.position() + Length.RES);
50          if (mapReply.getMappingRecordItem() != null) {
51              replyBuffer.put((byte) mapReply.getMappingRecordItem().size());
52          } else {
53              replyBuffer.put((byte) 0);
54  
55          }
56          replyBuffer.putLong(NumberUtil.asLong(mapReply.getNonce()));
57          if (mapReply.getMappingRecordItem() != null) {
58              for (MappingRecordItem eidToLocatorRecord : mapReply.getMappingRecordItem()) {
59                  MappingRecordSerializer.getInstance().serialize(replyBuffer, eidToLocatorRecord.getMappingRecord());
60              }
61          }
62          return replyBuffer;
63      }
64  
65      public MapReply deserialize(ByteBuffer replyBuffer) {
66          MapReplyBuilder builder = new MapReplyBuilder();
67          byte typeAndFlags = replyBuffer.get();
68          builder.setProbe(ByteUtil.extractBit(typeAndFlags, Flags.PROBE));
69          builder.setEchoNonceEnabled(ByteUtil.extractBit(typeAndFlags, Flags.ECHO_NONCE_ENABLED));
70          builder.setSecurityEnabled(ByteUtil.extractBit(typeAndFlags, Flags.SECURITY_ENABLED));
71          replyBuffer.getShort();
72          int recordCount = ByteUtil.getUnsignedByte(replyBuffer);
73          builder.setNonce(replyBuffer.getLong());
74          builder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
75          for (int i = 0; i < recordCount; i++) {
76              builder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(
77                      MappingRecordSerializer.getInstance().deserialize(replyBuffer)).build());
78          }
79  
80          return builder.build();
81      }
82  
83      private interface Length {
84          int RES = 2;
85          int HEADER_SIZE = 12;
86      }
87  
88      private interface Flags {
89          int PROBE = 0x08;
90          int ECHO_NONCE_ENABLED = 0x04;
91          int SECURITY_ENABLED = 0x02;
92      }
93  
94  }