added source/dest lookup lcaf TELSDN-176: #close
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / serializer / address / LispSegmentLCAFAddressSerializer.java
1 package org.opendaylight.lispflowmapping.southbound.serializer.address;
2
3 import java.nio.ByteBuffer;
4
5 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
6 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
7 import org.opendaylight.lispflowmapping.type.lisp.address.LispSegmentLCAFAddress;
8
9 public class LispSegmentLCAFAddressSerializer extends LispLCAFAddressSerializer{
10         
11         private static final LispSegmentLCAFAddressSerializer INSTANCE = new LispSegmentLCAFAddressSerializer();
12
13         // Private constructor prevents instantiation from other classes
14         private LispSegmentLCAFAddressSerializer() {
15         }
16
17         public static LispSegmentLCAFAddressSerializer getInstance() {
18                 return INSTANCE;
19         }
20
21
22         @Override
23     public short getLcafLength(LispAddress lispAddress) {
24                 LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(((LispSegmentLCAFAddress)lispAddress).getAddress().getAfi());
25         return (short) (Length.INSTANCE + serializer.getAddressSize(((LispSegmentLCAFAddress)lispAddress).getAddress()));
26     }
27
28         @Override
29     public void serialize(ByteBuffer buffer, LispAddress lispAddress) {
30         super.internalSerialize(buffer, lispAddress);
31         buffer.putInt(((LispSegmentLCAFAddress)lispAddress).getInstanceId());
32         LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(((LispSegmentLCAFAddress)lispAddress).getAddress().getAfi());
33         if (serializer == null) {
34             throw new LispMalformedPacketException("Unknown AFI type=" + ((LispSegmentLCAFAddress)lispAddress).getAddress().getAfi());
35         }
36         serializer.serialize(buffer, ((LispSegmentLCAFAddress)lispAddress).getAddress());
37     }
38         
39         @Override
40         public LispSegmentLCAFAddress innerDeserialize(ByteBuffer buffer, byte res2, short length) {
41         int instanceId = buffer.getInt();
42         LispAddress address = LispAddressSerializer.getInstance().deserialize(buffer);
43
44         return new LispSegmentLCAFAddress(res2, instanceId, address);
45     }
46
47         private interface Length {
48         int INSTANCE = 4;
49     }
50 }