2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.implementation.serializer.address;
10 import java.nio.ByteBuffer;
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.lispflowmapping.rev131031.LispAFIAddress;
17 public class LispAddressSerializer {
19 private static final LispAddressSerializer INSTANCE = new LispAddressSerializer();
21 // Private constructor prevents instantiation from other classes
22 protected LispAddressSerializer() {
25 public static LispAddressSerializer getInstance() {
29 protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
30 throw new RuntimeException("UnImplemented");
33 protected LispAFIAddress deserializeData(ByteBuffer buffer) {
34 throw new RuntimeException("UnImplemented");
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());
42 serializeAFIAddressHeader(buffer, lispAddress);
43 serializer.serializeData(buffer, lispAddress);
46 protected static void serializeAFIAddressHeader(ByteBuffer buffer, LispAFIAddress lispAddress) {
47 buffer.putShort(lispAddress.getAfi());
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);
56 return Length.AFI + serializer.getAddressSize(lispAddress);
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);
67 return serializer.deserializeData(buffer);
68 } catch (RuntimeException e) {
69 throw new LispSerializationException("Problem deserializing AFI=" + afiType, e);
73 private interface Length {