Provide Add Path support for all AFI/SAFI
[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.rev180329.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,
26             final Long pathId) {
27         super(state);
28         this.routeKey = requireNonNull(key);
29         this.offsetPosition = offsetPosition;
30         this.pathId = pathId;
31     }
32
33     public RouteKey getRouteKey() {
34         return this.routeKey;
35     }
36
37     @Override
38     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
39         toStringHelper.add("routeKey", this.routeKey);
40         toStringHelper.add("state", this.state);
41         toStringHelper.add("offsetPosition", this.offsetPosition);
42         toStringHelper.add("pathId", this.pathId);
43         return toStringHelper;
44     }
45
46     @Override
47     public int hashCode() {
48         final int prime = 31;
49         int result = 1;
50         result = prime * result + this.routeKey.hashCode();
51         result = prime * result + this.state.hashCode();
52         return result;
53     }
54
55     @Override
56     public boolean equals(final Object obj) {
57         if (this == obj) {
58             return true;
59         }
60         if (!(obj instanceof AddPathBestPath)) {
61             return false;
62         }
63         final AddPathBestPath other = (AddPathBestPath) obj;
64         if (!this.routeKey.equals(other.routeKey)) {
65             return false;
66         }
67         /*
68         We don't check offset position since it will change as new path is added, and we want to be able
69         to use List comparison between old bestpath list and new best path list.
70         */
71         if (!this.state.equals(other.state)) {
72             return false;
73         }
74
75         return this.pathId == other.pathId;
76     }
77
78     @Override
79     public UnsignedInteger getRouterId() {
80         return this.routeKey.getRouteId();
81     }
82
83     @Override
84     public PeerId getPeerId() {
85         return RouterIds.createPeerId(this.routeKey.getRouteId());
86     }
87
88     @Override
89     public long getPathId() {
90         return this.pathId;
91     }
92 }