-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_iot.py
254 lines (203 loc) · 8.63 KB
/
sim_iot.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
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
#!/usr/bin/python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.log import lg
from mininet.node import Node
from mininet.topolib import TreeNet
from reg import utils_hosts
from time import sleep
import time
import argparse
import logging
from traceback import print_exc
############## Parse Arguments #####
parser = argparse.ArgumentParser(prog='virtual_device', usage='%(prog)s [options]', description='Virtual Device')
parser.add_argument('-p','--port', type=str, help='Broker Ports',required=True)
parser.add_argument('-d','--direc', type=str, help='Direct data set',required=True)
args = parser.parse_args()
#Path Server and Gateway
gateway_path='/home/openflow/service-mix/'
server_path='/home/openflow/service-mix/server'
#################################
def startNAT( root, inetIntf='eth0', subnet='10.0/8' ):
"""Start NAT/forwarding between Mininet and external network
root: node to access iptables from
inetIntf: interface for internet access
subnet: Mininet subnet (default 10.0/8)="""
# Identificar interface conectada a rede
localIntf = root.defaultIntf()
# Limpar regras ativas
root.cmd( 'iptables -F' )
root.cmd( 'iptables -t nat -F' )
# Criar entradas de trfego padrao sem corresondencia
root.cmd( 'iptables -P INPUT ACCEPT' )
root.cmd( 'iptables -P OUTPUT ACCEPT' )
root.cmd( 'iptables -P FORWARD DROP' )
# Configure NAT
root.cmd( 'iptables -I FORWARD -i', localIntf, '-d', subnet, '-j DROP' )
root.cmd( 'iptables -A FORWARD -i', localIntf, '-s', subnet, '-j ACCEPT' )
root.cmd( 'iptables -A FORWARD -i', inetIntf, '-d', subnet, '-j ACCEPT' )
root.cmd( 'iptables -t nat -A POSTROUTING -o ', inetIntf, '-j MASQUERADE' )
# Encaminhamento kernel
root.cmd( 'sysctl net.ipv4.ip_forward=1' )
def stopNAT( root ):
"""Stop NAT/forwarding between Mininet and external network"""
# Limpar regras ativas
root.cmd( 'iptables -F' )
root.cmd( 'iptables -t nat -F' )
# Indicar o kernel p parar encaminhamento
root.cmd( 'sysctl net.ipv4.ip_forward=0' )
def fixNetworkManager( root, intf ):
"""Prevent network-manager from messing with our interface,
by specifying manual configuration in /etc/network/interfaces
root: a node in the root namespace (for running commands)
intf: interface name"""
cfile = '/etc/network/interfaces'
line = '\niface %s inet manual\n' % intf
config = open( cfile ).read()
if line not in config:
print '*** Adding', line.strip(), 'to', cfile
with open( cfile, 'a' ) as f:
f.write( line )
# Probably need to restart network-manager to be safe -
# hopefully this won't disconnect you
root.cmd( 'service network-manager restart' )
def connectToInternet( network, switch='s1', rootip='10.254', subnet='10.0/8'):
"""Connect the network to the internet
switch: switch to connect to root namespace
rootip: address for interface in root namespace
subnet: Mininet subnet"""
switch = network.get( switch )
prefixLen = subnet.split( '/' )[ 1 ]
# Criando node no namespace raiz
root = Node( 'root', inNamespace=False )
# Interface gerenciador prevencao
fixNetworkManager( root, 'root-eth0' )
# Criando link
link = network.addLink( root, switch )
link.intf1.setIP( rootip, prefixLen )
# Start network
network.start()
# Start NAT
startNAT( root )
# Estabelecendo rotas entre hosts
for host in network.hosts:
host.cmd( 'ip route flush root 0/0' )
host.cmd( 'route add -net', subnet, 'dev', host.defaultIntf() )
host.cmd( 'route add default gw', rootip )
return root
def init_sensors(net):
global args
i=1
#pega todos os devices/sensores
d=utils_hosts.return_hosts_per_type('device')
ass=utils_hosts.return_association()
print ("Init Sensors")
#inciar mosquitto nos devices
#for i in range(0,len(d)):
# net.get(d[i].name).cmd('mosquitto &')
time.sleep(5)
#iniciar devices virtuais
for i in range(0,len(d)):
print('python2.7 virtual_dev.py -n '+ass[i].name+' -s temperatureSensor -p '+args.port+' -i '+ass[i].gateway+' -d '+args.direc+ ' -m ' + d[i].moteid +' > virtual-device &')
term = net.get(d[i].name)
#term.cmd('screen -S virtual-dev')
#term.cmd('screen -r virtual-dev')
term.cmd('cd /home/mininet/FoT-Simulation; python2.7 virtual_dev.py -n '+ass[i].name+' -s temperatureSensor -p '+args.port+' -i '+ass[i].gateway+' -d '+args.direc+ ' -m ' + d[i].moteid +' > virtual-device-'+ d[i].name +'&')
time.sleep(7)
def init_server(net):
print("Init Server")
g=utils_hosts.return_hosts_per_type('server')
for i in range(0,len(g)):
#iniciar kafka e ....
print(g[i].name)
server = net.get(g[i].name)
server.cmd('cd /home/mininet/FoT-Simulation/; python2.7 sc_net.py -n '+g[i].name+' &')
time.sleep(5)
server.cmd('cd /home/mininet/FoT-Simulation/kafka_2.11-1.0.0; bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper-log &')
time.sleep(10)
server.cmd('cd /home/mininet/FoT-Simulation/kafka_2.11-1.0.0; bin/kafka-server-start.sh config/server.properties > kafka-log &')
print('python3 FoT-StreamServer.py -n '+ g[i].name + ' -i ' + g[i].ip + ' -p 9092 > server-log-'+ g[i].name +' &')
#server.cmd('cd /home/mininet/FoT-Simulation/FoTStreamServer/kafkaMqtt; python3 FoT-StreamServer.py -n '+ g[i].name + ' -i '+ g[i].ip +' -p 9092 > server-log-'+ g[i].name +' &')
sleep(5)
def init_gateways(net):
print("Init Gateways")
g=utils_hosts.return_hosts_per_type('gateway')
ass=utils_hosts.return_association_server()
for i in range(0,len(g)):
#iniciar mosquitto se precisar, comentado por padrao
gateway = net.get(g[i].name)
gateway.cmd('mosquitto &')
gateway.cmd('cd /home/mininet/FoT-Simulation; python2.7 sc_net.py -n '+g[i].name+' &')
time.sleep(5)
print('python2.7 FoT-StreamGateway.py -n '+ g[i].name + ' -i ' + ass[i].server +' -p 9092 > gateway-log-'+g[i].name+ ' &')
gateway.cmd('cd /home/mininet/FoT-Simulation; python2.7 FoT-StreamGateway.py -n '+ g[i].name + ' -i ' + ass[i].server +' -p 9092 > gateway-log-'+g[i].name+' &')
sleep(5)
#for i in range(0,len(g)):
#iniciar mosquitto se precisar, comentado por padrao
#net.get(g[i].name).cmd('mosquitto &')
#if((i+1)<10):
#net.get(g[i].name).cmd('cd '+gateway_path+'/0'+str(i+1)+'/apache-servicemix-7.0.1/bin; ./servicemix &')
#else:
#net.get(g[i].name).cmd('cd '+gateway_path+'/'+str(i+1)+'/apache-servicemix-7.0.1/bin; ./start')
#sleep(5)
def stop_gateways(net):
g=utils_hosts.return_hosts_per_type('gateway')
#for i in range(0,len(g)):
#iniciar mosquitto se precisar, comentado por padrao
#net.get(g[i].name).cmd('mosquitto &')
#if((i+1)<10):
#net.get(g[i].name).cmd('cd '+gateway_path+'/0'+str(i+1)+'/apache-servicemix-7.0.1/bin; ./stop &')
#else:
#net.get(g[i].name).cmd('cd '+gateway_path+'/'+str(i+1)+'/apache-servicemix-7.0.1/bin; ./stop &')
#sleep(5)
def stop_servers(net):
print("Stop Servers")
g=utils_hosts.return_hosts_per_type('server')
for i in range(0,len(g)):
#iniciar kafka e ....
print(g[i].name)
server = net.get(g[i].name)
server.cmd('cd /home/mininet/FoT-Simulation/kafka_2.11-1.0.0; bin/kafka-server-stop.sh > kafka-log &')
time.sleep(20)
server.cmd('cd /home/mininet/FoT-Simulation/kafka_2.11-1.0.0; zookeeper-server-stop.sh > zookeeper-log &')
print('python3 FoT-StreamServer.py -n '+ g[i].name + ' -i ' + g[i].ip + ' -p 9092 > server &')
sleep(5)
def init_flow(net):
print ("Temp: Init Flow")
g=utils_hosts.return_hosts_per_type('gateway')
ass=utils_hosts.return_association()
for i in range(0,len(g)):
for j in range(0,len(ass)):
if(g[i].name==ass[j].name_gateway):
print(g[i].name)
print("mosquitto_pub -t 'dev/"+ass[j].name+"' -m 'FLOW INFO temperatureSensor {collect:"+ ass[j].collect +",publish:"+ ass[j].collect +"}'")
net.get(g[i].name).cmd("mosquitto_pub -t 'dev/"+ass[j].name+"' -m 'FLOW INFO temperatureSensor {collect:"+ ass[j].collect +",publish:"+ ass[j].collect +"}'")
time.sleep(0.2)
if __name__ == '__main__':
lg.setLogLevel('info')
logging.basicConfig(filename = 'app.log', level = logging.INFO)
try:
net = Mininet(link=TCLink)
#criar switches, hosts e topologia
import create_topo
create_topo.create(net)
# Configurar e iniciar comunicacao externa
rootnode = connectToInternet( net )
init_server(net)
init_gateways(net)
init_sensors(net)
#init flow eh temporario
init_flow(net)
CLI( net )
# Shut down NAT
stopNAT( rootnode )
stop_gateways(net)
stop_servers(net)
time.sleep(3)
net.stop()
except Exception as inst:
print_exc()
print(inst)
logging.exception(str(inst))