Merge "Support proper route redistribution"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / OffsetMap.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableSet.Builder;
16 import com.google.common.primitives.UnsignedInteger;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.Set;
21
22 /**
23  * A map of Router identifier to an offset. Used to maintain a simple
24  * offset-based lookup across multiple {@link RouteEntry} objects,
25  * which share either contributors or consumers.
26  *
27  * We also provide utility reformat methods, which provide access to
28  * array members and array management features.
29  */
30 final class OffsetMap {
31     static final OffsetMap EMPTY = new OffsetMap(Collections.<UnsignedInteger>emptySet());
32     private static final LoadingCache<Set<UnsignedInteger>, OffsetMap> OFFSETMAPS = CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<UnsignedInteger>, OffsetMap>() {
33         @Override
34         public OffsetMap load(final Set<UnsignedInteger> key) throws Exception {
35             return new OffsetMap(key);
36         }
37     });
38     private static final Comparator<UnsignedInteger> IPV4_COMPARATOR = new Comparator<UnsignedInteger>() {
39         @Override
40         public int compare(final UnsignedInteger o1, final UnsignedInteger o2) {
41             return o1.compareTo(o2);
42         }
43     };
44     private final UnsignedInteger[] routerIds;
45
46     private OffsetMap(final Set<UnsignedInteger> routerIds) {
47         final UnsignedInteger[] array = routerIds.toArray(new UnsignedInteger[0]);
48         Arrays.sort(array, IPV4_COMPARATOR);
49         this.routerIds = array;
50     }
51
52     UnsignedInteger getRouterId(final int offset) {
53         Preconditions.checkArgument(offset >= 0);
54         return this.routerIds[offset];
55     }
56
57     int offsetOf(final UnsignedInteger routerId) {
58         return Arrays.binarySearch(this.routerIds, routerId, IPV4_COMPARATOR);
59     }
60
61     int size() {
62         return this.routerIds.length;
63     }
64
65     OffsetMap with(final UnsignedInteger routerId) {
66         // TODO: we could make this faster if we had an array-backed Set and requiring
67         //       the caller to give us the result of offsetOf() -- as that indicates
68         //       where to insert the new routerId while maintaining the sorted nature
69         //       of the array
70         final Builder<UnsignedInteger> b = ImmutableSet.builder();
71         b.add(this.routerIds);
72         b.add(routerId);
73
74         return OFFSETMAPS.getUnchecked(b.build());
75     }
76
77     <T> T getValue(final T[] array, final int offset) {
78         Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
79         return array[offset];
80     }
81
82     <T> void setValue(final T[] array, final int offset, final T value) {
83         Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
84         array[offset] = value;
85     }
86
87     <T> T[] expand(final OffsetMap oldOffsets, final T[] oldArray, final int offset) {
88         @SuppressWarnings("unchecked")
89         final T[] ret = (T[]) new Object[this.routerIds.length];
90         final int oldSize = oldOffsets.routerIds.length;
91
92         System.arraycopy(oldArray, 0, ret, 0, offset);
93         System.arraycopy(oldArray, offset, ret, offset + 1, oldSize - offset);
94
95         return ret;
96     }
97 }