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