This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionConfGenWF.py
219 lines (188 loc) · 7.93 KB
/
ionConfGenWF.py
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
#!/usr/bin/python2.7
# This is a ion config file auto-gen script. To use this script, you should
# edit a config file, add ion ip address and connection matrix just like the
# example config file written.
#
# Author: chengzhycn
# Email: [email protected]
# Date: 2016-04-12
import os
import optparse
import configparser
import csv
def gen_ipnadmin(rc_file, config):
with open(rc_file, 'a') as f:
f.write('## begin ipnadmin\n')
for key, value in config.items():
f.write('a plan %s ltp/%s\n' % (key, key))
f.write('## end ipnadmin\n')
f.write('\n')
def gen_cfdpadmin(rc_file):
with open(rc_file, 'a') as f:
f.write('## begin cfdpadmin\n')
f.write('1\n')
f.write('e 1\n')
f.write('m discard 0\n')
f.write('m requirecrc 1\n')
f.write('m segsize 32768\n')
f.write("s 'bputa'\n")
f.write('## end cfdpadmin\n')
f.write('\n')
def gen_bpadmin(rc_file, host, config):
with open(rc_file, 'a') as f:
f.write('## begin bpadmin' + '\n' + '1\n')
f.write("a scheme ipn 'ipnfw' 'ipnadminep'\n")
f.write('a endpoint ipn:%s.1 q\n' % host)
f.write('a endpoint ipn:%s.2 q\n' % host)
f.write('a protocol ltp 1400 100\n')
f.write('a induct ltp %s ltpcli\n' % host)
for key, value in config.items():
f.write('a outduct ltp %s ltpclo\n' % key)
f.write('s' + '\n' + '## end bpadmin\n')
f.write('\n')
def gen_ltpadmin(rc_file, host, config):
with open(rc_file, 'a') as f:
f.write('## begin ltpadmin\n')
f.write('1 100\n')
for key, value in config.items():
f.write("a span %s 100 100 1300 1400 1 'udplso %s:1113 1000000'\n" % (key, value))
f.write("s 'udplsi " + config.get(str(host)) + ":1113'\n")
f.write("## end ltpadmin\n")
f.write("\n")
def gen_ionadmin(rc_file, matrix, row, column):
host = matrix[row][column]
with open(rc_file, 'a') as f:
f.write('## begin ionadmin\n')
f.write("1 %s '/root/test/dtn.ionconfig'\n" % host)
f.write('s\n')
f.write('m horizon +0\n')
for i in matrix:
for j in i:
f.write('a contact +1 +86400 %s %s 1000000\n' % (j, j))
f.write('\n')
for i in matrix:
for j in i:
f.write('a range +1 +86400 %s %s 0\n' % (j, j))
f.write('\n')
meo = matrix[row][column] // 10
f.write('a contact +1 +86400 %s %s 1000000\n' % (meo, meo))
f.write('a range +1 +86400 %s %s 0\n' % (meo, meo))
dfs_print(matrix[row][column], meo, f)
found_node = []
# dfs_find(matrix, row, column, found_node, 3, f)
grey_node = {}
grey_node[matrix[row][column]] = (row, column)
bfs_find(matrix, found_node, grey_node, 3, f)
f.write('m production 1000000\n')
f.write('m consumption 1000000\n')
f.write('## end ionadmin\n')
f.write('\n')
def gen_file(rc_file, config):
if os.path.exists(rc_file):
print ("Warning: %s has existed and will be rewritten!!!" % rc_file)
opt = raw_input("To be continue?(y/n)")
if opt == 'n':
os._exit(0)
with open(rc_file, 'w') as f:
f.write('# ION CONFIGURATION\n')
for key, value in config.items():
f.write('# host%s %s\n' % (key, value))
f.write('\n')
def load_config(config, parser):
for name, value in parser.items('config'):
config[name] = value
return config
def sort_items(config):
return sorted(config.items(), key = lambda d:d[0], reverse = False)
def matrix_process(matrix_path):
"""
matrix = matrix.strip().split(',')
array = []
for item in matrix:
item = item.strip().split(' ')
array.append(map(int, item))
return array
"""
try:
with open(matrix_path, 'rt') as fin:
cin = csv.reader(fin)
matrix = [map(int, row) for row in cin]
except Exception as e:
print ("ERROR: cannot locate matrix file: %s" % e)
return matrix
def bfs_find(matrix, found_node, grey_node, deepth, f):
if deepth != 0:
grey_node_copy = grey_node.copy()
for key, value in grey_node_copy.items():
found_node.append(key)
row = value[0]
column = value[1]
if column != 0 and found_node.count(matrix[row][column-1]) == 0:
dfs_print(matrix[row][column], matrix[row][column-1], f)
if matrix[row][column-1] not in grey_node:
grey_node[matrix[row][column-1]] = (row, column-1)
if column != len(matrix[row])-1 and found_node.count(matrix[row][column+1]) == 0:
dfs_print(matrix[row][column], matrix[row][column+1], f)
if matrix[row][column+1] not in grey_node:
grey_node[matrix[row][column+1]] = (row, column+1)
if found_node.count(matrix[(row-1)%len(matrix)][column]) == 0:
dfs_print(matrix[row][column], matrix[(row-1)%len(matrix)][column], f)
if matrix[(row-1)%len(matrix)][column] not in grey_node:
grey_node[matrix[(row-1)%len(matrix)][column]] = ((row-1)%len(matrix), column)
if found_node.count(matrix[(row+1)%len(matrix)][column]) == 0:
dfs_print(matrix[row][column], matrix[(row+1)%len(matrix)][column], f)
if matrix[(row+1)%len(matrix)][column] not in grey_node:
grey_node[matrix[(row+1)%len(matrix)][column]] = ((row+1)%len(matrix), column)
grey_node.pop(key)
bfs_find(matrix, found_node, grey_node, deepth-1, f)
def dfs_find(matrix, row, column, found_node, deepth, f):
if found_node.count(matrix[row][column]) != 0:
return -1;
if deepth == 0:
return matrix[row][column]
found_node.append(matrix[row][column])
if column != 0:
matrix_l = dfs_find(matrix, row, column-1, found_node, deepth-1, f)
dfs_print(matrix[row][column], matrix_l, f)
if column != len(matrix[row])-1:
matrix_r = dfs_find(matrix, row, column+1, found_node, deepth-1, f)
dfs_print(matrix[row][column], matrix_r, f)
matrix_u = dfs_find(matrix, (row+1)%len(matrix), column, found_node, deepth-1, f)
dfs_print(matrix[row][column], matrix_u, f)
matrix_d = dfs_find(matrix, (row-1)%len(matrix), column, found_node, deepth-1, f)
dfs_print(matrix[row][column], matrix_d, f)
return matrix[row][column]
def dfs_print(node_1, node_2, f):
if node_2 >= 0:
f.write('a contact +1 +86400 %d %d 1000000\n' % (node_1, node_2))
f.write('a contact +1 +86400 %d %d 1000000\n' % (node_2, node_1))
f.write('a range +1 +86400 %d %d 1\n' % (node_1, node_2))
f.write('\n')
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option("-c", "--config", dest="config",
help="config files")
parser.add_option("-m", "--matrix", dest="matrix",
help="connection matrix, with file format in csv")
#parser.add_option("-p", "--protocol", dest="protocol",
# help="udp/ltp")
(options, args) = parser.parse_args()
config_path = options.config
matrix_path = options.matrix
#protocol = options.protocol
read_config = configparser.ConfigParser(delimiters='=')
read_config.read(config_path)
config = {}
config = load_config(config, read_config)
num = len(config)
sort_config = sort_items(config)
matrix = matrix_process(matrix_path)
for row in range(len(matrix)):
for column in range(len(matrix[row])):
filename = 'host%s.rc' % matrix[row][column]
gen_file(filename, config)
gen_ionadmin(filename, matrix, row, column)
gen_ltpadmin(filename, matrix[row][column], config)
gen_bpadmin(filename, matrix[row][column], config)
gen_cfdpadmin(filename)
gen_ipnadmin(filename, config)