BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BestPathSelectorTest.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.rib.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12
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;
31
32 public class BestPathSelectorTest {
33
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;
42
43     @Test
44     public void testBestPathForEquality() {
45         this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOriginASPath());
46         final BestPath processedPath = this.selector.result();
47
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());
54     }
55
56     @Test
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());
61
62         this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOriginASPath());   // local-pref 321
63         processedPath = this.selector.result();
64         assertEquals(321L, processedPath.getState().getLocalPref().longValue());
65
66         this.selector.processPath(this.ROUTER_ID2, createStateFromPrefMedOrigin());   // local-pref 123
67         processedPath = this.selector.result();
68         assertEquals(321L, processedPath.getState().getLocalPref().longValue());
69     }
70
71     @Test
72     public void testBestPathForNonEquality() {
73         this.selector.processPath(this.ROUTER_ID3, createStateFromPrefMedOrigin());
74         final BestPath processedPath = this.selector.result();
75
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());
82     }
83
84     private ContainerNode createStateFromPrefMedOrigin() {
85         this.dataContBuilder = createContBuilder(this.DATA_QNAME);
86         // local pref
87         this.dataContBuilder.addChild(createContBuilder(LocalPref.QNAME).addChild(createValueBuilder(123L, LocalPref.QNAME, "pref").build()).build());
88         // multi exit disc
89         this.dataContBuilder.addChild(createContBuilder(MultiExitDisc.QNAME).addChild(createValueBuilder(1234L, MultiExitDisc.QNAME, "med").build()).build());
90         // origin
91         this.dataContBuilder.addChild(createContBuilder(Origin.QNAME).addChild(createValueBuilder("igp", Origin.QNAME, "value").build()).build());
92         return this.dataContBuilder.build();
93     }
94
95     private ContainerNode createStateFromPrefMedOriginASPath() {
96         this.dataContBuilder = createContBuilder(this.DATA_QNAME);
97         // local pref
98         this.dataContBuilder.addChild(createContBuilder(LocalPref.QNAME).addChild(createValueBuilder(321L, LocalPref.QNAME, "pref").build()).build());
99         // multi exit disc
100         this.dataContBuilder.addChild(createContBuilder(MultiExitDisc.QNAME).addChild(createValueBuilder(4321L, MultiExitDisc.QNAME, "med").build()).build());
101         // origin
102         this.dataContBuilder.addChild(createContBuilder(Origin.QNAME).addChild(createValueBuilder("egp", Origin.QNAME, "value").build()).build());
103         // as path
104         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> asPathContBuilder = ImmutableContainerNodeSchemaAwareBuilder.create();
105         asPathContBuilder.withNodeIdentifier(new NodeIdentifier(AsPath.QNAME));
106
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());
115
116         asPathContBuilder.addChild(segments.build());
117         this.dataContBuilder.addChild(asPathContBuilder.build());
118         return this.dataContBuilder.build();
119     }
120
121     private DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> createContBuilder(final QName qname) {
122         return ImmutableContainerNodeSchemaAwareBuilder.create().withNodeIdentifier(new NodeIdentifier(qname));
123     }
124
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);
128         return valueBuilder;
129     }
130 }