Refactored LspDbVersion parser. 03/5303/1
authorDana Kutenicsova <dkutenic@cisco.com>
Thu, 13 Feb 2014 09:30:00 +0000 (10:30 +0100)
committerDana Kutenicsova <dkutenic@cisco.com>
Thu, 13 Feb 2014 09:30:48 +0000 (10:30 +0100)
Change-Id: Iebc6cf8626ae06b757c0f2f57e1365b3a60ba3a3
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02LspDbVersionTlvParser.java
pcep/ietf-stateful02/src/test/java/org/opendaylight/protocol/pcep/ietf/PCEPTlvParserTest.java

index 1e49ac10297ba5f634c1f9f45fbeb79303d25c44..0fb23d42cf5ef4d8c744ed8a20316610518cc91a 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful02;
 
-import java.util.Arrays;
+import java.math.BigInteger;
 
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
@@ -17,7 +17,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.cra
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 
-import com.google.common.primitives.UnsignedLong;
+import com.google.common.base.Preconditions;
 
 /**
  * Parser for {@link LspDbVersion}
@@ -30,28 +30,15 @@ public final class Stateful02LspDbVersionTlvParser implements TlvParser, TlvSeri
 
        @Override
        public LspDbVersion parseTlv(final byte[] buffer) throws PCEPDeserializerException {
-               return new LspDbVersionBuilder().setVersion(
-                               UnsignedLong.fromLongBits(ByteArray.bytesToLong(ByteArray.subByte(buffer, 0, DBV_F_LENGTH))).bigIntegerValue()).build();
+               return new LspDbVersionBuilder().setVersion(BigInteger.valueOf(ByteArray.bytesToLong(ByteArray.subByte(buffer, 0, DBV_F_LENGTH)))).build();
        }
 
        @Override
        public byte[] serializeTlv(final Tlv tlv) {
-               if (tlv == null) {
-                       throw new IllegalArgumentException("LspDbVersionTlv is mandatory.");
-               }
+               Preconditions.checkNotNull(tlv, "LspDbVersionTlv is mandatory.");
                final LspDbVersion lsp = (LspDbVersion) tlv;
-               final byte[] array = ByteArray.longToBytes(UnsignedLong.valueOf(lsp.getVersion()).longValue(), DBV_F_LENGTH);
-               if (array.length > DBV_F_LENGTH) {
-                       throw new IllegalArgumentException("LspDBVersion too big.");
-               }
-               final byte[] result = new byte[DBV_F_LENGTH];
-               Arrays.fill(result, (byte) 0);
-               int j = 7;
-               for (int i = array.length - 1; i >= 0; i--) {
-                       result[j] |= array[i];
-                       j--;
-               }
-               return result;
+               final byte[] array = ByteArray.longToBytes(lsp.getVersion().longValue(), DBV_F_LENGTH);
+               return array;
        }
 
        @Override
index 83bec50d9ab7b6f17656c7343f7c644f7e23ed46..060e40bebfb790476424ac6fbbcbd9d5c8dd95cf 100644 (file)
@@ -10,15 +10,20 @@ package org.opendaylight.protocol.pcep.ietf;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
+import java.math.BigInteger;
+
 import org.junit.Test;
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.concepts.Ipv6Util;
+import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02LspDbVersionTlvParser;
 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02LspSymbolicNameTlvParser;
-import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02StatefulCapabilityTlvParser;
 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02RSVPErrorSpecTlvParser;
+import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02StatefulCapabilityTlvParser;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCaseBuilder;
@@ -45,6 +50,8 @@ public class PCEPTlvParserTest {
        private static final byte[] userErrorBytes = { (byte) 0xc2, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x30, (byte) 0x39,
                        (byte) 0x05, (byte) 0x09, (byte) 0x00, (byte) 0x26, (byte) 0x75, (byte) 0x73, (byte) 0x65, (byte) 0x72, (byte) 0x20,
                        (byte) 0x64, (byte) 0x65, (byte) 0x73, (byte) 0x63 };
+       private static final byte[] lspDbBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+                       (byte) 0xb4 };
 
        @Test
        public void testStatefulTlv() throws PCEPDeserializerException {
@@ -103,4 +110,13 @@ public class PCEPTlvParserTest {
                assertEquals(tlv, parser.parseTlv(userErrorBytes));
                assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
        }
+
+       @Test
+       public void testLspDbVersionTlv() throws PCEPDeserializerException {
+               final Stateful02LspDbVersionTlvParser parser = new Stateful02LspDbVersionTlvParser();
+               final LspDbVersion tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(180L)).build();
+               assertEquals(tlv, parser.parseTlv(lspDbBytes));
+               assertArrayEquals(lspDbBytes, parser.serializeTlv(tlv));
+
+       }
 }