Auto-generated patch by python-black
[integration/test.git] / tools / odl-mdsal-clustering-tests / replace_cars.py
1 """
2 The purpose of this script to create enough traffic in config datastore
3 to trigger creation of Snapshot.
4 This script uses PUT http method for handling/replacing "moving segment" of cars.
5 The car data is minimal, containing only an ID (car-<num>).
6 """
7
8
9 import argparse
10 import string
11 import sys
12 import requests
13
14
15 def main():
16     """
17     The main function that does it all.
18
19     TODO: Move argument parsing to a separate function,
20     so allow the main logic to be started programmatically?
21     """
22
23     # Constants
24     car_entry_template = string.Template(
25         """      {
26        "id": "car-$NUM"
27       }"""
28     )
29
30     patch_data_template = string.Template(
31         """{
32  "cars": {
33   "car-entry": [
34 $ENTRIES
35      ]
36     }
37 }"""
38     )
39
40     # Arguments
41     parser = argparse.ArgumentParser(description="Config datastore" "scale test script")
42     parser.add_argument(
43         "--host",
44         default="127.0.0.1",
45         help="Host where odl controller is running." "(default: 127.0.0.1)",
46     )
47     parser.add_argument(
48         "--port",
49         default="8181",
50         help="Port on which odl's RESTCONF is listening" "(default: 8181)",
51     )
52     parser.add_argument(
53         "--start-id",
54         type=int,
55         default=1,
56         help="ID number of the first car. (default:1)",
57     )
58     parser.add_argument(
59         "--segment-size",
60         type=int,
61         default=1,
62         help="Number of cars in segment. (default:1)",
63     )
64     parser.add_argument(
65         "--iterations",
66         type=int,
67         default=1,
68         help="How many times the segment sent. (default:1)",
69     )
70     parser.add_argument(
71         "--move-per-iter",
72         type=int,
73         default=1,
74         help="Each segment has IDs moved by this. (default:1)",
75     )
76     parser.add_argument("--user", help="Restconf user name", default="admin")
77     parser.add_argument("--password", help="Restconf password", default="admin")
78
79     args = parser.parse_args()
80
81     # Logic
82     url = "http://" + args.host + ":" + args.port + "/restconf/config/car:cars"
83     auth = (args.user, args.password)
84     headers = {"Content-Type": "application/json"}
85     session = requests.Session()
86     for iteration in range(args.iterations):
87         entry_list = []
88         for num_entry in range(args.segment_size):
89             num_id = args.start_id + iteration * args.move_per_iter + num_entry
90             entry_list.append(car_entry_template.substitute({"NUM": str(num_id)}))
91         mapping = {"ID": str(iteration), "ENTRIES": ",\n".join(entry_list)}
92         data = patch_data_template.substitute(mapping)
93         response = session.put(url=url, auth=auth, headers=headers, data=data)
94         if response.status_code not in [200, 201, 204]:
95             print("status: {}".format(response.status_code))
96             print("text: {}".format(response.text))
97             sys.exit(1)
98
99
100 if __name__ == "__main__":
101     main()