BMP HA test
[integration/test.git] / csit / libraries / GBP / RestconfUtils.robot
1 *** Settings ***
2 Documentation     Utils for Restconf operations for GBP
3 Library           RequestsLibrary
4 Library           OperatingSystem
5 Library           String
6 Library           json
7 Variables         ../../variables/Variables.py
8 Resource          ../Utils.robot
9
10 *** Variables ***
11 ${ENDPOINT_UNREG_PATH}    ${GBP_UNREGEP_API}
12 ${ENDPOINTS_OPER_PATH}    /restconf/operational/endpoint:endpoints
13
14 *** Keywords ***
15 Unregister Endpoints
16     [Documentation]    Unregister Endpoints Endpoints from ODL
17     ${result} =    RequestsLibrary.Get Request    session    ${ENDPOINTS_OPER_PATH}
18     ${json_result} =    json.loads    ${result.text}
19     Pass Execution If    ${json_result['endpoints']}=={}    No Endpoints available
20     ${L2_ENDPOINTS} =    Set Variable    ${json_result['endpoints']['endpoint']}
21     ${L3_ENDPOINTS} =    Set Variable    ${json_result['endpoints']['endpoint-l3']}
22     Log    ${L2_ENDPOINTS}
23     Unregister L2Endpoints    ${L2_ENDPOINTS}
24     Log    ${L3_ENDPOINTS}
25     Unregister L3Endpoints    ${L3_ENDPOINTS}
26     ${result} =    RequestsLibrary.Get Request    session    ${ENDPOINTS_OPER_PATH}
27     ${json_result} =    json.loads    ${result.text}
28     Should Be Empty    ${json_result['endpoints']}
29
30 Unregister L2Endpoints
31     [Arguments]    ${l2_eps}
32     [Documentation]    Unregister Endpoints L2Endpoints from ODL
33     : FOR    ${endpoint}    IN    @{l2_eps}
34     \    ${l2_data} =    Create L2 Endpoint JSON Data    ${endpoint}
35     \    Post Elements To URI    ${ENDPOINT_UNREG_PATH}    ${l2_data}    ${HEADERS_YANG_JSON}
36
37 Unregister L3Endpoints
38     [Arguments]    ${l3_eps}
39     [Documentation]    Unregister Endpoints L3Endpoints from ODL
40     : FOR    ${endpoint}    IN    @{l3_eps}
41     \    ${l3_data} =    Create L3 Endpoint JSON Data    ${endpoint}
42     \    Post Elements To URI    ${ENDPOINT_UNREG_PATH}    ${l3_data}    ${HEADERS_YANG_JSON}
43
44 Create L2 Endpoint JSON Data
45     [Arguments]    ${endpoint}
46     [Documentation]    Generate the JSON data required for unregistering L2 Endpoints
47     ${data}    Set Variable    {"input": {"l2": [{"mac-address": "${endpoint['mac-address']}", "l2-context": "${endpoint['l2-context']}"}]}}
48     [Return]    ${data}
49
50 Create L3 Endpoint JSON Data
51     [Arguments]    ${endpoint}
52     [Documentation]    Generate the JSON data required for unregistering L3 Endpoints
53     ${data}    Set Variable    {"input": {"l3": [{"l3-context": "${endpoint['l3-context']}", "ip-address": "${endpoint['ip-address']}"}]}}
54     [Return]    ${data}
55
56 Get Endpoint Path
57     [Arguments]    ${l2-context}    ${mac_address}
58     [Documentation]    Returns path for a registered endpoint based on key in arguments
59     ${mac_address}    Convert To Uppercase    ${mac_address}
60     [Return]    restconf/operational/endpoint:endpoints/endpoint/${l2-context}/${mac_address}
61
62 Get EndpointL3 Path
63     [Arguments]    ${l3-context}    ${ip_address}
64     [Documentation]    Returns path for a registered endpoint-l3 based on key in arguments
65     [Return]    restconf/operational/endpoint:endpoints/endpoint-l3/${l3-context}/${ip_address}
66
67 Get Tenant Path
68     [Arguments]    ${tenant_id}
69     [Documentation]    Returns path for a tenant based on key in arguments
70     [Return]    ${TENANTS_CONF_PATH}/policy:tenant/${tenant_id}
71
72 Get Policy Path
73     [Arguments]    ${tenant_id}
74     [Documentation]    Returns policy path for a particular tenant
75     ${tenant_path}    Get Tenant Path    ${tenant_id}
76     [Return]    ${tenant_path}/policy
77
78 Get Contract Path
79     [Arguments]    ${tenant_id}    ${contract_id}
80     [Documentation]    Returns path for a contract based on key and tenant-id in arguments
81     ${policy_path}    Get Policy Path    ${tenant_id}
82     [Return]    ${policy_path}/contract/${contract_id}
83
84 Get Endpoint Group Path
85     [Arguments]    ${tenant_id}    ${endpoint_group_id}
86     [Documentation]    Returns path for an EPG based on key and tenant-id in arguments
87     ${policy_path}    Get Policy Path    ${tenant_id}
88     [Return]    ${policy_path}/endpoint-group/${endpoint_group_id}
89
90 Get Classifier Instance Path
91     [Arguments]    ${tenant_id}    ${classif_instance_id}
92     [Documentation]    Returns path for a classifier-instance based on key and tenant-id in arguments
93     ${policy_path}    Get Policy Path    ${tenant_id}
94     [Return]    ${policy_path}/subject-feature-instances/classifier-instance/${classif_instance_id}
95
96 Get Forwarding Context Path
97     [Arguments]    ${tenant_id}
98     [Documentation]    Returns forwarding path for a particular tenant
99     ${tenant_path}    Get Tenant Path    ${tenant_id}
100     [Return]    ${tenant_path}/forwarding-context
101
102 Get L3 Context Path
103     [Arguments]    ${tenant_id}    ${l3_ctx_id}
104     [Documentation]    Returns l3-context path based on key and tenant-id in arguments
105     ${fwd_ctx_path}    Get Forwarding Context Path    ${tenant_id}
106     [Return]    ${fwd_ctx_path}/l3-context/${l3_ctx_id}
107
108 Get L2 Bridge Domain Path
109     [Arguments]    ${tenant_id}    ${l2_br_domain_id}
110     [Documentation]    Returns l2-bridge-domain path based on key and tenant-id in arguments
111     ${fwd_ctx_path}    Get Forwarding Context Path    ${tenant_id}
112     [Return]    ${fwd_ctx_path}/l2-bridge-domain/${l2_br_domain_id}
113
114 Get L2 Flood Domain Path
115     [Arguments]    ${tenant_id}    ${l2_flood_domain_id}
116     [Documentation]    Returns l2-flood-domain path based on key and tenant-id in arguments
117     ${fwd_ctx_path}    Get Forwarding Context Path    ${tenant_id}
118     [Return]    ${fwd_ctx_path}/l2-flood-domain/${l2_flood_domain_id}
119
120 Get Subnet Path
121     [Arguments]    ${tenant_id}    ${subnet_id}
122     [Documentation]    Returns path for a subnet based on key and tenant-id in arguments
123     ${fwd_ctx_path}    Get Forwarding Context Path    ${tenant_id}
124     [Return]    ${fwd_ctx_path}/subnet/${subnet_id}
125
126 Get Prefix Constraint of Single Rule Contract
127     [Arguments]    ${contract}
128     [Documentation]    Returns first prefix-constraint from a single rule contract
129     ${contract_json}    To Json    ${contract}
130     ${eic}    Set Variable    ${contract_json['contract'][0]['clause'][0]['consumer-matchers']['endpoint-identification-constraints']}
131     [Return]    ${eic['l3-endpoint-identification-constraints']['prefix-constraint'][0]}
132
133 Get Action of Single Rule Contract
134     [Arguments]    ${contract}
135     [Documentation]    Returns first action-ref from a single rule contract
136     ${contract_json}    To Json    ${contract}
137     [Return]    ${contract_json['contract'][0]['subject'][0]['rule'][0]['action-ref'][0]}
138
139 Get Ip Prefix of Subnet
140     [Arguments]    ${subnet}
141     [Documentation]    Returns ip-prefix from a given subnet
142     ${subnet_json}    To Json    ${subnet}
143     [Return]    ${subnet_json['subnet'][0]['ip-prefix']}
144
145 Get Action Instance Name of Single Rule Contract
146     [Arguments]    ${contract}
147     [Documentation]    Returns action-instance name from a single rule contract
148     ${contract_json}    To Json    ${contract}
149     [Return]    ${contract_json['contract'][0]['subject'][0]['rule'][0]['classifier-ref'][0]['instance-name']}
150
151 Get Groups of Endpoint
152     [Arguments]    ${endpoint}
153     [Documentation]    Returns endpoint-groups from a given endpoint
154     ${endpoint_json}    To Json    ${endpoint}
155     [Return]    ${endpoint_json['endpoint'][0]['endpoint-groups']}
156
157 Get Groups of Endpoint-L3
158     [Arguments]    ${endpoint-l3}
159     [Documentation]    Returns endpoint-groups from a given endpoint-l3
160     ${endpoint_json}    To Json    ${endpoint-l3}
161     [Return]    ${endpoint_json['endpoint-l3'][0]['endpoint-groups']}
162
163 Get L3-Addresses of Endpoint
164     [Arguments]    ${endpoint}
165     [Documentation]    Returns l3-addresses from a given endpoint
166     ${endpoint_json}    To Json    ${endpoint}
167     [Return]    ${endpoint_json['endpoint'][0]['l3-address']}
168
169 Get Tenant of Endpoint
170     [Arguments]    ${endpoint}
171     [Documentation]    Returns tenant-id from a given endpoint
172     ${endpoint_json}    To Json    ${endpoint}
173     [Return]    ${endpoint_json['endpoint'][0]['tenant']}
174
175 Get Tenant of Endpoint-L3
176     [Arguments]    ${endpoint-l3}
177     [Documentation]    Returns tenant-id from a given endpoint-l3
178     ${endpoint_json}    To Json    ${endpoint-l3}
179     [Return]    ${endpoint_json['endpoint-l3'][0]['tenant']}
180
181 Get Network Containment of Endpoint
182     [Arguments]    ${endpoint}
183     [Documentation]    Returns network-containment from a given endpoint
184     ${endpoint_json}    To Json    ${endpoint}
185     [Return]    ${endpoint_json['endpoint'][0]['network-containment']}
186
187 Get Network Containment of Endpoint-L3
188     [Arguments]    ${endpoint-l3}
189     [Documentation]    Returns network-containment from a given endpoint-l3
190     ${endpoint_json}    To Json    ${endpoint-l3}
191     [Return]    ${endpoint_json['endpoint-l3'][0]['network-containment']}
192
193 Get Mac Address of Endpoint
194     [Arguments]    ${endpoint}
195     [Documentation]    Returns mac-address from a given endpoint
196     ${endpoint_json}    To Json    ${endpoint}
197     [Return]    ${endpoint_json['endpoint'][0]['mac-address']}
198
199 Get Mac Address of Endpoint-L3
200     [Arguments]    ${endpoint-l3}
201     [Documentation]    Returns mac-address from a given endpoint-l3
202     ${endpoint_json}    To Json    ${endpoint-l3}
203     [Return]    ${endpoint_json['endpoint-l3'][0]['mac-address']}
204
205 Get L2 Context of Endpoint-L3
206     [Arguments]    ${endpoint-l3}
207     [Documentation]    Returns l2-context from a given endpoint-l3
208     ${endpoint_json}    To Json    ${endpoint-l3}
209     [Return]    ${endpoint_json['endpoint-l3'][0]['l2-context']}