blob: 7f3a865efb9d42acfc38b32c6ac990e91744e495 [file] [log] [blame]
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -08001#!/usr/bin/env python
2"""
Brian O'Connor6e192432015-02-26 15:17:23 -08003 Generate the partitions json file from the $OC* environment variables
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -08004
Brian O'Connor6e192432015-02-26 15:17:23 -08005 Usage: onos-gen-partitions [output file]
6 If output file is not provided, the json is written to stdout.
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -08007"""
Brian O'Connor6e192432015-02-26 15:17:23 -08008
9from os import environ
10from collections import deque, OrderedDict
11import re
12import json
13import sys
Thomas Vachuskab56b9172015-11-24 11:24:23 -080014import hashlib
Brian O'Connor6e192432015-02-26 15:17:23 -080015
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080016convert = lambda text: int(text) if text.isdigit() else text.lower()
17alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
Brian O'Connor6e192432015-02-26 15:17:23 -080018
19def get_OC_vars():
20 vars = []
21 for var in environ:
22 if re.match(r"OC[0-9]+", var):
23 vars.append(var)
24 return sorted(vars, key=alphanum_key)
25
Madan Jampani27b69c62015-05-15 15:49:02 -070026def get_nodes(vars, port=9876):
Madan Jampaniec1df022015-10-13 21:23:03 -070027 node = lambda k: { 'id': k, 'ip': k, 'port': port }
Brian O'Connor6e192432015-02-26 15:17:23 -080028 return [ node(environ[v]) for v in vars ]
29
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080030def generate_base_partition(nodes):
31 return {
32 'id': 0,
33 'members': nodes
34 }
35
36def generate_extended_partitions(nodes, k):
Brian O'Connor6e192432015-02-26 15:17:23 -080037 l = deque(nodes)
Madan Jampaniec1df022015-10-13 21:23:03 -070038 perms = []
Brian O'Connor6e192432015-02-26 15:17:23 -080039 for i in range(1, len(nodes)+1):
Madan Jampaniec1df022015-10-13 21:23:03 -070040 part = {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080041 'id': i,
Madan Jampaniec1df022015-10-13 21:23:03 -070042 'members': list(l)[:k]
43 }
44 perms.append(part)
Brian O'Connor6e192432015-02-26 15:17:23 -080045 l.rotate(-1)
Madan Jampaniec1df022015-10-13 21:23:03 -070046 return perms
Brian O'Connor6e192432015-02-26 15:17:23 -080047
48if __name__ == '__main__':
49 vars = get_OC_vars()
50 nodes = get_nodes(vars)
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080051 base_partition = generate_base_partition([v.get('id') for v in nodes])
52 extended_partitions = generate_extended_partitions([v.get('id') for v in nodes], 3)
53 partitions = []
54 partitions.append(base_partition)
55 partitions.extend(extended_partitions)
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080056 name = 0
57 for node in nodes:
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080058 name = name ^ hash(node['ip'])
Madan Jampaniec1df022015-10-13 21:23:03 -070059 data = {
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080060 'name': name,
Brian O'Connor6e192432015-02-26 15:17:23 -080061 'nodes': nodes,
Aaron Kruglikoveb0ae4e2015-11-10 19:16:16 -080062 'partitions': partitions
Brian O'Connor6e192432015-02-26 15:17:23 -080063 }
64 output = json.dumps(data, indent=4)
65
66 if len(sys.argv) == 2:
67 filename = sys.argv[1]
68 with open(filename, 'w') as f:
69 f.write(output)
70 else:
71 print output