2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import java.util.Arrays;
11 import java.util.Comparator;
12 import java.util.List;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCase;
21 import com.google.common.annotations.VisibleForTesting;
24 * This comparator is intended to implement BGP Best Path Selection algorithm, as described at
26 * @see http://www.cisco.com/en/US/tech/tk365/technologies_tech_note09186a0080094431.shtml
28 * @param <T> Actual object state reference
30 final class BGPObjectComparator implements Comparator<PathAttributes> {
32 private final AsNumber ourAS;
34 private final byte[] id1;
36 private final byte[] id2;
38 public BGPObjectComparator(final AsNumber ourAs, final byte[] id1, final byte[] id2) {
45 public int compare(final PathAttributes o1, final PathAttributes o2) {
52 if (o1.equals(o2) && Arrays.equals(this.id1, this.id2)) {
55 // 1. prefer path with accessible nexthop
56 // - we assume that all nexthops are accessible
58 // 2. prefer path with higher LOCAL_PREF
59 if ((o1.getLocalPref() != null || o2.getLocalPref() != null)
60 && (o1.getLocalPref() != null && !o1.getLocalPref().equals(o2.getLocalPref()))) {
61 return o1.getLocalPref().getPref().compareTo(o2.getLocalPref().getPref());
64 // 3. prefer learned path
65 // - we assume that all paths are learned
67 // 4. prefer the path with the shortest AS_PATH.
68 if (!o1.getAsPath().equals(o2.getAsPath())) {
69 final Integer i1 = countAsPath(o1.getAsPath().getSegments());
70 final Integer i2 = countAsPath(o2.getAsPath().getSegments());
71 return i2.compareTo(i1);
74 // 5. prefer the path with the lowest origin type
75 // - IGP is lower than Exterior Gateway Protocol (EGP), and EGP is lower than INCOMPLETE
76 if (!o1.getOrigin().equals(o2.getOrigin())) {
77 if (o1.getOrigin().getValue().equals(BgpOrigin.Igp)) {
80 if (o2.getOrigin().getValue().equals(BgpOrigin.Igp)) {
83 if (o1.getOrigin().getValue().equals(BgpOrigin.Egp)) {
90 // 6. prefer the path with the lowest multi-exit discriminator (MED)
91 if ((o1.getMultiExitDisc() != null || o2.getMultiExitDisc() != null)
92 && (o1.getMultiExitDisc() != null && !o1.getMultiExitDisc().equals(o2.getMultiExitDisc()))) {
93 return o2.getMultiExitDisc().getMed().compareTo(o1.getMultiExitDisc().getMed());
96 // 7. prefer eBGP over iBGP paths
97 // EBGP is peering between two different AS, whereas IBGP is between same AS (Autonomous System).
98 final AsNumber first = getPeerAs(o1.getAsPath().getSegments());
99 final AsNumber second = getPeerAs(o2.getAsPath().getSegments());
100 if ((first != null || second != null) && (first != null && !first.equals(second))) {
101 if (first == null || first.equals(this.ourAS)) {
104 if (second == null || second.equals(this.ourAS)) {
109 // 8. Prefer the path with the lowest IGP metric to the BGP next hop.
110 // - no next hop metric is advertized
112 // 9. When both paths are external, prefer the path that was received first (the oldest one).
113 // if (first.equals(this.ourAS) && second.equals(this.ourAS)) {
114 // FIXME: do we have a way how to determine which one was received first?
117 // 10. Prefer the route that comes from the BGP router with the lowest router ID.
118 // The router ID is the highest IP address on the router, with preference given to loopback addresses.
119 // If a path contains route reflector (RR) attributes, the originator ID is substituted for the router ID in the
120 // path selection process.
121 byte[] oid1 = this.id1;
122 byte[] oid2 = this.id2;
123 if (o1.getOriginatorId() != null) {
124 oid1 = o1.getOriginatorId();
126 if (o2.getOriginatorId() != null) {
127 oid2 = o2.getOriginatorId();
129 if (!Arrays.equals(oid1, oid2)) {
130 return compareByteArrays(oid1, oid2);
132 // 11. prefer the path with the minimum cluster list length
135 if (o1.getClusterId() != null) {
136 cluster1 = o1.getClusterId().size();
138 if (o2.getClusterId() != null) {
139 cluster2 = o2.getClusterId().size();
141 if (cluster1 != cluster2) {
142 return ((Integer) cluster1).compareTo(cluster2);
145 // 12. Prefer the path that comes from the lowest neighbor address.
146 // FIXME: do we know this?
151 private static int countAsPath(final List<Segments> segments) {
152 // an AS_SET counts as 1, no matter how many ASs are in the set.
154 boolean setPresent = false;
155 for (final Segments s : segments) {
156 if (s.getCSegment() instanceof ASetCase) {
159 final AListCase list = (AListCase) s.getCSegment();
160 count += list.getAList().getAsSequence().size();
163 return (setPresent) ? ++count : count;
166 private static AsNumber getPeerAs(final List<Segments> segments) {
167 if (segments.size() == 0) {
170 final AListCase first = (AListCase) segments.get(0).getCSegment();
171 return first.getAList().getAsSequence().get(0).getAs();
175 public static int compareByteArrays(final byte[] byteOne, final byte[] byteTwo) {
176 for (int i = 0; i < byteOne.length; i++) {
177 final int res = Byte.compare(byteOne[i], byteTwo[i]);