8a9421c91929f4e5e02a302ae2f2bb32d3cb37bd
[bgpcep.git] / bgp / path-selection-mode / src / test / java / org / opendaylight / protocol / bgp / mode / impl / base / BasePathSelectorTest.java
1 /*
2  * Copyright (c) 2016 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.mode.impl.base;
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 java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.mode.impl.BestPathStateImpl;
19 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.LocalPrefBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.MultiExitDiscBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.OriginBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
30
31 public class BasePathSelectorTest {
32     private static final List<AsNumber> SEQ_SEGMENT
33             = Arrays.asList(new AsNumber(1L), new AsNumber(2L), new AsNumber(3L));
34     static final UnsignedInteger ROUTER_ID2 = RouterIds.routerIdForPeerId(new PeerId("bgp://127.0.0.1"));
35     private static final List<AsNumber> SEQ_SEGMENT2
36             = Arrays.asList(new AsNumber(20L), new AsNumber(2L), new AsNumber(3L));
37     private static final UnsignedInteger ROUTER_ID = RouterIds.routerIdForAddress("127.0.0.1");
38     private static final UnsignedInteger ROUTER_ID3 = RouterIds.routerIdForPeerId(new PeerId("bgp://127.0.0.2"));
39     private final BasePathSelector selector = new BasePathSelector(20L);
40     private final BestPathStateImpl state = new BestPathStateImpl(createStateFromPrefMedOriginASPath().build());
41     private final BaseBestPath originBestPath = new BaseBestPath(ROUTER_ID, this.state);
42
43     private static Attributes createStateFromPrefMedOrigin() {
44         AttributesBuilder dataContBuilder = new AttributesBuilder();
45         addLowerLocalRef(dataContBuilder);
46         addLowerMultiExitDisc(dataContBuilder);
47         addIgpOrigin(dataContBuilder);
48         return dataContBuilder.build();
49     }
50
51     protected static AttributesBuilder createStateFromPrefMedOriginASPath() {
52         AttributesBuilder dataContBuilder = new AttributesBuilder();
53         addHigherLocalRef(dataContBuilder);
54         addHigherMultiExitDisc(dataContBuilder);
55         addEgpOrigin(dataContBuilder);
56         addAsPath(dataContBuilder, SEQ_SEGMENT);
57         return dataContBuilder;
58     }
59
60     private static void addLowerLocalRef(final AttributesBuilder dataContBuilder) {
61         dataContBuilder.setLocalPref(new LocalPrefBuilder().setPref(123L).build());
62     }
63
64     private static void addHigherLocalRef(final AttributesBuilder dataContBuilder) {
65         dataContBuilder.setLocalPref(new LocalPrefBuilder().setPref(321L).build());
66     }
67
68     private static void addLowerMultiExitDisc(final AttributesBuilder dataContBuilder) {
69         dataContBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(1234L).build());
70     }
71
72     private static void addHigherMultiExitDisc(final AttributesBuilder dataContBuilder) {
73         dataContBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(4321L).build());
74     }
75
76     private static void addIgpOrigin(final AttributesBuilder dataContBuilder) {
77         dataContBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
78     }
79
80     private static void addEgpOrigin(final AttributesBuilder dataContBuilder) {
81         dataContBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
82     }
83
84     private static void addAsPath(final AttributesBuilder dataContBuilder, final List<AsNumber> segment) {
85         dataContBuilder.setAsPath(new AsPathBuilder().setSegments(
86                 Collections.singletonList(new SegmentsBuilder().setAsSequence(segment).build())).build());
87     }
88
89     @Test
90     public void testBestPathWithHigherLocalPref() {
91         this.selector.processPath(ROUTER_ID2, createStateFromPrefMedOrigin());   // local-pref 123
92         BaseBestPath processedPath = this.selector.result();
93         assertEquals(123L, processedPath.getState().getLocalPref().longValue());
94
95         this.selector.processPath(ROUTER_ID2, createStateFromPrefMedOriginASPath().build());   // local-pref 321
96         processedPath = this.selector.result();
97         assertEquals(321L, processedPath.getState().getLocalPref().longValue());
98
99         AttributesBuilder dataContBuilder = new AttributesBuilder();
100         addLowerLocalRef(dataContBuilder); // prefer path with higher LOCAL_PREF
101         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
102         processedPath = this.selector.result();
103         assertEquals(321L, processedPath.getState().getLocalPref().longValue());
104     }
105
106     @Test
107     public void testBestPathForEquality() {
108         this.selector.processPath(ROUTER_ID2, createStateFromPrefMedOriginASPath().build());
109         final BaseBestPath processedPath = this.selector.result();
110
111         assertEquals(this.originBestPath.getPeerId(), processedPath.getPeerId());
112         assertEquals(this.originBestPath.getState().getLocalPref(), processedPath.getState().getLocalPref());
113         assertEquals(this.originBestPath.getState().getMultiExitDisc(), processedPath.getState().getMultiExitDisc());
114         assertEquals(this.originBestPath.getState().getOrigin(), processedPath.getState().getOrigin());
115         assertEquals(this.originBestPath.getState().getPeerAs(), processedPath.getState().getPeerAs());
116         assertEquals(this.originBestPath.getState().getAsPathLength(), processedPath.getState().getAsPathLength());
117     }
118
119     @Test
120     public void testBestPathSelectionOptions() {
121         AttributesBuilder dataContBuilder = createStateFromPrefMedOriginASPath();
122         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
123         BaseBestPath processedPath = this.selector.result();
124         assertEquals(1, processedPath.getState().getOrigin().getIntValue());
125
126         addIgpOrigin(dataContBuilder); // prefer the path with the lowest origin type
127         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
128         processedPath = this.selector.result();
129         assertEquals(0, processedPath.getState().getOrigin().getIntValue());
130
131         addEgpOrigin(dataContBuilder);
132         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
133         processedPath = this.selector.result();
134         assertEquals(0, processedPath.getState().getOrigin().getIntValue());
135
136         // prefer the path with the lowest multi-exit discriminator (MED)
137         assertEquals(4321L, (long) processedPath.getState().getMultiExitDisc());
138         addIgpOrigin(dataContBuilder);
139         addLowerMultiExitDisc(dataContBuilder);
140         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
141         processedPath = this.selector.result();
142         assertEquals(1234L, (long) processedPath.getState().getMultiExitDisc());
143
144         addHigherMultiExitDisc(dataContBuilder);
145         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
146         processedPath = this.selector.result();
147         assertEquals(1234L, (long) processedPath.getState().getMultiExitDisc());
148
149         addLowerMultiExitDisc(dataContBuilder);
150         addAsPath(dataContBuilder, SEQ_SEGMENT2);
151         assertEquals(1L, processedPath.getState().getPeerAs());
152         assertEquals(3, processedPath.getState().getAsPathLength());
153         this.selector.processPath(ROUTER_ID2, dataContBuilder.build());
154         processedPath = this.selector.result();
155         assertEquals(1L, processedPath.getState().getPeerAs());
156         assertEquals(3, processedPath.getState().getAsPathLength());
157     }
158
159     @Test
160     public void testBestPathForNonEquality() {
161         this.selector.processPath(ROUTER_ID3, createStateFromPrefMedOrigin());
162         final BaseBestPath processedPath = this.selector.result();
163
164         assertNotEquals(this.originBestPath.getPeerId(), processedPath.getPeerId());
165         assertNotEquals(this.originBestPath.getState().getLocalPref(), processedPath.getState().getLocalPref());
166         assertNotEquals(this.originBestPath.getState().getMultiExitDisc(), processedPath.getState().getMultiExitDisc());
167         assertNotEquals(this.originBestPath.getState().getOrigin(), processedPath.getState().getOrigin());
168         assertNotEquals(this.originBestPath.getState().getPeerAs(), processedPath.getState().getPeerAs());
169         assertNotEquals(this.originBestPath.getState().getAsPathLength(), processedPath.getState().getAsPathLength());
170     }
171
172     @Test
173     public void testBgpOrigin() {
174         this.selector.processPath(ROUTER_ID3, new AttributesBuilder().setOrigin(new OriginBuilder()
175                 .setValue(BgpOrigin.Incomplete).build()).build());
176         final BaseBestPath processedPathIncom = this.selector.result();
177         assertEquals(BgpOrigin.Incomplete, processedPathIncom.getState().getOrigin());
178     }
179 }