c9916f33f0126e746ce8403b5ca991dff55fc98b
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / all / paths / AbstractAllPathsRouteEntry.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.all.paths;
10
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.mode.api.BestPathState;
15 import org.opendaylight.protocol.bgp.mode.impl.BestPathStateImpl;
16 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathAbstractRouteEntry;
17 import org.opendaylight.protocol.bgp.mode.impl.add.AddPathBestPath;
18 import org.opendaylight.protocol.bgp.mode.impl.add.RouteKey;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 abstract class AbstractAllPathsRouteEntry extends AddPathAbstractRouteEntry {
24     @Override
25     public final boolean selectBest(final long localAs) {
26         final List<AddPathBestPath> newBestPathList = new ArrayList<>();
27         final List<RouteKey> keyList = this.offsets.getRouteKeysList();
28
29         if (!keyList.isEmpty()) {
30             /* we set the best path first on List for not supported Add path cases*/
31             final AddPathBestPath newBest = selectBest(localAs, keyList);
32             newBestPathList.add(newBest);
33             keyList.remove(newBest.getRouteKey());
34             /*we add the rest of path, regardless in what order they are, since this is all path case */
35             for (final RouteKey key : keyList) {
36                 final int offset = this.offsets.offsetOf(key);
37                 final ContainerNode attributes = this.offsets.getValue(this.values, offset);
38                 Preconditions.checkNotNull(key.getRouteId(), "Router ID may not be null");
39                 if (attributes != null) {
40                     final BestPathState state = new BestPathStateImpl(attributes);
41                     final AddPathBestPath bestPath = new AddPathBestPath(state, key, offset, this.offsets.getValue(this.pathsId, offset));
42                     newBestPathList.add(bestPath);
43                 }
44             }
45         }
46         return isBestPathNew(newBestPathList);
47     }
48 }