blob: 614d5cad632612b3b8aa947e74d81116e50ac29c [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
Caio188d2f32015-01-22 23:35:08 -020013import pdb
carlosmscabralf40ecd12013-02-01 18:15:58 -020014
15def parse_args():
carlosmscabral6d3dd602013-03-23 11:12:34 -030016 usage="""Usage: miniccnx [template_file] [ -t | --testbed ]
17 If no template_file is given, will try to load template
carlosmscabralf40ecd12013-02-01 18:15:58 -020018 from file miniccnx.conf in the current directory.
carlosmscabral6d3dd602013-03-23 11:12:34 -030019 If --testbed is used, miniccnx will run the NDN Project Testbed.
20 This assumes you are in the testbed directory in the miniccnx installation
21 directory.
carlosmscabralf40ecd12013-02-01 18:15:58 -020022 """
23
carlosmscabral6d3dd602013-03-23 11:12:34 -030024 testbed = False
carlosmscabralf40ecd12013-02-01 18:15:58 -020025 parser = optparse.OptionParser(usage)
26
carlosmscabral6d3dd602013-03-23 11:12:34 -030027 parser.add_option("-t", "--testbed", action="store_true", dest="testbed",
28 help="instantiates NDN Testbed")
29
30 (options, arg) = parser.parse_args()
31
32 testbed = options.testbed
33
34 if len(arg) == 0 or len(arg) > 2:
35 file = ''
carlosmscabralf40ecd12013-02-01 18:15:58 -020036 else:
carlosmscabral6d3dd602013-03-23 11:12:34 -030037 file = arg[0]
38
39 return file, testbed
carlosmscabralf40ecd12013-02-01 18:15:58 -020040
41
42class CCNTopo(Topo):
43 def __init__(self, conf_arq, **opts):
44 Topo.__init__(self, **opts)
carlosmscabral6d3dd602013-03-23 11:12:34 -030045
carlosmscabral29432252013-02-04 11:54:16 -020046 hosts_conf = parse_hosts(conf_arq)
Caio188d2f32015-01-22 23:35:08 -020047 routers_conf = parse_routers(conf_arq)
carlosmscabral29432252013-02-04 11:54:16 -020048 links_conf = parse_links(conf_arq)
carlosmscabralf40ecd12013-02-01 18:15:58 -020049
carlosmscabral6d3dd602013-03-23 11:12:34 -030050
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:
carlosmscabral3d060fc2013-07-18 13:18:09 -030055# print host
carlosmscabral29432252013-02-04 11:54:16 -020056 if host.cpu != None and self.isLimited != True:
57 self.isLimited = True
carlosmscabralb9d85b72013-07-15 15:24:33 -030058 self.addHost(host.name, app=host.app, fib=host.uri_tuples,cpu=host.cpu,cores=host.cores,cache=host.cache)
carlosmscabral6d3dd602013-03-23 11:12:34 -030059
Caio188d2f32015-01-22 23:35:08 -020060 for router in routers_conf:
61 if router.cpu != None and self.isLimited != True:
62 self.isLimited = True
63 self.addHost(router.name,fib=router.uri_tuples,cpu=router.cpu,cores=router.cores, cache=router.cache)
carlosmscabral6d3dd602013-03-23 11:12:34 -030064
carlosmscabralf40ecd12013-02-01 18:15:58 -020065 for link in links_conf:
66 if len(link.linkDict) == 0:
Caio188d2f32015-01-22 23:35:08 -020067 #pdb.set_trace()
carlosmscabralf40ecd12013-02-01 18:15:58 -020068 self.addLink(link.h1, link.h2)
69 else:
70 self.addLink(link.h1, link.h2, **link.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -030071 self.isTCLink = True
72
carlosmscabralf40ecd12013-02-01 18:15:58 -020073 info('Parse of ' + conf_arq + ' done.\n')
74
carlosmscabral6d3dd602013-03-23 11:12:34 -030075def execute(template_file='miniccnx.conf', testbed=False):
carlosmscabralf40ecd12013-02-01 18:15:58 -020076 "Create a network based on template_file"
77
78 if template_file == '':
79 template_file='miniccnx.conf'
carlosmscabral6d3dd602013-03-23 11:12:34 -030080
carlosmscabralf40ecd12013-02-01 18:15:58 -020081 if os.path.exists(template_file) == False:
82 info('No template file given and default template file miniccnx.conf not found. Exiting...\n')
83 quit()
carlosmscabral6d3dd602013-03-23 11:12:34 -030084
carlosmscabralf40ecd12013-02-01 18:15:58 -020085 topo = CCNTopo(template_file)
carlosmscabral6d3dd602013-03-23 11:12:34 -030086
carlosmscabralf40ecd12013-02-01 18:15:58 -020087 t = datetime.datetime.now()
carlosmscabral6d3dd602013-03-23 11:12:34 -030088
carlosmscabral29432252013-02-04 11:54:16 -020089 if topo.isTCLink == True and topo.isLimited == True:
carlosmscabralf40ecd12013-02-01 18:15:58 -020090 net = Mininet(topo,host=CPULimitedCCNHost,link=TCLink)
carlosmscabral29432252013-02-04 11:54:16 -020091 elif topo.isTCLink == True and topo.isLimited == False:
92 net = Mininet(topo,host=CCNHost,link=TCLink)
93 elif topo.isTCLink == False and topo.isLimited == True:
94 net = Mininet(topo,host=CPULimitedCCNHost)
carlosmscabralf40ecd12013-02-01 18:15:58 -020095 else:
96 net = Mininet(topo,host=CCNHost)
carlosmscabral6d3dd602013-03-23 11:12:34 -030097
carlosmscabralf40ecd12013-02-01 18:15:58 -020098 t2 = datetime.datetime.now()
carlosmscabral6d3dd602013-03-23 11:12:34 -030099
carlosmscabralf40ecd12013-02-01 18:15:58 -0200100 delta = t2 - t
carlosmscabral6d3dd602013-03-23 11:12:34 -0300101
carlosmscabralf40ecd12013-02-01 18:15:58 -0200102 info('Setup time: ' + str(delta.seconds) + '\n')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300103
carlosmscabralf40ecd12013-02-01 18:15:58 -0200104 net.start()
105
carlosmscabral6d3dd602013-03-23 11:12:34 -0300106 if testbed == True:
107 info('Starting OSPFN ...\n')
108 for host in net.hosts:
109 host.cmd("cd {0}".format(host.name))
110 host.cmd("./routing.sh {0}".format(host.name))
111
112 time.sleep(60)
113
114 for host in net.hosts:
115 host.cmd("./ospfn-start.sh {0}".format(host.name))
116
117 info('OSPFN configuration completed!\n')
118
carlosmscabralf40ecd12013-02-01 18:15:58 -0200119 for host in net.hosts:
120 if 'app' in host.params:
121 if host.params['app'] != '_':
122 host.cmd(host.params['app'])
carlosmscabral6d3dd602013-03-23 11:12:34 -0300123
carlosmscabralf40ecd12013-02-01 18:15:58 -0200124 CLI(net)
125 net.stop()
126
127if __name__ == '__main__':
carlosmscabral6d3dd602013-03-23 11:12:34 -0300128
129 template, testbed = parse_args()
carlosmscabralf40ecd12013-02-01 18:15:58 -0200130 setLogLevel('info')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300131 execute(template, testbed)