-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
191 lines (154 loc) · 6.09 KB
/
sim.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
#!/usr/bin/python
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI
from mininet.log import lg
from mininet.node import Node, RemoteController, Controller, OVSKernelSwitch
from mininet.topolib import TreeNet
import time
import argparse
import utils_hosts
import json
import random
#seed usados 10, 11 e 12
#random.seed(12)
service_mix_path= '/home/openflow/service-mix'
fuseki_path='/home/openflow/fuseki/apache-jena-fuseki-3.11.0'
#################################
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)="""
# Identify the interface connecting to the mininet network
localIntf = root.defaultIntf()
# Flush any currently active rules
root.cmd( 'iptables -F' )
root.cmd( 'iptables -t nat -F' )
# Create default entries for unmatched traffic
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' )
# Instruct the kernel to perform forwarding
root.cmd( 'sysctl net.ipv4.ip_forward=1' )
def stopNAT( root ):
"""Stop NAT/forwarding between Mininet and external network"""
# Flush any currently active rules
root.cmd( 'iptables -F' )
root.cmd( 'iptables -t nat -F' )
# Instruct the kernel to stop forwarding
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 ]
# Create a node in root namespace
root = Node( 'root', inNamespace=False )
# Prevent network-manager from interfering with our interface
fixNetworkManager( root, 'root-eth0' )
# Create link between root NS and switch
link = network.addLink( root, switch )
link.intf1.setIP( rootip, prefixLen )
# Start network that now includes link to root namespace
network.start()
# Start NAT and establish forwarding
startNAT( root )
# Establish routes from end 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_servers(net):
print("Init Servers")
s=utils_hosts.return_hosts_per_type('server')
for i in range(0,len(s)):
net.get(s[i].name).cmd('mosquitto &')
net.get(s[i].name).cmdPrint('cd '+fuseki_path+'; ./fuseki-server &')
time.sleep(15)
def init_gateways(net):
print("Init Gateways")
g=utils_hosts.return_hosts_per_type('gateway')
for i in range(0,len(g)):
print(g[i].name)
net.get(g[i].name).cmdPrint('mosquitto &')
#net.get('h6').cmd('python servico.py &')
time.sleep(5)
def stop_gateways(net):
g=utils_hosts.return_hosts_per_type('gateway')
for i in range(0,len(g)):
if((i+1)<10):
net.get(g[i].name).cmdPrint('cd '+service_mix_path+'/0'+str(i+1)+'/bin; ./stop &')
else:
net.get(g[i].name).cmdPrint('cd '+service_mix_path+'/'+str(i+1)+'/bin; ./stop &')
time.sleep(5)
def init_sensors(net):
#tipos de sensores no arquivo sensors.py, ex: temperatureSensor, soilmoistureSensor, solarradiationSensor, ledActuator
s=utils_hosts.return_hosts_per_type('sensor')
ass=utils_hosts.return_association()
for i in range(0,len(s)):
if((i+1)<10):
net.get(s[i].name).cmdPrint('python main.py --name sc0'+str(i+1)+' --broker '+str(ass[i].gateway)+' &')
else:
net.get(s[i].name).cmdPrint('python main.py --name sc'+str(i+1)+' --broker '+str(ass[i].gateway)+' &')
time.sleep(0.2)
def init_flow(net):
print ("Temp: Init Flow")
g=utils_hosts.return_hosts_per_type('gateway')
ass=utils_hosts.return_association()
#10seg
col=10000
pub=10000
ind=0
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+' com '+ass[j].name)
print("mosquitto_pub -h "+str(ass[j].gateway)+" -t 'dev/"+ass[j].name+"' -m '{\"method\":\"flow\", \"sensor\":\""+ass[j].type+"\", \"time\":{\"collect\":"+str(col)+",\"publish\":"+str(pub)+"}}'")
net.get(g[i].name).cmd("mosquitto_pub -h "+str(ass[j].gateway)+" -t 'dev/"+ass[j].name+"' -m '{\"method\":\"flow\", \"sensor\":\""+ass[j].type+"\", \"time\":{\"collect\":"+str(col)+",\"publish\":"+str(pub)+"}}'")
time.sleep(0.5)
ind+=1
if __name__ == '__main__':
lg.setLogLevel( 'info')
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_gateways(net)
#Iniciar sensores virtuais
init_sensors(net)
#Iniciar fluxo de comunicacao
init_flow(net)
#init_servers(net)
#init_servers(net)
CLI( net )
# Shut down NAT
stopNAT( rootnode )
#stop_gateways(net)
#time.sleep(15)
net.stop()