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.LispLCAFAddressSerializerFactory;
14 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
15 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
16 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
17 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispLcafAddress;
19 public class LispLCAFAddressSerializer extends LispAddressSerializer {
21 private static final LispLCAFAddressSerializer INSTANCE = new LispLCAFAddressSerializer();
23 // Private constructor prevents instantiation from other classes
24 protected LispLCAFAddressSerializer() {
27 public static LispLCAFAddressSerializer getInstance() {
32 protected LispLcafAddress deserializeData(ByteBuffer buffer) {
33 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
34 byte lispCode = (byte) ByteUtil.getUnsignedByte(buffer);
35 LispCanonicalAddressFormatEnum lcafType = LispCanonicalAddressFormatEnum.valueOf(lispCode);
36 byte res2 = buffer.get();
37 short length = buffer.getShort();
39 LispLCAFAddressSerializer serializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(lcafType);
40 if (serializer == null) {
41 throw new LispSerializationException("Unknown LispLCAFAddress type=" + lispCode);
43 return serializer.deserializeData(buffer, res2, length);
46 protected LispLcafAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
47 throw new RuntimeException("Not implemented");
51 public int getAddressSize(LispAFIAddress lispAddress) {
52 return Length.LCAF_HEADER
53 + LispLCAFAddressSerializerFactory.getLCAFSerializer(
54 LispCanonicalAddressFormatEnum.valueOf(((LispLcafAddress) lispAddress).getLcafType())).getLcafLength(lispAddress);
57 protected short getLcafLength(LispAFIAddress lispAddress) {
58 throw new RuntimeException("Not implemented");
62 protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
63 serializeLCAFAddressHeader(buffer, lispAddress);
65 LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(LispCanonicalAddressFormatEnum
66 .valueOf(((LispLcafAddress) lispAddress).getLcafType()));
67 lcafSerializer.serializeData(buffer, lispAddress);
70 private void serializeLCAFAddressHeader(ByteBuffer buffer, LispAFIAddress lispAddress) {
71 LispLcafAddress lispLcafAddress = (LispLcafAddress) lispAddress;
72 buffer.putShort((short) 0); // RES + Flags.
73 buffer.put(lispLcafAddress.getLcafType().byteValue());
75 LispLCAFAddressSerializer lcafSerializer = LispLCAFAddressSerializerFactory.getLCAFSerializer(LispCanonicalAddressFormatEnum
76 .valueOf(lispLcafAddress.getLcafType()));
77 buffer.putShort(lcafSerializer.getLcafLength(lispAddress));
80 private interface Length {