summaryrefslogtreecommitdiffstats
path: root/verigraph/service/src/tests/j-verigraph-generator/routing_generator.py
blob: c8956f2509edb3fd1fd2ca8449c3c748d31cb68c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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:])