d6a70616f71afcb06ce8bd696b4c8c70232030fc
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispAddressSerializer.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.address;
9
10 import java.nio.ByteBuffer;
11
12 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
13 import org.opendaylight.lispflowmapping.implementation.serializer.address.factory.LispAFIAddressSerializerFactory;
14 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
16
17 public class LispAddressSerializer {
18
19     private static final LispAddressSerializer INSTANCE = new LispAddressSerializer();
20
21     // Private constructor prevents instantiation from other classes
22     protected LispAddressSerializer() {
23     }
24
25     public static LispAddressSerializer getInstance() {
26         return INSTANCE;
27     }
28
29     protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
30         throw new RuntimeException("UnImplemented");
31     }
32
33     protected LispAFIAddress deserializeData(ByteBuffer buffer) {
34         throw new RuntimeException("UnImplemented");
35     }
36
37     public void serialize(ByteBuffer buffer, LispAFIAddress lispAddress) {
38         LispAddressSerializer serializer = LispAFIAddressSerializerFactory.getSerializer(AddressFamilyNumberEnum.valueOf(lispAddress.getAfi()));
39         if (serializer == null) {
40             throw new LispSerializationException("Unknown AFI type=" + lispAddress.getAfi());
41         }
42         serializeAFIAddressHeader(buffer, lispAddress);
43         serializer.serializeData(buffer, lispAddress);
44     }
45
46     protected static void serializeAFIAddressHeader(ByteBuffer buffer, LispAFIAddress lispAddress) {
47         buffer.putShort(lispAddress.getAfi());
48     }
49
50     public int getAddressSize(LispAFIAddress lispAddress) {
51         AddressFamilyNumberEnum afiType = AddressFamilyNumberEnum.valueOf(lispAddress.getAfi());
52         LispAddressSerializer serializer = LispAFIAddressSerializerFactory.getSerializer(afiType);
53         if (serializer == null) {
54             throw new LispSerializationException("Unknown AFI type=" + afiType);
55         }
56         return Length.AFI + serializer.getAddressSize(lispAddress);
57     }
58
59     public LispAFIAddress deserialize(ByteBuffer buffer) {
60         short afi = buffer.getShort();
61         AddressFamilyNumberEnum afiType = AddressFamilyNumberEnum.valueOf(afi);
62         LispAddressSerializer serializer = LispAFIAddressSerializerFactory.getSerializer(afiType);
63         if (serializer == null) {
64             throw new LispSerializationException("Unknown AFI type=" + afiType);
65         }
66         try {
67             return serializer.deserializeData(buffer);
68         } catch (RuntimeException e) {
69             throw new LispSerializationException("Problem deserializing AFI=" + afiType, e);
70         }
71     }
72
73     private interface Length {
74         int AFI = 2;
75     }
76
77 }