Bump versions to 0.21.8-SNAPSHOT
[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 org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.protocol.bgp.mode.api.BestPathState;
15 import org.opendaylight.protocol.bgp.mode.spi.AbstractBestPath;
16 import org.opendaylight.protocol.bgp.rib.spi.RouterId;
17 import org.opendaylight.yangtools.yang.common.Uint32;
18
19 public final class AddPathBestPath extends AbstractBestPath {
20     private final @NonNull RouteKey routeKey;
21     private final @NonNull Uint32 pathId;
22     private final int offset;
23
24     AddPathBestPath(final @NonNull BestPathState state, final @NonNull RouteKey key, final @NonNull Uint32 pathId,
25             final int offset) {
26         super(state);
27         this.routeKey = requireNonNull(key);
28         this.pathId = requireNonNull(pathId);
29         this.offset = offset;
30     }
31
32     RouteKey getRouteKey() {
33         return this.routeKey;
34     }
35
36     public int getOffset() {
37         return offset;
38     }
39
40     @Override
41     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
42         return super.addToStringAttributes(toStringHelper.add("routeKey", this.routeKey)
43             .add("pathId", this.pathId)
44             .add("offset", offset));
45     }
46
47     @Override
48     public int hashCode() {
49         final int prime = 31;
50         int result = 1;
51         result = prime * result + this.routeKey.hashCode();
52         result = prime * result + this.state.hashCode();
53         return result;
54     }
55
56     @Override
57     public boolean equals(final Object obj) {
58         if (this == obj) {
59             return true;
60         }
61         if (!(obj instanceof AddPathBestPath)) {
62             return false;
63         }
64         final AddPathBestPath other = (AddPathBestPath) obj;
65         if (!this.routeKey.equals(other.routeKey)) {
66             return false;
67         }
68         /*
69         We don't check offset position since it will change as new path is added, and we want to be able
70         to use List comparison between old bestpath list and new best path list.
71         */
72         if (!this.state.equals(other.state)) {
73             return false;
74         }
75
76         return this.pathId.equals(other.pathId);
77     }
78
79     @Override
80     public RouterId getRouterId() {
81         return this.routeKey.getRouterId();
82     }
83
84     @Override
85     public long getPathId() {
86         // FIXME: this should return int bits...
87         return this.pathId.longValue();
88     }
89
90     /**
91      * Return the boxed value equivalent of {@link #getPathId()}. This class uses boxed instances internally, hence
92      * this method exposes it.
93      *
94      * @return Path Id as a {@link Long}
95      */
96     @NonNull Uint32 getPathIdLong() {
97         return this.pathId;
98     }
99 }