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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
13 import com.google.common.primitives.UnsignedInteger;
14 import org.junit.Test;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPath;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.LocalPref;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.MultiExitDisc;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Origin;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeSchemaAwareBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
32 public class BestPathSelectorTest {
34 private final QName DATA_QNAME = QName.create("data");
35 private final UnsignedInteger ROUTER_ID = RouterIds.routerIdForAddress("127.0.0.1");
36 private final UnsignedInteger ROUTER_ID2 = RouterIds.routerIdForPeerId(new PeerId("bgp://127.0.0.1"));
37 private final UnsignedInteger ROUTER_ID3 = RouterIds.routerIdForPeerId(new PeerId("bgp://127.0.0.2"));
38 private final BestPathState STATE = new BestPathState(createStateFromPrefMedOriginASPath());
39 private final BestPath originBestPath = new BestPath(this.ROUTER_ID, this.STATE);
40 private final BestPathSelector selector = new BestPathSelector(20L);
41 private DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> dataContBuilder;
44 public void testBestPathForEquality() {
45 this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOriginASPath());
46 final BestPath processedPath = this.selector.result();
48 assertEquals(this.originBestPath.getRouterId(), processedPath.getRouterId());
49 assertEquals(this.originBestPath.getState().getAsPathLength(), processedPath.getState().getAsPathLength());
50 assertEquals(this.originBestPath.getState().getLocalPref(), processedPath.getState().getLocalPref());
51 assertEquals(this.originBestPath.getState().getMultiExitDisc(), processedPath.getState().getMultiExitDisc());
52 assertEquals(this.originBestPath.getState().getOrigin(), processedPath.getState().getOrigin());
53 assertEquals(this.originBestPath.getState().getPeerAs(), processedPath.getState().getPeerAs());
57 public void testBestPathWithHigherLocalPref() {
58 this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOrigin()); // local-pref 123
59 BestPath processedPath = this.selector.result();
60 assertEquals(123L, processedPath.getState().getLocalPref().longValue());
62 this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOriginASPath()); // local-pref 321
63 processedPath = this.selector.result();
64 assertEquals(321L, processedPath.getState().getLocalPref().longValue());
66 this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOrigin()); // local-pref 123
67 processedPath = this.selector.result();
68 assertEquals(321L, processedPath.getState().getLocalPref().longValue());
72 public void testBestPathForNonEquality() {
73 this.selector.processPath(this.ROUTER_ID3, createStateFromPrefMedOrigin());
74 final BestPath processedPath = this.selector.result();
76 assertNotEquals(this.originBestPath.getRouterId(), processedPath.getRouterId());
77 assertEquals(this.originBestPath.getState().getAsPathLength(), processedPath.getState().getAsPathLength());
78 assertNotEquals(this.originBestPath.getState().getLocalPref(), processedPath.getState().getLocalPref());
79 assertNotEquals(this.originBestPath.getState().getMultiExitDisc(), processedPath.getState().getMultiExitDisc());
80 assertNotEquals(this.originBestPath.getState().getOrigin(), processedPath.getState().getOrigin());
81 assertEquals(0L, (long)processedPath.getState().getPeerAs());
84 private ContainerNode createStateFromPrefMedOrigin() {
85 this.dataContBuilder = createContBuilder(this.DATA_QNAME);
87 this.dataContBuilder.addChild(createContBuilder(LocalPref.QNAME).addChild(createValueBuilder(123L, LocalPref.QNAME, "pref").build()).build());
89 this.dataContBuilder.addChild(createContBuilder(MultiExitDisc.QNAME).addChild(createValueBuilder(1234L, MultiExitDisc.QNAME, "med").build()).build());
91 this.dataContBuilder.addChild(createContBuilder(Origin.QNAME).addChild(createValueBuilder("igp", Origin.QNAME, "value").build()).build());
92 return this.dataContBuilder.build();
95 private ContainerNode createStateFromPrefMedOriginASPath() {
96 this.dataContBuilder = createContBuilder(this.DATA_QNAME);
98 this.dataContBuilder.addChild(createContBuilder(LocalPref.QNAME).addChild(createValueBuilder(321L, LocalPref.QNAME, "pref").build()).build());
100 this.dataContBuilder.addChild(createContBuilder(MultiExitDisc.QNAME).addChild(createValueBuilder(4321L, MultiExitDisc.QNAME, "med").build()).build());
102 this.dataContBuilder.addChild(createContBuilder(Origin.QNAME).addChild(createValueBuilder("egp", Origin.QNAME, "value").build()).build());
104 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> asPathContBuilder = ImmutableContainerNodeSchemaAwareBuilder.create();
105 asPathContBuilder.withNodeIdentifier(new NodeIdentifier(AsPath.QNAME));
107 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> segments = ImmutableUnkeyedListNodeBuilder.create();
108 segments.withNodeIdentifier(new NodeIdentifier(AsPath.QNAME));
109 final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> segmentBuilder = ImmutableUnkeyedListEntryNodeBuilder.create();
110 segmentBuilder.withNodeIdentifier(new NodeIdentifier(AsPath.QNAME));
111 final ImmutableLeafNodeBuilder<Long> segmentLeaf = new ImmutableLeafNodeBuilder<>();
112 segmentLeaf.withNodeIdentifier(new NodeIdentifier(QName.create(AsPath.QNAME, "segments"))).withValue(123454L);
113 segmentBuilder.addChild(segmentLeaf.build());
114 segments.addChild(segmentBuilder.build());
116 asPathContBuilder.addChild(segments.build());
117 this.dataContBuilder.addChild(asPathContBuilder.build());
118 return this.dataContBuilder.build();
121 private DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> createContBuilder(final QName qname) {
122 return ImmutableContainerNodeSchemaAwareBuilder.create().withNodeIdentifier(new NodeIdentifier(qname));
125 private <T> ImmutableLeafNodeBuilder<T> createValueBuilder(final T value, final QName qname, final String localName) {
126 final ImmutableLeafNodeBuilder<T> valueBuilder = new ImmutableLeafNodeBuilder<>();
127 valueBuilder.withNodeIdentifier(new NodeIdentifier(QName.create(qname, localName))).withValue(value);