Change types of CLUSTER_ID and ORIGINATOR
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BestPathSelectionTest.java
1 /*
2  * Copyright (c) 2013 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.assertTrue;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.LocalPrefBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.MultiExitDiscBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.OriginBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.SegmentsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AListBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequenceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASetBuilder;
35
36 import com.google.common.collect.Lists;
37
38 /**
39  * @see <a href="http://www.cisco.com/c/en/us/support/docs/ip/border-gateway-protocol-bgp/13753-25.html">BGP Best Path
40  *      Selection</a>
41  */
42 public class BestPathSelectionTest {
43
44         private final BGPObjectComparator comparator = new BGPObjectComparator(new AsNumber(40L), new byte[] { (byte) 192, (byte) 150, 20, 38 }, new byte[] {
45                 (byte) 192, (byte) 150, 20, 38 });
46
47         private PathAttributes attr1;
48         private PathAttributes attr2;
49         private PathAttributes attr3;
50         private PathAttributes attr4;
51         private PathAttributes attr5;
52         private PathAttributes attr6;
53         private PathAttributes attr7;
54
55         @Before
56         public void setUp() {
57                 final AsPathBuilder asBuilder1 = new AsPathBuilder();
58                 final AsPathBuilder asBuilder2 = new AsPathBuilder();
59                 List<Segments> segs = new ArrayList<>();
60                 final List<AsNumber> ases = Lists.newArrayList(new AsNumber(100L), new AsNumber(30L));
61                 final List<AsSequence> seqs = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(50L)).build());
62                 segs.add(new SegmentsBuilder().setCSegment(new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(seqs).build()).build()).build());
63                 asBuilder1.setSegments(segs);
64                 segs = new ArrayList<>();
65                 segs.add(new SegmentsBuilder().setCSegment(new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(seqs).build()).build()).build());
66                 segs.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(ases).build()).build()).build());
67                 asBuilder2.setSegments(segs);
68
69                 final List<ClusterIdentifier> clusters = new ArrayList<>();
70                 clusters.add(new ClusterIdentifier(new Ipv4Address("0.0.0.0")));
71                 clusters.add(new ClusterIdentifier(new Ipv4Address("0.0.0.0")));
72
73                 final PathAttributesBuilder builder = new PathAttributesBuilder();
74                 builder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
75                 this.attr1 = builder.build();
76                 builder.setLocalPref(new LocalPrefBuilder().setPref(230L).build());
77                 builder.setAsPath(asBuilder2.build());
78                 this.attr2 = builder.build();
79                 builder.setAsPath(asBuilder1.build());
80                 builder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
81                 this.attr3 = builder.build();
82                 builder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
83                 builder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(15L).build());
84                 this.attr4 = builder.build();
85                 builder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(12L).build());
86                 this.attr5 = builder.build();
87                 builder.setAsPath(new AsPathBuilder().setSegments(new ArrayList<Segments>()).build());
88                 builder.setClusterId(new ArrayList<ClusterIdentifier>());
89                 this.attr6 = builder.build();
90                 builder.setClusterId(clusters);
91                 this.attr7 = builder.build();
92         }
93
94         @Test
95         public void testCompare() {
96                 assertTrue(this.comparator.compare(this.attr1, this.attr2) < 0);
97                 assertTrue(this.comparator.compare(this.attr2, this.attr1) > 0);
98
99                 assertTrue(this.comparator.compare(this.attr2, this.attr3) < 0);
100                 assertTrue(this.comparator.compare(this.attr3, this.attr2) > 0);
101
102                 assertTrue(this.comparator.compare(this.attr3, this.attr4) < 0);
103                 assertTrue(this.comparator.compare(this.attr4, this.attr3) > 0);
104
105                 assertTrue(this.comparator.compare(this.attr4, this.attr5) < 0);
106                 assertTrue(this.comparator.compare(this.attr5, this.attr4) > 0);
107
108                 assertTrue(this.comparator.compare(this.attr5, this.attr6) < 0);
109                 assertTrue(this.comparator.compare(this.attr6, this.attr5) > 0);
110
111                 assertTrue(this.comparator.compare(this.attr6, this.attr7) < 0);
112                 assertTrue(this.comparator.compare(this.attr7, this.attr6) > 0);
113         }
114
115         @Test
116         public void testByteCompare() {
117                 assertTrue(BGPObjectComparator.compareByteArrays(new byte[] { (byte) 192, (byte) 150, 20, 38 }, new byte[] { (byte) 192,
118                                 (byte) 168, 25, 1 }) < 0);
119                 assertTrue(BGPObjectComparator.compareByteArrays(new byte[] { (byte) 192, (byte) 168, 25, 1 }, new byte[] { (byte) 192, (byte) 150,
120                                 20, 38 }) > 0);
121         }
122 }