f1faf1634ae02bc21bc016353d19bc2a7314e20c
[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 java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathAbstractRouteEntry;
14 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathBestPath;
15 import org.opendaylight.protocol.bgp.mode.impl.add.RouteKey;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 abstract class AbstractNPathsRouteEntry extends AddPathAbstractRouteEntry {
20     private final long nBestPaths;
21
22     AbstractNPathsRouteEntry(final Long nBestPaths) {
23         this.nBestPaths = nBestPaths;
24     }
25
26     @Override
27     public final boolean selectBest(final long localAs) {
28         final List<AddPathBestPath> newBestPathList = new ArrayList<>();
29         final List<RouteKey> keyList = this.offsets.getRouteKeysList();
30         final long maxSearch = this.nBestPaths < this.offsets.size() && this.nBestPaths != 0 ? this.nBestPaths : this.offsets.size();
31         for (long i = 0; i < maxSearch; ++i) {
32             final AddPathBestPath newBest = selectBest(localAs, keyList);
33             newBestPathList.add(newBest);
34             keyList.remove(newBest.getRouteKey());
35         }
36         return isBestPathNew(newBestPathList);
37     }
38 }