29e5b096d833f19197002aad307eb782834d0cbe
[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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedInteger;
12 import org.opendaylight.protocol.bgp.mode.api.BestPathState;
13 import org.opendaylight.protocol.bgp.mode.impl.BestPathStateImpl;
14 import org.opendaylight.protocol.bgp.mode.spi.AbstractBestPathSelector;
15 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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 int bestOffsetPosition;
24     private Long bestPathId;
25
26     public AddPathSelector(final Long ourAs) {
27         super(ourAs);
28     }
29
30     public void processPath(final ContainerNode attrs, final RouteKey key, final int offsetPosition, final Long pathId) {
31         Preconditions.checkNotNull(key.getRouteId(), "Router ID may not be null");
32
33         // Consider only non-null attributes
34         if (attrs != null) {
35             final UnsignedInteger originatorId = replaceOriginator(key.getRouteId(), attrs);
36
37             /*
38              * Store the new details if we have nothing stored or when the selection algorithm indicates new details
39              * are better.
40              */
41             final BestPathState state = new BestPathStateImpl(attrs);
42             if (this.bestOriginatorId == null || !isExistingPathBetter(state)) {
43                 LOG.trace("Selecting path from router {}", key);
44                 this.bestOriginatorId = originatorId;
45                 this.bestState = state;
46                 this.bestRouteKey = key;
47                 this.bestOffsetPosition = offsetPosition;
48                 this.bestPathId = pathId;
49             }
50         }
51     }
52
53     public AddPathBestPath result() {
54         return this.bestRouteKey == null ? null : new AddPathBestPath(this.bestState, this.bestRouteKey, this.bestOffsetPosition, this.bestPathId);
55     }
56 }