2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.mode.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.mode.BesthPathStateUtil;
16 import org.opendaylight.protocol.bgp.mode.api.BestPathState;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
21 public final class BestPathStateImpl implements BestPathState {
22 private final Attributes attributes;
23 private long peerAs = 0L;
24 private int asPathLength = 0;
25 private Long localPref;
26 private Long multiExitDisc;
27 private BgpOrigin origin;
28 private boolean resolved;
30 public BestPathStateImpl(final Attributes attributes) {
31 this.attributes = requireNonNull(attributes);
35 private static int countAsPath(final List<Segments> segments) {
36 // an AS_SET counts as 1, no matter how many ASs are in the set.
38 boolean setPresent = false;
39 for (final Segments s : segments) {
40 if (s.getAsSet() != null && !setPresent) {
43 } else if (s.getAsSequence() != null) {
44 count += s.getAsSequence().size();
50 private void resolveValues() {
55 if (this.attributes.getLocalPref() != null) {
56 this.localPref = this.attributes.getLocalPref().getPref();
58 this.localPref = null;
61 if (this.attributes.getMultiExitDisc() != null) {
62 this.multiExitDisc = this.attributes.getMultiExitDisc().getMed();
64 this.multiExitDisc = null;
67 if (this.attributes.getOrigin() != null) {
68 this.origin = this.attributes.getOrigin().getValue();
72 if (this.attributes.getAsPath() != null) {
73 final List<Segments> segs = this.attributes.getAsPath().getSegments();
74 if (segs != null && !segs.isEmpty()) {
75 this.peerAs = BesthPathStateUtil.getPeerAs(segs);
76 this.asPathLength = countAsPath(segs);
83 public Long getLocalPref() {
85 return this.localPref;
89 public Long getMultiExitDisc() {
91 return this.multiExitDisc;
95 public BgpOrigin getOrigin() {
101 public long getPeerAs() {
107 public int getAsPathLength() {
109 return this.asPathLength;
113 public Attributes getAttributes() {
114 return this.attributes;
117 private ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
118 toStringHelper.add("attributes", this.attributes);
119 toStringHelper.add("localPref", this.localPref);
120 toStringHelper.add("multiExitDisc", this.multiExitDisc);
121 toStringHelper.add("origin", this.origin);
122 toStringHelper.add("resolved", this.resolved);
123 return toStringHelper;
127 public String toString() {
128 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
132 public int hashCode() {
133 final int prime = 31;
135 result = prime * result + this.attributes.hashCode();
136 result = prime * result + (this.localPref == null ? 0 : this.localPref.hashCode());
137 result = prime * result + (this.multiExitDisc == null ? 0 : this.multiExitDisc.hashCode());
138 result = prime * result + (this.origin == null ? 0 : this.origin.hashCode());
143 public boolean equals(final Object obj) {
147 if (!(obj instanceof BestPathStateImpl)) {
150 final BestPathStateImpl other = (BestPathStateImpl) obj;
151 if (!this.attributes.equals(other.attributes)) {
154 if (this.localPref == null) {
155 if (other.localPref != null) {
158 } else if (!this.localPref.equals(other.localPref)) {
161 if (this.multiExitDisc == null) {
162 if (other.multiExitDisc != null) {
165 } else if (!this.multiExitDisc.equals(other.multiExitDisc)) {
168 if (this.origin != other.origin) {