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.type.lisp.address;
10 import java.lang.reflect.InvocationTargetException;
11 import java.lang.reflect.Method;
12 import java.nio.ByteBuffer;
14 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
15 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
16 import org.opendaylight.lispflowmapping.type.exception.LispMalformedPacketException;
18 public abstract class LispLCAFAddress extends LispAddress {
20 protected LispCanonicalAddressFormatEnum lcafType;
23 protected LispLCAFAddress(ByteBuffer buffer) {
24 super(AddressFamilyNumberEnum.LCAF);
27 public LispLCAFAddress(LispCanonicalAddressFormatEnum lcafType, byte res2) {
28 super(AddressFamilyNumberEnum.LCAF);
29 this.lcafType = lcafType;
33 public static LispLCAFAddress valueOf(ByteBuffer buffer) {
34 buffer.position(buffer.position() + Length.RES + Length.FLAGS);
35 byte lispCode = buffer.get();
36 LispCanonicalAddressFormatEnum lcafType = LispCanonicalAddressFormatEnum.valueOf(lispCode);
37 byte res2 = buffer.get();
38 short length = buffer.getShort();
40 Class<? extends LispAddress> addressClass = lcafType.getLcafClass();
41 if (addressClass == null) {
42 throw new LispMalformedPacketException("Unknown LispLCAFAddress type=" + lispCode);
47 valueOfMethod = addressClass.getMethod("valueOf", byte.class, short.class, ByteBuffer.class);
48 return (LispLCAFAddress) valueOfMethod.invoke(null, res2, length, buffer);
49 } catch (NoSuchMethodException e) {
51 } catch (SecurityException e) {
53 } catch (IllegalAccessException e) {
55 } catch (IllegalArgumentException e) {
57 } catch (InvocationTargetException e) {
60 throw new LispMalformedPacketException("Couldn't parse LispLCAFAddress type=" + lispCode, t);
64 public final int getAddressSize() {
65 return super.getAddressSize() + Length.LCAF_HEADER + getLcafLength();
68 public abstract short getLcafLength();
71 protected void internalSerialize(ByteBuffer buffer) {
72 super.internalSerialize(buffer);
73 buffer.putShort((short) 0); // RES + Flags.
74 buffer.put(lcafType.getLispCode());
75 buffer.put(getRes2());
76 buffer.putShort(getLcafLength());
79 public LispCanonicalAddressFormatEnum getType() {
83 public byte getRes2() {
88 public int hashCode() {
90 int result = super.hashCode();
91 result = prime * result + ((lcafType == null) ? 0 : lcafType.hashCode());
92 result = prime * result + res2;
97 public boolean equals(Object obj) {
100 if (!super.equals(obj))
102 if (getClass() != obj.getClass())
104 LispLCAFAddress other = (LispLCAFAddress) obj;
105 if (lcafType != other.lcafType)
107 if (res2 != other.res2)
113 public String toString() {
114 return "[lcafType=" + lcafType + ", res2=" + res2 + "]";
117 private interface Length {