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 appendBlacklist(node_ids):
53 if not isinstance(node_ids, list):
54 node_ids = [ node_ids ]
56 blpath = environ.homepath('plblacklist')
57 bl = open(blpath, "a")
60 for node_id in node_ids:
61 bl.write("%s\n" % (node_id,))
65 def getNodes(api, num, **constraints):
66 # Now do the backtracking search for a suitable solution
67 # First with existing slice nodes
75 node.min_num_external_interface = 1
79 candidates = filterBlacklist(node.find_candidates())
80 reqs = [candidates] * num
82 def pickbest(fullset, nreq, node=nodes[0]):
83 if len(fullset) > nreq:
84 fullset = zip(node.rate_nodes(fullset),fullset)
85 fullset.sort(reverse=True)
87 return set(map(operator.itemgetter(1),fullset))
91 solution = resourcealloc.alloc(reqs, sample=pickbest)
94 runner = ParallelRun(maxthreads=4)
95 for node, node_id in zip(nodes, solution):
96 runner.put(node.assign_node_id, node_id)
101 def getSpanningTree(nodes, root = None, maxbranching = 2, hostgetter = operator.attrgetter('hostname')):
103 # Pick root (deterministically)
104 root = min(nodes, key=hostgetter)
106 # Obtain all IPs in numeric format
107 # (which means faster distance computations)
109 node._ip = socket.gethostbyname(hostgetter(node))
110 node._ip_n = struct.unpack('!L', socket.inet_aton(node._ip))[0]
113 # NOTE: the plan is an iterator
116 lambda a,b : ipaddr2.ipdistn(a._ip_n, b._ip_n),
118 maxbranching = maxbranching)