Bug 2362: Added range validation as last part of deserialization. 55/17855/3
authorTony Tkacik <ttkacik@cisco.com>
Tue, 7 Apr 2015 14:46:38 +0000 (16:46 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Tue, 7 Apr 2015 18:54:30 +0000 (20:54 +0200)
Change-Id: I801aa22f868a13c098686aa077c46a378af8dfe4
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/AbstractIntegerStringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Int16StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Int32StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Int64StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Int8StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Uint16StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Uint32StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Uint64StringCodec.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/Uint8StringCodec.java

index d8e7df867cbc534d9301fb4bd87c57259f300230..79118c3676207a80430c7d4da242e2e4960ba925 100644 (file)
@@ -9,9 +9,16 @@ package org.opendaylight.yangtools.yang.data.impl.codec;
 
 import com.google.common.base.CharMatcher;
 import com.google.common.base.Optional;
+import com.google.common.collect.Range;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 
 abstract class AbstractIntegerStringCodec<N extends Number & Comparable<N>, T extends TypeDefinition<T>> extends TypeDefinitionAwareCodec<N, T>{
 
@@ -28,6 +35,86 @@ abstract class AbstractIntegerStringCodec<N extends Number & Comparable<N>, T ex
             + "\n  - a hexadecimal number (prefix 0x)," + "%n  - an octal number (prefix 0)."
             + "\nSigned values are allowed. Spaces between digits are NOT allowed.";
 
+
+    private final List<Range<N>> rangeConstraints;
+
+    protected AbstractIntegerStringCodec(final Optional<T> typeDefinition, final List<RangeConstraint> constraints , final Class<N> outputClass) {
+        super(typeDefinition, outputClass);
+        if(constraints.isEmpty()) {
+            rangeConstraints = Collections.emptyList();
+        } else {
+            final ArrayList<Range<N>> builder = new ArrayList<>(constraints.size());
+            for(final RangeConstraint yangConstraint : constraints) {
+                builder.add(createRange(yangConstraint.getMin(),yangConstraint.getMax()));
+            }
+            rangeConstraints = builder;
+        }
+
+    }
+
+    private Range<N> createRange(final Number yangMin, final Number yangMax) {
+        final N min = convertValue(yangMin);
+        final N max = convertValue(yangMax);
+        return Range.closed(min, max);
+    }
+
+    @Override
+    public final N deserialize(final String stringRepresentation) {
+        final int base = provideBase(stringRepresentation);
+        final N deserialized;
+        if (base == 16) {
+            deserialized = deserialize(normalizeHexadecimal(stringRepresentation),base);
+        } else {
+            deserialized = deserialize(stringRepresentation,base);
+        }
+        validate(deserialized);
+        return deserialized;
+    }
+
+
+    private final void validate(final N value) {
+        if (rangeConstraints.isEmpty()) {
+            return;
+        }
+        for (final Range<N> constraint : rangeConstraints) {
+            if (constraint.contains(value)) {
+                return;
+            }
+        }
+        // FIXME: Provide better error report.
+        throw new IllegalArgumentException("Value is not in required range.");
+    }
+
+    /**
+     * Deserializes value from supplied string representation
+     * is supplied radix.
+     *
+     * See {@link Integer#parseInt(String, int)} for in-depth
+     * description about string and radix relationship.
+     *
+     * @param stringRepresentation String representation
+     * @param radix numeric base.
+     * @return Deserialized value.
+     */
+    protected abstract N deserialize(String stringRepresentation, int radix);
+
+    protected abstract N convertValue(Number value);
+
+
+    protected static List<RangeConstraint> extractRange(final IntegerTypeDefinition type) {
+        if(type == null) {
+            return Collections.emptyList();
+        }
+        return type.getRangeConstraints();
+    }
+
+    protected static List<RangeConstraint> extractRange(final UnsignedIntegerTypeDefinition type) {
+        if(type == null) {
+            return Collections.emptyList();
+        }
+        return type.getRangeConstraints();
+    }
+
     private static final int provideBase(final String integer) {
         if (integer == null) {
             throw new IllegalArgumentException("String representing integer number cannot be NULL");
@@ -62,21 +149,4 @@ abstract class AbstractIntegerStringCodec<N extends Number & Comparable<N>, T ex
 
         return X_MATCHER.removeFrom(hexInt);
     }
-
-    protected AbstractIntegerStringCodec(final Optional<T> typeDefinition, final Class<N> outputClass) {
-        super(typeDefinition, outputClass);
-
-    }
-
-    @Override
-    public final N deserialize(final String stringRepresentation) {
-        final int base = provideBase(stringRepresentation);
-        if (base == 16) {
-            return deserialize(normalizeHexadecimal(stringRepresentation),base);
-        }
-        return deserialize(stringRepresentation,base);
-    }
-
-    protected abstract N deserialize(String stringRepresentation, int base);
-
 }
index f686cc3816bb741b7932567c57e04f6428d303f1..5a0e90794a817841acb6f737773b3d8dc8aa307b 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,13 +11,14 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Int16Codec;
 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
 
-class Int16StringCodec extends AbstractIntegerStringCodec<Short, IntegerTypeDefinition> implements
-        Int16Codec<String> {
+class Int16StringCodec extends AbstractIntegerStringCodec<Short, IntegerTypeDefinition> implements Int16Codec<String> {
 
     protected Int16StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
-        super(typeDef, Short.class);
+        super(typeDef, extractRange(typeDef.orNull()), Short.class);
     }
 
+
+
     @Override
     public final Short deserialize(final String stringRepresentation, final int base) {
         return Short.valueOf(stringRepresentation, base);
@@ -27,4 +28,9 @@ class Int16StringCodec extends AbstractIntegerStringCodec<Short, IntegerTypeDefi
     public final String serialize(final Short data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Short convertValue(final Number value) {
+        return value.shortValue();
+    }
+}
index b2575c6192294b177ef100c256eaad07f359bad9..5c9785d8ee157c678100df3327afd448074fdd6f 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,11 +11,10 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Int32Codec;
 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
 
-class Int32StringCodec extends AbstractIntegerStringCodec<Integer, IntegerTypeDefinition> implements
-        Int32Codec<String> {
+class Int32StringCodec extends AbstractIntegerStringCodec<Integer, IntegerTypeDefinition> implements Int32Codec<String> {
 
     protected Int32StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
-        super(typeDef, Integer.class);
+        super(typeDef, extractRange(typeDef.orNull()), Integer.class);
     }
 
     @Override
@@ -27,4 +26,9 @@ class Int32StringCodec extends AbstractIntegerStringCodec<Integer, IntegerTypeDe
     public final String serialize(final Integer data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Integer convertValue(final Number value) {
+        return value.intValue();
+    }
+}
index 3e6aa678cef9af8e58ca1453353e0969c3a42fe5..98be5782c35f9f3a7f43c6330784b901983ebadc 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,11 +11,10 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Int64Codec;
 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
 
-class Int64StringCodec extends AbstractIntegerStringCodec<Long, IntegerTypeDefinition> implements
-        Int64Codec<String> {
+class Int64StringCodec extends AbstractIntegerStringCodec<Long, IntegerTypeDefinition> implements Int64Codec<String> {
 
     protected Int64StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
-        super(typeDef, Long.class);
+        super(typeDef, extractRange(typeDef.orNull()), Long.class);
     }
 
     @Override
@@ -27,4 +26,9 @@ class Int64StringCodec extends AbstractIntegerStringCodec<Long, IntegerTypeDefin
     public final String serialize(final Long data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Long convertValue(final Number value) {
+        return value.longValue();
+    }
+}
index a475d7afccb0d2308a784edb22a61717ee88432d..a1af2adee59740611b8b908a2d7e289ea249e5ef 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,11 +11,10 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Int8Codec;
 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
 
-class Int8StringCodec extends AbstractIntegerStringCodec<Byte, IntegerTypeDefinition> implements
-        Int8Codec<String> {
+class Int8StringCodec extends AbstractIntegerStringCodec<Byte, IntegerTypeDefinition> implements Int8Codec<String> {
 
     protected Int8StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
-        super(typeDef, Byte.class);
+        super(typeDef, extractRange(typeDef.orNull()), Byte.class);
     }
 
     @Override
@@ -27,4 +26,9 @@ class Int8StringCodec extends AbstractIntegerStringCodec<Byte, IntegerTypeDefini
     public String serialize(final Byte data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Byte convertValue(final Number value) {
+        return value.byteValue();
+    }
+}
index 30480bd44f066d36b56607638c46fea6a959803e..af4b3f76dc6fd0195d447385ef16011a0acf7f61 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,19 +11,24 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Uint16Codec;
 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 
-class Uint16StringCodec extends AbstractIntegerStringCodec<Integer, UnsignedIntegerTypeDefinition>
-        implements Uint16Codec<String> {
+class Uint16StringCodec extends AbstractIntegerStringCodec<Integer, UnsignedIntegerTypeDefinition> implements
+        Uint16Codec<String> {
     protected Uint16StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
-        super(typeDef, Integer.class);
+        super(typeDef, extractRange(typeDef.orNull()), Integer.class);
     }
 
     @Override
-    public Integer deserialize(final String stringRepresentation, final int base) {
+    public final Integer deserialize(final String stringRepresentation, final int base) {
         return Integer.valueOf(stringRepresentation, base);
     }
 
     @Override
-    public String serialize(final Integer data) {
+    public final String serialize(final Integer data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Integer convertValue(final Number value) {
+        return value.intValue();
+    }
+}
index f16d25e6684a5cea9f605807e44eff0e0a899343..ba305db52895a74b24a8f60caaece7c2bef93691 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,11 +11,11 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Uint32Codec;
 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 
-class Uint32StringCodec extends AbstractIntegerStringCodec<Long, UnsignedIntegerTypeDefinition>
-        implements Uint32Codec<String> {
+class Uint32StringCodec extends AbstractIntegerStringCodec<Long, UnsignedIntegerTypeDefinition> implements
+        Uint32Codec<String> {
 
     protected Uint32StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
-        super(typeDef, Long.class);
+        super(typeDef, extractRange(typeDef.orNull()), Long.class);
     }
 
     @Override
@@ -27,4 +27,9 @@ class Uint32StringCodec extends AbstractIntegerStringCodec<Long, UnsignedInteger
     public final String serialize(final Long data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Long convertValue(final Number value) {
+        return value.longValue();
+    }
+}
index 5c98112fb237fe669fbb2cb042b28bb76d414534..222a52897e916fa75efae115cced24e6c7d84f50 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -12,10 +12,11 @@ import java.math.BigInteger;
 import org.opendaylight.yangtools.yang.data.api.codec.Uint64Codec;
 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 
-class Uint64StringCodec extends AbstractIntegerStringCodec<BigInteger, UnsignedIntegerTypeDefinition> implements Uint64Codec<String> {
+class Uint64StringCodec extends AbstractIntegerStringCodec<BigInteger, UnsignedIntegerTypeDefinition> implements
+        Uint64Codec<String> {
 
     protected Uint64StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
-        super(typeDef, BigInteger.class);
+        super(typeDef, extractRange(typeDef.orNull()), BigInteger.class);
     }
 
     @Override
@@ -27,4 +28,12 @@ class Uint64StringCodec extends AbstractIntegerStringCodec<BigInteger, UnsignedI
     public final String serialize(final BigInteger data) {
         return data == null ? "" : data.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    protected BigInteger convertValue(final Number value) {
+        if (value instanceof BigInteger) {
+            return (BigInteger) value;
+        }
+        return BigInteger.valueOf(value.longValue());
+    }
+}
index 62824f71c75cf84e8d47c466fc7a8d5d1bcaca6f..4c84501f981fa0afa51f2c08e9b3d1ab4a7dcfd6 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
@@ -11,11 +11,11 @@ import com.google.common.base.Optional;
 import org.opendaylight.yangtools.yang.data.api.codec.Uint8Codec;
 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 
-class Uint8StringCodec extends AbstractIntegerStringCodec<Short, UnsignedIntegerTypeDefinition>
-        implements Uint8Codec<String> {
+class Uint8StringCodec extends AbstractIntegerStringCodec<Short, UnsignedIntegerTypeDefinition> implements
+        Uint8Codec<String> {
 
     protected Uint8StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
-        super(typeDef, Short.class);
+        super(typeDef, extractRange(typeDef.orNull()), Short.class);
     }
 
     @Override
@@ -24,7 +24,12 @@ class Uint8StringCodec extends AbstractIntegerStringCodec<Short, UnsignedInteger
     }
 
     @Override
-    public final Short deserialize(final String stringRepresentation,final int base) {
+    public final Short deserialize(final String stringRepresentation, final int base) {
         return Short.valueOf(stringRepresentation, base);
     }
-}
\ No newline at end of file
+
+    @Override
+    protected Short convertValue(final Number value) {
+        return value.shortValue();
+    }
+}