summaryrefslogtreecommitdiffstats
path: root/verigraph/service/src/tests/j-verigraph-generator/utility.py
blob: 47d018028ecb358564a71ebd72d4f19c6e81a7a5 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/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
##############################################################################

import json
import contextlib
import sys
import os
import subprocess
from pprint import pprint

#manages output easily (can either write to file or to stdout)
@contextlib.contextmanager
def smart_open(filename=None):
    if filename and filename != '-':
        fh = open(filename, 'w')
    else:
        fh = sys.stdout
    try:
        yield fh
    finally:
        if fh is not sys.stdout:
            fh.close()

def check_input_is_int(text):
    while True:
        data = raw_input(text)
        try:
            int_value = int(data)
        except ValueError:
            print "Please enter a valid number!"
            continue
        return int_value

#parses a json file into a unicode dictionary
def parse_json_file(filename):
    with open(filename) as json_file:
        return json.load(json_file)

#returns an ascii dictionary from a unicode one
def convert_unicode_to_ascii(input):
    if isinstance(input, dict):
        return {convert_unicode_to_ascii(key): convert_unicode_to_ascii(value) for key, value in input.iteritems()}
    elif isinstance(input, list):
        return [convert_unicode_to_ascii(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input

#parses a chains file
def parse_chains(chains_file):
    chains_json = convert_unicode_to_ascii(parse_json_file(chains_file))

    chains = {}

    for chn in chains_json["chains"]:
        try:
            chains[chn["id"]] = {}
            #initiatlize the config dictionary for each node
            for node in chn["nodes"]:
                chains[chn["id"]][node["name"]] = {}
        except:
            raise KeyError("Chains file is not valid!")

    for chn in chains_json["chains"]:
        try:
            #set chn values ---> chn(name, (field, value))
            for node in chn["nodes"]:
                for key, value in node.items():
                    #name key is redundant in map
                    if key != "name":
                        chains[chn["id"]][node["name"]][key] = value
        except:
            raise KeyError("Chains file is not valid!")
    return chains

def check_chains_integrity(filename):
    print "Checking input file..."
    try:
        chains = convert_unicode_to_ascii(parse_json_file(filename))
        print "File correctly parsed"
        if isinstance(chains["chains"], list) == False:
            print "Child of chains is not a list!"
            return False
        for chain in chains["chains"]:
            print "Chain found, checking its fields..."
            print "Checking chain id field... "
            chain["id"]
            print "OK!"
            print "Checking chain flowspace field... "
            chain["flowspace"]
            print "OK!"
            if isinstance(chain["nodes"], list) == False:
                print "Chain #" + chain["id"] + " does not have a list of nodes!"
                return False
            for node in chain["nodes"]:
                print "Node found, checking its fields..."
                print "Checking node name... "
                node["name"]
                print "OK!"
                print "Checking node functional_type field... "
                node["functional_type"]
                print "OK!"
                print "Checking node address field... "
                node["address"]
                print "OK!"
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print "One or more required fields are missing!"
        return False
    print filename + " validated successfully!"
    return True

def check_config_integrity(filename):
    print "Checking input file..."
    try:
        config = convert_unicode_to_ascii(parse_json_file(filename))
        pprint(config)
        print "File correctly parsed"
        if isinstance(config["nodes"], list) == False:
            print "Child of nodes is not a list!"
            return False
        for node in config["nodes"]:
            print "Node found, checking its fields..."
            print "Checking id field... "
            node["id"]
            print "OK!"
            print "Checking description field... "
            node["description"]
            print "OK!"
            print "Checking configuration field... "
            node["configuration"]
            print "OK!"
            if isinstance(node["configuration"], list) == False:
                print "Checking if node configuration is a list..."
                print "Node with id " + node["id"] + " does not have a configuration list!"
                return False
            for c in node["configuration"]:
                print "Checking if node configuration element is a string or a dictionary..."
                if (isinstance(c, str) == False and isinstance(c, dict) == False):
                    print "At least one element of node with id " + node["id"] + " has an invalid configuration (it is neither a string or a map)"
                    return False
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print "One or more required fields are missing!"
        return False
    print filename + " validated successfully!"
    return True

def check_routing_integrity(filename):
    print "Checking input file..."
    try:
        routing = convert_unicode_to_ascii(parse_json_file(filename))
        print "File correctly parsed"
        if isinstance(routing["routing_table"], list) == False:
            print "Child of routing_table is not a list!"
            return False
        for node in routing["routing_table"]:
            if isinstance(node, dict) == False:
                print "Child of routing_table is not a map!"
                return False
            for n, rt in node.items():
                if isinstance(rt, list) == False:
                    print "Routing table of element " + n + " is not a list!"
                    return False
                for entry in rt:
                    if isinstance(entry, dict) == False:
                        print "Invalid entry for node " + n + " (not a map)!"
                        return False
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        print "One or more required fields are missing!"
        return False
    return True

#prints every node for each input chain
def print_chains(chains):
    for chain in chains["chains"]:
        print "CHAIN #" + str(chain["id"])
        for node in chain["nodes"]:
            print "Name: " + str(node["name"])
            print "Functional type: " + str(node["functional_type"])
            print "Address: " + str(node["address"])
            print "-----------------------------------"
        print ""

#prints every node's configuration
def print_configuration(configuration):
    print "NODES CONFIGURATION"
    for node in configuration["nodes"]:
        print "Name: " + str(node["id"])
        print "Description: " + str(node["description"])
        print "Configuration: "
        pprint(node["configuration"])
        print "-----------------------------------"
    print ""

#print every node's routing table
def print_routing_table(routing):
    print "ROUTING"
    for table in routing["routing_table"]:
        for node,rt in table.items():
            print "Name: " + str(node)
            pprint(rt)
            print "-----------------------------------"
        print ""

#returns a list of tuple [(k1, v1), (k2, v2)] from a list of maps like [{k1 : v1},{k2 : v2}]
def formatted_list_from_list_of_maps(maps):
    l = []
    for map in maps:
        if isinstance(map, dict):
            for k, v in map.items():
                #l.append("(ctx." + str(k) + ", ctx." + str(v) + ")")
                l.append(str(k))
                l.append(str(v))
        else:
            #l.append("ctx." + map)
            l.append(map)
    return l

def list_directories(dir):
    #output = subprocess.call(["ls", "-d", "*/"])
    output = subprocess.call(["find", dir, "-type", "d"])

    #TREE VERSION
    #find = subprocess.Popen(["find", ".", "-type", "d"], stdout=subprocess.PIPE)
    #output = subprocess.check_output(["sed", "-e", "s/[^-][^\/]*\//  |/g", "-e", "s/|\([^ ]\)/|-\1/"], stdin=find.stdout)
    #find.wait()

#     ps = subprocess.Popen(('ps', '-A'), stdout=subprocess.PIPE)
#     output = subprocess.check_output(('grep', 'process_name'), stdin=ps.stdout)
#     ps.wait()
    return output

def list_files(dir):
    output = subprocess.call(["find", dir, "-type", "f"])
    return output

def search_node_in_chains(n):
    found = []
    for chain in chains["chains"]:
        for node in chain["nodes"]:
            if node["name"] == n:
                found.append(node)
    return found