blob: 36c3938c4a0192a03257f60481a098b3811712a2 [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 print links_conf
50
carlosmscabralf40ecd12013-02-01 18:15:58 -020051 self.isTCLink = False
carlosmscabral29432252013-02-04 11:54:16 -020052 self.isLimited = False
carlosmscabral6d3dd602013-03-23 11:12:34 -030053
carlosmscabralf40ecd12013-02-01 18:15:58 -020054 for host in hosts_conf:
carlosmscabral29432252013-02-04 11:54:16 -020055 if host.cpu != None and self.isLimited != True:
56 self.isLimited = True
carlosmscabrale121a7b2013-02-18 18:14:53 -030057 self.addHost(host.name, app=host.app, fib=host.uri_tuples,cpu=host.cpu,cores=host.cores)
carlosmscabral6d3dd602013-03-23 11:12:34 -030058
carlosmscabralf40ecd12013-02-01 18:15:58 -020059 for router in routers_conf:
carlosmscabral29432252013-02-04 11:54:16 -020060 if router.cpu != None and self.isLimited != True:
61 self.isLimited = True
carlosmscabrale121a7b2013-02-18 18:14:53 -030062 self.addHost(router.name,fib=router.uri_tuples,cpu=router.cpu,cores=router.cores)
carlosmscabral6d3dd602013-03-23 11:12:34 -030063
carlosmscabralf40ecd12013-02-01 18:15:58 -020064 for link in links_conf:
65 if len(link.linkDict) == 0:
66 self.addLink(link.h1, link.h2)
67 else:
68 self.addLink(link.h1, link.h2, **link.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -030069 self.isTCLink = True
70
carlosmscabralf40ecd12013-02-01 18:15:58 -020071 info('Parse of ' + conf_arq + ' done.\n')
72
carlosmscabral6d3dd602013-03-23 11:12:34 -030073def execute(template_file='miniccnx.conf', testbed=False):
carlosmscabralf40ecd12013-02-01 18:15:58 -020074 "Create a network based on template_file"
75
76 if template_file == '':
77 template_file='miniccnx.conf'
carlosmscabral6d3dd602013-03-23 11:12:34 -030078
carlosmscabralf40ecd12013-02-01 18:15:58 -020079 if os.path.exists(template_file) == False:
80 info('No template file given and default template file miniccnx.conf not found. Exiting...\n')
81 quit()
carlosmscabral6d3dd602013-03-23 11:12:34 -030082
carlosmscabralf40ecd12013-02-01 18:15:58 -020083 topo = CCNTopo(template_file)
carlosmscabral6d3dd602013-03-23 11:12:34 -030084
carlosmscabralf40ecd12013-02-01 18:15:58 -020085 t = datetime.datetime.now()
carlosmscabral6d3dd602013-03-23 11:12:34 -030086
carlosmscabral29432252013-02-04 11:54:16 -020087 if topo.isTCLink == True and topo.isLimited == True:
carlosmscabralf40ecd12013-02-01 18:15:58 -020088 net = Mininet(topo,host=CPULimitedCCNHost,link=TCLink)
carlosmscabral29432252013-02-04 11:54:16 -020089 elif topo.isTCLink == True and topo.isLimited == False:
90 net = Mininet(topo,host=CCNHost,link=TCLink)
91 elif topo.isTCLink == False and topo.isLimited == True:
92 net = Mininet(topo,host=CPULimitedCCNHost)
carlosmscabralf40ecd12013-02-01 18:15:58 -020093 else:
94 net = Mininet(topo,host=CCNHost)
carlosmscabral6d3dd602013-03-23 11:12:34 -030095
carlosmscabralf40ecd12013-02-01 18:15:58 -020096 t2 = datetime.datetime.now()
carlosmscabral6d3dd602013-03-23 11:12:34 -030097
carlosmscabralf40ecd12013-02-01 18:15:58 -020098 delta = t2 - t
carlosmscabral6d3dd602013-03-23 11:12:34 -030099
carlosmscabralf40ecd12013-02-01 18:15:58 -0200100 info('Setup time: ' + str(delta.seconds) + '\n')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300101
carlosmscabralf40ecd12013-02-01 18:15:58 -0200102 net.start()
103
carlosmscabral6d3dd602013-03-23 11:12:34 -0300104 if testbed == True:
105 info('Starting OSPFN ...\n')
106 for host in net.hosts:
107 host.cmd("cd {0}".format(host.name))
108 host.cmd("./routing.sh {0}".format(host.name))
109
110 time.sleep(60)
111
112 for host in net.hosts:
113 host.cmd("./ospfn-start.sh {0}".format(host.name))
114
115 info('OSPFN configuration completed!\n')
116
carlosmscabralf40ecd12013-02-01 18:15:58 -0200117 for host in net.hosts:
118 if 'app' in host.params:
119 if host.params['app'] != '_':
120 host.cmd(host.params['app'])
carlosmscabral6d3dd602013-03-23 11:12:34 -0300121
carlosmscabralf40ecd12013-02-01 18:15:58 -0200122 CLI(net)
123 net.stop()
124
125if __name__ == '__main__':
carlosmscabral6d3dd602013-03-23 11:12:34 -0300126
127 template, testbed = parse_args()
carlosmscabralf40ecd12013-02-01 18:15:58 -0200128 setLogLevel('info')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300129 execute(template, testbed)