Fix expected_status types
[integration/test.git] / csit / libraries / SwitchClasses / BaseSwitch.py
1 """
2 Base Switch Object Definition
3 Authors: james.luhrsen@hp.com
4 Created: 2014-09-20
5 """
6 import importlib
7 import xml.etree.ElementTree as ElementTree
8
9
10 class BaseSwitch(object):
11     """
12     Switch Base Class
13     """
14
15     make = ""
16     model = ""
17
18     mgmt_protocol = ""
19     ssh_key = ""
20     mgmt_ip = ""
21     mgmt_port = ""
22     mgmt_user = ""
23     mgmt_password = ""
24     mgmt_prompt = ""
25
26     connection_index = ""
27
28     initialization_type = ""
29
30     of_controller_ip = ""
31
32     connection_configs = []
33
34     initialization_cmds = []
35
36     base_openflow_config = []
37
38     openflow_enable_config = []
39
40     openflow_enable_validations = []
41
42     openflow_disable_config = []
43     openflow_disable_validations = []
44
45     dump_all_flows = []
46
47     src_mac = ""
48     dst_mac = ""
49     ip_src = ""
50     ip_dst = ""
51     table_id = ""
52     action = ""
53
54     datapath_id_output_string = ""
55     datapath_id_output_command = ""
56     datapath_id = ""
57
58     def set_connection_index(self, idx):
59         self.connection_index = idx
60
61     def set_controller_ip(self, ip):
62         self.of_controller_ip = ip
63
64     def set_mgmt_ip(self, ip):
65         self.mgmt_ip = ip
66
67     def set_mgmt_user(self, user):
68         self.mgmt_user = user
69
70     def set_mgmt_prompt(self, prompt):
71         self.mgmt_prompt = prompt
72
73     def set_ssh_key(self, key):
74         self.ssh_key = key
75
76     def update_datapath_id(self):
77         raise NotImplementedError("Please implement this method")
78
79     def create_flow_match_elements(self, flow_xml):
80         flow_tree = ElementTree.fromstring(flow_xml)
81         self.table_id = flow_tree.find("{urn:opendaylight:flow:inventory}table_id").text
82         instructions_element = flow_tree.find(
83             "{urn:opendaylight:flow:inventory}instructions"
84         )
85         instruction_element = instructions_element.find(
86             "{urn:opendaylight:flow:inventory}instruction"
87         )
88         apply_actions = instruction_element.find(
89             "{urn:opendaylight:flow:inventory}apply-actions"
90         )
91         action = apply_actions.find("{urn:opendaylight:flow:inventory}action")
92         output_action = action.find("{urn:opendaylight:flow:inventory}output-action")
93         output_node_connector = output_action.find(
94             "{urn:opendaylight:" "flow:inventory}output-node-connector"
95         )
96         self.action = output_node_connector.text
97         match_element = flow_tree.find("{urn:opendaylight:flow:inventory}match")
98         ethernet_match_element = match_element.find(
99             "{urn:opendaylight:flow:inventory}ethernet-match"
100         )
101         ethernet_source = ethernet_match_element.find(
102             "{urn:opendaylight:flow:inventory}ethernet-source"
103         )
104         ethernet_source_address = ethernet_source.find(
105             "{urn:opendaylight:flow:inventory}address"
106         )
107         self.src_mac = ethernet_source_address.text
108         ethernet_destination = ethernet_match_element.find(
109             "{urn:opendaylight:flow:inventory}ethernet-destination"
110         )
111         ethernet_destination_address = ethernet_destination.find(
112             "{urn:opendaylight:flow:inventory}address"
113         )
114         self.dst_mac = ethernet_destination_address.text
115         self.ip_src = match_element.find(
116             "{urn:opendaylight:flow:inventory}ipv4-source"
117         ).text
118         self.ip_dst = match_element.find(
119             "{urn:opendaylight:flow:inventory}ipv4-destination"
120         ).text
121
122     def convert_hex_to_decimal_as_string(self, hex_string):
123         # TODO: need to add error checking in case the hex_string is
124         # not fully hex
125         return str(int(hex_string, 16))
126
127     def get_switch(self, switch_type):
128         """
129         Generic method that will allow Robot Code to pass a string
130         to this "keyword - Get Switch" and create an object of that
131         type.  (EX: Get Switch  OVS)
132         """
133
134         # TODO: what if the module "switch_type" does not exist.  Need some
135         # error checking for that.
136         module = importlib.import_module(switch_type)
137         return getattr(module, switch_type)()