Step 2: Move test folder to root
[integration/test.git] / tools / odl-mdsal-clustering-tests / clustering-performance-test / flow_config_blaster_fle.py
1 #!/usr/bin/python
2 __author__ = "Jan Medved"
3 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
4 __license__ = "New-style BSD"
5 __email__ = "jmedved@cisco.com"
6
7 from flow_config_blaster import FlowConfigBlaster
8 import argparse
9 import netaddr
10 import time
11 import json
12
13
14 class FlowConfigBlasterFLE(FlowConfigBlaster):
15     """
16     FlowConfigBlaster, Floodlight Edition; Uses the Floodlight Static Flow Entry Pusher REST API to inject flows.
17     """
18     flow = {
19         'switch': "00:00:00:00:00:00:00:01",
20         "name": "flow-mod",
21         "cookie": "0",
22         "priority": "32768",
23         "ether-type": "2048",
24         "dst-ip": "10.0.0.1/32",
25         "active": "true",
26         "actions": "output=flood"
27     }
28
29     def __init__(self, host, port, ncycles, nthreads, nnodes, nflows, startflow):
30         FlowConfigBlaster.__init__(self, host, port, ncycles, nthreads, nnodes, nflows, startflow, False, '')
31
32         # Create the service URL
33         self.url = 'http://' + self.host + ":" + self.port + '/wm/staticflowentrypusher/json'
34
35     def get_num_nodes(self, session):
36         """
37         Determines the number of nodes in the network. Overrides the get_num_nodes method in FlowConfigBlaster.
38         :param session:
39         :return:
40         """
41         url = 'http://' + self.host + ":" + self.port + '/wm/core/controller/switches/json'
42         nodes = self.nnodes
43
44         r = session.get(url, headers=self.getheaders, stream=False)
45
46         if r.status_code == 200:
47             try:
48                 nodes = len(json.loads(r.content))
49             except KeyError:
50                 pass
51
52         return nodes
53
54     def post_flows(self, session, node, flow_id, ipaddr):
55         """
56         Adds a flow. Overrides the add_flow method in FlowConfigBlaster.
57         :param session:
58         :param node:
59         :param flow_id:
60         :param ipaddr:
61         :return:
62         """
63         self.flow['switch'] = "00:00:00:00:00:00:00:%s" % '{0:02x}'.format(node)
64         self.flow['name'] = 'TestFlow-%d' % flow_id
65         self.flow['cookie'] = str(flow_id)
66         self.flow['dst-ip'] = "%s/32" % str(netaddr.IPAddress(ipaddr))
67
68         flow_data = json.dumps(self.flow)
69         # print flow_data
70         # print flow_url
71
72         r = session.post(self.url, data=flow_data, headers=self.putheaders, stream=False)
73         return r.status_code
74
75     def delete_flow(self, session, node, flow_id):
76         """
77         Deletes a flow. Overrides the delete_flow method in FlowConfigBlaster.
78         :param session:
79         :param node:
80         :param flow_id:
81         :return:
82         """
83         f = {'name': 'TestFlow-%d' % flow_id}
84         flow_data = json.dumps(f)
85
86         r = session.delete(self.url, data=flow_data, headers=self.getheaders)
87         return r.status_code
88
89
90 if __name__ == "__main__":
91
92     parser = argparse.ArgumentParser(description='Flow programming performance test for Floodlight: First adds and '
93                                                  'then deletes flows using the Static Flow Entry Pusher REST API.')
94
95     parser.add_argument('--host', default='127.0.0.1',
96                         help='Host where the controller is running (default is 127.0.0.1)')
97     parser.add_argument('--port', default='8080',
98                         help='Port on which the controller\'s RESTCONF is listening (default is 8080)')
99     parser.add_argument('--cycles', type=int, default=1,
100                         help='Number of flow add/delete cycles; default 1. Both Flow Adds and Flow Deletes are '
101                              'performed in cycles. <THREADS> worker threads are started in each cycle and the cycle '
102                              'ends when all threads finish. Another cycle is started when the previous cycle finished.')
103     parser.add_argument('--threads', type=int, default=1,
104                         help='Number of request worker threads to start in each cycle; default=1. '
105                              'Each thread will add/delete <FLOWS> flows.')
106     parser.add_argument('--flows', type=int, default=10,
107                         help='Number of flows that will be added/deleted by each worker thread in each cycle; '
108                              'default 10')
109     parser.add_argument('--nodes', type=int, default=16,
110                         help='Number of nodes if mininet is not connected; default=16. If mininet is connected, '
111                              'flows will be evenly distributed (programmed) into connected nodes.')
112     parser.add_argument('--delay', type=int, default=0,
113                         help='Time (in seconds) to wait between the add and delete cycles; default=0')
114     parser.add_argument('--no-delete', dest='delete', action='store_false',
115                         help='Do not perform the delete cycle.')
116     parser.add_argument('--startflow', type=int, default=0,
117                         help='The starting Flow ID; default=0')
118
119     in_args = parser.parse_args()
120
121     fct = FlowConfigBlasterFLE(in_args.host, in_args.port, in_args.cycles, in_args.threads, in_args.nodes,
122                                in_args.flows, in_args.startflow)
123
124     # Run through <cycles>, where <threads> are started in each cycle and <flows> are added from each thread
125     fct.add_blaster()
126
127     print '\n*** Total flows added: %s' % fct.get_total_flows()
128     print '    HTTP[OK] results:  %d\n' % fct.get_ok_flows()
129
130     if in_args.delay > 0:
131         print '*** Waiting for %d seconds before the delete cycle ***\n' % in_args.delay
132         time.sleep(in_args.delay)
133
134     # Run through <cycles>, where <threads> are started in each cycle and <flows> previously added in an add cycle are
135     # deleted in each thread
136     if in_args.delete:
137         fct.delete_blaster()