* Returns the first byte from the byte array
* @return byte value
*/
- public static byte getByte(byte[] data) {
+ public static byte getByte(final byte[] data) {
if ((data.length * NetUtils.NumBitsInAByte) > Byte.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
* Size of byte array is restricted to Short.SIZE
* @return short value
*/
- public static short getShort(byte[] data) {
+ public static short getShort(final byte[] data) {
if (data.length > Short.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
* Size of byte array is restricted to Integer.SIZE
* @return int - the integer value of byte array
*/
- public static int getInt(byte[] data) {
+ public static int getInt(final byte[] data) {
if (data.length > Integer.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
* Size of byte array is restricted to Long.SIZE
* @return long - the integer value of byte array
*/
- public static long getLong(byte[] data) {
+ public static long getLong(final byte[] data) {
if (data.length > Long.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (Exception e) {
+ } catch (final Exception e) {
logger.error("", e);
}
}
* Size of numBits is restricted to Short.SIZE
* @return short - the short value of byte array
*/
- public static short getShort(byte[] data, int numBits) {
+ public static short getShort(final byte[] data, final int numBits) {
if (numBits > Short.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
byte[] bits = null;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
return (short) toNumber(bits, numBits);
* Size of numBits is restricted to Integer.SIZE
* @return int - the integer value of byte array
*/
- public static int getInt(byte[] data, int numBits) {
+ public static int getInt(final byte[] data, final int numBits) {
if (numBits > Integer.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
byte[] bits = null;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
return (int) toNumber(bits, numBits);
* Size of numBits is restricted to Long.SIZE
* @return long - the integer value of byte array
*/
- public static long getLong(byte[] data, int numBits) {
+ public static long getLong(final byte[] data, final int numBits) {
if (numBits > Long.SIZE) {
try {
throw new BufferException(
"Container is too small for the number of requested bits");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
try {
throw new BufferException(
"Trying to read more bits than contained in the data buffer");
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
}
byte[] bits = null;
try {
bits = BitBufferHelper.getBits(data, startOffset, numBits);
- } catch (BufferException e) {
+ } catch (final BufferException e) {
logger.error("", e);
}
return toNumber(bits, numBits);
* when the startOffset and numBits parameters are not congruent
* with the data buffer size
*/
- public static byte[] getBits(byte[] data, int startOffset, int numBits)
+ public static byte[] getBits(final byte[] data, final int startOffset, final int numBits)
throws BufferException {
int startByteOffset = 0;
* when the input, startOffset and numBits are not congruent
* with the data buffer size
*/
- public static void setByte(byte[] data, byte input, int startOffset,
- int numBits) throws BufferException {
+ public static void setByte(final byte[] data, final byte input, final int startOffset,
+ final int numBits) throws BufferException {
byte[] inputByteArray = new byte[1];
Arrays.fill(inputByteArray, 0, 1, input);
setBytes(data, inputByteArray, startOffset, numBits);
* when the startOffset and numBits parameters are not congruent
* with data and input buffers' size
*/
- public static void setBytes(byte[] data, byte[] input, int startOffset,
- int numBits) throws BufferException {
+ public static void setBytes(final byte[] data, final byte[] input, final int startOffset,
+ final int numBits) throws BufferException {
checkExceptions(data, startOffset, numBits);
insertBits(data, input, startOffset, numBits);
}
/**
* Returns numBits 1's in the MSB position
*/
- public static int getMSBMask(int numBits) {
+ public static int getMSBMask(final int numBits) {
int mask = 0;
for (int i = 0; i < numBits; i++) {
mask = mask | (1 << (7 - i));
/**
* Returns numBits 1's in the LSB position
*/
- public static int getLSBMask(int numBits) {
+ public static int getLSBMask(final int numBits) {
int mask = 0;
for (int i = 0; i < numBits; i++) {
mask = mask | (1 << i);
*
* @return long - numerical value of byte array passed
*/
- static public long toNumber(byte[] array) {
+ static public long toNumber(final byte[] array) {
long ret = 0;
long length = array.length;
int value = 0;
*
* @return long - numerical value of byte array passed
*/
- static public long toNumber(byte[] array, int numBits) {
+ static public long toNumber(final byte[] array, final int numBits) {
int length = numBits / NetUtils.NumBitsInAByte;
int bitsRest = numBits % NetUtils.NumBitsInAByte;
int startOffset = array.length - length;
* aligned form example: input = 5000 [1001110001000] bytes = 19, -120
* [00010011] [10001000]
*/
- public static byte[] toByteArray(Number input) {
+ public static byte[] toByteArray(final Number input) {
Class<? extends Number> dataType = input.getClass();
short size = 0;
long longValue = input.longValue();
* @return byte[]
*
*/
- public static byte[] toByteArray(Number input, int numBits) {
+ public static byte[] toByteArray(final Number input, final int numBits) {
Class<? extends Number> dataType = input.getClass();
short size = 0;
long longValue = input.longValue();
* @param numBits - number of bits to be left aligned
* @return byte[]
*/
- public static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits) {
+ public static byte[] shiftBitsToMSB(final byte[] inputBytes, final int numBits) {
int numBitstoShiftBy = 0, leadZeroesMSB = 8, numEndRestBits = 0;
int size = inputBytes.length;
byte[] shiftedBytes = new byte[size];
* @param numBits - number of bits to be right aligned
* @return byte[]
*/
- public static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits) {
+ public static byte[] shiftBitsToLSB(final byte[] inputBytes, final int numBits) {
int numBytes = inputBytes.length;
int numBitstoShift = numBits % NetUtils.NumBitsInAByte;
byte[] shiftedBytes = new byte[numBytes];
* of bits specified from the input data byte array. The input byte array
* has the bits stored starting from the LSB
*/
- public static void insertBits(byte[] data, byte[] inputdataLSB,
- int startOffset, int numBits) {
+ public static void insertBits(final byte[] data, final byte[] inputdataLSB,
+ final int startOffset, final int numBits) {
byte[] inputdata = shiftBitsToMSB(inputdataLSB, numBits); // Align to
// MSB the
// passed byte
* @throws BufferException when the startOffset and numBits parameters
* are not congruent with the data buffer's size
*/
- public static void checkExceptions(byte[] data, int startOffset, int numBits)
+ public static void checkExceptions(final byte[] data, final int startOffset, final int numBits)
throws BufferException {
int endOffsetByte;
int startByteOffset;
public class BufferException extends Exception {
private static final long serialVersionUID = 1L;
- public BufferException(String message) {
+ public BufferException(final String message) {
super(message);
}
}
public class ConstructionException extends Exception {
private static final long serialVersionUID = 1L;
- public ConstructionException(String message) {
+ public ConstructionException(final String message) {
super(message);
}
}
* @param oui
* @param subtype
*/
- public CustomTLVKey(int oui, byte subtype) {
+ public CustomTLVKey(final int oui, final byte subtype) {
this.oui = oui;
this.subtype = subtype;
}
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
* @param name Create a new DataLink, not for general use but
* available only for sub classes
*/
- protected DataLinkAddress(String name) {
+ protected DataLinkAddress(final String name) {
this.name = name;
}
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
private String description;
private int number;
- EtherTypes(String description, int number) {
+ EtherTypes(final String description, final int number) {
this.description = description;
this.number = number;
}
return ((Integer) number).shortValue();
}
- public static String getEtherTypeName(int number) {
+ public static String getEtherTypeName(final int number) {
return getEtherTypeInternal(number);
}
- public static String getEtherTypeName(short number) {
+ public static String getEtherTypeName(final short number) {
return getEtherTypeInternal(number & 0xffff);
}
- public static String getEtherTypeName(byte number) {
+ public static String getEtherTypeName(final byte number) {
return getEtherTypeInternal(number & 0xff);
}
- private static String getEtherTypeInternal(int number) {
+ private static String getEtherTypeInternal(final int number) {
for (EtherTypes type : EtherTypes.values()) {
if (type.number == number) {
return type.toString();
return "0x" + Integer.toHexString(number);
}
- public static short getEtherTypeNumberShort(String name) {
+ public static short getEtherTypeNumberShort(final String name) {
if (name.matches(regexNumberString)) {
return Short.valueOf(name);
}
return 0;
}
- public static int getEtherTypeNumberInt(String name) {
+ public static int getEtherTypeNumberInt(final String name) {
if (name.matches(regexNumberString)) {
return Integer.valueOf(name);
}
return ethertypesList;
}
- public static EtherTypes loadFromString(String string) {
+ public static EtherTypes loadFromString(final String string) {
int intType = Integer.parseInt(string);
for (EtherTypes type : EtherTypes.values()) {
* Constructor that sets the access level for the packet and
* creates and sets the HashMap
*/
- public Ethernet(boolean writeAccess) {
+ public Ethernet(final boolean writeAccess) {
super(writeAccess);
fieldValues = new HashMap<>();
hdrFieldCoordMap = fieldCoordinates;
}
@Override
- public void setHeaderField(String headerField, byte[] readValue) {
+ public void setHeaderField(final String headerField, final byte[] readValue) {
if (headerField.equals(ETHT)) {
payloadClass = etherTypeClassMap.get(BitBufferHelper
.getShort(readValue));
* Sets the destination MAC address for the current Ethernet object instance
* @param destinationMACAddress the destinationMACAddress to set
*/
- public Ethernet setDestinationMACAddress(byte[] destinationMACAddress) {
+ public Ethernet setDestinationMACAddress(final byte[] destinationMACAddress) {
fieldValues.put(DMAC, destinationMACAddress);
return this;
}
* Sets the source MAC address for the current Ethernet object instance
* @param sourceMACAddress the sourceMACAddress to set
*/
- public Ethernet setSourceMACAddress(byte[] sourceMACAddress) {
+ public Ethernet setSourceMACAddress(final byte[] sourceMACAddress) {
fieldValues.put(SMAC, sourceMACAddress);
return this;
}
* Sets the etherType for the current Ethernet object instance
* @param etherType the etherType to set
*/
- public Ethernet setEtherType(short etherType) {
+ public Ethernet setEtherType(final short etherType) {
byte[] ethType = BitBufferHelper.toByteArray(etherType);
fieldValues.put(ETHT, ethType);
return this;
public static final String addressName = "Ethernet MAC Address";
public static final int SIZE = 6;
- private static final EthernetAddress createWellKnownAddress(byte[] mac) {
+ private static final EthernetAddress createWellKnownAddress(final byte[] mac) {
try {
return new EthernetAddress(mac);
- } catch (ConstructionException ce) {
+ } catch (final ConstructionException ce) {
return null;
}
}
* @param macAddress A byte array in big endian format
* representing the Ethernet MAC Address
*/
- public EthernetAddress(byte[] macAddress) throws ConstructionException {
+ public EthernetAddress(final byte[] macAddress) throws ConstructionException {
super(addressName);
if (macAddress == null) {
public EthernetAddress clone() {
try {
return new EthernetAddress(this.macAddress.clone());
- } catch (ConstructionException ce) {
+ } catch (final ConstructionException ce) {
return null;
}
}
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
* @return The hexadecimal representation of the byte array. If bytes is
* null, "null" string is returned
*/
- public static String bytesToHexString(byte[] bytes) {
+ public static String bytesToHexString(final byte[] bytes) {
if (bytes == null) {
return "null";
return ret;
}
- public static String longToHexString(long val) {
+ public static String longToHexString(final long val) {
char arr[] = Long.toHexString(val).toCharArray();
StringBuffer buf = new StringBuffer();
// prepend the right number of leading zeros
}
- public static byte[] bytesFromHexString(String values) {
+ public static byte[] bytesFromHexString(final String values) {
String target = "";
if (values != null) {
target = values;
return ret;
}
- public static long stringToLong(String values) {
+ public static long stringToLong(final String values) {
long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();
return value;
}
/**
* This method converts byte array into HexString format with ":" inserted.
*/
- public static String bytesToHexStringFormat(byte[] bytes) {
+ public static String bytesToHexStringFormat(final byte[] bytes) {
if (bytes == null) {
return "null";
}
/**
* Constructor that creates the tlvList LinkedHashMap and sets the write access for the same
*/
- public LLDP(boolean writeAccess) {
+ public LLDP(final boolean writeAccess) {
super(writeAccess);
init();
}
* - description of the type of TLV
* @return byte - type of TLV
*/
- private byte getType(String typeDesc) {
+ private byte getType(final String typeDesc) {
if (typeDesc.equals(CHASSISID)) {
return LLDPTLV.TLVType.ChassisID.getValue();
} else if (typeDesc.equals(PORTID)) {
}
}
- private LLDPTLV getFromTLVs(Byte type) {
+ private LLDPTLV getFromTLVs(final Byte type) {
LLDPTLV tlv = null;
tlv = mandatoryTLVs.get(type);
if (tlv == null) {
* - description of the type of TLV
* @return LLDPTLV - full TLV
*/
- public LLDPTLV getTLV(String type) {
+ public LLDPTLV getTLV(final String type) {
return getFromTLVs(getType(type));
}
- public LLDPTLV getCustomTLV(CustomTLVKey key) {
+ public LLDPTLV getCustomTLV(final CustomTLVKey key) {
return customTLVs.get(key);
}
* @param tlv
* - tlv to set
*/
- public void setTLV(String type, LLDPTLV tlv) {
+ public void setTLV(final String type, final LLDPTLV tlv) {
putToTLVs(getType(type), tlv);
}
* @param chassisId
* - the chassisId to set
*/
- public LLDP setChassisId(LLDPTLV chassisId) {
+ public LLDP setChassisId(final LLDPTLV chassisId) {
setTLV(CHASSISID, chassisId);
return this;
}
* @param systemNameId
* - the systemNameId to set
*/
- public LLDP setSystemNameId(LLDPTLV systemNameId) {
+ public LLDP setSystemNameId(final LLDPTLV systemNameId) {
setTLV(SYSTEMNAMEID, systemNameId);
return this;
}
* - the portId to set
* @return LLDP
*/
- public LLDP setPortId(LLDPTLV portId) {
+ public LLDP setPortId(final LLDPTLV portId) {
setTLV(PORTID, portId);
return this;
}
* - the ttl to set
* @return LLDP
*/
- public LLDP setTtl(LLDPTLV ttl) {
+ public LLDP setTtl(final LLDPTLV ttl) {
setTLV(TTL, ttl);
return this;
}
* the optionalTLVList to set
* @return LLDP
*/
- public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
+ public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
for (LLDPTLV tlv : optionalTLVList) {
optionalTLVs.put(tlv.getType(), tlv);
}
}
@Override
- public Packet deserialize(byte[] data, int bitOffset, int size) throws PacketException {
+ public Packet deserialize(final byte[] data, final int bitOffset, final int size) throws PacketException {
int lldpOffset = bitOffset; // LLDP start
int lldpSize = size; // LLDP size
int numBits = tlv.getTLVSize();
try {
BitBufferHelper.setBytes(serializedBytes, tlv.serialize(), startOffset, numBits);
- } catch (BufferException e) {
+ } catch (final BufferException e) {
throw new PacketException(e.getMessage());
}
startOffset += numBits;
try {
BitBufferHelper.setBytes(serializedBytes, LLDP.emptyTLV.serialize(), startOffset,
LLDP.emptyTLV.getTLVSize());
- } catch (BufferException e) {
+ } catch (final BufferException e) {
throw new PacketException(e.getMessage());
}
private byte value;
- TLVType(byte value) {
+ TLVType(final byte value) {
this.value = value;
}
/**
* Constructor that writes the passed LLDPTLV values to the hdrFieldsMap
*/
- public LLDPTLV(LLDPTLV other) {
+ public LLDPTLV(final LLDPTLV other) {
for (Map.Entry<String, byte[]> entry : other.hdrFieldsMap.entrySet()) {
this.hdrFieldsMap.put(entry.getKey(), entry.getValue());
}
* @param type the type to set
* @return LLDPTLV
*/
- public LLDPTLV setType(byte type) {
+ public LLDPTLV setType(final byte type) {
byte[] lldpTLVtype = { type };
fieldValues.put(TYPE, lldpTLVtype);
return this;
* @param length the length to set
* @return LLDPTLV
*/
- public LLDPTLV setLength(short length) {
+ public LLDPTLV setLength(final short length) {
fieldValues.put(LENGTH, BitBufferHelper.toByteArray(length));
return this;
}
* @param value the value to set
* @return LLDPTLV
*/
- public LLDPTLV setValue(byte[] value) {
+ public LLDPTLV setValue(final byte[] value) {
fieldValues.put(VALUE, value);
return this;
}
@Override
- public void setHeaderField(String headerField, byte[] readValue) {
+ public void setHeaderField(final String headerField, final byte[] readValue) {
hdrFieldsMap.put(headerField, readValue);
}
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
}
@Override
- public int getfieldnumBits(String fieldName) {
+ public int getfieldnumBits(final String fieldName) {
if (fieldName.equals(VALUE)) {
return (NetUtils.NumBitsInAByte * BitBufferHelper.getShort(
fieldValues.get(LENGTH), fieldCoordinates.get(LENGTH)
* node identifier string
* @return the SystemName TLV value in byte array
*/
- static public byte[] createSystemNameTLVValue(String nodeId) {
+ static public byte[] createSystemNameTLVValue(final String nodeId) {
byte[] nid = nodeId.getBytes();
return nid;
}
* node identifier string
* @return the ChassisID TLV value in byte array
*/
- static public byte[] createChassisIDTLVValue(String nodeId) {
+ static public byte[] createChassisIDTLVValue(final String nodeId) {
byte[] nid = HexEncode.bytesFromHexString(nodeId);
byte[] cid = new byte[6];
int srcPos = 0, dstPos = 0;
* port identifier string
* @return the PortID TLV value in byte array
*/
- static public byte[] createPortIDTLVValue(String portId) {
+ static public byte[] createPortIDTLVValue(final String portId) {
byte[] pid = portId.getBytes(Charset.defaultCharset());
byte[] pidValue = new byte[pid.length + portIDSubType.length];
* @return the custom TLV value in byte array
* @see #createCustomTLVValue(byte[],byte[])
*/
- static public byte[] createCustomTLVValue(String customString) {
+ static public byte[] createCustomTLVValue(final String customString) {
byte[] customByteArray = customString.getBytes(Charset.defaultCharset());
return createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID, customByteArray);
}
* port identifier string
* @return the custom TLV value in byte array
*/
- static public byte[] createCustomTLVValue(byte[] subtype, byte[] customByteArray) {
+ static public byte[] createCustomTLVValue(final byte[] subtype, final byte[] customByteArray) {
byte[] customValue = new byte[customTlvOffset + customByteArray.length];
System.arraycopy(OFOUI, 0, customValue, 0, OFOUI.length);
* the TLV length
* @return the HexString
*/
- static public String getHexStringValue(byte[] tlvValue, int tlvLen) {
+ static public String getHexStringValue(final byte[] tlvValue, final int tlvLen) {
byte[] cidBytes = new byte[tlvLen - chassisIDSubType.length];
System.arraycopy(tlvValue, chassisIDSubType.length, cidBytes, 0,
cidBytes.length);
* the TLV length
* @return the string
*/
- static public String getStringValue(byte[] tlvValue, int tlvLen) {
+ static public String getStringValue(final byte[] tlvValue, final int tlvLen) {
byte[] pidSubType = new byte[portIDSubType.length];
byte[] pidBytes = new byte[tlvLen - portIDSubType.length];
System.arraycopy(tlvValue, 0, pidSubType, 0,
* the custom TLV length
* @return the custom string
*/
- static public String getCustomString(byte[] customTlvValue, int customTlvLen) {
+ static public String getCustomString(final byte[] customTlvValue, final int customTlvLen) {
String customString = "";
byte[] vendor = new byte[3];
System.arraycopy(customTlvValue, 0, vendor, 0, vendor.length);
customArrayLength);
try {
customString = new String(customArray, "UTF-8");
- } catch (UnsupportedEncodingException e) {
+ } catch (final UnsupportedEncodingException e) {
}
}
* the 4 bytes long byte array
* @return the integer number
*/
- public static int byteArray4ToInt(byte[] ba) {
+ public static int byteArray4ToInt(final byte[] ba) {
if (ba == null || ba.length != 4) {
return 0;
}
* Zero is returned if {@code ba} is {@code null} or
* the length of it is not six.
*/
- public static long byteArray6ToLong(byte[] ba) {
+ public static long byteArray6ToLong(final byte[] ba) {
if (ba == null || ba.length != MACAddrLengthInBytes) {
return 0L;
}
* the integer number
* @return the byte array
*/
- public static byte[] intToByteArray4(int i) {
+ public static byte[] intToByteArray4(final int i) {
return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
(byte) (i & 0xff) };
}
* the IP address in integer form
* @return the IP address in InetAddress form
*/
- public static InetAddress getInetAddress(int address) {
+ public static InetAddress getInetAddress(final int address) {
InetAddress ip = null;
try {
ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
logger.error("", e);
}
return ip;
* boolean representing the IP version of the returned address
* @return
*/
- public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
+ public static InetAddress getInetNetworkMask(final int prefixMaskLength, final boolean isV6) {
if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) {
return null;
}
try {
return InetAddress.getByAddress(address);
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
logger.error("", e);
}
return null;
* the subnet mask as byte array
* @return the prefix length as number of bits
*/
- public static int getSubnetMaskLength(byte[] subnetMask) {
+ public static int getSubnetMaskLength(final byte[] subnetMask) {
int maskLength = 0;
if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) {
int index = 0;
* the subnet mask as InetAddress
* @return the prefix length as number of bits
*/
- public static int getSubnetMaskLength(InetAddress subnetMask) {
+ public static int getSubnetMaskLength(final InetAddress subnetMask) {
return subnetMask == null ? 0 : NetUtils.getSubnetMaskLength(subnetMask.getAddress());
}
* the length of the prefix network mask
* @return the subnet prefix IP address in InetAddress form
*/
- public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
+ public static InetAddress getSubnetPrefix(final InetAddress ip, final int maskLen) {
int bytes = maskLen / 8;
int bits = maskLen % 8;
byte modifiedByte;
}
try {
return InetAddress.getByAddress(sn);
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
return null;
}
}
* @param filterMask
* @return
*/
- public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
- InetAddress filterMask) {
+ public static boolean inetAddressConflict(final InetAddress testAddress, final InetAddress filterAddress, final InetAddress testMask,
+ final InetAddress filterMask) {
// Sanity check
if ((testAddress == null) || (filterAddress == null)) {
return false;
* the byte array representing the MAC address
* @return true if all MAC bytes are zero
*/
- public static boolean isZeroMAC(byte[] mac) {
+ public static boolean isZeroMAC(final byte[] mac) {
for (short i = 0; i < 6; i++) {
if (mac[i] != 0) {
return false;
* @param MACAddress
* @return
*/
- public static boolean isBroadcastMACAddr(byte[] MACAddress) {
+ public static boolean isBroadcastMACAddr(final byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes) {
for (int i = 0; i < 6; i++) {
if (MACAddress[i] != BroadcastMACAddr[i]) {
* @param MACAddress
* @return
*/
- public static boolean isUnicastMACAddr(byte[] MACAddress) {
+ public static boolean isUnicastMACAddr(final byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes) {
return (MACAddress[0] & 1) == 0;
}
* @param MACAddress
* @return
*/
- public static boolean isMulticastMACAddr(byte[] MACAddress) {
+ public static boolean isMulticastMACAddr(final byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
return (MACAddress[0] & 1) != 0;
}
* the IP address to test
* @return true if the address is all zero
*/
- public static boolean isAny(InetAddress ip) {
+ public static boolean isAny(final InetAddress ip) {
for (byte b : ip.getAddress()) {
if (b != 0) {
return false;
return true;
}
- public static boolean fieldsConflict(int field1, int field2) {
+ public static boolean fieldsConflict(final int field1, final int field2) {
if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
return false;
}
return true;
}
- public static InetAddress parseInetAddress(String addressString) {
+ public static InetAddress parseInetAddress(final String addressString) {
InetAddress address = null;
try {
address = InetAddress.getByName(addressString);
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
logger.error("", e);
}
return address;
* the v4 address as A.B.C.D/MM
* @return
*/
- public static boolean isIPv4AddressValid(String cidr) {
+ public static boolean isIPv4AddressValid(final String cidr) {
if (cidr == null) {
return false;
}
* the v6 address as A::1/MMM
* @return
*/
- public static boolean isIPv6AddressValid(String cidr) {
+ public static boolean isIPv6AddressValid(final String cidr) {
if (cidr == null) {
return false;
}
if (!(addr instanceof Inet6Address)) {
return false;
}
- } catch (UnknownHostException ex) {
+ } catch (final UnknownHostException ex) {
return false;
}
* the v4 or v6 address as IP/MMM
* @return
*/
- public static boolean isIPAddressValid(String cidr) {
+ public static boolean isIPAddressValid(final String cidr) {
return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
}
* the byte value
* @return the int variable containing the unsigned byte value
*/
- public static int getUnsignedByte(byte b) {
+ public static int getUnsignedByte(final byte b) {
return b & 0xFF;
}
* the short value
* @return the int variable containing the unsigned short value
*/
- public static int getUnsignedShort(short s) {
+ public static int getUnsignedShort(final short s) {
return s & 0xFFFF;
}
* true for IPv6, false for Ipv4
* @return The highest IPv4 or IPv6 address
*/
- public static InetAddress gethighestIP(boolean v6) {
+ public static InetAddress gethighestIP(final boolean v6) {
try {
return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
.getByName("255.255.255.255");
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
return null;
}
}
corrupted = false;
}
- public Packet(boolean writeAccess) {
+ public Packet(final boolean writeAccess) {
this.writeAccess = writeAccess;
corrupted = false;
}
return payload;
}
- public void setParent(Packet parent) {
+ public void setParent(final Packet parent) {
this.parent = parent;
}
- public void setPayload(Packet payload) {
+ public void setPayload(final Packet payload) {
this.payload = payload;
}
- public void setHeaderField(String headerField, byte[] readValue) {
+ public void setHeaderField(final String headerField, final byte[] readValue) {
hdrFieldsMap.put(headerField, readValue);
}
* @return Packet
* @throws PacketException
*/
- public Packet deserialize(byte[] data, int bitOffset, int size)
+ public Packet deserialize(final byte[] data, final int bitOffset, final int size)
throws PacketException {
// Deserialize the header fields one by one
try {
hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
numBits);
- } catch (BufferException e) {
+ } catch (final BufferException e) {
throw new PacketException(e.getMessage());
}
if (payloadClass != null) {
try {
payload = payloadClass.newInstance();
- } catch (Exception e) {
+ } catch (final Exception e) {
throw new RuntimeException(
"Error parsing payload for Ethernet packet", e);
}
try {
BitBufferHelper.setBytes(packetBytes, fieldBytes,
getfieldOffset(field), getfieldnumBits(field));
- } catch (BufferException e) {
+ } catch (final BufferException e) {
throw new PacketException(e.getMessage());
}
}
*
* @return Integer - startOffset of the requested field
*/
- public int getfieldOffset(String fieldName) {
+ public int getfieldOffset(final String fieldName) {
return hdrFieldCoordMap.get(fieldName).getLeft();
}
*
* @return Integer - number of bits of the requested field
*/
- public int getfieldnumBits(String fieldName) {
+ public int getfieldnumBits(final String fieldName) {
return hdrFieldCoordMap.get(fieldName).getRight();
}
*
* @param payload The raw payload as byte array
*/
- public void setRawPayload(byte[] payload) {
+ public void setRawPayload(final byte[] payload) {
this.rawPayload = Arrays.copyOf(payload, payload.length);
}
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
public class PacketException extends Exception {
private static final long serialVersionUID = 1L;
- public PacketException(String message) {
+ public PacketException(final String message) {
super(message);
}
}
* @param customItem
* @param expectedValue
*/
- private static void checkCustomTlv(LLDPTLV customItem, String expectedValue) {
+ private static void checkCustomTlv(final LLDPTLV customItem, final String expectedValue) {
Assert.assertEquals(127, customItem.getType());
LOG.debug("custom TLV1.length: {}", customItem.getLength());
Assert.assertEquals(expectedValue,
new String(LLDPTLV.getCustomString(customItem.getValue(), customItem.getLength())));
}
- private static int checkTLV(byte[] serializedData, int offset, byte typeTLVBits, String typeTLVName,
- short lengthTLV, byte[] valueTLV, byte... bytesBeforeValue) throws ArrayComparisonFailure {
+ private static int checkTLV(final byte[] serializedData, final int offset, final byte typeTLVBits, final String typeTLVName,
+ final short lengthTLV, final byte[] valueTLV, final byte... bytesBeforeValue) throws ArrayComparisonFailure {
byte[] concreteTlvAwaited = awaitedBytes(typeTLVBits, lengthTLV, valueTLV, bytesBeforeValue);
int concreteTlvAwaitLength = concreteTlvAwaited.length;
assertArrayEquals("Serialization problem " + typeTLVName, concreteTlvAwaited,
return offset + concreteTlvAwaitLength;
}
- private static byte[] awaitedBytes(byte typeTLV, short length, byte[] value, byte[] bytesBeforeValue) {
+ private static byte[] awaitedBytes(final byte typeTLV, final short length, final byte[] value, final byte[] bytesBeforeValue) {
byte[] awaited = ArrayUtils.EMPTY_BYTE_ARRAY;
// 0 - the less meaning byte (right), 1 most meaning byte (left)
return awaited;
}
- private static LLDPTLV dummyCustomTlv(final byte tlvType, byte[] oui, byte[] ouiSubtype, short customLength,
- byte[] subtypeValue) {
+ private static LLDPTLV dummyCustomTlv(final byte tlvType, final byte[] oui, final byte[] ouiSubtype, final short customLength,
+ final byte[] subtypeValue) {
byte[] fullCustomValue = new byte[0];
fullCustomValue = ArrayUtils.addAll(fullCustomValue, oui);
fullCustomValue = ArrayUtils.addAll(fullCustomValue, ouiSubtype);
// Exception is expected if NOT raised test will fail
Assert.assertTrue(false);
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
}
// Array too short
// Exception is expected if NOT raised test will fail
Assert.assertTrue(false);
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
}
// Array too long
// Exception is expected if NOT raised test will fail
Assert.assertTrue(false);
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
}
}
ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
Assert.assertTrue(ea1.equals(ea2));
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail
Assert.assertTrue(false);
}
ea2 = ea1.clone();
Assert.assertTrue(ea1.equals(ea2));
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail
Assert.assertTrue(false);
}
ea2 = new EthernetAddress(new byte[] { (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
Assert.assertTrue(ea1.equals(ea2));
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail
Assert.assertTrue(false);
}
ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
Assert.assertTrue(!ea1.equals(ea2));
- } catch (ConstructionException e) {
+ } catch (final ConstructionException e) {
// Exception is NOT expected if raised test will fail
Assert.assertTrue(false);
}