Correctly space expected_status
[integration/test.git] / csit / libraries / SxpLib.robot
1 *** Settings ***
2 Documentation       Library containing Keywords used for SXP testing
3
4 Library             Collections
5 Library             RequestsLibrary
6 Library             ./Sxp.py
7 Resource            KarafKeywords.robot
8 Resource            TemplatedRequests.robot
9 Resource            Utils.robot
10 Resource            ../variables/Variables.robot
11
12
13 *** Variables ***
14 ${REST_CONTEXT}     /restconf/operations/sxp-controller
15
16
17 *** Keywords ***
18 Post To Controller
19     [Documentation]    Post request to Controller and checks response
20     [Arguments]    ${session}    ${path}    ${data}    ${rest_context}=${REST_CONTEXT}
21     ${resp} =    RequestsLibrary.Post Request
22     ...    ${session}
23     ...    ${rest_context}:${path}
24     ...    data=${data}
25     ...    headers=${HEADERS_XML}
26     Log    ${resp.text}
27     Log    ${session}
28     Log    ${path}
29     Log    ${data}
30     BuiltIn.Should be Equal As Strings    ${resp.status_code}    200
31     ${content} =    BuiltIn.Evaluate    json.loads('''${resp.text}''')    json
32     ${output} =    collections.Get From Dictionary    ${content}    output
33     ${result} =    collections.Get From Dictionary    ${output}    result
34     BuiltIn.Should Be True    ${result}    RPC result is False
35
36 Add Node
37     [Documentation]    Add node via RPC to ODL
38     [Arguments]    ${node}    ${password}=${EMPTY}    ${version}=version4    ${port}=64999    ${session}=session    ${ip}=${node}
39     ...    ${ssl_stores}=${EMPTY}    ${retry_open_timer}=1
40     ${data} =    Sxp.Add Node Xml    ${node}    ${port}    ${password}    ${version}    ${ip}
41     ...    keystores=${ssl_stores}    retry_open_timer=${retry_open_timer}
42     Post To Controller    ${session}    add-node    ${data}
43
44 Delete Node
45     [Documentation]    Delete connection via RPC from node
46     [Arguments]    ${node}    ${session}=session
47     ${data} =    Sxp.Delete Node Xml    ${node}
48     Post To Controller    ${session}    delete-node    ${data}
49
50 Add Connection
51     [Documentation]    Add connection via RPC to node
52     [Arguments]    ${version}    ${mode}    ${ip}    ${port}    ${node}=127.0.0.1    ${password}=${EMPTY}
53     ...    ${session}=session    ${domain}=global    ${security_mode}=${EMPTY}
54     ${data} =    Sxp.Add Connection Xml    ${version}    ${mode}    ${ip}    ${port}    ${node}
55     ...    ${password}    ${domain}    security_mode=${security_mode}
56     Post To Controller    ${session}    add-connection    ${data}
57
58 Get Connections
59     [Documentation]    Gets all connections via RPC from node
60     [Arguments]    ${node}=127.0.0.1    ${session}=session    ${domain}=global
61     ${data} =    Sxp.Get Connections From Node Xml    ${node}    ${domain}
62     ${resp} =    RequestsLibrary.Post Request
63     ...    ${session}
64     ...    ${REST_CONTEXT}:get-connections
65     ...    data=${data}
66     ...    headers=${HEADERS_XML}
67     BuiltIn.Should be Equal As Strings    ${resp.status_code}    200
68     RETURN    ${resp.text}
69
70 Delete Connections
71     [Documentation]    Delete connection via RPC from node
72     [Arguments]    ${ip}    ${port}    ${node}=127.0.0.1    ${session}=session    ${domain}=global
73     ${data} =    Sxp.Delete Connections Xml    ${ip}    ${port}    ${node}    ${domain}
74     Post To Controller    ${session}    delete-connection    ${data}
75
76 Clean Connections
77     [Documentation]    Delete all connections via RPC from node
78     [Arguments]    ${node}=127.0.0.1    ${session}=session    ${domain}=global
79     ${resp} =    Get Connections    ${node}    ${session}    ${domain}
80     @{connections} =    Sxp.Parse Connections    ${resp}
81     FOR    ${connection}    IN    @{connections}
82         Delete Connections
83         ...    ${connection['peer-address']}
84         ...    ${connection['tcp-port']}
85         ...    ${node}
86         ...    ${session}
87         ...    ${domain}
88     END
89
90 Verify Connection
91     [Documentation]    Verify that connection is ON
92     [Arguments]    ${version}    ${mode}    ${ip}    ${port}=64999    ${node}=127.0.0.1    ${state}=on
93     ...    ${session}=session    ${domain}=global
94     ${resp} =    Get Connections    ${node}    ${session}    ${domain}
95     Should Contain Connection    ${resp}    ${ip}    ${port}    ${mode}    ${version}    ${state}
96
97 Add Bindings
98     [Documentation]    Based on ODL version decide if bindings will be added with or without origin type (introduced in Fluorine)
99     [Arguments]    ${sgt}    ${prefixes}    ${node}=127.0.0.1    ${session}=session    ${domain}=global    ${origin}=LOCAL
100     Add Bindings Fluorine
101     ...    ${sgt}
102     ...    ${prefixes}
103     ...    ${node}
104     ...    ${session}
105     ...    ${domain}
106     ...    ${origin}
107
108 Add Bindings Fluorine
109     [Documentation]    Add/Update one or more bindings with origin type via RPC to Master DB of the node
110     [Arguments]    ${sgt}    ${prefixes}    ${node}    ${session}    ${domain}    ${origin}
111     ${data} =    Sxp.Add Bindings Xml Fluorine    ${node}    ${domain}    ${sgt}    ${prefixes}    ${origin}
112     Post To Controller    ${session}    add-bindings    ${data}
113
114 Get Bindings
115     [Documentation]    Gets all binding via RPC from Master DB of node
116     [Arguments]    ${node}=127.0.0.1    ${session}=session    ${domain}=global    ${scope}=all
117     ${data} =    Sxp.Get Bindings From Node Xml    ${node}    ${scope}    ${domain}
118     ${resp} =    TemplatedRequests.Post_To_Uri
119     ...    ${REST_CONTEXT}:get-node-bindings
120     ...    data=${data}
121     ...    accept=${ACCEPT_JSON}
122     ...    content_type=${HEADERS_XML}
123     ...    session=${session}
124     RETURN    ${resp}
125
126 Clean Bindings
127     [Documentation]    Delete all bindings via RPC from Master DB of node
128     [Arguments]    ${node}=127.0.0.1    ${session}=session    ${domain}=global    ${scope}=local
129     ${resp} =    Get Bindings    ${node}    ${session}    ${domain}    ${scope}
130     @{bindings} =    Sxp.Parse Bindings    ${resp}
131     FOR    ${binding}    IN    @{bindings}
132         @{prefixes_list} =    collections.Get From Dictionary    ${binding}    ip-prefix
133         ${prefixes} =    BuiltIn.Catenate    SEPARATOR=,    @{prefixes_list}
134         Delete Bindings    ${binding['sgt']}    ${prefixes}    ${node}    ${domain}    ${session}
135     END
136
137 Delete Bindings
138     [Documentation]    Delete one or more bindings via RPC from Master DB of node
139     [Arguments]    ${sgt}    ${prefixes}    ${node}=127.0.0.1    ${domain}=global    ${session}=session
140     ${data} =    Sxp.Delete Bindings Xml    ${node}    ${domain}    ${sgt}    ${prefixes}
141     Post To Controller    ${session}    delete-bindings    ${data}
142
143 Add PeerGroup
144     [Documentation]    Adds new PeerGroup via RPC to Node
145     [Arguments]    ${name}    ${peers}=    ${node}=127.0.0.1    ${session}=session
146     ${data} =    Sxp.Add Peer Group Xml    ${name}    ${peers}    ${node}
147     Post To Controller    ${session}    add-peer-group    ${data}
148
149 Delete Peer Group
150     [Documentation]    Delete PeerGroup via RPC from Node
151     [Arguments]    ${name}    ${node}=127.0.0.1    ${session}=session
152     ${data} =    Sxp.Delete Peer Group Xml    ${name}    ${node}
153     Post To Controller    ${session}    delete-peer-group    ${data}
154
155 Get Peer Groups
156     [Documentation]    Gets all PeerGroups via RPC from node
157     [Arguments]    ${node}=127.0.0.1    ${session}=session
158     ${data} =    Sxp.Get Peer Groups From Node Xml    ${node}
159     ${resp} =    RequestsLibrary.Post Request
160     ...    ${session}
161     ...    ${REST_CONTEXT}:get-peer-groups
162     ...    data=${data}
163     ...    headers=${HEADERS_XML}
164     BuiltIn.Should be Equal As Strings    ${resp.status_code}    200
165     RETURN    ${resp.text}
166
167 Clean Peer Groups
168     [Documentation]    Delete all PeerGroups via RPC from node
169     [Arguments]    ${node}=127.0.0.1    ${session}=session
170     ${resp} =    Get Peer Groups    ${node}    ${session}
171     @{prefixes} =    Sxp.Parse Peer Groups    ${resp}
172     FOR    ${group}    IN    @{prefixes}
173         Delete Peer Group    ${group['name']}    ${node}    ${session}
174     END
175
176 Add Filter
177     [Documentation]    Add Filter via RPC from Node
178     [Arguments]    ${name}    ${type}    ${entries}    ${node}=127.0.0.1    ${session}=session    ${policy}=auto-update
179     ${data} =    Prepare Add/Update Filter Data    ${name}    ${type}    ${entries}    ${node}    ${policy}
180     Post To Controller    ${session}    add-filter    ${data}
181
182 Update Filter
183     [Documentation]    Update Filter via RPC
184     [Arguments]    ${name}    ${type}    ${entries}    ${node}=127.0.0.1    ${session}=session    ${policy}=auto-update
185     ${data} =    Prepare Add/Update Filter Data    ${name}    ${type}    ${entries}    ${node}    ${policy}
186     Post To Controller    ${session}    update-filter    ${data}
187
188 Prepare Add/Update Filter Data
189     [Documentation]    Prepare XML payload for add-filter and update-filter RPCs. Payloads for both RPCs are the same.
190     [Arguments]    ${name}    ${type}    ${entries}    ${node}=127.0.0.1    ${policy}=auto-update
191     ${data} =    Add Filter Xml
192     ...    ${name}
193     ...    ${type}
194     ...    ${entries}
195     ...    ${node}
196     ...    ${policy}
197     RETURN    ${data}
198
199 Add Domain Filter
200     [Documentation]    Add Domain Filter via RPC from Node
201     [Arguments]    ${name}    ${domains}    ${entries}    ${node}=127.0.0.1    ${filter_name}=base-domain-filter    ${session}=session
202     ${data} =    Sxp.Add Domain Filter Xml    ${name}    ${domains}    ${entries}    ${node}    ${filter_name}
203     Post To Controller    ${session}    add-domain-filter    ${data}
204
205 Delete Filter
206     [Documentation]    Delete Filter via RPC from Node
207     [Arguments]    ${name}    ${type}    ${node}=127.0.0.1    ${session}=session
208     ${data} =    Sxp.Delete Filter Xml    ${name}    ${type}    ${node}
209     Post To Controller    ${session}    delete-filter    ${data}
210
211 Delete Domain Filter
212     [Documentation]    Delete Filter via RPC from Node
213     [Arguments]    ${name}    ${node}=127.0.0.1    ${filter_name}=base-domain-filter    ${session}=session
214     ${data} =    Sxp.Delete Domain Filter Xml    ${name}    ${node}    ${filter_name}
215     Post To Controller    ${session}    delete-domain-filter    ${data}
216
217 Should Contain Binding
218     [Documentation]    Tests if data contains specified binding
219     [Arguments]    ${resp}    ${sgt}    ${prefix}
220     ${out} =    Sxp.Find Binding    ${resp}    ${sgt}    ${prefix}
221     BuiltIn.Should Be True    ${out}    Doesn't have ${sgt} ${prefix}
222
223 Should Not Contain Binding
224     [Documentation]    Tests if data doesn't contains specified binding
225     [Arguments]    ${resp}    ${sgt}    ${prefix}
226     ${out} =    Sxp.Find Binding    ${resp}    ${sgt}    ${prefix}
227     BuiltIn.Should Not Be True    ${out}    Should't have ${sgt} ${prefix}
228
229 Should Contain Connection
230     [Documentation]    Test if data contains specified connection
231     [Arguments]    ${resp}    ${ip}    ${port}    ${mode}    ${version}    ${state}=none
232     ${out} =    Sxp.Find Connection    ${resp}    ${version}    ${mode}    ${ip}    ${port}
233     ...    ${state}
234     BuiltIn.Should Be True    ${out}    Doesn't have ${ip}:${port} ${mode} ${version} in state ${state}
235
236 Should Not Contain Connection
237     [Documentation]    Test if data doesn't contains specified connection
238     [Arguments]    ${resp}    ${ip}    ${port}    ${mode}    ${version}    ${state}=none
239     ${out} =    Sxp.Find Connection    ${resp}    ${version}    ${mode}    ${ip}    ${port}
240     ...    ${state}
241     BuiltIn.Should Not Be True    ${out}    Shouldn't have ${ip}:${port} ${mode} ${version}
242
243 Bindings Should Contain
244     [Documentation]    Retrieves bindings and verifies they contain given binding
245     [Arguments]    ${sgt}    ${prefix}    ${domain}=global    ${scope}=all
246     ${resp} =    Get Bindings    domain=${domain}    scope=${scope}
247     Should Contain Binding    ${resp}    ${sgt}    ${prefix}
248
249 Bindings Should Not Contain
250     [Documentation]    Retrieves bindings and verifies they do not contain given binding
251     [Arguments]    ${sgt}    ${prefix}    ${domain}=global    ${scope}=all
252     ${resp} =    Get Bindings    domain=${domain}    scope=${scope}
253     Should Not Contain Binding    ${resp}    ${sgt}    ${prefix}
254
255 Connections Should Contain
256     [Documentation]    Retrieves connections and verifies they contain given connection
257     [Arguments]    ${ip}    ${port}    ${mode}    ${version}    ${state}=none
258     ${resp} =    Get Connections
259     Should Contain Connection    ${resp}    ${ip}    ${port}    ${mode}    ${version}    ${state}
260
261 Connections Should Not Contain
262     [Documentation]    Retrieves connections and verifies they do not contain given connection
263     [Arguments]    ${ip}    ${port}    ${mode}    ${version}    ${state}=none
264     ${resp} =    Get Connections
265     Should Not Contain Connection    ${resp}    ${ip}    ${port}    ${mode}    ${version}    ${state}
266
267 Setup Topology Complex
268     [Arguments]    ${version}=version4    ${password}=none
269     FOR    ${node}    IN RANGE    2    6
270         Add Connection    ${version}    both    127.0.0.1    64999    127.0.0.${node}
271         ...    ${password}
272         Add Connection    ${version}    both    127.0.0.${node}    64999    127.0.0.1
273         ...    ${password}
274         BuiltIn.Wait Until Keyword Succeeds    15    1    Verify Connection    ${version}    both
275         ...    127.0.0.${node}
276         Add Bindings    ${node}0    10.10.10.${node}0/32    127.0.0.${node}
277         Add Bindings    ${node}0    10.10.${node}0.0/24    127.0.0.${node}
278         Add Bindings    ${node}0    10.${node}0.0.0/16    127.0.0.${node}
279         Add Bindings    ${node}0    ${node}0.0.0.0/8    127.0.0.${node}
280     END
281     Add Bindings    10    10.10.10.10/32    127.0.0.1
282     Add Bindings    10    10.10.10.0/24    127.0.0.1
283     Add Bindings    10    10.10.0.0/16    127.0.0.1
284     Add Bindings    10    10.0.0.0/8    127.0.0.1
285
286 Verify Snapshot Was Pushed
287     [Documentation]    Will succeed if the ${snapshot_string} is found in the karaf logs
288     [Arguments]    ${snapshot_string}=22-sxp-controller-one-node.xml
289     ${output} =    Utils.Run Command On Controller
290     ...    ${ODL_SYSTEM_IP}
291     ...    cat ${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log* | grep -c 'Successfully pushed configuration snapshot.*${snapshot_string}'
292     BuiltIn.Should Not Be Equal As Strings    ${output}    0
293
294 Add Domain
295     [Documentation]    Based on ODL version decide if domain's bindings will be added with or without origin type (introduced in Fluorine)
296     [Arguments]    ${domain_name}    ${sgt}=None    ${prefixes}=None    ${origin}=LOCAL    ${node}=127.0.0.1    ${session}=session
297     Add Domain Fluorine
298     ...    ${domain_name}
299     ...    ${sgt}
300     ...    ${prefixes}
301     ...    ${origin}
302     ...    ${node}
303     ...    ${session}
304
305 Add Domain Fluorine
306     [Documentation]    Add Domain with bindings (with origin) via RPC
307     [Arguments]    ${domain_name}    ${sgt}    ${prefixes}    ${origin}    ${node}    ${session}
308     ${data} =    Sxp.Add Domain Xml Fluorine    ${node}    ${domain_name}    ${sgt}    ${prefixes}    ${origin}
309     Post To Controller    ${session}    add-domain    ${data}
310
311 Delete Domain
312     [Documentation]    Delete Domain via RPC
313     [Arguments]    ${domain_name}    ${node}=127.0.0.1    ${session}=session
314     ${data} =    Sxp.Delete Domain Xml    ${node}    ${domain_name}
315     Post To Controller    ${session}    delete-domain    ${data}
316
317 Add Bindings Range
318     [Documentation]    Add Bindings to Node specified by range
319     [Arguments]    ${sgt}    ${start}    ${size}    ${node}=127.0.0.1
320     ${prefixes} =    Sxp.Prefix Range    ${start}    ${size}
321     Add Bindings    ${sgt}    ${prefixes}    ${node}
322
323 Delete Bindings Range
324     [Documentation]    Delete Bindings to Node specified by range
325     [Arguments]    ${sgt}    ${start}    ${size}    ${node}=127.0.0.1
326     ${prefixes} =    Sxp.Prefix Range    ${start}    ${size}
327     Delete Bindings    ${sgt}    ${prefixes}    ${node}
328
329 Check Binding Range
330     [Documentation]    Check if Node contains Bindings specified by range
331     [Arguments]    ${sgt}    ${start}    ${end}    ${node}=127.0.0.1
332     ${resp} =    Get Bindings    ${node}
333     FOR    ${num}    IN RANGE    ${start}    ${end}
334         ${ip} =    Sxp.Get Ip From Number    ${num}
335         Should Contain Binding    ${resp}    ${sgt}    ${ip}/32
336     END
337
338 Check Binding Range Negative
339     [Documentation]    Check if Node does not contains Bindings specified by range
340     [Arguments]    ${sgt}    ${start}    ${end}    ${node}=127.0.0.1
341     ${resp} =    Get Bindings    ${node}
342     FOR    ${num}    IN RANGE    ${start}    ${end}
343         ${ip} =    Sxp.Get Ip From Number    ${num}
344         Should Not Contain Binding    ${resp}    ${sgt}    ${ip}/32
345     END
346
347 Setup SXP Environment
348     [Documentation]    Create session to Controller, ${node_range} parameter specifies number of localhost nodes to be created on ${ODL_SYSTEM_IP}.
349     [Arguments]    ${node_range}=1
350     RequestsLibrary.Create Session
351     ...    session
352     ...    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}
353     ...    auth=${AUTH}
354     ...    timeout=${DEFAULT_TIMEOUT_HTTP}
355     ...    max_retries=0
356     FOR    ${num}    IN RANGE    1    ${node_range}+1
357         ${node} =    Sxp.Get Ip From Number    ${num}
358         Add Node    ${node}    retry_open_timer=${num}
359         BuiltIn.Wait Until Keyword Succeeds    20    1    Check Node Started    ${node}    system=${ODL_SYSTEM_IP}
360     END
361
362 Check Node Started
363     [Documentation]    Verify that SxpNode has data writen to Operational datastore and is running
364     [Arguments]    ${node}    ${port}=64999    ${system}=${node}    ${session}=session    ${ip}=${node}
365     ${resp} =    RequestsLibrary.Get Request
366     ...    ${session}
367     ...    /restconf/operational/network-topology:network-topology/topology/sxp/node/${node}/
368     BuiltIn.Should Be Equal As Strings    ${resp.status_code}    200
369     ${rc} =    Get Node Running Status    ${node}    ${port}    ${system}    ${session}    ${ip}
370     BuiltIn.Should Be Equal As Strings    ${rc}    0
371
372 Check Node Stopped
373     [Documentation]    Verify that SxpNode has data removed from Operational datastore and is not running
374     [Arguments]    ${node}    ${port}=64999    ${system}=${node}    ${session}=session    ${ip}=${node}
375     ${resp} =    RequestsLibrary.Get Request
376     ...    ${session}
377     ...    /restconf/operational/network-topology:network-topology/topology/sxp/node/${node}/
378     BuiltIn.Should Be Equal As Strings    ${resp.status_code}    404
379     ${rc} =    Get Node Running Status    ${node}    ${port}    ${system}    ${session}    ${ip}
380     BuiltIn.Should Be Equal As Strings    ${rc}    1
381
382 Get Node Running Status
383     [Documentation]    Get status if node is running by checking that netty server is running
384     [Arguments]    ${node}    ${port}    ${system}    ${session}    ${ip}
385     ${rc} =    Utils.Run Command On Remote System
386     ...    ${system}
387     ...    netstat -tln | grep -q ${ip}:${port} && echo 0 || echo 1
388     ...    ${ODL_SYSTEM_USER}
389     ...    ${ODL_SYSTEM_PASSWORD}
390     ...    prompt=${ODL_SYSTEM_PROMPT}
391     RETURN    ${rc}
392
393 Clean SXP Environment
394     [Documentation]    Destroy created sessions
395     [Arguments]    ${node_range}=1
396     FOR    ${num}    IN RANGE    1    ${node_range}+1
397         ${ip} =    Sxp.Get Ip From Number    ${num}
398         Delete Node    ${ip}
399         BuiltIn.Wait Until Keyword Succeeds    12x    10s    Check Node Stopped    ${ip}    system=${ODL_SYSTEM_IP}
400     END
401     RequestsLibrary.Delete All Sessions
402
403 Get Routing Configuration From Controller
404     [Documentation]    Get Routing configuration from config DS
405     [Arguments]    ${session}
406     ${resp} =    RequestsLibrary.Get Request
407     ...    ${session}
408     ...    /restconf/config/sxp-cluster-route:sxp-cluster-route/
409     ...    headers=${ACCEPT_XML}
410     ${data} =    BuiltIn.Set Variable If    "${resp.status_code}" == "200"    ${resp.text}    ${EMPTY}
411     RETURN    ${data}
412
413 Put Routing Configuration To Controller
414     [Documentation]    Put Routing configuration to Config DS
415     [Arguments]    ${data}    ${session}
416     ${resp} =    RequestsLibrary.Put Request
417     ...    ${session}
418     ...    /restconf/config/sxp-cluster-route:sxp-cluster-route/
419     ...    data=${data}
420     ...    headers=${HEADERS_XML}
421     BuiltIn.Log    ${resp}
422     BuiltIn.Should Match    "${resp.status_code}"    "20?"
423
424 Clean Routing Configuration To Controller
425     [Documentation]    Delete Routing configuration from Config DS
426     [Arguments]    ${session}
427     ${resp} =    RequestsLibrary.Get Request
428     ...    ${session}
429     ...    /restconf/config/sxp-cluster-route:sxp-cluster-route/
430     ...    headers=${ACCEPT_XML}
431     IF    "${resp.status_code}" == "200"
432         RequestsLibrary.Delete Request    ${session}    /restconf/config/sxp-cluster-route:sxp-cluster-route/
433     END