Statistics
| Branch: | Revision:

iof-tools / confFileGenerator / confFileGen.py @ 7c83f065

History | View | Annotate | Download (4.72 KB)

1 29b7b024 tiamilani
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
#
17
# Copyright (C) 2019  Mattia Milani <mattia.milani@studenti.unitn.it>
18
19
import getopt
20
from networkx import *
21
import os.path
22
import shutil
23
from Node import Node
24
from Edge import Edge
25 227f2eb7 tiamilani
import constants
26 29b7b024 tiamilani
from constants import *
27
import sys
28
29 cba5eec4 tiamilani
gname = ""
30
outDir = ""
31
directories = False
32 227f2eb7 tiamilani
mrai = True
33 29b7b024 tiamilani
34
options, remainder = getopt.getopt(sys.argv[1:], '', ['graph=',
35 cba5eec4 tiamilani
                                                      'out=',
36
                                                      'nnodes=',
37
                                                      'directories',
38
                                                      'help',
39 227f2eb7 tiamilani
                                                      'h',
40
                                                      'nomrai',
41
                                                      'mraitype='
42 29b7b024 tiamilani
                                                      ])
43 227f2eb7 tiamilani
44 cba5eec4 tiamilani
if set([x[0] for x in options]).issubset({'--help', '-h'}):
45
    print(HELP_MESSAGE)
46
    sys.exit(0)
47
if not {'--graph', '--out'}.issubset(set([x[0] for x in options])):
48
    print(HELP_MESSAGE)
49 29b7b024 tiamilani
    sys.exit(1)
50
for opt, arg in options:
51
    if opt in '--graph':
52
        gname = arg
53
    if opt in '--out':
54
        outDir = arg
55 cba5eec4 tiamilani
    if opt in '--nnodes':
56
        node_number = int(arg)
57
    if opt in '--directories':
58
        directories = True
59 227f2eb7 tiamilani
    if opt in '--nomrai':
60
        mrai = False
61
    if opt in '--mraitype':
62
        constants.mrai_type = int(arg)
63 29b7b024 tiamilani
64
# If the graph file is not present it will be created with a predefined number of nodes
65
if not os.path.isfile(gname):
66
    small_g = internet_as_graph(node_number)
67
    nx.write_graphml(small_g, gname)
68
69
# If the output dire does not exists it will be created
70
if not os.path.isdir(outDir):
71
    os.mkdir(outDir)
72 cba5eec4 tiamilani
else:
73
    shutil.rmtree(outDir)
74
    os.mkdir(outDir)
75 29b7b024 tiamilani
76
# I read the graph
77
graph = read_graphml(gname)
78
79 cba5eec4 tiamilani
if directories:
80
    for node in graph.nodes(data=True):
81
        os.mkdir(outDir + 'h_' + node[0])
82
83 29b7b024 tiamilani
# I read all the nodes and I config the objects
84
nodes_dict = {}
85
for n in graph.nodes(data=True):
86 227f2eb7 tiamilani
    if 'mrai' in n[1] and mrai:
87 cba5eec4 tiamilani
        if directories:
88
            new_node = Node(n[0], n[1]['type'], outDir + '/h_' + n[0] + '/', n[1]['mrai'])
89
        else:
90
            new_node = Node(n[0], n[1]['type'], outDir, n[1]['mrai'])
91 61af9e95 tiamilani
    else:
92 cba5eec4 tiamilani
        if directories:
93
            new_node = Node(n[0], n[1]['type'], outDir + '/h_' + n[0] + '/')
94
        else:
95
            new_node = Node(n[0], n[1]['type'], outDir)
96 29b7b024 tiamilani
    # If the node is of type c it will share some addresses
97
    if n[1][TYPE_KEY] == 'C':
98
        new_node.add_addr_to_export()
99
    nodes_dict[n[0]] = new_node
100
101
# I read all the edges and config the objects
102
edges_dict = {}
103
for edg in graph.edges(data=True):
104 8f9efafa tiamilani
    ipAddrEth1 = ""
105
    ipAddrEth2 = ""
106
107
    if {'ip_eth_n1', 'ip_eth_n2'}.issubset(edg[2]):
108
        ipAddrEth1 = edg[2]['ip_eth_n1']
109
        ipAddrEth2 = edg[2]['ip_eth_n2']
110
111 cba5eec4 tiamilani
    if directories:
112 8f9efafa tiamilani
        new_edge = Edge(nodes_dict[edg[0]], nodes_dict[edg[1]], edg[2]['type'], [ipAddrEth1, ipAddrEth2],
113
                        outDir + '/h_' + nodes_dict[edg[0]].name + '/', outDir + '/h_' + nodes_dict[edg[1]].name + '/')
114 cba5eec4 tiamilani
    else:
115 8f9efafa tiamilani
        new_edge = Edge(nodes_dict[edg[0]], nodes_dict[edg[1]], edg[2]['type'], [ipAddrEth1, ipAddrEth2],
116
                        outDir, outDir)
117 29b7b024 tiamilani
    edges_dict["h_" + str(new_edge.node1.name) + "_h_" + str(new_edge.node2.name)] = new_edge
118
119
# Write the sharing policies
120
for edg in edges_dict:
121
    edges_dict[edg].write_static_exporter()
122
123 7c83f065 marco.nesler
# Write the network config script
124
for _,node in nodes_dict.items():
125
    node.write_network_configuration()
126
127 29b7b024 tiamilani
# Copy the base files to the simulation directory
128 cba5eec4 tiamilani
if not directories:
129
    src_files = os.listdir(src)
130
    for file_name in src_files:
131
        full_file_name = os.path.join(src, file_name)
132
        if os.path.isfile(full_file_name):
133
            shutil.copy(full_file_name, outDir)
134
else:
135
    src_files = os.listdir(src)
136
    for file_name in src_files:
137
        full_file_name = os.path.join(src, file_name)
138
        if os.path.isfile(full_file_name):
139
            for node in graph.nodes(data=True):
140
                out = outDir + 'h_' + node[0]
141
                shutil.copy(full_file_name, out)