Merge branch 'master' into topic/ietf_yang
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / serializer / MappingRecordSerializer.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
13 import org.apache.commons.lang3.BooleanUtils;
14 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
15 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializerContext;
16 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
17 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
18 import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
23
24 public class MappingRecordSerializer {
25
26     private static final MappingRecordSerializer INSTANCE = new MappingRecordSerializer();
27
28     // Private constructor prevents instantiation from other classes
29     private MappingRecordSerializer() {
30     }
31
32     public static MappingRecordSerializer getInstance() {
33         return INSTANCE;
34     }
35
36     public MappingRecord deserialize(ByteBuffer buffer) {
37         MappingRecordBuilder builder = new MappingRecordBuilder();
38         builder.setRecordTtl(buffer.getInt());
39         byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
40         builder.setMaskLength((short) ByteUtil.getUnsignedByte(buffer));
41         byte actionAndAuthoritative = buffer.get();
42         Action act = Action.forValue(actionAndAuthoritative >> 5);
43         if (act == null) {
44             act = Action.NoAction;
45         }
46         builder.setAction(act);
47         builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
48         buffer.position(buffer.position() + Length.RESERVED);
49         builder.setMapVersion(buffer.getShort());
50
51         LispAddressSerializerContext ctx = new LispAddressSerializerContext(builder.getMaskLength());
52         builder.setEid(LispAddressSerializer.getInstance().deserializeEid(buffer, ctx));
53
54         builder.setLocatorRecord(new ArrayList<LocatorRecord>());
55         for (int i = 0; i < locatorCount; i++) {
56             builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
57         }
58
59         return builder.build();
60     }
61
62     public void serialize(ByteBuffer replyBuffer, MappingRecord record) {
63         replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
64         if (record.getLocatorRecord() != null) {
65             replyBuffer.put((byte) record.getLocatorRecord().size());
66         } else {
67             replyBuffer.put((byte) 0);
68         }
69         if (record.getEid() != null && MaskUtil.getMaskForAddress(record.getEid().getAddress()) != -1) {
70             replyBuffer.put((byte) NumberUtil.asShort(MaskUtil.getMaskForAddress(record.getEid().getAddress())));
71         } else {
72             replyBuffer.put((byte) 0);
73         }
74         Action act = Action.NoAction;
75         if (record.getAction() != null) {
76             act = record.getAction();
77         }
78         replyBuffer.put((byte) ((act.getIntValue() << 5) | //
79                 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
80         replyBuffer.position(replyBuffer.position() + Length.RESERVED);
81         replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
82         if (record.getEid() != null && record.getEid().getAddress() != null) {
83             LispAddressSerializer.getInstance().serialize(replyBuffer, record.getEid());
84         }
85
86         if (record.getLocatorRecord() != null) {
87             for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
88                 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
89             }
90         }
91     }
92
93     public int getSerializationSize(MappingRecord record) {
94         int size = Length.HEADER_SIZE;
95         if (record.getEid() != null) {
96             size += LispAddressSerializer.getInstance().getAddressSize(record.getEid());
97         }
98         if (record.getLocatorRecord() != null) {
99             for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
100                 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
101             }
102         }
103         return size;
104     }
105
106     private interface Flags {
107         int AUTHORITATIVE = 0x10;
108     }
109
110     private interface Length {
111         int HEADER_SIZE = 10;
112         int RESERVED = 1;
113     }
114 }