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.rib.impl;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.cache.Cache;
16 import com.google.common.cache.CacheBuilder;
17 import com.google.common.collect.ImmutableList;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import javax.annotation.concurrent.NotThreadSafe;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPath;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.LocalPref;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.MultiExitDisc;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Origin;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 final class BestPathState {
49 private static final class NamespaceSpecificIds {
50 private final Collection<PathArgument> asPath;
51 private final Collection<PathArgument> locPref;
52 private final Collection<PathArgument> med;
53 private final Collection<PathArgument> orig;
54 private final NodeIdentifier asSetNid;
55 private final NodeIdentifier asSeqNid;
57 NamespaceSpecificIds(final QName namespace) {
58 NodeIdentifier container = new NodeIdentifier(QName.cachedReference(QName.create(namespace, AsPath.QNAME.getLocalName())));
59 NodeIdentifier leaf = new NodeIdentifier(QName.cachedReference(QName.create(namespace, "segments")));
60 this.asPath = ImmutableList.<PathArgument>of(container, leaf);
62 container = new NodeIdentifier(QName.cachedReference(QName.create(namespace, LocalPref.QNAME.getLocalName())));
63 leaf = new NodeIdentifier(QName.cachedReference(QName.create(namespace, "pref")));
64 this.locPref = ImmutableList.<PathArgument>of(container, leaf);
66 container = new NodeIdentifier(QName.cachedReference(QName.create(namespace, MultiExitDisc.QNAME.getLocalName())));
67 leaf = new NodeIdentifier(QName.cachedReference(QName.create(namespace, "med")));
68 this.med = ImmutableList.<PathArgument>of(container, leaf);
70 container = new NodeIdentifier(QName.cachedReference(QName.create(namespace, Origin.QNAME.getLocalName())));
71 leaf = new NodeIdentifier(QName.cachedReference(QName.create(namespace, "value")));
72 this.orig = ImmutableList.<PathArgument>of(container, leaf);
74 this.asSetNid = new NodeIdentifier(QName.cachedReference(QName.create(namespace, "as-set")));
75 this.asSeqNid = new NodeIdentifier(QName.cachedReference(QName.create(namespace, "as-sequence")));
78 Collection<PathArgument> getAsPath() {
82 Collection<PathArgument> getLocPref() {
86 Collection<PathArgument> getMed() {
90 Collection<PathArgument> getOrig() {
94 NodeIdentifier getAsSet() {
98 NodeIdentifier getAsSeq() {
103 private static final Logger LOG = LoggerFactory.getLogger(BestPathState.class);
104 private static final Cache<QNameModule, NamespaceSpecificIds> PATH_CACHE = CacheBuilder.newBuilder().weakKeys().weakValues().build();
106 private long peerAs = 0L;
107 private int asPathLength = 0;
109 private final ContainerNode attributes;
110 private final NamespaceSpecificIds ids;
111 private Long localPref;
112 private Long multiExitDisc;
113 private BgpOrigin origin;
114 private boolean resolved;
116 BestPathState(final ContainerNode attributes) {
117 final NamespaceSpecificIds col;
119 col = PATH_CACHE.get(attributes.getNodeType().getModule(), new Callable<NamespaceSpecificIds>() {
121 public NamespaceSpecificIds call() {
122 return new NamespaceSpecificIds(attributes.getNodeType());
125 } catch (final ExecutionException e) {
126 LOG.error("Error creating namespace-specific attributes collection.", e);
127 throw new IllegalStateException("Error creating namespace-specific attributes collection.", e);
130 this.attributes = Preconditions.checkNotNull(attributes);
134 private static BgpOrigin fromString(final String originStr) {
137 return BgpOrigin.Igp;
139 return BgpOrigin.Egp;
141 return BgpOrigin.Incomplete;
143 throw new IllegalArgumentException("Unhandled origin value " + originStr);
147 private void resolveValues() {
152 final Optional<NormalizedNode<?, ?>> maybeLocalPref = NormalizedNodes.findNode(this.attributes, this.ids.getLocPref());
153 if (maybeLocalPref.isPresent()) {
154 this.localPref = (Long) ((LeafNode<?>)maybeLocalPref.get()).getValue();
156 this.localPref = null;
159 final Optional<NormalizedNode<?, ?>> maybeMultiExitDisc = NormalizedNodes.findNode(this.attributes, this.ids.getMed());
160 if (maybeMultiExitDisc.isPresent()) {
161 this.multiExitDisc = (Long) ((LeafNode<?>)maybeMultiExitDisc.get()).getValue();
163 this.multiExitDisc = null;
166 final Optional<NormalizedNode<?, ?>> maybeOrigin = NormalizedNodes.findNode(this.attributes, this.ids.getOrig());
167 if (maybeOrigin.isPresent()) {
168 this.origin = fromString((String) ((LeafNode<?>)maybeOrigin.get()).getValue());
173 final Optional<NormalizedNode<?, ?>> maybeSegments = NormalizedNodes.findNode(this.attributes, this.ids.getAsPath());
174 if (maybeSegments.isPresent()) {
175 final UnkeyedListNode segments = (UnkeyedListNode) maybeSegments.get();
176 final List<Segments> segs = extractSegments(segments);
177 if (segs.size() != 0) {
178 this.peerAs = getPeerAs(segs).getValue();
179 this.asPathLength = countAsPath(segs);
182 this.resolved = true;
185 Long getLocalPref() {
187 return this.localPref;
190 Long getMultiExitDisc() {
192 return this.multiExitDisc;
195 BgpOrigin getOrigin() {
205 int getAsPathLength() {
207 return this.asPathLength;
210 private static int countAsPath(final List<Segments> segments) {
211 // an AS_SET counts as 1, no matter how many ASs are in the set.
213 boolean setPresent = false;
214 for (final Segments s : segments) {
215 if (s.getAsSet() != null && !setPresent) {
218 } else if (s.getAsSequence() != null) {
219 count += s.getAsSequence().size();
225 private static AsNumber getPeerAs(final List<Segments> segments) {
226 if (segments.isEmpty()) {
227 return new AsNumber(0L);
229 for (final Segments seg : segments) {
230 if (seg.getAsSequence() != null && !seg.getAsSequence().isEmpty()) {
231 return segments.get(0).getAsSequence().get(0);
234 return new AsNumber(0L);
238 public List<Segments> extractSegments(final UnkeyedListNode segments) {
240 final List<Segments> extracted = new ArrayList<>();
241 for (final UnkeyedListEntryNode segment : segments.getValue()) {
242 final SegmentsBuilder sb = new SegmentsBuilder();
243 // We are expecting that segment contains either as-sequence or as-set, so just one of them will be set, other would be null
244 sb.setAsSequence(extractAsList(segment, this.ids.getAsSeq())).setAsSet(extractAsList(segment, this.ids.getAsSet()));
245 extracted.add(sb.build());
250 private List<AsNumber> extractAsList(final UnkeyedListEntryNode segment, final NodeIdentifier nid) {
251 final List<AsNumber> ases = new ArrayList<>();
252 final Optional<NormalizedNode<?, ?>> maybeAsList = NormalizedNodes.findNode(segment, nid);
253 if (maybeAsList.isPresent()) {
254 final LeafSetNode<?> list = (LeafSetNode<?>)maybeAsList.get();
255 for (final LeafSetEntryNode<?> as : list.getValue()) {
256 ases.add(new AsNumber((Long)as.getValue()));
263 ContainerNode getAttributes() {
264 return this.attributes;
267 private ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
268 toStringHelper.add("attributes", this.attributes);
269 toStringHelper.add("localPref", this.localPref);
270 toStringHelper.add("multiExitDisc", this.multiExitDisc);
271 toStringHelper.add("origin", this.origin);
272 toStringHelper.add("resolved", this.resolved);
273 return toStringHelper;
277 public String toString() {
278 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
282 public int hashCode() {
283 final int prime = 31;
285 result = prime * result + this.attributes.hashCode();
286 result = prime * result + ((this.localPref == null) ? 0 : this.localPref.hashCode());
287 result = prime * result + ((this.multiExitDisc == null) ? 0 : this.multiExitDisc.hashCode());
288 result = prime * result + ((this.origin == null) ? 0 : this.origin.hashCode());
293 public boolean equals(final Object obj) {
297 if (!(obj instanceof BestPathState)) {
300 final BestPathState other = (BestPathState) obj;
301 if (!this.attributes.equals(other.attributes)) {
304 if (this.localPref == null) {
305 if (other.localPref != null) {
308 } else if (!this.localPref.equals(other.localPref)) {
311 if (this.multiExitDisc == null) {
312 if (other.multiExitDisc != null) {
315 } else if (!this.multiExitDisc.equals(other.multiExitDisc)) {
318 if (this.origin != other.origin) {