blob: 99ff449ba68281e34332ac2c24dad4bcaf1b30e1 [file] [log] [blame]
carlosmscabralf40ecd12013-02-01 18:15:58 -02001#!/usr/bin/python
2
3from mininet.topo import Topo
4from mininet.net import Mininet
5from mininet.util import dumpNodeConnections
6from mininet.log import setLogLevel, output, info
7from mininet.cli import CLI
8from mininet.node import CPULimitedCCNHost, CCNHost
9from mininet.link import TCLink
10from conf_parser import extrai_hosts,extrai_routers, extrai_links
11import os.path
12import optparse
13import datetime
14
15def parse_args():
16 usage="""Usage: miniccnx [template_file]
17 If no template_file is given, will try to load template
18 from file miniccnx.conf in the current directory.
19 """
20
21 parser = optparse.OptionParser(usage)
22
23 _, arg = parser.parse_args()
24
25 if len(arg) != 1:
26 arg = ''
27 else:
28 arg = arg[0]
29
30 return arg
31
32
33class CCNTopo(Topo):
34 def __init__(self, conf_arq, **opts):
35 Topo.__init__(self, **opts)
36
37 hosts_conf = extrai_hosts(conf_arq)
38 routers_conf = extrai_routers(conf_arq)
39 links_conf = extrai_links(conf_arq)
40
41 self.isTCLink = False
42
43 for host in hosts_conf:
44 self.addHost(host.name, app=host.app, fib=host.uri_tuples,cpu=.025)
45
46 for router in routers_conf:
47 self.addHost(router.name,fib=router.uri_tuples)
48
49 for link in links_conf:
50 if len(link.linkDict) == 0:
51 self.addLink(link.h1, link.h2)
52 else:
53 self.addLink(link.h1, link.h2, **link.linkDict)
54 self.isTCLink = True
55
56 info('Parse of ' + conf_arq + ' done.\n')
57
58def execute(template_file='miniccnx.conf'):
59 "Create a network based on template_file"
60
61 if template_file == '':
62 template_file='miniccnx.conf'
63
64 if os.path.exists(template_file) == False:
65 info('No template file given and default template file miniccnx.conf not found. Exiting...\n')
66 quit()
67
68 topo = CCNTopo(template_file)
69
70 t = datetime.datetime.now()
71
72 if topo.isTCLink == True:
73 net = Mininet(topo,host=CPULimitedCCNHost,link=TCLink)
74 else:
75 net = Mininet(topo,host=CCNHost)
76
77 t2 = datetime.datetime.now()
78
79 delta = t2 - t
80
81 info('Setup time: ' + str(delta.seconds) + '\n')
82
83 net.start()
84
85 for host in net.hosts:
86 if 'app' in host.params:
87 if host.params['app'] != '_':
88 host.cmd(host.params['app'])
89
90 CLI(net)
91 net.stop()
92
93if __name__ == '__main__':
94
95 template = parse_args()
96 setLogLevel('info')
97 execute(template)