Auto-generated patch by python-black
[integration/test.git] / tools / odl-mdsal-clustering-tests / clustering-performance-test / flow_config_blaster_bulk.py
1 #!/usr/bin/python
2 import time
3
4 import flow_config_blaster
5 import json
6
7
8 class FlowConfigBulkBlaster(flow_config_blaster.FlowConfigBlaster):
9     """
10     Reusing FlowConfigBlaster class providing flow operations based on bulk processing
11     """
12
13     FLW_ADD_RPC_URL = "restconf/operations/sal-bulk-flow:add-flows-rpc"
14     FLW_REMOVE_RPC_URL = "restconf/operations/sal-bulk-flow:remove-flows-rpc"
15     FLW_ADD_DS_URL = "restconf/operations/sal-bulk-flow:add-flows-ds"
16     FLW_REMOVE_DS_URL = "restconf/operations/sal-bulk-flow:remove-flows-ds"
17
18     def __init__(self, *args, **kwargs):
19         super(FlowConfigBulkBlaster, self).__init__(*args, **kwargs)
20         self.bulk_type = "RPC"
21
22     def update_post_url_template(self, action):
23         """
24         Update url templates (defined in parent class) in order to point to bulk API rpcs.
25         :param action: user intention (currently only 'ADD' is supported)
26         """
27         if self.bulk_type == "RPC":
28             self.post_url_template = "http://%s:" + self.port + "/"
29             if action == "ADD":
30                 self.post_url_template += self.FLW_ADD_RPC_URL
31             elif action == "REMOVE":
32                 self.post_url_template += self.FLW_REMOVE_RPC_URL
33         elif self.bulk_type == "DS":
34             self.post_url_template = "http://%s:" + self.port + "/"
35             if action == "ADD":
36                 self.post_url_template += self.FLW_ADD_DS_URL
37             elif action == "REMOVE":
38                 self.post_url_template += self.FLW_REMOVE_DS_URL
39
40     def assemble_post_url(self, host, node):
41         """
42         Format url to final form using substitutions. Here the node is ignored.
43         :param host: controller address
44         :param node: node identifier
45         :return: finalized url
46         """
47         return self.post_url_template % host
48
49     def create_flow_from_template(self, flow_id, ipaddr, node_id):
50         """
51         Create one flow beased on template and given values
52         :param flow_id: flow identifier
53         :param ipaddr: part of flow match
54         :param node_id: node identifier
55         :return: flow structure ready to use
56         """
57         # python 2.7 specific syntax (super)
58         flow = super(FlowConfigBulkBlaster, self).create_flow_from_template(
59             flow_id, ipaddr, node_id
60         )
61         flow_id = flow["id"]
62         del flow["id"]
63         if self.bulk_type == "DS":
64             flow["flow-id"] = flow_id
65         flow["node"] = (
66             "/opendaylight-inventory:nodes/opendaylight-inventory"
67             ':node[opendaylight-inventory:id="openflow:{}"]'.format(node_id)
68         )
69         return flow
70
71     def convert_to_json(self, flow_list, node_id=None):
72         """
73         Converts given list of flows into string of json form.
74         :param flow_list: list of flows to convert
75         :param node_id: identifier of corresponding node
76         :return: json string
77         """
78         json_input = None
79         if self.bulk_type == "RPC":
80             json_input = {"input": {"bulk-flow-item": flow_list}}
81         elif self.bulk_type == "DS":
82             json_input = {"input": {"bulk-flow-ds-item": flow_list}}
83
84         flow_data = json.dumps(json_input)
85         return flow_data
86
87
88 if __name__ == "__main__":
89     ############################################################################
90     # This program executes the base performance test. The test adds flows into
91     # the controller's config space. This function is basically the CLI frontend
92     # to the FlowConfigBlaster class and drives its main functions: adding and
93     # deleting flows from the controller's config data store
94     ############################################################################
95     parser = flow_config_blaster.create_arguments_parser()
96     parser.add_argument(
97         "--bulk-type",
98         default="RPC",
99         dest="bulk_type",
100         choices=["RPC", "DS"],
101         help="Bulk type to use: RPC, DS (default is RPC)",
102     )
103
104     in_args = parser.parse_args()
105
106     if in_args.file != "":
107         flow_template = flow_config_blaster.get_json_from_file(in_args.file)
108     else:
109         flow_template = None
110
111     fcbb = FlowConfigBulkBlaster(
112         in_args.host,
113         in_args.port,
114         in_args.cycles,
115         in_args.threads,
116         in_args.fpr,
117         in_args.nodes,
118         in_args.flows,
119         in_args.startflow,
120         in_args.auth,
121     )
122     fcbb.bulk_type = in_args.bulk_type
123     fcbb.update_post_url_template("ADD")
124
125     # Run through <cycles>, where <threads> are started in each cycle and
126     # <flows> are added from each thread
127     fcbb.add_blaster()
128
129     print("\n*** Total flows added: %s" % fcbb.get_ok_flows())
130     print("    HTTP[OK] results:  %d\n" % fcbb.get_ok_rqsts())
131
132     if in_args.delay > 0:
133         print(
134             "*** Waiting for %d seconds before the delete cycle ***\n" % in_args.delay
135         )
136         time.sleep(in_args.delay)
137
138     # Run through <cycles>, where <threads> are started in each cycle and
139     # <flows> previously added in an add cycle are deleted in each thread
140     if in_args.delete:
141         fcbb.delete_blaster()
142         print("\n*** Total flows deleted: %s" % fcbb.get_ok_flows())
143         print("    HTTP[OK] results:    %d\n" % fcbb.get_ok_rqsts())