carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from mininet.topo import Topo |
| 4 | from mininet.net import Mininet |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 5 | from mininet.log import setLogLevel, output, info |
| 6 | from mininet.cli import CLI |
| 7 | from mininet.node import CPULimitedCCNHost, CCNHost |
| 8 | from mininet.link import TCLink |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 9 | from mininet.conf_parser import parse_hosts,parse_routers, parse_links |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 10 | import os.path |
| 11 | import optparse |
| 12 | import datetime |
| 13 | |
| 14 | def 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 | |
| 32 | class CCNTopo(Topo): |
| 33 | def __init__(self, conf_arq, **opts): |
| 34 | Topo.__init__(self, **opts) |
| 35 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 36 | hosts_conf = parse_hosts(conf_arq) |
| 37 | routers_conf = parse_routers(conf_arq) |
| 38 | links_conf = parse_links(conf_arq) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 39 | |
| 40 | self.isTCLink = False |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 41 | self.isLimited = False |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 42 | |
| 43 | for host in hosts_conf: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 44 | 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) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 47 | |
| 48 | for router in routers_conf: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 49 | if router.cpu != None and self.isLimited != True: |
| 50 | self.isLimited = True |
| 51 | self.addHost(router.name,fib=router.uri_tuples,cpu=router.cpu) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 52 | |
| 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 | |
| 62 | def 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 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 76 | if topo.isTCLink == True and topo.isLimited == True: |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 77 | net = Mininet(topo,host=CPULimitedCCNHost,link=TCLink) |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 78 | 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) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 82 | 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 | |
| 101 | if __name__ == '__main__': |
| 102 | |
| 103 | template = parse_args() |
| 104 | setLogLevel('info') |
| 105 | execute(template) |