2 * Copyright (c) 2013 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.LispLCAFAddressSerializerFactory;
14 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
15 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
16 import org.opendaylight.lispflowmapping.type.lisp.address.LispLCAFAddress;
18 public class LispLCAFAddressSerializer extends LispAddressSerializer {
20 private static final LispLCAFAddressSerializer INSTANCE = new LispLCAFAddressSerializer();
22 // Private constructor prevents instantiation from other classes
23 protected LispLCAFAddressSerializer() {
26 public static LispLCAFAddressSerializer getInstance() {
31 protected LispLCAFAddress deserializeData(ByteBuffer buffer) {
32 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
33 byte lispCode = buffer.get();
34 LispCanonicalAddressFormatEnum lcafType = LispCanonicalAddressFormatEnum.valueOf(lispCode);
35 byte res2 = buffer.get();
36 short length = buffer.getShort();
38 LispLCAFAddressSerializer serializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(lcafType);
39 if (serializer == null) {
40 throw new LispSerializationException("Unknown LispLCAFAddress type=" + lispCode);
42 return serializer.deserializeData(buffer, res2, length);
45 protected LispLCAFAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
46 throw new RuntimeException("Not implemented");
50 public int getAddressSize(LispAddress lispAddress) {
51 return Length.LCAF_HEADER
52 + LispLCAFAddressSerializerFactory.getLCAFSerializer(((LispLCAFAddress) lispAddress).getType()).getLcafLength(lispAddress);
55 protected short getLcafLength(LispAddress lispAddress) {
56 throw new RuntimeException("Not implemented");
60 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
61 serializeLCAFAddressHeader(buffer, lispAddress);
63 LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(((LispLCAFAddress) lispAddress).getType());
64 lcafSerializer.serializeData(buffer, lispAddress);
67 private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAddress lispAddress) {
68 LispLCAFAddress lispLCAFAddress = (LispLCAFAddress) lispAddress;
69 buffer.putShort((short) 0); // RES + Flags.
70 buffer.put(lispLCAFAddress.getType().getLispCode());
71 buffer.put(lispLCAFAddress.getRes2());
72 LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(lispLCAFAddress.getType());
73 buffer.putShort(lcafSerializer.getLcafLength(lispAddress));
76 private interface Length {