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.util.ByteUtil;
14 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
15 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
16 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafSegmentAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafsegmentaddress.AddressBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafsegment.LcafSegmentAddrBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispsimpleaddress.PrimitiveAddress;
23 public class LispSegmentLCAFAddressSerializer extends LispLCAFAddressSerializer {
25 private static final int MAX_INSTANCE_ID = 16777216;
26 private static final LispSegmentLCAFAddressSerializer INSTANCE = new LispSegmentLCAFAddressSerializer();
28 // Private constructor prevents instantiation from other classes
29 private LispSegmentLCAFAddressSerializer() {
32 public static LispSegmentLCAFAddressSerializer getInstance() {
37 protected short getLcafLength(LispAFIAddress lispAddress) {
38 return (short) (Length.INSTANCE + LispAddressSerializer.getInstance().getAddressSize(
39 LispAFIConvertor.toAFIfromPrimitive(((LcafSegmentAddress) lispAddress).getAddress().getPrimitiveAddress())));
43 protected void serializeData(ByteBuffer buffer, LispAFIAddress lispAddress) {
44 // The IID mask-len field is in the LCAF header on the res2 position
45 buffer.put(buffer.position() - 3, ((LcafSegmentAddress) lispAddress).getIidMaskLength().byteValue());
47 buffer.putInt(((LcafSegmentAddress) lispAddress).getInstanceId().intValue());
48 LispAddressSerializer.getInstance().serialize(buffer, LispAFIConvertor.toAFIfromPrimitive(((LcafSegmentAddress) lispAddress).getAddress().getPrimitiveAddress()));
52 protected LcafSegmentAddress deserializeData(ByteBuffer buffer, byte res2, short length) {
53 long instanceId = (int) ByteUtil.asUnsignedInteger(buffer.getInt());
55 if (instanceId > MAX_INSTANCE_ID) {
56 throw new LispSerializationException("Instance ID is longer than 24 bits. got " + instanceId);
58 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(buffer);
59 LcafSegmentAddrBuilder builder = new LcafSegmentAddrBuilder();
60 builder.setInstanceId(instanceId);
61 builder.setIidMaskLength((short) res2);
62 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode())
63 .setAddress(new AddressBuilder().setPrimitiveAddress((PrimitiveAddress) LispAFIConvertor.toPrimitive(address)).build());
65 return builder.build();
68 private interface Length {