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.lisp.serializer.address;
10 import java.nio.ByteBuffer;
11 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
12 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
13 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.InstanceId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
19 import org.opendaylight.yangtools.yang.common.Uint32;
21 public final class InstanceIdSerializer extends LcafSerializer {
22 private static final int MAX_INSTANCE_ID = 16777216;
23 private static final InstanceIdSerializer INSTANCE = new InstanceIdSerializer();
25 // Private constructor prevents instantiation from other classes
26 private InstanceIdSerializer() {
29 public static InstanceIdSerializer getInstance() {
34 protected byte getLcafType() {
35 return LispCanonicalAddressFormatEnum.SEGMENT.getLispCode();
39 protected short getLcafLength(LispAddress lispAddress) {
40 return (short) (Length.INSTANCE + LispAddressSerializer.getInstance().getAddressSize(lispAddress));
43 protected int getInstanceIdSize() {
44 return Length.INSTANCE;
48 protected short getAfi() {
49 return (short) AddressFamily.LispCanonicalAddressFormat.getIntValue();
53 protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
54 InstanceId iid = (InstanceId) lispAddress.getAddress();
55 // The IID mask-len field is in the LCAF header on the res2 position
56 buffer.put(buffer.position() - 3, iid.getInstanceId().getMaskLength().byteValue());
58 buffer.putInt(iid.getInstanceId().getIid().getValue().intValue());
59 LispAddressSerializer.getInstance().serialize(buffer, lispAddress);
62 protected void serializeNonLcafAddress(ByteBuffer buffer, LispAddress lispAddress) {
63 buffer.putInt(lispAddress.getVirtualNetworkId().getValue().intValue());
67 protected Eid deserializeLcafEidData(ByteBuffer buffer, byte res2, short length, LispAddressSerializerContext ctx) {
68 long instanceId = (int) ByteUtil.asUnsignedInteger(buffer.getInt());
70 if (instanceId > MAX_INSTANCE_ID) {
71 throw new LispSerializationException("Instance ID is longer than 24 bits: " + instanceId);
73 ctx.setVni(new InstanceIdType(Uint32.valueOf(instanceId)));
74 return LispAddressSerializer.getInstance().deserializeEid(buffer, ctx);
77 private interface Length {