diff options
author | Zhipeng (Howard) Huang <huangzhipeng@huawei.com> | 2017-02-27 13:08:54 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-02-27 13:08:54 +0000 |
commit | 69e33063b3703ae4529b556b63b3c4cc239c3d9a (patch) | |
tree | b30d14715b34f2e4e06928c555c940ddb3365534 /verigraph/service/src/tests/j-verigraph-generator/routing_generator.py | |
parent | 280310852bb5e1c7639dedf7ce268e1e66157a51 (diff) | |
parent | 53d83244c1bf36af86e90ce5fe758a369f73563e (diff) |
Merge "Add verigraph code base"
Diffstat (limited to 'verigraph/service/src/tests/j-verigraph-generator/routing_generator.py')
-rw-r--r-- | verigraph/service/src/tests/j-verigraph-generator/routing_generator.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/verigraph/service/src/tests/j-verigraph-generator/routing_generator.py b/verigraph/service/src/tests/j-verigraph-generator/routing_generator.py new file mode 100644 index 0000000..c8956f2 --- /dev/null +++ b/verigraph/service/src/tests/j-verigraph-generator/routing_generator.py @@ -0,0 +1,80 @@ +#!/usr/bin/python + +############################################################################## +# Copyright (c) 2017 Politecnico di Torino and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from pprint import pprint +import sys, getopt +import os +from utility import * + +# used by test_class_generator +def generate_routing_from_chain(chain): + routing = {} + routing["routing_table"] = {} + + chain = chain["nodes"] + for i in range(0, len(chain)): + routing["routing_table"][chain[i]["name"]] = {} + for j in range(i-1, -1, -1): + routing["routing_table"][chain[i]["name"]][chain[j]["address"]] = chain[i-1]["name"] + for k in range (i+1, len(chain)): + routing["routing_table"][chain[i]["name"]][chain[k]["address"]] = chain[i+1]["name"] + pprint(routing) + return routing + +def generate_routing_from_chains_file(chains_file, chain_number): + routing = {} + routing["routing_table"] = {} + + chains = convert_unicode_to_ascii(parse_json_file(chains_file)) + chain = None + for chn in chains["chains"]: + if chn["id"] == chain_number: + chain = chn["nodes"] + break + if chain == None: + return routing + + for i in range(0, len(chain)): + routing["routing_table"][chain[i]["name"]] = {} + for j in range(i-1, -1, -1): + routing["routing_table"][chain[i]["name"]][chain[j]["address"]] = chain[i-1]["name"] + for k in range (i+1, len(chain)): + routing["routing_table"][chain[i]["name"]][chain[k]["address"]] = chain[i+1]["name"] + pprint(routing) + return routing + +def main(argv): + if len(argv) < 4: + print 'routing_generator.py -c <chains_file> -n <chain_number>' + sys.exit(2) + chains_file = "" + chain_number = "" + try: + opts, args = getopt.getopt(argv,"hc:n:",["chains=","id="]) + except getopt.GetoptError: + print 'routing_generator.py -c <chains_file> -n <chain_number>' + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + print 'routing_generator.py -c <chains_file> -n <chain_number>' + sys.exit() + elif opt in ("-c", "--chains"): + chains_file = arg + elif opt in ("-n", "--id"): + chain_number = arg + + print "Chains file is " + chains_file + print "Chain id is " + chain_number + + return generate_routing_from_chains_file(chains_file, chain_number) + +if __name__ == '__main__': + main(sys.argv[1:])
\ No newline at end of file |