6a7f6c836e5eb7bf5d1ea460fa643f845f427af5
[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.MaskUtil;
18 import org.opendaylight.lispflowmapping.lisp.util.NumberUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.EidToLocatorRecord;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.EidToLocatorRecord.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispAFIAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eidtolocatorrecords.EidToLocatorRecordBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.LispAddressContainer;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
25
26 public class EidToLocatorRecordSerializer {
27
28     private static final EidToLocatorRecordSerializer INSTANCE = new EidToLocatorRecordSerializer();
29
30     // Private constructor prevents instantiation from other classes
31     private EidToLocatorRecordSerializer() {
32     }
33
34     public static EidToLocatorRecordSerializer getInstance() {
35         return INSTANCE;
36     }
37
38     public EidToLocatorRecord deserialize(ByteBuffer buffer) {
39         EidToLocatorRecordBuilder builder = new EidToLocatorRecordBuilder();
40         builder.setRecordTtl(buffer.getInt());
41         byte locatorCount = (byte) ByteUtil.getUnsignedByte(buffer);
42         builder.setMaskLength((short) ByteUtil.getUnsignedByte(buffer));
43         byte actionAndAuthoritative = buffer.get();
44         Action act = Action.forValue(actionAndAuthoritative >> 5);
45         if (act == null) {
46             act = Action.NoAction;
47         }
48         builder.setAction(act);
49         builder.setAuthoritative(ByteUtil.extractBit(actionAndAuthoritative, Flags.AUTHORITATIVE));
50         buffer.position(buffer.position() + Length.RESERVED);
51         builder.setMapVersion(buffer.getShort());
52
53         LispAFIAddress afiAddress = LispAddressSerializer.getInstance().deserialize(buffer);
54         afiAddress = MaskUtil.fixMask(afiAddress, builder.getMaskLength());
55         LispAddressContainer container = LispAFIConvertor.toContainer(afiAddress);
56         builder.setLispAddressContainer(container);
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         return builder.build();
64     }
65
66     public void serialize(ByteBuffer replyBuffer, EidToLocatorRecord record) {
67         replyBuffer.putInt(NumberUtil.asInt(record.getRecordTtl()));
68         if (record.getLocatorRecord() != null) {
69             replyBuffer.put((byte) record.getLocatorRecord().size());
70         } else {
71             replyBuffer.put((byte) 0);
72         }
73         replyBuffer.put((byte) NumberUtil.asShort(record.getMaskLength()));
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.getLispAddressContainer() != null && record.getLispAddressContainer().getAddress() != null) {
83             LispAddressSerializer.getInstance().serialize(replyBuffer, LispAFIConvertor.toAFI(record.getLispAddressContainer()));
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(EidToLocatorRecord record) {
94         int size = Length.HEADER_SIZE;
95         if (record.getLispAddressContainer() != null) {
96             size += LispAddressSerializer.getInstance().getAddressSize((LispAFIConvertor.toAFI(record.getLispAddressContainer())));
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 }