BUG-4438: When delete a route from the app RIB transaction chain brokes
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / OffsetMap.java
index 1dcb46dba3f22c2f2c1edd5ec47e2befd4801ffd..9f7710d2e76379a0e81d63f4eef12d1d9d504ddf 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.cache.LoadingCache;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSet.Builder;
 import com.google.common.primitives.UnsignedInteger;
+import java.lang.reflect.Array;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
@@ -21,7 +22,7 @@ import java.util.Set;
 
 /**
  * A map of Router identifier to an offset. Used to maintain a simple
- * offset-based lookup across multiple {@link RouteEntry} objects,
+ * offset-based lookup across multiple {@link AbstractRouteEntry} objects,
  * which share either contributors or consumers.
  *
  * We also provide utility reformat methods, which provide access to
@@ -75,18 +76,20 @@ final class OffsetMap {
     }
 
     <T> T getValue(final T[] array, final int offset) {
-        Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
+        Preconditions.checkArgument(offset >= 0, "Invalid negative offset %s", offset);
+        Preconditions.checkArgument(offset < routerIds.length, "Invalid offset %s for %s router IDs", offset, routerIds.length);
         return array[offset];
     }
 
     <T> void setValue(final T[] array, final int offset, final T value) {
-        Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
+        Preconditions.checkArgument(offset >= 0, "Invalid negative offset %s", offset);
+        Preconditions.checkArgument(offset < routerIds.length, "Invalid offset %s for %s router IDs", offset, routerIds.length);
         array[offset] = value;
     }
 
     <T> T[] expand(final OffsetMap oldOffsets, final T[] oldArray, final int offset) {
         @SuppressWarnings("unchecked")
-        final T[] ret = (T[]) new Object[this.routerIds.length];
+        final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), this.routerIds.length);
         final int oldSize = oldOffsets.routerIds.length;
 
         System.arraycopy(oldArray, 0, ret, 0, offset);