Bug: 5287
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ComplexRouteEntry.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.primitives.UnsignedInteger;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15
16 final class ComplexRouteEntry extends AbstractRouteEntry {
17     private static final MapEntryNode[] EMPTY_VALUES = new MapEntryNode[0];
18     private MapEntryNode[] values = EMPTY_VALUES;
19
20     @Override
21     protected int addRoute(final UnsignedInteger routerId, final NodeIdentifier attributesIdentifier, final NormalizedNode<?, ?> data) {
22         final OffsetMap oldMap = getOffsets();
23         final int offset = super.addRoute(routerId, attributesIdentifier, data);
24         final OffsetMap newMap = getOffsets();
25
26         if (!newMap.equals(oldMap)) {
27             this.values = newMap.expand(oldMap, this.values, offset);
28         }
29
30         newMap.setValue(this.values, offset, data);
31         return offset;
32     }
33
34     @Override
35     protected MapEntryNode createValue(final PathArgument routeId) {
36         final OffsetMap map = getOffsets();
37         final int offset = map.offsetOf(getBestRouterId());
38         return map.getValue(this.values, offset);
39     }
40
41     @Override
42     boolean removeRoute(final UnsignedInteger routerId) {
43         final OffsetMap map = getOffsets();
44         final int offset = map.offsetOf(routerId);
45         final boolean ret = removeRoute(routerId, offset);
46         this.values = map.removeValue(this.values, offset);
47         return ret;
48     }
49 }