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