Moving vpn-instance yang from VPNMgr > NeutronVPN
[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        "id": "car-$NUM"
26       }''')
27
28     patch_data_template = string.Template('''{
29  "cars": {
30   "car-entry": [
31 $ENTRIES
32      ]
33     }
34 }''')
35
36     # Arguments
37     parser = argparse.ArgumentParser(description="Config datastore"
38                                                  "scale test script")
39     parser.add_argument("--host", default="127.0.0.1",
40                         help="Host where odl controller is running."
41                              "(default: 127.0.0.1)")
42     parser.add_argument("--port", default="8181",
43                         help="Port on which odl's RESTCONF is listening"
44                              "(default: 8181)")
45     parser.add_argument("--start-id", type=int, default=1,
46                         help="ID number of the first car. (default:1)")
47     parser.add_argument("--segment-size", type=int, default=1,
48                         help="Number of cars in segment. (default:1)")
49     parser.add_argument("--iterations", type=int, default=1,
50                         help="How many times the segment sent. (default:1)")
51     parser.add_argument("--move-per-iter", type=int, default=1,
52                         help="Each segment has IDs moved by this. (default:1)")
53     parser.add_argument("--user", help="Restconf user name", default="admin")
54     parser.add_argument("--password", help="Restconf password", default="admin")
55
56     args = parser.parse_args()
57
58     # Logic
59     url = "http://" + args.host + ':' + args.port + "/restconf/config/car:cars"
60     auth = (args.user, args.password)
61     headers = {"Content-Type": "application/json"}
62     session = requests.Session()
63     for iteration in range(args.iterations):
64         entry_list = []
65         for num_entry in range(args.segment_size):
66             num_id = args.start_id + iteration * args.move_per_iter + num_entry
67             entry_list.append(car_entry_template.substitute({"NUM": str(num_id)}))
68         mapping = {"ID": str(iteration), "ENTRIES": ",\n".join(entry_list)}
69         data = patch_data_template.substitute(mapping)
70         response = session.put(url=url, auth=auth, headers=headers, data=data)
71         if response.status_code not in [200, 201, 204]:
72             print "status: {}".format(response.status_code)
73             print "text: {}".format(response.text)
74             sys.exit(1)
75
76
77 if __name__ == "__main__":
78     main()