Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / base / BasePathSelector.java
1 /*
2  * Copyright (c) 2015 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.base;
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 final class BasePathSelector extends AbstractBestPathSelector {
21     private static final Logger LOG = LoggerFactory.getLogger(BasePathSelector.class);
22
23     private UnsignedInteger bestRouterId = null;
24
25     BasePathSelector(final Long ourAs) {
26         super(ourAs);
27     }
28
29     void processPath(final UnsignedInteger routerId, final ContainerNode attrs) {
30         requireNonNull(routerId, "Router ID may not be null");
31
32         // Consider only non-null attributes
33         if (attrs != null) {
34             final UnsignedInteger originatorId = replaceOriginator(routerId, attrs);
35             /*
36              * Store the new details if we have nothing stored or when the selection algorithm indicates new details
37              * are better.
38              */
39             final BestPathState state = new BestPathStateImpl(attrs);
40             if (this.bestOriginatorId == null || !isExistingPathBetter(state)) {
41                 LOG.trace("Selecting path from router {}", routerId);
42                 this.bestOriginatorId = originatorId;
43                 this.bestRouterId = routerId;
44                 this.bestState = state;
45             }
46         }
47     }
48
49
50     BaseBestPath result() {
51         return this.bestRouterId == null ? null : new BaseBestPath(this.bestRouterId, this.bestState);
52     }
53 }