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 getNodes(api, num, **constraints):
35 # Now do the backtracking search for a suitable solution
36 # First with existing slice nodes
44 node.min_num_external_interface = 1
48 candidates = node.find_candidates()
49 reqs = [candidates] * num
51 def pickbest(fullset, nreq, node=nodes[0]):
52 if len(fullset) > nreq:
53 fullset = zip(node.rate_nodes(fullset),fullset)
54 fullset.sort(reverse=True)
56 return set(map(operator.itemgetter(1),fullset))
60 solution = resourcealloc.alloc(reqs, sample=pickbest)
63 for node, node_id in zip(nodes, solution):
64 node.assign_node_id(node_id)
68 def getSpanningTree(nodes, root = None, maxbranching = 2, hostgetter = operator.attrgetter('hostname')):
70 # Pick root (deterministically)
71 root = min(nodes, key=hostgetter)
73 # Obtain all IPs in numeric format
74 # (which means faster distance computations)
76 node._ip = socket.gethostbyname(hostgetter(node))
77 node._ip_n = struct.unpack('!L', socket.inet_aton(node._ip))[0]
80 # NOTE: the plan is an iterator
83 lambda a,b : ipaddr2.ipdistn(a._ip_n, b._ip_n),
85 maxbranching = maxbranching)