added yang file describing openflowplugin-impl as CoSS module
[openflowplugin.git] / test-scripts / openvswitch / testclass_components.py
1 import logging
2 import re
3 import requests
4 import time
5 from xml.etree import ElementTree as ET
6
7 from openvswitch.flow_tools import TO_GET, OPERATIONAL_DELAY, FLOWS_PER_SECOND
8
9
10 class GetFlowsComponent():
11
12     log = logging.getLogger('GetFlowsComponent')
13
14     @staticmethod
15     def get_flows(host, port, store='config'):
16             url = 'http://%s:%d/restconf/%s/opendaylight-inventory:nodes' \
17                 '/node/openflow:1/table/2/' % (host, port, store)
18             GetFlowsComponent.log.debug('checking flows in controller - sending request to url: {0}'.format(url))
19             response = requests.get(url, auth=('admin', 'admin'),
20                                     headers={'Accept': 'application/xml'}, timeout=TO_GET)
21             return response
22
23 class CheckSwitchDump():
24
25     log = logging.getLogger('CheckSwitchDump')
26
27     def get_id_by_entry(self, dump_flow, id_map):
28         flow_id = None
29         
30         try:
31            cookie_regexp = re.compile("cookie=0x[0-9,a-f,A-F]+")
32            cookie_match = re.search(cookie_regexp, dump_flow)
33            if cookie_match is not None:
34                cookie_id = cookie_match.group(0)[7:-1]
35                flow_id = id_map[cookie_id]
36            else:
37                CheckSwitchDump.log.info('skipping parsing dump entry: {0} '.format(dump_flow))
38
39         except KeyError as e:
40            CheckSwitchDump.log.error('cookie: {0} is not contained in stored flows'.format(cookie_id))
41         except StandardError as e:
42            CheckSwitchDump.log.error('problem getting stored flow flow_id from cookie from flow dump:{0} reason:{1}'.format(dump_flow, str(e)))
43
44         return flow_id
45
46             
47 class CheckConfigFlowsComponent(): 
48
49     log = logging.getLogger('CheckConfigFlowsComponent')
50
51     def check_config_flows(self, host, port, id_map):
52         try:
53             # check config
54             response = GetFlowsComponent.get_flows(host, port)
55             assert response.status_code == 200, 'response from config must be 200, is {0}'.format(response.status_code)
56             tree = ET.ElementTree(ET.fromstring(response.text))
57
58             return GetXMLFlowsComponent.get_xml_flows_by_map(tree.getroot(), id_map)
59         except StandardError as e:
60             CheckConfigFlowsComponent.log.error('problem getting flows from config: {0}'.format(str(e)))
61             return -1
62
63   
64 class CheckOperFlowsComponent():            
65
66     log = logging.getLogger('CheckOperFlowsComponent')
67
68     def check_oper_flows(self, host, port, id_map, wait_time=OPERATIONAL_DELAY):
69         time.sleep(wait_time)
70
71         try:
72             response = GetFlowsComponent.get_flows(host, port, 'operational')
73             assert response.status_code == 200, 'response from config must be 200, is {0}'.format(response.status_code)
74             CheckOperFlowsComponent.log.debug('got resposnse: {0}'.format(response.status_code))
75             CheckOperFlowsComponent.log.debug('operational dump:\n{0}'.format(response.text))
76
77             tree = ET.ElementTree(ET.fromstring(response.text))
78
79             # fliter id's
80             return GetXMLFlowsComponent.get_xml_flows_by_map(tree.getroot(), id_map)
81         except StandardError as e:
82             CheckOperFlowsComponent.log.error('problem getting flows from operational: {0}'.format(str(e)))
83             return -1
84
85     def check_oper_flows_loop(self, host, port, id_map, max_tries=2):
86         # determine how much time we will wait for flows to get on switch
87         target_oper_flows = len(id_map)
88         current_try = 0
89         current_oper_flows = 0
90
91         wait_for_flows = (target_oper_flows / FLOWS_PER_SECOND) + OPERATIONAL_DELAY
92
93         #check operational - in loop for determined number of tries
94         while current_oper_flows < target_oper_flows and max_tries > current_try:
95             CheckOperFlowsComponent.log.info('checking operational... waiting {0} seconds, {1}/{2}'.format(wait_for_flows, current_try + 1, max_tries))
96             current_oper_flows = self.check_oper_flows(host, port, id_map, wait_for_flows)
97             CheckOperFlowsComponent.log.info('got {0} flows on {1}. try'.format(current_oper_flows, current_try + 1))
98             current_try += 1
99         return current_oper_flows
100         
101 class GetXMLFlowsComponent():
102
103     @staticmethod
104     def get_xml_flows_by_map(xml_tree, id_map, namespace='{urn:opendaylight:flow:inventory}'):
105         element_count = 0
106
107         for e in xml_tree.findall(namespace + 'flow'):
108             xml_id = int(e.find(namespace + 'id').text)
109
110             if xml_id in id_map.values():
111                 element_count += 1
112
113         return element_count