BUG-6651: Route Advertisement improvement
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / n / paths / AbstractNPathsRouteEntry.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.n.paths;
10
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathAbstractRouteEntry;
15 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathBestPath;
16 import org.opendaylight.protocol.bgp.mode.impl.add.RouteKey;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 abstract class AbstractNPathsRouteEntry extends AddPathAbstractRouteEntry {
21     private final long nBestPaths;
22
23     AbstractNPathsRouteEntry(final Long nBestPaths) {
24         this.nBestPaths = nBestPaths;
25     }
26
27     @Override
28     public final boolean selectBest(final long localAs) {
29         final List<AddPathBestPath> newBestPathList = new ArrayList<>();
30         final List<RouteKey> keyList = this.offsets.getRouteKeysList();
31         final long maxSearch = this.nBestPaths < this.offsets.size() && this.nBestPaths != 0 ? this.nBestPaths : this.offsets.size();
32         for (long i = 0; i < maxSearch; ++i) {
33             final AddPathBestPath newBest = selectBest(localAs, keyList);
34             newBestPathList.add(newBest);
35             keyList.remove(newBest.getRouteKey());
36         }
37         return isBestPathNew(ImmutableList.copyOf(newBestPathList));
38     }
39 }