Step 1: Move vm scripts to the right place
[integration/test.git] / 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 *  # 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':
46                     {'id':'00:00:00:00:00:00:00:02','type':'OF'},
47                     'priority':'1','etherType':'0x800','nwDst':'10.0.0.1/32','actions':['OUTPUT=1']}
48         >>> ForwardingRuleManager().test_flow_operations('OF','00:00:00:00:00:00:00:02','flow1',body)
49         True
50         >>> body = {'installInHw':'true','name':'flow2','node':
51                     {'id':'00:00:00:00:00:00:00:02','type':'OF'},
52                     'priority':'1','etherType':'0x800','nwDst':'10.0.0.2/32','actions':['OUTPUT=2']}
53         >>> ForwardingRuleManager().test_flow_operations('OF','00:00:00:00:00:00:00:02','flow2',body)
54         True
55         """
56         result = []
57         # current flow table should be empty.
58         r = self.get_flows()
59         result.append(body not in r['flowConfig'])
60         # Add a flow
61         self.add_flow_to_node(node_type, node_id, name, body)
62         r = self.get_flows()
63         result.append(body in r['flowConfig'])
64         # Remove the flow and test if succeed
65         if result == [True, True]:
66             self.remove_flow_from_node(node_type, node_id, name)
67             r = self.get_flows()
68             result.append(body not in r['flowConfig'])
69         return result == [True, True, True]