Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / AddPathBestPath.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.add;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.primitives.UnsignedInteger;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.protocol.bgp.mode.api.BestPathState;
16 import org.opendaylight.protocol.bgp.mode.spi.AbstractBestPath;
17 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
19
20 public final class AddPathBestPath extends AbstractBestPath {
21     private final RouteKey routeKey;
22     private final int offsetPosition;
23     private final long pathId;
24
25     public AddPathBestPath(@Nonnull final BestPathState state, @Nonnull final RouteKey key, final int offsetPosition, final Long pathId) {
26         super(state);
27         this.routeKey = requireNonNull(key);
28         this.offsetPosition = offsetPosition;
29         this.pathId = pathId;
30     }
31
32     public RouteKey getRouteKey() {
33         return this.routeKey;
34     }
35
36     @Override
37     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
38         toStringHelper.add("routeKey", this.routeKey);
39         toStringHelper.add("state", this.state);
40         toStringHelper.add("offsetPosition", this.offsetPosition);
41         toStringHelper.add("pathId", this.pathId);
42         return toStringHelper;
43     }
44
45     @Override
46     public int hashCode() {
47         final int prime = 31;
48         int result = 1;
49         result = prime * result + this.routeKey.hashCode();
50         result = prime * result + this.state.hashCode();
51         return result;
52     }
53
54     @Override
55     public boolean equals(final Object obj) {
56         if (this == obj) {
57             return true;
58         }
59         if (!(obj instanceof AddPathBestPath)) {
60             return false;
61         }
62         final AddPathBestPath other = (AddPathBestPath) obj;
63         if (!this.routeKey.equals(other.routeKey)) {
64             return false;
65         }
66         /*
67         We don't check offset position since it will change as new path is added, and we want to be able to use List comparison between old
68         bestpath list and new best path list.
69         */
70         if (!this.state.equals(other.state)) {
71             return false;
72         }
73
74         return this.pathId == other.pathId;
75     }
76
77     @Override
78     public final UnsignedInteger getRouterId() {
79         return this.routeKey.getRouteId();
80     }
81
82     @Override
83     public final PeerId getPeerId() {
84         return RouterIds.createPeerId(this.routeKey.getRouteId());
85     }
86
87     @Override
88     public long getPathId() {
89         return this.pathId;
90     }
91 }