xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-018-next-hop-and-path-cost.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2023, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify
30*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify_within
31*cfb92d14SAndroid Build Coastguard Workerimport cli
32*cfb92d14SAndroid Build Coastguard Workerimport time
33*cfb92d14SAndroid Build Coastguard Worker
34*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
35*cfb92d14SAndroid Build Coastguard Worker# Test description: Next hop and path cost calculation.
36*cfb92d14SAndroid Build Coastguard Worker#
37*cfb92d14SAndroid Build Coastguard Worker# Network topology
38*cfb92d14SAndroid Build Coastguard Worker#
39*cfb92d14SAndroid Build Coastguard Worker#     r1 ---- r2...2...r3
40*cfb92d14SAndroid Build Coastguard Worker#    / |       \      /  \
41*cfb92d14SAndroid Build Coastguard Worker#   /  |        \    /    \
42*cfb92d14SAndroid Build Coastguard Worker# fed1 fed2       r4...1...r5 ---- fed3
43*cfb92d14SAndroid Build Coastguard Worker#
44*cfb92d14SAndroid Build Coastguard Worker# Link r2 --> r3 is configured to be at link quality of 2.
45*cfb92d14SAndroid Build Coastguard Worker# Link r5 --> r4 is configured to be at link quality of 1.
46*cfb92d14SAndroid Build Coastguard Worker# Link r1 --> fed2 is configured to be at link quality 1.
47*cfb92d14SAndroid Build Coastguard Worker# Other links are at link quality 3 (best possible link quality).
48*cfb92d14SAndroid Build Coastguard Worker#
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
51*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
52*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
55*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Node` instances
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Workerspeedup = 40
58*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup)
59*cfb92d14SAndroid Build Coastguard Worker
60*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node()
61*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node()
62*cfb92d14SAndroid Build Coastguard Workerr3 = cli.Node()
63*cfb92d14SAndroid Build Coastguard Workerr4 = cli.Node()
64*cfb92d14SAndroid Build Coastguard Workerr5 = cli.Node()
65*cfb92d14SAndroid Build Coastguard Workerfed1 = cli.Node()
66*cfb92d14SAndroid Build Coastguard Workerfed2 = cli.Node()
67*cfb92d14SAndroid Build Coastguard Workerfed3 = cli.Node()
68*cfb92d14SAndroid Build Coastguard Worker
69*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
70*cfb92d14SAndroid Build Coastguard Worker# Form topology
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
73*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed1)
74*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed2)
75*cfb92d14SAndroid Build Coastguard Worker
76*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
77*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r3)
78*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r4)
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2)
81*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r4)
82*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r5)
83*cfb92d14SAndroid Build Coastguard Workerr3.set_macfilter_lqi_to_node(r2, 2)
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r2)
86*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r3)
87*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r5)
88*cfb92d14SAndroid Build Coastguard Workerr4.set_macfilter_lqi_to_node(r5, 1)
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Workerr5.allowlist_node(r4)
91*cfb92d14SAndroid Build Coastguard Workerr5.allowlist_node(r3)
92*cfb92d14SAndroid Build Coastguard Workerr5.allowlist_node(fed3)
93*cfb92d14SAndroid Build Coastguard Worker
94*cfb92d14SAndroid Build Coastguard Workerfed1.allowlist_node(r1)
95*cfb92d14SAndroid Build Coastguard Workerfed2.allowlist_node(r1)
96*cfb92d14SAndroid Build Coastguard Workerfed3.allowlist_node(r5)
97*cfb92d14SAndroid Build Coastguard Workerfed2.set_macfilter_lqi_to_node(r1, 1)
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Workerr1.form('hop-cost')
100*cfb92d14SAndroid Build Coastguard Workerr2.join(r1)
101*cfb92d14SAndroid Build Coastguard Workerr3.join(r1)
102*cfb92d14SAndroid Build Coastguard Workerr4.join(r1)
103*cfb92d14SAndroid Build Coastguard Workerr5.join(r1)
104*cfb92d14SAndroid Build Coastguard Workerfed1.join(r1, cli.JOIN_TYPE_REED)
105*cfb92d14SAndroid Build Coastguard Workerfed2.join(r1, cli.JOIN_TYPE_REED)
106*cfb92d14SAndroid Build Coastguard Workerfed3.join(r1, cli.JOIN_TYPE_REED)
107*cfb92d14SAndroid Build Coastguard Worker
108*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
109*cfb92d14SAndroid Build Coastguard Worker# Test Implementation
110*cfb92d14SAndroid Build Coastguard Worker
111*cfb92d14SAndroid Build Coastguard Workerr1_rloc = int(r1.get_rloc16(), 16)
112*cfb92d14SAndroid Build Coastguard Workerr2_rloc = int(r2.get_rloc16(), 16)
113*cfb92d14SAndroid Build Coastguard Workerr3_rloc = int(r3.get_rloc16(), 16)
114*cfb92d14SAndroid Build Coastguard Workerr4_rloc = int(r4.get_rloc16(), 16)
115*cfb92d14SAndroid Build Coastguard Workerr5_rloc = int(r5.get_rloc16(), 16)
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Workerfed1_rloc = int(fed1.get_rloc16(), 16)
118*cfb92d14SAndroid Build Coastguard Workerfed2_rloc = int(fed2.get_rloc16(), 16)
119*cfb92d14SAndroid Build Coastguard Workerfed3_rloc = int(fed3.get_rloc16(), 16)
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Workerdef parse_nexthop(line):
123*cfb92d14SAndroid Build Coastguard Worker    # Example: "0x5000 cost:3" -> (0x5000, 3).
124*cfb92d14SAndroid Build Coastguard Worker    items = line.strip().split(' ', 2)
125*cfb92d14SAndroid Build Coastguard Worker    return (int(items[0], 16), int(items[1].split(':')[1]))
126*cfb92d14SAndroid Build Coastguard Worker
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Workerdef check_nexthops_and_costs():
129*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130*cfb92d14SAndroid Build Coastguard Worker    # `r1` next hops and costs
131*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r1_rloc)) == (r1_rloc, 0))
132*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r2_rloc)) == (r2_rloc, 1))
133*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r3_rloc)) == (r2_rloc, 3))
134*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r4_rloc)) == (r2_rloc, 2))
135*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r5_rloc)) == (r2_rloc, 4))
136*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(fed3_rloc)) == (r2_rloc, 5))
137*cfb92d14SAndroid Build Coastguard Worker    # On `r1` its children can be reached directly.
138*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(fed1_rloc)) == (fed1_rloc, 1))
139*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(fed2_rloc)) == (fed2_rloc, 1))
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142*cfb92d14SAndroid Build Coastguard Worker    # `r2` next hops and costs
143*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r1_rloc)) == (r1_rloc, 1))
144*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r2_rloc)) == (r2_rloc, 0))
145*cfb92d14SAndroid Build Coastguard Worker    # On `r2` the direct link to `r3` and the path through `r4` both
146*cfb92d14SAndroid Build Coastguard Worker    # have the same cost, but the direct link should be preferred.
147*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r3_rloc)) == (r3_rloc, 2))
148*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r4_rloc)) == (r4_rloc, 1))
149*cfb92d14SAndroid Build Coastguard Worker    # On 'r2' the path to `r5` can go through `r3` or `r4`
150*cfb92d14SAndroid Build Coastguard Worker    # as both have the same cost.
151*cfb92d14SAndroid Build Coastguard Worker    (nexthop, cost) = parse_nexthop(r2.get_nexthop(r5_rloc))
152*cfb92d14SAndroid Build Coastguard Worker    verify(cost == 3)
153*cfb92d14SAndroid Build Coastguard Worker    verify(nexthop in [r3_rloc, r4_rloc])
154*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(fed1_rloc)) == (r1_rloc, 2))
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
157*cfb92d14SAndroid Build Coastguard Worker    # `r3` next hops and costs
158*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r3_rloc)) == (r3_rloc, 0))
159*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r5_rloc)) == (r5_rloc, 1))
160*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r4_rloc)) == (r4_rloc, 1))
161*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r2_rloc)) == (r2_rloc, 2))
162*cfb92d14SAndroid Build Coastguard Worker    # On `r3` the path to `r1` can go through `r2` or `r4`
163*cfb92d14SAndroid Build Coastguard Worker    # as both have the same cost.
164*cfb92d14SAndroid Build Coastguard Worker    (nexthop, cost) = parse_nexthop(r3.get_nexthop(r1_rloc))
165*cfb92d14SAndroid Build Coastguard Worker    verify(cost == 3)
166*cfb92d14SAndroid Build Coastguard Worker    verify(nexthop in [r2_rloc, r4_rloc])
167*cfb92d14SAndroid Build Coastguard Worker    # On `r3` the path to fed1 should use the same next hop as `r1`
168*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(fed2_rloc)) == (nexthop, 4))
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171*cfb92d14SAndroid Build Coastguard Worker    # `r4` next hops and costs
172*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(fed1_rloc)) == (r2_rloc, 3))
173*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(r1_rloc)) == (r2_rloc, 2))
174*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(r2_rloc)) == (r2_rloc, 1))
175*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(r3_rloc)) == (r3_rloc, 1))
176*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(r4_rloc)) == (r4_rloc, 0))
177*cfb92d14SAndroid Build Coastguard Worker    # On `r4` even though we have a direct link to `r5`
178*cfb92d14SAndroid Build Coastguard Worker    # the path cost through `r3` has a smaller cost over
179*cfb92d14SAndroid Build Coastguard Worker    # the direct link cost.
180*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(r5_rloc)) == (r3_rloc, 2))
181*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r4.get_nexthop(fed3_rloc)) == (r3_rloc, 3))
182*cfb92d14SAndroid Build Coastguard Worker
183*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
184*cfb92d14SAndroid Build Coastguard Worker    # `r5` next hops and costs
185*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(fed3_rloc)) == (fed3_rloc, 1))
186*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r5_rloc)) == (r5_rloc, 0))
187*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r3_rloc)) == (r3_rloc, 1))
188*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r4_rloc)) == (r3_rloc, 2))
189*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r2_rloc)) == (r3_rloc, 3))
190*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r1_rloc)) == (r3_rloc, 4))
191*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(fed1_rloc)) == (r3_rloc, 5))
192*cfb92d14SAndroid Build Coastguard Worker
193*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194*cfb92d14SAndroid Build Coastguard Worker    # `fed1` next hops and costs
195*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(fed1_rloc)) == (fed1_rloc, 0))
196*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(r1_rloc)) == (r1_rloc, 1))
197*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(r2_rloc)) == (r1_rloc, 2))
198*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(r3_rloc)) == (r1_rloc, 4))
199*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(r4_rloc)) == (r1_rloc, 3))
200*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(r5_rloc)) == (r1_rloc, 5))
201*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(fed3_rloc)) == (r1_rloc, 6))
202*cfb92d14SAndroid Build Coastguard Worker    # On `fed1`, path to `fed2` should go through our parent.
203*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed1.get_nexthop(fed2_rloc)) == (r1_rloc, 2))
204*cfb92d14SAndroid Build Coastguard Worker
205*cfb92d14SAndroid Build Coastguard Worker    #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206*cfb92d14SAndroid Build Coastguard Worker    # `fed2` next hops and costs
207*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(fed2_rloc)) == (fed2_rloc, 0))
208*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(r1_rloc)) == (r1_rloc, 4))
209*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(r2_rloc)) == (r1_rloc, 5))
210*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(r3_rloc)) == (r1_rloc, 7))
211*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(r4_rloc)) == (r1_rloc, 6))
212*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(r5_rloc)) == (r1_rloc, 8))
213*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(fed3_rloc)) == (r1_rloc, 9))
214*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed2.get_nexthop(fed1_rloc)) == (r1_rloc, 5))
215*cfb92d14SAndroid Build Coastguard Worker
216*cfb92d14SAndroid Build Coastguard Worker
217*cfb92d14SAndroid Build Coastguard Workerverify_within(check_nexthops_and_costs, 5)
218*cfb92d14SAndroid Build Coastguard Worker
219*cfb92d14SAndroid Build Coastguard Worker#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
220*cfb92d14SAndroid Build Coastguard Worker# Disable `r4` and check nexthop and cost on it and on other
221*cfb92d14SAndroid Build Coastguard Worker# nodes.
222*cfb92d14SAndroid Build Coastguard Worker#
223*cfb92d14SAndroid Build Coastguard Worker#
224*cfb92d14SAndroid Build Coastguard Worker#     r1 ---- r2...2...r3
225*cfb92d14SAndroid Build Coastguard Worker#    / |                 \
226*cfb92d14SAndroid Build Coastguard Worker#   /  |                  \
227*cfb92d14SAndroid Build Coastguard Worker# fed1 fed2       r4       r5 ---- fed3
228*cfb92d14SAndroid Build Coastguard Worker
229*cfb92d14SAndroid Build Coastguard Workerr4.thread_stop()
230*cfb92d14SAndroid Build Coastguard Workerr4.interface_down()
231*cfb92d14SAndroid Build Coastguard Worker
232*cfb92d14SAndroid Build Coastguard Workerverify(parse_nexthop(r4.get_nexthop(r2_rloc)) == (0xfffe, 16))
233*cfb92d14SAndroid Build Coastguard Workerverify(parse_nexthop(r4.get_nexthop(r4_rloc)) == (0xfffe, 16))
234*cfb92d14SAndroid Build Coastguard Worker
235*cfb92d14SAndroid Build Coastguard Worker
236*cfb92d14SAndroid Build Coastguard Workerdef check_nexthops_and_costs_after_r4_detach():
237*cfb92d14SAndroid Build Coastguard Worker    # Make sure we have no next hop towards `r4`.
238*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r4_rloc)) == (0xfffe, 16))
239*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r4_rloc)) == (0xfffe, 16))
240*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r4_rloc)) == (0xfffe, 16))
241*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r4_rloc)) == (0xfffe, 16))
242*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed3.get_nexthop(r4_rloc)) == (r5_rloc, 16))
243*cfb92d14SAndroid Build Coastguard Worker    # Check cost and next hop on other nodes
244*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r1.get_nexthop(r5_rloc)) == (r2_rloc, 4))
245*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r3_rloc)) == (r3_rloc, 2))
246*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r2_rloc)) == (r2_rloc, 2))
247*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed3.get_nexthop(fed1_rloc)) == (r5_rloc, 6))
248*cfb92d14SAndroid Build Coastguard Worker
249*cfb92d14SAndroid Build Coastguard Worker
250*cfb92d14SAndroid Build Coastguard Workerverify_within(check_nexthops_and_costs_after_r4_detach, 45)
251*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader')
252*cfb92d14SAndroid Build Coastguard Worker
253*cfb92d14SAndroid Build Coastguard Worker#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
254*cfb92d14SAndroid Build Coastguard Worker# Disable `r1` (which was previous leader) and check
255*cfb92d14SAndroid Build Coastguard Worker# routes on other nodes
256*cfb92d14SAndroid Build Coastguard Worker#
257*cfb92d14SAndroid Build Coastguard Worker#
258*cfb92d14SAndroid Build Coastguard Worker#     r1      r2...2...r3
259*cfb92d14SAndroid Build Coastguard Worker#    / |                 \
260*cfb92d14SAndroid Build Coastguard Worker#   /  |                  \
261*cfb92d14SAndroid Build Coastguard Worker# fed1 fed2       r4       r5 ---- fed3
262*cfb92d14SAndroid Build Coastguard Worker
263*cfb92d14SAndroid Build Coastguard Workerr1.thread_stop()
264*cfb92d14SAndroid Build Coastguard Workerr1.interface_down()
265*cfb92d14SAndroid Build Coastguard Workerfed1.thread_stop()
266*cfb92d14SAndroid Build Coastguard Workerfed1.interface_down()
267*cfb92d14SAndroid Build Coastguard Workerfed2.thread_stop()
268*cfb92d14SAndroid Build Coastguard Workerfed2.interface_down()
269*cfb92d14SAndroid Build Coastguard Worker
270*cfb92d14SAndroid Build Coastguard Workerverify(parse_nexthop(r1.get_nexthop(r2_rloc)) == (0xfffe, 16))
271*cfb92d14SAndroid Build Coastguard Workerverify(parse_nexthop(r1.get_nexthop(r1_rloc)) == (0xfffe, 16))
272*cfb92d14SAndroid Build Coastguard Workerverify(parse_nexthop(r1.get_nexthop(fed1_rloc)) == (0xfffe, 16))
273*cfb92d14SAndroid Build Coastguard Worker
274*cfb92d14SAndroid Build Coastguard Worker
275*cfb92d14SAndroid Build Coastguard Workerdef check_nexthops_and_costs_after_r1_detach():
276*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r1_rloc)) == (0xfffe, 16))
277*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r3.get_nexthop(r1_rloc)) == (0xfffe, 16))
278*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r5.get_nexthop(r1_rloc)) == (0xfffe, 16))
279*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed3.get_nexthop(r1_rloc)) == (r5_rloc, 16))
280*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(r2.get_nexthop(r5_rloc)) == (r3_rloc, 3))
281*cfb92d14SAndroid Build Coastguard Worker    verify(parse_nexthop(fed3.get_nexthop(r2_rloc)) == (r5_rloc, 4))
282*cfb92d14SAndroid Build Coastguard Worker
283*cfb92d14SAndroid Build Coastguard Worker
284*cfb92d14SAndroid Build Coastguard Workerverify_within(check_nexthops_and_costs_after_r1_detach, 30)
285*cfb92d14SAndroid Build Coastguard Worker
286*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
287*cfb92d14SAndroid Build Coastguard Worker# Test finished
288*cfb92d14SAndroid Build Coastguard Worker
289*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes()
290*cfb92d14SAndroid Build Coastguard Worker
291*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
292