2874db9a1dbde5900953ffedf2aa5d58f9333331
[integration/test.git] / tools / CSIT_Test / base / modules / forwarding_rule_manager.py
1 """
2 CSIT test tools.
3 Authors: Baohua Yang@IBM, Denghui Huang@IBM
4 Updated: 2013-11-05
5 """
6
7 import sys
8
9 sys.path.append('..')
10 from restlib import *  # noqa
11 from testmodule import TestModule
12
13 sys.path.remove('..')
14
15
16 class ForwardingRuleManager(TestModule):
17     """
18     Test for the forwarding rule manager.
19     Start 2-layer tree topology network. e.g., in Mininet, run
20         'sudo mn --controller=remote,ip=127.0.0.1 --mac --topo tree,2'
21     """
22
23     def __init__(self, restSubContext='/controller/nb/v2/flowprogrammer', user=DEFAULT_USER, password=DEFAULT_PWD,
24                  container=DEFAULT_CONTAINER, contentType='json', prefix=DEFAULT_PREFIX):
25         super(self.__class__, self).__init__(restSubContext, user, password, container, contentType, prefix)
26
27     def get_flows(self):
28         """
29         The name is suggested to match the NB API.
30         Show the flows
31         """
32         return super(self.__class__, self).get_entries('')
33
34     def add_flow_to_node(self, node_type, node_id, name, body):
35         suffix = 'node/' + node_type + '/' + node_id + '/staticFlow'
36         super(self.__class__, self).add_entry(suffix, name, body)
37
38     def remove_flow_from_node(self, node_type, node_id, name):
39         suffix = 'node/' + node_type + '/' + node_id + '/staticFlow'
40         super(self.__class__, self).remove_entry(suffix, name)
41
42     def test_flow_operations(self, node_type, node_id, name, body):
43         """
44         Test the add,remove,show actions on flows.
45         >>> body = {'installInHw':'true','name':'flow1','node':{'id':'00:00:00:00:00:00:00:02','type':'OF'},
46                 'priority':'1','etherType':'0x800','nwDst':'10.0.0.1/32','actions':['OUTPUT=1']}
47         >>> ForwardingRuleManager().test_flow_operations('OF','00:00:00:00:00:00:00:02','flow1',body)
48         True
49         >>> body = {'installInHw':'true','name':'flow2','node':{'id':'00:00:00:00:00:00:00:02','type':'OF'},
50                 'priority':'1','etherType':'0x800','nwDst':'10.0.0.2/32','actions':['OUTPUT=2']}
51         >>> ForwardingRuleManager().test_flow_operations('OF','00:00:00:00:00:00:00:02','flow2',body)
52         True
53         """
54         result = []
55         # current flow table should be empty.
56         r = self.get_flows()
57         result.append(body not in r['flowConfig'])
58         # Add a flow
59         self.add_flow_to_node(node_type, node_id, name, body)
60         r = self.get_flows()
61         result.append(body in r['flowConfig'])
62         # Remove the flow and test if succeed
63         if result == [True, True]:
64             self.remove_flow_from_node(node_type, node_id, name)
65             r = self.get_flows()
66             result.append(body not in r['flowConfig'])
67         return result == [True, True, True]