blob: c40bfc9d594d65fe11bb344c89a078aca79bbff9 [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
carlosmscabral6d3dd602013-03-23 11:12:34 -030010import os.path, time
carlosmscabralf40ecd12013-02-01 18:15:58 -020011import optparse
12import datetime
13
14def parse_args():
carlosmscabral6d3dd602013-03-23 11:12:34 -030015 usage="""Usage: miniccnx [template_file] [ -t | --testbed ]
16 If no template_file is given, will try to load template
carlosmscabralf40ecd12013-02-01 18:15:58 -020017 from file miniccnx.conf in the current directory.
carlosmscabral6d3dd602013-03-23 11:12:34 -030018 If --testbed is used, miniccnx will run the NDN Project Testbed.
19 This assumes you are in the testbed directory in the miniccnx installation
20 directory.
carlosmscabralf40ecd12013-02-01 18:15:58 -020021 """
22
carlosmscabral6d3dd602013-03-23 11:12:34 -030023 testbed = False
carlosmscabralf40ecd12013-02-01 18:15:58 -020024 parser = optparse.OptionParser(usage)
25
carlosmscabral6d3dd602013-03-23 11:12:34 -030026 parser.add_option("-t", "--testbed", action="store_true", dest="testbed",
27 help="instantiates NDN Testbed")
28
29 (options, arg) = parser.parse_args()
30
31 testbed = options.testbed
32
33 if len(arg) == 0 or len(arg) > 2:
34 file = ''
carlosmscabralf40ecd12013-02-01 18:15:58 -020035 else:
carlosmscabral6d3dd602013-03-23 11:12:34 -030036 file = arg[0]
37
38 return file, testbed
carlosmscabralf40ecd12013-02-01 18:15:58 -020039
40
41class CCNTopo(Topo):
42 def __init__(self, conf_arq, **opts):
43 Topo.__init__(self, **opts)
carlosmscabral6d3dd602013-03-23 11:12:34 -030044
carlosmscabral29432252013-02-04 11:54:16 -020045 hosts_conf = parse_hosts(conf_arq)
46 routers_conf = parse_routers(conf_arq)
47 links_conf = parse_links(conf_arq)
carlosmscabralf40ecd12013-02-01 18:15:58 -020048
carlosmscabral6d3dd602013-03-23 11:12:34 -030049
carlosmscabralf40ecd12013-02-01 18:15:58 -020050 self.isTCLink = False
carlosmscabral29432252013-02-04 11:54:16 -020051 self.isLimited = False
carlosmscabral6d3dd602013-03-23 11:12:34 -030052
carlosmscabralf40ecd12013-02-01 18:15:58 -020053 for host in hosts_conf:
carlosmscabral29432252013-02-04 11:54:16 -020054 if host.cpu != None and self.isLimited != True:
55 self.isLimited = True
carlosmscabrale121a7b2013-02-18 18:14:53 -030056 self.addHost(host.name, app=host.app, fib=host.uri_tuples,cpu=host.cpu,cores=host.cores)
carlosmscabral6d3dd602013-03-23 11:12:34 -030057
carlosmscabralf40ecd12013-02-01 18:15:58 -020058 for router in routers_conf:
carlosmscabral29432252013-02-04 11:54:16 -020059 if router.cpu != None and self.isLimited != True:
60 self.isLimited = True
carlosmscabrale121a7b2013-02-18 18:14:53 -030061 self.addHost(router.name,fib=router.uri_tuples,cpu=router.cpu,cores=router.cores)
carlosmscabral6d3dd602013-03-23 11:12:34 -030062
carlosmscabralf40ecd12013-02-01 18:15:58 -020063 for link in links_conf:
64 if len(link.linkDict) == 0:
65 self.addLink(link.h1, link.h2)
66 else:
67 self.addLink(link.h1, link.h2, **link.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -030068 self.isTCLink = True
69
carlosmscabralf40ecd12013-02-01 18:15:58 -020070 info('Parse of ' + conf_arq + ' done.\n')
71
carlosmscabral6d3dd602013-03-23 11:12:34 -030072def execute(template_file='miniccnx.conf', testbed=False):
carlosmscabralf40ecd12013-02-01 18:15:58 -020073 "Create a network based on template_file"
74
75 if template_file == '':
76 template_file='miniccnx.conf'
carlosmscabral6d3dd602013-03-23 11:12:34 -030077
carlosmscabralf40ecd12013-02-01 18:15:58 -020078 if os.path.exists(template_file) == False:
79 info('No template file given and default template file miniccnx.conf not found. Exiting...\n')
80 quit()
carlosmscabral6d3dd602013-03-23 11:12:34 -030081
carlosmscabralf40ecd12013-02-01 18:15:58 -020082 topo = CCNTopo(template_file)
carlosmscabral6d3dd602013-03-23 11:12:34 -030083
carlosmscabralf40ecd12013-02-01 18:15:58 -020084 t = datetime.datetime.now()
carlosmscabral6d3dd602013-03-23 11:12:34 -030085
carlosmscabral29432252013-02-04 11:54:16 -020086 if topo.isTCLink == True and topo.isLimited == True:
carlosmscabralf40ecd12013-02-01 18:15:58 -020087 net = Mininet(topo,host=CPULimitedCCNHost,link=TCLink)
carlosmscabral29432252013-02-04 11:54:16 -020088 elif topo.isTCLink == True and topo.isLimited == False:
89 net = Mininet(topo,host=CCNHost,link=TCLink)
90 elif topo.isTCLink == False and topo.isLimited == True:
91 net = Mininet(topo,host=CPULimitedCCNHost)
carlosmscabralf40ecd12013-02-01 18:15:58 -020092 else:
93 net = Mininet(topo,host=CCNHost)
carlosmscabral6d3dd602013-03-23 11:12:34 -030094
carlosmscabralf40ecd12013-02-01 18:15:58 -020095 t2 = datetime.datetime.now()
carlosmscabral6d3dd602013-03-23 11:12:34 -030096
carlosmscabralf40ecd12013-02-01 18:15:58 -020097 delta = t2 - t
carlosmscabral6d3dd602013-03-23 11:12:34 -030098
carlosmscabralf40ecd12013-02-01 18:15:58 -020099 info('Setup time: ' + str(delta.seconds) + '\n')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300100
carlosmscabralf40ecd12013-02-01 18:15:58 -0200101 net.start()
102
carlosmscabral6d3dd602013-03-23 11:12:34 -0300103 if testbed == True:
104 info('Starting OSPFN ...\n')
105 for host in net.hosts:
106 host.cmd("cd {0}".format(host.name))
107 host.cmd("./routing.sh {0}".format(host.name))
108
109 time.sleep(60)
110
111 for host in net.hosts:
112 host.cmd("./ospfn-start.sh {0}".format(host.name))
113
114 info('OSPFN configuration completed!\n')
115
carlosmscabralf40ecd12013-02-01 18:15:58 -0200116 for host in net.hosts:
117 if 'app' in host.params:
118 if host.params['app'] != '_':
119 host.cmd(host.params['app'])
carlosmscabral6d3dd602013-03-23 11:12:34 -0300120
carlosmscabralf40ecd12013-02-01 18:15:58 -0200121 CLI(net)
122 net.stop()
123
124if __name__ == '__main__':
carlosmscabral6d3dd602013-03-23 11:12:34 -0300125
126 template, testbed = parse_args()
carlosmscabralf40ecd12013-02-01 18:15:58 -0200127 setLogLevel('info')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300128 execute(template, testbed)