Merge "o introducing FlowLib robot/python library. Gives ability to dynamically...
[integration/test.git] / test / csit / libraries / FlowLib.py
1 """
2 Library for dynamic flow construction.
3 Authors: james.luhrsen@hp.com
4 Updated: 2014-08-29
5 """
6 '''
7 xmltodict and json libs not needed at this point, but may be useful in
8 the future.
9 '''
10 ##import xmltodict
11 ##import json
12 import string
13 import robot
14 from robot.libraries.BuiltIn import BuiltIn
15
16 ##bare bones xml for building a flow
17 xml_skeleton = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' +      \
18                '<flow xmlns="urn:opendaylight:flow:inventory">'         +      \
19                     '<instructions></instructions>'                     +      \
20                     '<match></match>'                                   +      \
21                '</flow>'
22
23 class Flow:
24     '''
25     Flow class for creating and interacting with OpenFlow flows
26     '''
27
28     strict = "false"
29     instruction_xmls = ""
30     match_xmls = ""
31     cookie = 0
32     cookie_mask = 0
33     table_id = 0
34     id = 1
35     hard_timeout = 60
36     idle_timeout = 30
37     flow_name = "No Name"
38     priority = 0
39     barrier = "false"
40
41     xml = xml_skeleton
42
43     json = ""
44
45     def set_field(self, field, value):
46         '''
47            allows for generically setting any attribute in this
48            class based on the 'field' passed in.  In the future,
49            adding a new attribute only requires that single line
50            addition.  no need for additional setter.
51         '''
52         setattr(self, field, value)
53
54 def Make_Flow_Object():
55     '''
56         Robot Keyword to create and return an instance of the Flow
57         class.
58     '''
59     flow = Flow()
60     return flow
61
62 def Set_Flow_Field(flow, field, value):
63     '''
64         Robot Keyword to allow the modification (setting) of the 
65         flow object attributes
66     '''
67     flow.set_field(field,value)
68     return flow
69
70
71 #def Convert_Flow_XML_To_Json(flow):
72 #    '''
73 #       There may be a need in the future to use json to push
74 #       flows, as opposed to xml format that is prevalent in 
75 #       test code at this point.  This function will give a 
76 #       conversion, but unsure if it's proper.  Also, unsure
77 #       if the xmltodict library is viable in the CSIT environment
78 #    '''
79 #    flowXmlDict = xmltodict.parse(flow.xml)
80 #    flow.json = json.dumps(flowXmlDict)
81 #    return flow