Adding priority to flow mods in TTL suite
[integration/test.git] / test / csit / libraries / FlowLib.txt
1 *** Settings ***
2 Documentation     Keywords used to create/modify flow objects. The object is defined in the
3 ...               corresponding FlowLib.py library and contains pertinent fields and methods (e.g.,
4 ...               cookie and barrier fields, string formatted xml that can be used to push to
5 ...               controller)
6 Library           ./FlowLib.py
7 Library           XML
8 Variables         ../variables/Variables.py
9
10 *** Variables ***
11
12 *** Keywords ***
13 Create Inventory Flow
14     [Documentation]    Calls FlowLib.Make_Inventory_Flow function and initializes and sanitizes
15     ...    the basic flow elements that can be given to flow:inventory
16     ${flow}=    Make Inventory Flow
17     [Return]    ${flow}
18
19 Create Service Flow
20     [Documentation]    Used for creating an object that will use an XML format that
21     ...    can be given to flow:service.
22     ${flow}=    Make Service Flow
23     [Return]    ${flow}
24
25 Set "${flow}" "${property}" With "${property_val}"
26     [Documentation]    Embedded variables to make higher level keywords more readable.
27     ...    There are some cases where the python attribute uses an underscore,
28     ...    but a hyphen needs to be used. This seems inconsistent, and may need
29     ...    to be looked at from the openflow plugin perspective.
30     ...
31     ...    At this point, this library will remove the element ${property} from the
32     ...    xml representation of the flow and reset with the given value. \ It's not
33     ...    possible, yet, to have multiple elements with the same name. \ That will
34     ...    likely be needed in the future.
35     ${property}    Run Keyword If    "table_id" != "${property}" and "cookie_mask" != "${property}"    Replace String    ${property}    _    -
36     ...    ELSE    Set Variable    ${property}
37     Remove Flow XML Element    ${flow}    ${property}
38     Add Flow XML Element    ${flow}    ${property}    ${property_val}
39     Set Flow Field    ${flow}    ${property}    ${property_val}
40     [Return]    ${flow}
41
42 Set Flow Action
43     [Arguments]    ${flow}    ${instruction_order}    ${action_order}    ${action}    ${action_val}=${EMPTY}
44     [Documentation]    Will remove the instruction element first, then add the proper xml structure
45     ...    to implement the action as given in the arguments
46     ##For the case that any of the instruction/apply-actions/action elements are not there we need to add them'
47     Remove Flow XML Element    ${flow}    instruction
48     Add Flow XML Element    ${flow}    instruction    ${EMPTY}    instructions
49     Add Flow XML Element    ${flow}    order    ${instruction_order}    instructions/instruction
50     Add Flow XML Element    ${flow}    apply-actions    ${EMPTY}    instructions/instruction
51     Add Flow XML Element    ${flow}    action    ${EMPTY}    instructions/instruction/apply-actions
52     Add Flow XML Element    ${flow}    order    ${action_order}    instructions/instruction/apply-actions/action
53     Add Flow XML Element    ${flow}    ${action}    ${action_val}    instructions/instruction/apply-actions/action
54     [Return]    ${flow}
55
56 Set Flow Output Action
57     [Arguments]    ${flow}    ${instruction_order}    ${action_order}    ${output_port}
58     Set Flow Action    ${flow}    ${instruction_order}    ${action_order}    output-action
59     Add Flow XML Element    ${flow}    output-node-connector    ${output_port}    instructions/instruction/apply-actions/action/output-action
60     [Return]    ${flow}
61
62 Set Flow Ethernet Match
63     [Arguments]    ${flow}    ${match_value_dict}
64     [Documentation]    Specific keyword for adding an ethernet match rules where the elements are given
65     ...    in key/value pairs inside the ${match_value_dict} argument. This keyword will also remove any
66     ...    existing ethernet-match elements from the flow before adding
67     Clear Flow Matches    ${flow}    match/ethernet-match
68     Add Flow XML Element    ${flow}    ethernet-match    ${EMPTY}    match
69     ${type}=    Get From Dictionary    ${match_value_dict}    type
70     Add Flow XML Element    ${flow}    ethernet-type    ${EMPTY}    match/ethernet-match
71     Add Flow XML Element    ${flow}    type    ${type}    match/ethernet-match/ethernet-type
72     ${src}=    Get From Dictionary    ${match_value_dict}    source
73     Add Flow XML Element    ${flow}    ethernet-source    ${EMPTY}    match/ethernet-match
74     Add Flow XML Element    ${flow}    address    ${src}    match/ethernet-match/ethernet-source
75     ${dst}=    Get From Dictionary    ${match_value_dict}    destination
76     Add Flow XML Element    ${flow}    ethernet-destination    ${EMPTY}    match/ethernet-match
77     Add Flow XML Element    ${flow}    address    ${dst}    match/ethernet-match/ethernet-destination
78     [Return]    ${flow}
79
80 Set Flow IPv4 Match
81     [Arguments]    ${flow}    ${match_value_dict}
82     [Documentation]    Specific keyword for adding an ipv4 match rules where the elements are given
83     ...    in key/value pairs inside the ${match_value_dict} argument. This keyword will also remove any
84     ...    existing ipv4 match elements from the flow before adding
85     Clear Flow Matches    ${flow}    match/ipv4-source
86     Clear Flow Matches    ${flow}    match/ipv4-destination
87     ${src}=    Get From Dictionary    ${match_value_dict}    source
88     Add Flow XML Element    ${flow}    ipv4-source    ${src}    match
89     ${dst}=    Get From Dictionary    ${match_value_dict}    destination
90     Add Flow XML Element    ${flow}    ipv4-destination    ${dst}    match
91     [Return]    ${flow}
92
93 Clear Flow Actions
94     [Arguments]    ${flow}
95     [Documentation]    Will clean out any existing flow actions in the given ${flow} object
96     Remove Flow XML Element    ${flow}    instructions/instruction
97     [Return]    ${flow}
98
99 Clear Flow Matches
100     [Arguments]    ${flow}    ${match_element}
101     [Documentation]    Will clean out any existing flow matches in the given ${flow} object
102     Remove Flow XML Element    ${flow}    match/${match_element}
103     [Return]    ${flow}
104
105 Set Flow XML Element Attribute
106     [Arguments]    ${flow}    ${element}    ${id}    ${value}
107     [Documentation]    Will set the given id/value pair to the given to the element provided
108     ...    and make the proper changes to the ${flow} object also provided.
109     ${flow_xml}=    Parse XML    ${flow.xml}
110     Set Element Attribute    ${flow_xml}    ${id}    ${value}    xpath=${element}
111     ${xml_string}=    Element To String    ${flow_xml}
112     Set Flow Field    ${flow}    xml    ${xml_string}
113     Log    ${flow.xml}
114     [Return]    ${flow}
115
116 Add Flow XML Element
117     [Arguments]    ${flow}    ${element}    ${element_val}=${EMPTY}    ${xpath}=.
118     [Documentation]    Will modify the current xml representation of the ${flow} object to contain
119     ...    the given ${element} at the given ${xpath}. If the ${element} uses a value, that can be
120     ...    passed eith the ${element_val} which defaults to ${EMPTY} if not used. NOTE: since there
121     ...    are two default parameters to this keyword, if you have an ${xpath} to use, but no ${element_val}
122     ...    you will still need to pass ${EMPTY} when invoking so that ${xpath} will end up at the right
123     ...    location in the parameter list
124     ${flow_xml}=    Parse XML    ${flow.xml}
125     Add Element    ${flow_xml}    <${element}>${element_val}</${element}>    xpath=${xpath}
126     ${xml_string}=    Element To String    ${flow_xml}
127     Set Flow Field    ${flow}    xml    ${xml_string}
128     Log    ${flow.xml}
129     [Return]    ${flow}
130
131 Remove Flow XML Element
132     [Arguments]    ${flow}    ${element_xpath}
133     [Documentation]    Removes the element at the given ${element_xpath} within the given ${flow}
134     ...    object. The ${flow} object's xml representation will be updated to reflect this removal.
135     ${flow_xml}=    Parse XML    ${flow.xml}
136     Run Keyword And Ignore Error    Remove Elements    ${flow_xml}    xpath=${element_xpath}
137     ${xml_string}=    Element To String    ${flow_xml}
138     Set Flow Field    ${flow}    xml    ${xml_string}
139     [Return]    ${flow}
140
141 Add Flow To Controller And Verify
142     [Arguments]    ${flow_body}    ${node_id}    ${table_id}    ${flow_id}
143     [Documentation]    Push flow through REST-API and verify in data-store
144     ${resp}    Put Xml    session    ${REST_CON}/node/${node_id}/table/${table_id}/flow/${flow_id}    data=${flow_body}
145     Log    ${resp.content}
146     Should Be Equal As Strings    ${resp.status_code}    200
147     ${resp}    RequestsLibrary.Get    session    ${REST_CON}/node/${node_id}/table/${table_id}/flow/${flow_id}    headers=${ACCEPT_XML}
148     Log    ${resp.content}
149     Should Be Equal As Strings    ${resp.status_code}    200
150     compare xml    ${flow_body}    ${resp.content}
151
152 Verify Flow On Mininet Switch
153     [Arguments]    ${flow_elements}
154     [Documentation]    Checking flow on switch
155     sleep    1
156     write    dpctl dump-flows -O OpenFlow13
157     ${switchoutput}    Read Until    >
158     : FOR    ${flowElement}    IN    @{flow_elements}
159     \    should Contain    ${switchoutput}    ${flowElement}
160
161 Remove Flow From Controller And Verify
162     [Arguments]    ${flow_body}    ${node_id}    ${table_id}    ${flow_id}
163     [Documentation]    Remove flow
164     ${resp}    RequestsLibrary.Delete    session    ${REST_CON}/node/${node_id}/table/${table_id}/flow/${flow_id}
165     Log    ${resp.content}
166     Should Be Equal As Strings    ${resp.status_code}    200
167     ${resp}    RequestsLibrary.Get    session    ${REST_CON}/node/${node_id}/table/${table_id}
168     Log    ${resp.content}
169     Should Not Contain    ${resp.content}    ${flow_id}
170
171 Verify Flow Does Not Exist On Mininet Switch
172     [Arguments]    ${flow_elements}
173     [Documentation]    Checking flow on switch is removed
174     sleep    1
175     write    dpctl dump-flows -O OpenFlow13
176     ${switchoutput}    Read Until    >
177     : FOR    ${flowElement}    IN    @{flow_elements}
178     \    should Not Contain    ${switchoutput}    ${flowElement}
179
180 Remove Default Flows
181     [Arguments]    ${node_id}
182     [Documentation]    Removes any flows considered "default". one such flow is
183     ...    to forward all traffic to the CONTROLLER with priority 0 at flow-table 0
184     ...    If/When others are implemented this keyword can be updated to include those.
185     ${flow}=    Make Service Flow
186     Set "${flow}" "priority" With "0"
187     Set "${flow}" "flow-table" With "0"
188     Add Flow XML Element    ${flow}    node    /inv:nodes/inv:node[inv:id="${node_id}"]
189     Set Flow XML Element Attribute    ${flow}    node    xmlns:inv    urn:opendaylight:inventory
190     Log    Flow XML is ${flow.xml}
191     write    dpctl dump-flows -O OpenFlow13
192     ${switchoutput}    Read Until    >
193     ${headers}=    Create Dictionary    Content-Type    application/yang.data+xml
194     ${resp}    RequestsLibrary.Post    session    restconf/operations/sal-flow:remove-flow    data=${flow.xml}    headers=${headers}
195     Log    ${resp.content}
196     Should Be Equal As Strings    ${resp.status_code}    200
197     ${resp}=    RequestsLibrary.Get    session    ${OPERATIONAL_NODES_API}
198     Log    ${resp.content}
199     Should Not Contain    ${resp.content}    "output-node-connector": "CONTROLLER",
200     ${strings_to_check_for}=    Create List    CONTROLLER
201     Verify Flow Does Not Exist On Mininet Switch    ${strings_to_check_for}