fixed export problem in lispflowmapping.implementation
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispApplicationDataLCAFAddressSerializer.java
1 package org.opendaylight.lispflowmapping.implementation.serializer.address;
2
3 import java.nio.ByteBuffer;
4
5 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
6 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
7 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
8 import org.opendaylight.lispflowmapping.type.lisp.address.LispApplicationDataLCAFAddress;
9 import org.opendaylight.lispflowmapping.type.lisp.address.LispSegmentLCAFAddress;
10
11 public class LispApplicationDataLCAFAddressSerializer extends LispLCAFAddressSerializer {
12
13     private static final LispApplicationDataLCAFAddressSerializer INSTANCE = new LispApplicationDataLCAFAddressSerializer();
14
15     // Private constructor prevents instantiation from other classes
16     private LispApplicationDataLCAFAddressSerializer() {
17     }
18
19     public static LispApplicationDataLCAFAddressSerializer getInstance() {
20         return INSTANCE;
21     }
22
23     @Override
24     public short getLcafLength(LispAddress lispAddress) {
25         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(((LispApplicationDataLCAFAddress) lispAddress).getAddress()
26                 .getAfi());
27         return (short) (Length.ALL_FIELDS + serializer.getAddressSize(lispAddress));
28     }
29
30     @Override
31     public void serialize(ByteBuffer buffer, LispAddress lispAddress) {
32         super.internalSerialize(buffer, lispAddress);
33         LispApplicationDataLCAFAddress applicationDataAddress = ((LispApplicationDataLCAFAddress) lispAddress);
34         buffer.put(ByteUtil.partialIntToByteArray(applicationDataAddress.getIPTos(), Length.TOC));
35         buffer.put(applicationDataAddress.getProtocol());
36         buffer.putShort(applicationDataAddress.getLocalPort());
37         buffer.putShort(applicationDataAddress.getRemotePort());
38         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(applicationDataAddress.getAddress().getAfi());
39         if (serializer == null) {
40             throw new LispSerializationException("Unknown AFI type=" + ((LispSegmentLCAFAddress) lispAddress).getAddress().getAfi());
41         }
42         serializer.serialize(buffer, ((LispApplicationDataLCAFAddress) lispAddress).getAddress());
43     }
44
45     @Override
46     public LispApplicationDataLCAFAddress innerDeserialize(ByteBuffer buffer, byte res2, short length) {
47
48         LispApplicationDataLCAFAddress applicationDataAddress = new LispApplicationDataLCAFAddress(res2);
49         byte[] rawIPTos = new byte[3];
50         buffer.get(rawIPTos);
51         applicationDataAddress.setIPTos(ByteUtil.getPartialInt(rawIPTos));
52         applicationDataAddress.setProtocol(buffer.get());
53         applicationDataAddress.setLocalPort(buffer.getShort());
54         applicationDataAddress.setRemotePort(buffer.getShort());
55         LispAddress address = LispAddressSerializer.getInstance().deserialize(buffer);
56         applicationDataAddress.setAddress(address);
57
58         return applicationDataAddress;
59     }
60
61     private interface Length {
62         int LOCAL_PORT = 2;
63         int REMOTE_PORT = 2;
64         int TOC = 3;
65         int PROTOCOL = 1;
66         int ALL_FIELDS = LOCAL_PORT + REMOTE_PORT + TOC + PROTOCOL;
67     }
68
69 }