fixed northbound source dest mapping and auth TELSDN-644: #close
[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.util.ByteUtil;
6 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
7 import org.opendaylight.lispflowmapping.implementation.util.NumberUtil;
8 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
9 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
10 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafApplicationDataAddress;
11 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
12 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder;
13 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationDataBuilder;
14 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.PrimitiveAddress;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
16
17 public class LispApplicationDataLCAFAddressSerializer extends LispLCAFAddressSerializer {
18
19     private static final LispApplicationDataLCAFAddressSerializer INSTANCE = new LispApplicationDataLCAFAddressSerializer();
20
21     // Private constructor prevents instantiation from other classes
22     private LispApplicationDataLCAFAddressSerializer() {
23     }
24
25     public static LispApplicationDataLCAFAddressSerializer getInstance() {
26         return INSTANCE;
27     }
28
29     @Override
30     protected short getLcafLength(LispAFIAddress lispAddress) {
31         return (short) (Length.ALL_FIELDS + LispAddressSerializer.getInstance().getAddressSize(
32                 (LispAFIAddress) ((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress()));
33     }
34
35     @Override
36     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
37         LcafApplicationDataAddress applicationDataAddress = ((LcafApplicationDataAddress) lispAddress);
38         buffer.put(ByteUtil.partialIntToByteArray(applicationDataAddress.getIpTos(), Length.TOC));
39         buffer.put(NumberUtil.asByte(applicationDataAddress.getProtocol().byteValue()));
40         buffer.putShort(applicationDataAddress.getLocalPort().getValue().shortValue());
41         buffer.putShort(applicationDataAddress.getRemotePort().getValue().shortValue());
42         LispAddressSerializer.getInstance().serialize(buffer,
43                 (LispAFIAddress) ((LcafApplicationDataAddress) lispAddress).getAddress().getPrimitiveAddress());
44     }
45
46     @Override
47     protected LcafApplicationDataAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
48
49         LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
50         byte[] rawIPTos = new byte[3];
51         buffer.get(rawIPTos);
52         builder.setIpTos(ByteUtil.getPartialInt(rawIPTos));
53         builder.setProtocol((short) buffer.get());
54         builder.setLocalPort(new PortNumber(new Integer(buffer.getShort())));
55         builder.setRemotePort(new PortNumber(new Integer(buffer.getShort())));
56         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(buffer);
57         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
58                 .setAddress(new AddressBuilder().setPrimitiveAddress((PrimitiveAddress) LispAFIConvertor.toPrimitive(address)).build());
59
60         return builder.build();
61     }
62
63     private interface Length {
64         int LOCAL_PORT = 2;
65         int REMOTE_PORT = 2;
66         int TOC = 3;
67         int PROTOCOL = 1;
68         int ALL_FIELDS = LOCAL_PORT + REMOTE_PORT + TOC + PROTOCOL;
69     }
70
71 }