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