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