Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.72 KB)

1
#!/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
import constants
26
from constants import *
27
import sys
28

    
29
gname = ""
30
outDir = ""
31
directories = False
32
mrai = True
33

    
34
options, remainder = getopt.getopt(sys.argv[1:], '', ['graph=',
35
                                                      'out=',
36
                                                      'nnodes=',
37
                                                      'directories',
38
                                                      'help',
39
                                                      'h',
40
                                                      'nomrai',
41
                                                      'mraitype='
42
                                                      ])
43

    
44
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
    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
    if opt in '--nnodes':
56
        node_number = int(arg)
57
    if opt in '--directories':
58
        directories = True
59
    if opt in '--nomrai':
60
        mrai = False
61
    if opt in '--mraitype':
62
        constants.mrai_type = int(arg)
63

    
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
else:
73
    shutil.rmtree(outDir)
74
    os.mkdir(outDir)
75

    
76
# I read the graph
77
graph = read_graphml(gname)
78

    
79
if directories:
80
    for node in graph.nodes(data=True):
81
        os.mkdir(outDir + 'h_' + node[0])
82

    
83
# I read all the nodes and I config the objects
84
nodes_dict = {}
85
for n in graph.nodes(data=True):
86
    if 'mrai' in n[1] and mrai:
87
        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
    else:
92
        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
    # 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
    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
    if directories:
112
        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
    else:
115
        new_edge = Edge(nodes_dict[edg[0]], nodes_dict[edg[1]], edg[2]['type'], [ipAddrEth1, ipAddrEth2],
116
                        outDir, outDir)
117
    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
# Write the network config script
124
for _,node in nodes_dict.items():
125
    node.write_network_configuration()
126

    
127
# Copy the base files to the simulation directory
128
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)