/*
- * Copyright (c) 2013 Contextream, Inc. and others. All rights reserved.
+ * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
*/
package org.opendaylight.lispflowmapping.type.lisp.address;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.nio.ByteBuffer;
-
import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
-import org.opendaylight.lispflowmapping.type.exception.LispMalformedPacketException;
public abstract class LispLCAFAddress extends LispAddress {
protected LispCanonicalAddressFormatEnum lcafType;
protected byte res2;
- protected LispLCAFAddress(ByteBuffer buffer) {
- super(AddressFamilyNumberEnum.LCAF);
- }
-
public LispLCAFAddress(LispCanonicalAddressFormatEnum lcafType, byte res2) {
super(AddressFamilyNumberEnum.LCAF);
this.lcafType = lcafType;
this.res2 = res2;
}
- public static LispLCAFAddress valueOf(ByteBuffer buffer) {
- buffer.position(buffer.position() + Length.RES + Length.FLAGS);
- byte lispCode = buffer.get();
- LispCanonicalAddressFormatEnum lcafType = LispCanonicalAddressFormatEnum.valueOf(lispCode);
- byte res2 = buffer.get();
- short length = buffer.getShort();
-
- Class<? extends LispAddress> addressClass = lcafType.getLcafClass();
- if (addressClass == null) {
- throw new LispMalformedPacketException("Unknown LispLCAFAddress type=" + lispCode);
- }
- Method valueOfMethod;
- Throwable t = null;
- try {
- valueOfMethod = addressClass.getMethod("valueOf", byte.class, short.class, ByteBuffer.class);
- return (LispLCAFAddress) valueOfMethod.invoke(null, res2, length, buffer);
- } catch (NoSuchMethodException e) {
- t = e;
- } catch (SecurityException e) {
- t = e;
- } catch (IllegalAccessException e) {
- t = e;
- } catch (IllegalArgumentException e) {
- t = e;
- } catch (InvocationTargetException e) {
- t = e;
- }
- throw new LispMalformedPacketException("Couldn't parse LispLCAFAddress type=" + lispCode, t);
- }
-
- @Override
- public final int getAddressSize() {
- return super.getAddressSize() + Length.LCAF_HEADER + getLcafLength();
- }
-
- public abstract short getLcafLength();
-
- @Override
- protected void internalSerialize(ByteBuffer buffer) {
- super.internalSerialize(buffer);
- buffer.putShort((short) 0); // RES + Flags.
- buffer.put(lcafType.getLispCode());
- buffer.put(getRes2());
- buffer.putShort(getLcafLength());
- }
public LispCanonicalAddressFormatEnum getType() {
return lcafType;
return "[lcafType=" + lcafType + ", res2=" + res2 + "]";
}
- private interface Length {
- int RES = 1;
- int FLAGS = 1;
-
- int LCAF_HEADER = 6;
- }
}