From d1e610f3777acc54452fb4fec8d86ffe28ee7102 Mon Sep 17 00:00:00 2001 From: Dana Kutenicsova Date: Fri, 22 Nov 2013 15:06:05 +0100 Subject: [PATCH] Adjusted byte array method to remove most of magic numbers. Change-Id: I6227e2f710cd2d396c69ce9502bb57da7442677f Signed-off-by: Dana Kutenicsova --- .../bgp/linkstate/LinkstateNlriParser.java | 2 + .../message/BGPNotificationMessageParser.java | 4 +- .../impl/message/BGPOpenMessageParser.java | 11 ++- .../message/open/As4CapabilityHandler.java | 3 +- .../protocol/bgp/parser/spi/MessageUtil.java | 4 +- .../protocol/concepts/BandwidthTest.java | 8 +- .../impl/message/AbstractMessageParser.java | 4 +- .../object/AbstractObjectWithTlvsParser.java | 4 +- .../impl/object/PCEPLspaObjectParser.java | 9 ++- .../PCEPRequestParameterObjectParser.java | 13 ++-- .../pcep/impl/object/PCEPSrpObjectParser.java | 2 +- .../impl/object/PCEPSvecObjectParser.java | 4 +- .../subobject/EROAsNumberSubobjectParser.java | 4 +- ...EROUnnumberedInterfaceSubobjectParser.java | 5 +- ...RROUnnumberedInterfaceSubobjectParser.java | 5 +- .../pcep/impl/subobject/Type1LabelParser.java | 2 +- .../WavebandSwitchingLabelParser.java | 7 +- .../subobject/XROAsNumberSubobjectParser.java | 4 +- .../subobject/XROIpPrefixSubobjectParser.java | 4 +- .../subobject/XROSRLGSubobjectParser.java | 3 +- ...XROUnnumberedInterfaceSubobjectParser.java | 5 +- .../pcep/impl/tlv/LSPIdentifierTlvParser.java | 8 +- .../impl/tlv/LspUpdateErrorTlvParser.java | 2 +- .../pcep/impl/tlv/OrderTlvParser.java | 4 +- .../impl/tlv/OverloadedDurationTlvParser.java | 2 +- .../pcep/impl/tlv/RSVPErrorSpecTlvParser.java | 6 +- .../pcep/impl/tlv/ReqMissingTlvParser.java | 2 +- .../opendaylight/protocol/util/ByteArray.java | 36 +++++++-- .../protocol/util/ByteArrayTest.java | 78 ++++++++++--------- 29 files changed, 134 insertions(+), 111 deletions(-) diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParser.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParser.java index c40ffb67e8..7de0652014 100644 --- a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParser.java +++ b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParser.java @@ -365,6 +365,8 @@ public final class LinkstateNlriParser implements NlriParser { } public static byte[] serializeNlri(final CLinkstateDestination destination) { + final byte[] typeBytes = ByteArray.intToBytes(destination.getNlriType().getIntValue(), TYPE_LENGTH); + // FIXME: BUG-108: finish this throw new UnsupportedOperationException("BUG-108: not implemented"); } diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPNotificationMessageParser.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPNotificationMessageParser.java index 54b92d3e9f..d078394bc0 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPNotificationMessageParser.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPNotificationMessageParser.java @@ -50,9 +50,9 @@ public final class BGPNotificationMessageParser implements MessageParser, Messag final byte[] msgBody = (ntf.getData() == null) ? new byte[ERROR_SIZE] : new byte[ERROR_SIZE + ntf.getData().length]; - msgBody[0] = ByteArray.intToBytes(ntf.getErrorCode())[Integer.SIZE / Byte.SIZE - 1]; + msgBody[0] = UnsignedBytes.checkedCast(ntf.getErrorCode()); - msgBody[1] = ByteArray.intToBytes(ntf.getErrorSubcode())[Integer.SIZE / Byte.SIZE - 1]; + msgBody[1] = UnsignedBytes.checkedCast(ntf.getErrorSubcode()); if (ntf.getData() != null) { System.arraycopy(ntf.getData(), 0, msgBody, ERROR_SIZE, ntf.getData().length); diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java index 3eced158a9..ba6385171c 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java @@ -94,7 +94,7 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali int offset = 0; - msgBody[offset] = ByteArray.intToBytes(BGP_VERSION)[(Integer.SIZE / Byte.SIZE) - 1]; + msgBody[offset] = UnsignedBytes.checkedCast(BGP_VERSION); offset += VERSION_SIZE; // When our AS number does not fit into two bytes, we report it as AS_TRANS @@ -103,16 +103,16 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali openAS = AS_TRANS; } - System.arraycopy(ByteArray.longToBytes(openAS), 6, msgBody, offset, AS_SIZE); + System.arraycopy(ByteArray.longToBytes(openAS, AS_SIZE), 0, msgBody, offset, AS_SIZE); offset += AS_SIZE; - System.arraycopy(ByteArray.intToBytes(open.getHoldTimer()), 2, msgBody, offset, HOLD_TIME_SIZE); + System.arraycopy(ByteArray.intToBytes(open.getHoldTimer(), HOLD_TIME_SIZE), 0, msgBody, offset, HOLD_TIME_SIZE); offset += HOLD_TIME_SIZE; System.arraycopy(Ipv4Util.bytesForAddress(open.getBgpIdentifier()), 0, msgBody, offset, BGP_ID_SIZE); offset += BGP_ID_SIZE; - msgBody[offset] = ByteArray.intToBytes(optParamsLength)[Integer.SIZE / Byte.SIZE - 1]; + msgBody[offset] = UnsignedBytes.checkedCast(optParamsLength); int index = MIN_MSG_LENGTH; if (optParams != null) { @@ -145,8 +145,7 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength); } if (UnsignedBytes.toInt(body[0]) != BGP_VERSION) { - throw new BGPDocumentedException("BGP Protocol version " + UnsignedBytes.toInt(body[0]) + " not supported.", BGPError.VERSION_NOT_SUPPORTED, ByteArray.subByte( - ByteArray.intToBytes(BGP_VERSION), 2, 2)); + throw new BGPDocumentedException("BGP Protocol version " + UnsignedBytes.toInt(body[0]) + " not supported.", BGPError.VERSION_NOT_SUPPORTED); } int offset = VERSION_SIZE; diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/As4CapabilityHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/As4CapabilityHandler.java index f976affd6f..e158c10ccc 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/As4CapabilityHandler.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/As4CapabilityHandler.java @@ -36,7 +36,6 @@ public final class As4CapabilityHandler implements CapabilityParser, CapabilityS } private static byte[] putAS4BytesParameterValue(final CAs4Bytes param) { - return ByteArray.subByte(ByteArray.longToBytes(param.getAs4BytesCapability().getAsNumber().getValue()), Long.SIZE / Byte.SIZE - - AS4_LENGTH, AS4_LENGTH); + return ByteArray.longToBytes(param.getAs4BytesCapability().getAsNumber().getValue(), AS4_LENGTH); } } \ No newline at end of file diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java index 890c5e4d22..fc500ee880 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/MessageUtil.java @@ -36,8 +36,8 @@ public final class MessageUtil { final byte[] retBytes = new byte[COMMON_HEADER_LENGTH + body.length]; Arrays.fill(retBytes, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE); - System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH), Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH, - retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH); + System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH, LENGTH_FIELD_LENGTH), 0, retBytes, MARKER_LENGTH, + LENGTH_FIELD_LENGTH); retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(type); ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH); diff --git a/concepts/src/test/java/org/opendaylight/protocol/concepts/BandwidthTest.java b/concepts/src/test/java/org/opendaylight/protocol/concepts/BandwidthTest.java index 6fced21e9d..6fed7a34e1 100644 --- a/concepts/src/test/java/org/opendaylight/protocol/concepts/BandwidthTest.java +++ b/concepts/src/test/java/org/opendaylight/protocol/concepts/BandwidthTest.java @@ -27,10 +27,10 @@ public class BandwidthTest { @Before public void setUp() { - this.b1 = new Bandwidth(ByteArray.intToBytes(1000)); - this.b2 = new Bandwidth(ByteArray.intToBytes(2000)); - this.b3 = new Bandwidth(ByteArray.intToBytes(2000)); - this.b4 = new Bandwidth(ByteArray.intToBytes(100)); + this.b1 = new Bandwidth(ByteArray.intToBytes(1000, 4)); + this.b2 = new Bandwidth(ByteArray.intToBytes(2000, 4)); + this.b3 = new Bandwidth(ByteArray.intToBytes(2000, 4)); + this.b4 = new Bandwidth(ByteArray.intToBytes(100, 4)); } @Test diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/AbstractMessageParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/AbstractMessageParser.java index 46dbb3fae0..d9a963960f 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/AbstractMessageParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/AbstractMessageParser.java @@ -83,8 +83,8 @@ public abstract class AbstractMessageParser implements MessageParser, MessageSer } // objLength - System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH), Integer.SIZE / Byte.SIZE - - OBJ_LENGTH_F_LENGTH, retBytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH); + System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH, OBJ_LENGTH_F_LENGTH), 0, retBytes, + OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH); ByteArray.copyWhole(valueBytes, retBytes, COMMON_OBJECT_HEADER_LENGTH); return retBytes; diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractObjectWithTlvsParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractObjectWithTlvsParser.java index d09ad9da99..07bedc6616 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractObjectWithTlvsParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/AbstractObjectWithTlvsParser.java @@ -74,11 +74,11 @@ public abstract class AbstractObjectWithTlvsParser implements ObjectParser, O final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv); - final byte[] typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - TLV_TYPE_F_LENGTH)); + final byte[] typeBytes = ByteArray.intToBytes(serializer.getType(), TLV_TYPE_F_LENGTH); final byte[] valueBytes = serializer.serializeTlv(tlv); - final byte[] lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - TLV_LENGTH_F_LENGTH); + final byte[] lengthBytes = ByteArray.intToBytes(valueBytes.length, TLV_LENGTH_F_LENGTH); final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)]; diff --git a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLspaObjectParser.java b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLspaObjectParser.java index 6642900ba9..fe501ca3ab 100644 --- a/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLspaObjectParser.java +++ b/pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/object/PCEPLspaObjectParser.java @@ -96,9 +96,12 @@ public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser