Add the robot based CSIT tool with the base edition
[integration/test.git] / test / tools / Robot_Tool / libraries / ForwardingRuleManager.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 *
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  'sudo mn --controller=remote,ip=127.0.0.1 --mac --topo tree,2'
20     """
21
22     def __init__(self, restSubContext='/controller/nb/v2/flowprogrammer', user=DEFAULT_USER, password=DEFAULT_PWD,
23                  container=DEFAULT_CONTAINER, contentType='json', prefix=DEFAULT_PREFIX):
24         super(self.__class__, self).__init__(restSubContext, user, password, container, contentType, prefix)
25
26     def get_flows(self):
27         """
28         The name is suggested to match the NB API.
29         Show the flows
30         """
31         return super(self.__class__, self).get_entries('')
32
33     def add_flow_to_node(self, node_type, node_id, name, body):
34         suffix = 'node/' + node_type + '/' + node_id + '/staticFlow'
35         r = super(self.__class__, self).add_entry(suffix, name, body)
36
37     def remove_flow_from_node(self, node_type, node_id, name):
38         suffix = 'node/' + node_type + '/' + node_id + '/staticFlow'
39         r = super(self.__class__, self).remove_entry(suffix, name)
40
41     def test_flow_operations(self, node_type, node_id, name, body):
42         """
43         Test the add,remove,show actions on flows.
44         >>> body = {'installInHw':'true','name':'flow1','node':{'id':'00:00:00:00:00:00:00:02','type':'OF'},'priority':'1','etherType':'0x800','nwDst':'10.0.0.1/32','actions':['OUTPUT=1']}
45         >>> ForwardingRuleManager().test_flow_operations('OF','00:00:00:00:00:00:00:02','flow1',body)
46         True
47         >>> body = {'installInHw':'true','name':'flow2','node':{'id':'00:00:00:00:00:00:00:02','type':'OF'},'priority':'1','etherType':'0x800','nwDst':'10.0.0.2/32','actions':['OUTPUT=2']}
48         >>> ForwardingRuleManager().test_flow_operations('OF','00:00:00:00:00:00:00:02','flow2',body)
49         True
50         """
51         result = []
52         #current flow table should be empty.
53         r = self.get_flows()
54         result.append(body not in r['flowConfig'])
55         #Add a flow
56         self.add_flow_to_node(node_type, node_id, name, body)
57         r = self.get_flows()
58         result.append(body in r['flowConfig'])
59         #Remove the flow and test if succeed
60         if result == [True, True]:
61             self.remove_flow_from_node(node_type, node_id, name)
62             r = self.get_flows()
63             result.append(body not in r['flowConfig'])
64         return result == [True, True, True]