Example #1
0
def startLocalMininet(data, infect=False):
  config = Config()
  ip = filter(lambda ip: config.isInControlNetwork(ip[1]), util.getIPs())[0][1]
  setLogLevel('debug')
  net = Mininet(topo=None, build=False, host=CPULimitedHost, link=TCLink)
  net.addController(RemoteController('c0', ip=config.getControlIP()))

  for h in data['nodes']:
    print "-- add host: %s (%s)" % (h['name'], h['ip'])
    net.addHost(h['name'], ip=h['ip'])

  for s in data['switches']:
    print "-- add switch: %s (%s)" % (s['name'], h['ip'])
    net.addSwitch(s['name'], ip=h['ip'])

  for l in data['links']:
    if l['source'] == 'backbone' or l['target'] == 'backbone':
      continue
    opts = { k: l[k] for k in ['bw', 'delay', 'loss'] if k in l }
    if 'bw' in opts:
      opts['bw'] = int(opts['bw'])
    if 'loss' in opts:
      opts['loss'] = int(opts['loss'])
    print "-- add link: %s <-> %s %s" % (l['source'], l['target'], repr(opts))
    net.addLink(net.get(l['source']), net.get(l['target']), **opts)

  rootSwitch = findRootSwitch(net, data)
  iface = filter(lambda ip: config.isInMininetNetwork(ip[1]), util.getIPs())[0][0]
  Intf(iface, node=rootSwitch)

  net.start()
  rootSwitch.cmd("ovs-vsctl add-port %s %s" % (rootSwitch.name, iface))

  # This allows infected Mininet instances to lookup nodes via Pyro4.
  if infect:
    from onenet.inject import infect

  print "** Publishing network"
  net.publishNet(ip, data['nodes'][0]['pno'])