blob: bf9a77b63300e29a7e54f848d18a23162ec5b706 [file] [log] [blame]
Jon Hall3b489db2015-10-05 14:38:37 -07001#!/usr/bin/env python
2'''
3 Generate the partitions json file from the $OC* environment variables
4
5 Usage: onos-gen-partitions [output file]
6 If output file is not provided, the json is written to stdout.
7'''
8
9from os import environ
10from collections import deque, OrderedDict
11import re
12import json
13import sys
14
15convert = lambda text: int(text) if text.isdigit() else text.lower()
16alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
17
18def get_OC_vars():
19 vars = []
20 for var in environ:
21 if re.match(r"OC[0-9]+", var):
22 vars.append(var)
23 return sorted(vars, key=alphanum_key)
24
25def get_nodes(vars, port=9876):
26 node = lambda k: { 'id': k, 'ip': k, 'tcpPort': port }
27 return [ node(environ[v]) for v in vars ]
28
29def generate_permutations(nodes, k):
30 l = deque(nodes)
31 perms = {}
32 for i in range(1, len(nodes)+1):
33 perms['p%d' % i] = list(l)[:k]
34 l.rotate(-1)
35 return OrderedDict(sorted(perms.iteritems(), key=lambda (k, v): alphanum_key(k)))
36
37def generate_permutations2(nodes, k):
38 l = deque(nodes)
39 perms = {}
40 for i in range(1, (len(nodes) + 1) / 2 + 1):
41 perms['p%d' % i] = list(l)[:k]
42 l.rotate(-2)
43 return OrderedDict(sorted(perms.iteritems(), key=lambda (k, v): alphanum_key(k)))
44
45
46if __name__ == '__main__':
47 vars = get_OC_vars()
48 nodes = get_nodes(vars)
49 partitions = generate_permutations2(nodes, 3)
50 data = {
51 'nodes': nodes,
52 'partitions': partitions
53 }
54 output = json.dumps(data, indent=4)
55
56 if len(sys.argv) == 2:
57 filename = sys.argv[1]
58 with open(filename, 'w') as f:
59 f.write(output)
60 else:
61 print output