2c829c4ad95349a5d246d5654336bb54400d77a0
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / all / paths / ComplexRouteEntry.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.mode.impl.add.all.paths;
10
11 import com.google.common.primitives.UnsignedInteger;
12 import org.opendaylight.protocol.bgp.mode.api.BestPath;
13 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathBestPath;
14 import org.opendaylight.protocol.bgp.mode.impl.add.OffsetMap;
15 import org.opendaylight.protocol.bgp.mode.impl.add.RouteKey;
16 import org.opendaylight.protocol.bgp.mode.spi.RouteEntryUtil;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21
22 final class ComplexRouteEntry extends AbstractAllPathsRouteEntry {
23     private static final MapEntryNode[] EMPTY_VALUES = new MapEntryNode[0];
24     private MapEntryNode[] values = EMPTY_VALUES;
25
26     @Override
27     public boolean removeRoute(final UnsignedInteger routerId, final Long remotePathId) {
28         final RouteKey key = new RouteKey(routerId, remotePathId);
29         final OffsetMap map = getOffsets();
30         final int offset = map.offsetOf(key);
31         this.values = map.removeValue(this.values, offset);
32         return removeRoute(key, offset);
33     }
34
35     @Override
36     public MapEntryNode createValue(final PathArgument routeId, final BestPath path) {
37         final OffsetMap map = getOffsets();
38         final MapEntryNode mapValues = map.getValue(this.values, map.offsetOf(((AddPathBestPath) path).getRouteKey()));
39         return RouteEntryUtil.createComplexRouteValue(routeId, path, mapValues);
40     }
41
42     @Override
43     public int addRoute(final UnsignedInteger routerId, final Long remotePathId,
44             final NodeIdentifier attII, final NormalizedNode<?, ?> data) {
45         final OffsetMap oldMap = getOffsets();
46         final int offset = addRoute(new RouteKey(routerId, remotePathId), attII, data);
47         final OffsetMap newMap = getOffsets();
48
49         if (!newMap.equals(oldMap)) {
50             this.values = newMap.expand(oldMap, this.values, offset);
51         }
52
53         newMap.setValue(this.values, offset, data);
54         return offset;
55     }
56 }