BUG-6237: Topology freezes or slows down due to java.util.concurrent.TimeoutException
[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 static final Logger LOG = LoggerFactory.getLogger(AbstractNPathsRouteEntry.class);
21
22     private final long nBestPaths;
23
24     AbstractNPathsRouteEntry(final Long nBestPaths) {
25         this.nBestPaths = nBestPaths;
26     }
27
28     @Override
29     public final boolean selectBest(final long localAs) {
30         final List<AddPathBestPath> newBestPathList = new ArrayList<>();
31         final List<RouteKey> keyList = this.offsets.getRouteKeysList();
32         final long maxSearch = this.nBestPaths < this.offsets.size() && this.nBestPaths != 0 ? this.nBestPaths : this.offsets.size();
33         for (long i = 0; i < maxSearch; ++i) {
34             final AddPathBestPath newBest = selectBest(localAs, keyList);
35             newBestPathList.add(newBest);
36             keyList.remove(newBest.getRouteKey());
37         }
38
39         if(this.bestPath != null) {
40             this.bestPathRemoved = new ArrayList<>(this.bestPath);
41         }
42         if (!newBestPathList.equals(this.bestPath) ||
43             this.bestPathRemoved != null && this.bestPathRemoved.removeAll(newBestPathList) && !this.bestPathRemoved.isEmpty()) {
44             this.bestPath = newBestPathList;
45             LOG.trace("Actual Best {}, removed best {}", this.bestPath, this.bestPathRemoved);
46             return true;
47         }
48         return false;
49     }
50 }