Checkstyle: fix issues and enforce on lisp-proto
[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 final 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         return deserializeToBuilder(buffer).build();
38     }
39
40     public MappingRecordBuilder deserializeToBuilder(ByteBuffer buffer) {
41         MappingRecordBuilder builder = new MappingRecordBuilder();
42         builder.setRecordTtl(buffer.getInt());
43         final byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
44         final short maskLength = ((short) ByteUtil.getUnsignedByte(buffer));
45         final byte actionAndAuthoritative = buffer.get();
46         Action act = Action.forValue(actionAndAuthoritative >> 5);
47         if (act == null) {
48             act = Action.NoAction;
49         }
50         builder.setAction(act);
51         builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
52         buffer.position(buffer.position() + Length.RESERVED);
53         builder.setMapVersion(buffer.getShort());
54
55         LispAddressSerializerContext ctx = new LispAddressSerializerContext(maskLength);
56         builder.setEid(LispAddressSerializer.getInstance().deserializeEid(buffer, ctx));
57
58         builder.setLocatorRecord(new ArrayList<LocatorRecord>());
59         for (int i = 0; i < locatorCount; i++) {
60             builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
61         }
62
63         builder.setTimestamp(System.currentTimeMillis());
64
65         return builder;
66     }
67
68     public void serialize(ByteBuffer replyBuffer, MappingRecord record) {
69         replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
70         if (record.getLocatorRecord() != null) {
71             replyBuffer.put((byte) record.getLocatorRecord().size());
72         } else {
73             replyBuffer.put((byte) 0);
74         }
75         if (record.getEid() != null && MaskUtil.getMaskForAddress(record.getEid().getAddress()) != -1) {
76             replyBuffer.put((byte) NumberUtil.asShort(MaskUtil.getMaskForAddress(record.getEid().getAddress())));
77         } else {
78             replyBuffer.put((byte) 0);
79         }
80         Action act = Action.NoAction;
81         if (record.getAction() != null) {
82             act = record.getAction();
83         }
84         replyBuffer.put((byte) ((act.getIntValue() << 5) | //
85                 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
86         replyBuffer.position(replyBuffer.position() + Length.RESERVED);
87         replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
88         if (record.getEid() != null && record.getEid().getAddress() != null) {
89             LispAddressSerializer.getInstance().serialize(replyBuffer, record.getEid());
90         }
91
92         if (record.getLocatorRecord() != null) {
93             for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
94                 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
95             }
96         }
97     }
98
99     public int getSerializationSize(MappingRecord record) {
100         int size = Length.HEADER_SIZE;
101         if (record.getEid() != null) {
102             size += LispAddressSerializer.getInstance().getAddressSize(record.getEid());
103         }
104         if (record.getLocatorRecord() != null) {
105             for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
106                 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
107             }
108         }
109         return size;
110     }
111
112     private interface Flags {
113         int AUTHORITATIVE = 0x10;
114     }
115
116     private interface Length {
117         int HEADER_SIZE = 10;
118         int RESERVED = 1;
119     }
120 }