0e03e460b859d2f600ffafcaf316638cc142070a
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / AddPathSelector.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 package org.opendaylight.protocol.bgp.mode.impl.add;
9
10 import org.opendaylight.protocol.bgp.mode.api.BestPathState;
11 import org.opendaylight.protocol.bgp.mode.impl.BestPathStateImpl;
12 import org.opendaylight.protocol.bgp.mode.spi.AbstractBestPathSelector;
13 import org.opendaylight.protocol.bgp.rib.spi.RouterId;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
15 import org.opendaylight.yangtools.yang.common.Uint32;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public final class AddPathSelector extends AbstractBestPathSelector {
20     private static final Logger LOG = LoggerFactory.getLogger(AddPathSelector.class);
21
22     private RouteKey bestRouteKey;
23     private Uint32 bestPathId;
24     private int bestOffset;
25
26     public AddPathSelector(final long ourAs) {
27         super(ourAs);
28     }
29
30     void processPath(final Attributes attrs, final RouteKey key, final int offsetPosition, final Uint32 pathId) {
31         // Consider only non-null attributes
32         if (attrs != null) {
33             final RouterId routerId = key.getRouterId();
34             final RouterId originatorId = replaceOriginator(routerId, attrs.getOriginatorId());
35
36             /*
37              * Store the new details if we have nothing stored or when the selection algorithm indicates new details
38              * are better.
39              */
40             final BestPathState state = new BestPathStateImpl(attrs);
41             if (this.bestOriginatorId == null || !isExistingPathBetter(state)) {
42                 LOG.trace("Selecting path from router {}", routerId);
43                 this.bestOriginatorId = originatorId;
44                 this.bestState = state;
45                 this.bestRouteKey = key;
46                 this.bestOffset = offsetPosition;
47                 this.bestPathId = pathId;
48             }
49         }
50     }
51
52     public AddPathBestPath result() {
53         return this.bestRouteKey == null ? null : new AddPathBestPath(this.bestState, this.bestRouteKey,
54             this.bestPathId, this.bestOffset);
55     }
56 }