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