Make sure PCEPMessageHeader is threadsafe 05/1105/1
authorRobert Varga <rovarga@cisco.com>
Thu, 5 Sep 2013 13:12:35 +0000 (15:12 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 5 Sep 2013 13:12:35 +0000 (15:12 +0200)
Change-Id: I9c8707b298a9b32d168e6c86e24f1c10b5ebfd25
Signed-off-by: Robert Varga <rovarga@cisco.com>
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/PCEPMessageFactory.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/PCEPMessageHeader.java

index 0753ebea2319eaa2c6547e41cbda6890884f2bd5..b665ca96cf96ebd751d0e362b6f845869c653e7c 100644 (file)
@@ -124,7 +124,6 @@ public class PCEPMessageFactory implements ProtocolMessageFactory {
        /**
         * 
         * @param bytes assume array of bytes without common header
-        * @param msgHeader
         * @return Parsed specific PCEPMessage
         * @throws PCEPDeserializerException
         * @throws PCEPDocumentedException
index 6cdf6383531880e9163604de3e077aec0cf8d3ea..d77386a272213c403ba850b220e224ca7cc22222 100644 (file)
@@ -9,11 +9,11 @@ package org.opendaylight.protocol.pcep.impl;
 
 import java.util.Arrays;
 
+import org.opendaylight.protocol.framework.ProtocolMessageHeader;
+import org.opendaylight.protocol.util.ByteArray;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import org.opendaylight.protocol.framework.ProtocolMessageHeader;
-import org.opendaylight.protocol.util.ByteArray;
 import com.google.common.primitives.UnsignedBytes;
 
 /**
@@ -53,15 +53,9 @@ public final class PCEPMessageHeader implements ProtocolMessageHeader {
         */
        public static final int COMMON_HEADER_LENGTH = VER_FLAGS_MF_LENGTH + TYPE_F_LENGTH + LENGTH_F_LENGTH;
 
-       private int type;
-       private int length;
-       private int version;
-
-       private boolean parsed = false;
-
-       public PCEPMessageHeader() {
-
-       }
+       private final int type;
+       private final int length;
+       private final int version;
 
        public PCEPMessageHeader(final int type, final int length, final int version) {
                this.type = type;
@@ -69,25 +63,28 @@ public final class PCEPMessageHeader implements ProtocolMessageHeader {
                this.version = version;
        }
 
-       public PCEPMessageHeader fromBytes(final byte[] bytes) {
-               if (bytes == null)
+       public static PCEPMessageHeader fromBytes(final byte[] bytes) {
+               if (bytes == null) {
                        throw new IllegalArgumentException("Array of bytes is mandatory");
+               }
 
                logger.trace("Attempt to parse message header: {}", ByteArray.bytesToHexString(bytes));
 
-               if (bytes.length < COMMON_HEADER_LENGTH)
+               if (bytes.length < COMMON_HEADER_LENGTH) {
                        throw new IllegalArgumentException("Too few bytes in passed array. Passed: " + bytes.length + "; Expected: >= " + COMMON_HEADER_LENGTH + ".");
+               }
 
-               this.type = UnsignedBytes.toInt(bytes[TYPE_F_OFFSET]);
+               final int type = UnsignedBytes.toInt(bytes[TYPE_F_OFFSET]);
 
-               this.length = ByteArray.bytesToInt(Arrays.copyOfRange(bytes,
+               final int length = ByteArray.bytesToInt(Arrays.copyOfRange(bytes,
                                LENGTH_F_OFFSET, LENGTH_F_OFFSET + LENGTH_F_LENGTH));
 
-               this.version = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
+               final int version = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
+
+               final PCEPMessageHeader ret = new PCEPMessageHeader(type, length, version);
 
-               logger.trace("Message header was parsed. {}", this);
-               this.parsed = true;
-               return this;
+               logger.trace("Message header was parsed. {}", ret);
+               return ret;
        }
 
        public byte[] toBytes() {
@@ -105,18 +102,6 @@ public final class PCEPMessageHeader implements ProtocolMessageHeader {
                return retBytes;
        }
 
-       public void setLength(final int length) {
-               this.length = length;
-       }
-
-       public void setType(final int type) {
-               this.type = type;
-       }
-
-       public void setVersion(final int version) {
-               this.version = version;
-       }
-
        public int getLength() {
                return this.length;
        }
@@ -129,20 +114,6 @@ public final class PCEPMessageHeader implements ProtocolMessageHeader {
                return this.type;
        }
 
-       /**
-        * @return the parsed
-        */
-       public boolean isParsed() {
-               return this.parsed;
-       }
-
-       /**
-        * @param parsed the parsed to set
-        */
-       public void setParsed() {
-               this.parsed = false;
-       }
-
        @Override
        public String toString() {
                final StringBuilder builder = new StringBuilder();