2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID, TESTBED_VERSION
5 from nepi.core import testbed_impl
6 from nepi.core.metadata import Parallel
7 from nepi.util.constants import TIME_NOW
8 from nepi.util.graphtools import mst
9 from nepi.util import ipaddr2
10 from nepi.util import environ
11 from nepi.util.parallel import ParallelRun
30 def getAPI(user, pass_):
32 return plcapi.PLCAPI(username=user, password=pass_)
34 def filterBlacklist(candidates):
35 blpath = environ.homepath('plblacklist')
38 bl = open(blpath, "r")
45 map(str.strip, bl.readlines())
48 return [ x for x in candidates if x not in blacklist ]
52 def getNodes(api, num, **constraints):
53 # Now do the backtracking search for a suitable solution
54 # First with existing slice nodes
62 node.min_num_external_interface = 1
66 candidates = filterBlacklist(node.find_candidates())
67 reqs = [candidates] * num
69 def pickbest(fullset, nreq, node=nodes[0]):
70 if len(fullset) > nreq:
71 fullset = zip(node.rate_nodes(fullset),fullset)
72 fullset.sort(reverse=True)
74 return set(map(operator.itemgetter(1),fullset))
78 solution = resourcealloc.alloc(reqs, sample=pickbest)
81 runner = ParallelRun(maxthreads=4)
82 for node, node_id in zip(nodes, solution):
83 runner.put(node.assign_node_id, node_id)
88 def getSpanningTree(nodes, root = None, maxbranching = 2, hostgetter = operator.attrgetter('hostname')):
90 # Pick root (deterministically)
91 root = min(nodes, key=hostgetter)
93 # Obtain all IPs in numeric format
94 # (which means faster distance computations)
96 node._ip = socket.gethostbyname(hostgetter(node))
97 node._ip_n = struct.unpack('!L', socket.inet_aton(node._ip))[0]
100 # NOTE: the plan is an iterator
103 lambda a,b : ipaddr2.ipdistn(a._ip_n, b._ip_n),
105 maxbranching = maxbranching)