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