E
- type of the bitmap enumerationpublic class OfpBitmapCodec<E extends OfpBitmapEnum> extends AbstractBitCodec<E>
The type parameter <E> is the enumeration class that implements the set of bit values.
Suppose you have a set of values encoded as bits in a field; in OpenFlow version 1.0:
0x1
0x2
0x4
0x8
0x10
private static final int[] MASKS = { 0x07, // 1.0 0x0f, // 1.1 0x0f, // 1.2 (no change from 1.1) 0x1d, // 1.3 };The enumeration class representing the field, should be written something like this:
public enum FooBar implements OfpBitmapEnum { FOO(0x1), GOO(0x2), ZOO(0x4), BAR(0x8), BAZ(0x10), ; private int bit; FooBar(int bit) { this.bit = bit; } private static final int[] MASKS = { 0x07, // 1.0 0x0f, // 1.1 0x0f, // 1.2 (no change from 1.1) 0x1d, // 1.3 } @Override public int getBit(ProtocolVersion pv) { return bit; } // secret decoder ring private static final OfpBitmapCodec<FooBar> CODEC = new OfpBitmapCodec<FooBar>(MASKS, values()); // (javadocs go here) public static Set<FooBar> decodeBitmap(int bitmap, ProtocolVersion pv) { return CODEC.decode(bitmap, pv); } // (javadocs go here) public static int encodeBitmap(Set<FooBar>, ProtocolVersion pv) { return CODEC.encode(flags, pv); } }A couple of things to note:
pv
parameter
is not used in the getBit()
implementation. Sometimes, however, the bit value changes for the
same flag; for example PortFeature.COPPER
is encoded as
0x80
in version 1.0, and as 0x800
in versions 1.1,
1.2 and 1.3.
values()
). But in a few cases, bits have been grouped
together to represent mutually exclusive values; these bit positions
should not be iterated over by the decoder, but handled separately.
See PortState
for an example of this.
flagSet, masks, NA_CODE
Constructor and Description |
---|
OfpBitmapCodec(int[] masks,
E[] flagSet)
Constructs the codec, which squirrels away the bit masks and flag set
for use during the
AbstractBitCodec.encode(java.util.Set<E>, com.hp.of.lib.ProtocolVersion) and AbstractBitCodec.decode(int, com.hp.of.lib.ProtocolVersion) methods. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
cannotBeMapped(int value)
This test should return true if the given value (code, or bit) cannot
be mapped into the bitmap; false if it is okay.
|
protected String |
formatValue(int value)
Returns the appropriate formatting of the value for an exception message.
|
protected int |
getBitToMap(E flag,
ProtocolVersion pv)
Given a flag and a protocol version, this method should return the
appropriate bit in the map that represents this flag.
|
protected int |
getValue(E flag,
ProtocolVersion pv)
Returns the value (bit, or code) from the given flag, for the
given version.
|
decode, disposeStorage, encode, initStorage
public OfpBitmapCodec(int[] masks, E[] flagSet)
AbstractBitCodec.encode(java.util.Set<E>, com.hp.of.lib.ProtocolVersion)
and AbstractBitCodec.decode(int, com.hp.of.lib.ProtocolVersion)
methods.
Note that, most of the time, the flag set should be
EnumClass.values()
.masks
- the "valid bit position" masks; one for each
protocol versionflagSet
- the set of flags to iterate over during decodeNullPointerException
- if either parameter is nullprotected int getValue(E flag, ProtocolVersion pv)
AbstractBitCodec
getValue
in class AbstractBitCodec<E extends OfpBitmapEnum>
flag
- the flagpv
- the protocol versionprotected boolean cannotBeMapped(int value)
AbstractBitCodec
cannotBeMapped
in class AbstractBitCodec<E extends OfpBitmapEnum>
value
- the value to testprotected String formatValue(int value)
AbstractBitCodec
formatValue
in class AbstractBitCodec<E extends OfpBitmapEnum>
value
- the valueprotected int getBitToMap(E flag, ProtocolVersion pv)
AbstractBitCodec
getBitToMap
in class AbstractBitCodec<E extends OfpBitmapEnum>
flag
- the flagpv
- the protocol versionCopyright © 2015. All Rights Reserved.