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_, **kw):
32 return plcapi.PLCAPI(username=user, password=pass_, **kw)
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 getVnet(api, slicename):
66 slicetags = api.GetSliceTags(
68 tagname = 'vsys_vnet',
71 return slicetags[0]['value']
75 def getNodes(api, num, **constraints):
76 # Now do the backtracking search for a suitable solution
77 # First with existing slice nodes
85 node.min_num_external_interface = 1
89 candidates = filterBlacklist(node.find_candidates())
90 reqs = [candidates] * num
92 def pickbest(fullset, nreq, node=nodes[0]):
93 if len(fullset) > nreq:
94 fullset = zip(node.rate_nodes(fullset),fullset)
95 fullset.sort(reverse=True)
97 return set(map(operator.itemgetter(1),fullset))
101 solution = resourcealloc.alloc(reqs, sample=pickbest)
104 runner = ParallelRun(maxthreads=4)
105 for node, node_id in zip(nodes, solution):
106 runner.put(node.assign_node_id, node_id)
111 def getSpanningTree(nodes, root = None, maxbranching = 2, hostgetter = operator.attrgetter('hostname')):
113 # Pick root (deterministically)
114 root = min(nodes, key=hostgetter)
116 # Obtain all IPs in numeric format
117 # (which means faster distance computations)
119 node._ip = socket.gethostbyname(hostgetter(node))
120 node._ip_n = struct.unpack('!L', socket.inet_aton(node._ip))[0]
123 # NOTE: the plan is an iterator
126 lambda a,b : ipaddr2.ipdistn(a._ip_n, b._ip_n),
128 maxbranching = maxbranching)