f24e95ee084327b719cc67efb62d9e4f54c26758
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / 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.implementation.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.implementation.serializer.address.LispAddressSerializer;
15 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
16 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
17 import org.opendaylight.lispflowmapping.implementation.util.NumberUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.EidToLocatorRecord;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.EidToLocatorRecord.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecordBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.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         LispAddressContainer container = LispAFIConvertor.toContainer(afiAddress);
54         builder.setLispAddressContainer(container);
55
56         builder.setLocatorRecord(new ArrayList<LocatorRecord>());
57         for (int i = 0; i < locatorCount; i++) {
58             builder.getLocatorRecord().add(LocatorRecordSerializer.getInstance().deserialize(buffer));
59         }
60
61         return builder.build();
62     }
63
64     public void serialize(ByteBuffer replyBuffer, EidToLocatorRecord record) {
65         replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
66         if (record.getLocatorRecord() != null) {
67             replyBuffer.put((byte) record.getLocatorRecord().size());
68         } else {
69             replyBuffer.put((byte) 0);
70         }
71         replyBuffer.put((byte) NumberUtil.asShort(record.getMaskLength()));
72         Action act = Action.NoAction;
73         if (record.getAction() != null) {
74             act = record.getAction();
75         }
76         replyBuffer.put((byte) ((act.getIntValue() << 5) | //
77                 ByteUtil.boolToBit(BooleanUtils.isTrue(record.isAuthoritative()), Flags.AUTHORITATIVE)));
78         replyBuffer.position(replyBuffer.position() + Length.RESERVED);
79         replyBuffer.putShort(NumberUtil.asShort(record.getMapVersion()));
80         if (record.getLispAddressContainer() != null && record.getLispAddressContainer().getAddress() != null) {
81             LispAddressSerializer.getInstance().serialize(replyBuffer, LispAFIConvertor.toAFI(record.getLispAddressContainer()));
82         }
83
84         if (record.getLocatorRecord() != null) {
85             for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
86                 LocatorRecordSerializer.getInstance().serialize(replyBuffer, locatorRecord);
87             }
88         }
89     }
90
91     public int getSerializationSize(EidToLocatorRecord record) {
92         int size = Length.HEADER_SIZE;
93         if (record.getLispAddressContainer() != null) {
94             size += LispAddressSerializer.getInstance().getAddressSize((LispAFIConvertor.toAFI(record.getLispAddressContainer())));
95         }
96         if (record.getLocatorRecord() != null) {
97             for (LocatorRecord locatorRecord : record.getLocatorRecord()) {
98                 size += LocatorRecordSerializer.getInstance().getSerializationSize(locatorRecord);
99             }
100         }
101         return size;
102     }
103
104     private interface Flags {
105         int AUTHORITATIVE = 0x10;
106     }
107
108     private interface Length {
109         int HEADER_SIZE = 10;
110         int RESERVED = 1;
111     }
112 }