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