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