Fix metadata breakage from recent commit
[nepi.git] / src / nepi / testbeds / planetlab / metadata.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import time
5
6 from constants import TESTBED_ID, TESTBED_VERSION
7 from nepi.core import metadata
8 from nepi.core.metadata import Parallel
9 from nepi.core.attributes import Attribute
10 from nepi.util import tags, validation
11 from nepi.util.constants import ApplicationStatus as AS, \
12         FactoryCategories as FC, \
13         ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP, \
14         DeploymentConfiguration as DC
15
16 import functools
17 import os
18 import os.path
19 import weakref
20
21 NODE = "Node"
22 NODEIFACE = "NodeInterface"
23 TUNIFACE = "TunInterface"
24 TAPIFACE = "TapInterface"
25 APPLICATION = "Application"
26 DEPENDENCY = "Dependency"
27 NEPIDEPENDENCY = "NepiDependency"
28 NS3DEPENDENCY = "NS3Dependency"
29 INTERNET = "Internet"
30 NETPIPE = "NetPipe"
31 TUNFILTER = "TunFilter"
32 CLASSQUEUEFILTER = "ClassQueueFilter"
33 TOSQUEUEFILTER = "TosQueueFilter"
34 MULTICASTFORWARDER = "MulticastForwarder"
35 MULTICASTANNOUNCER = "MulticastAnnouncer"
36 MULTICASTROUTER = "MulticastRouter"
37
38 TUNFILTERS = (TUNFILTER, CLASSQUEUEFILTER, TOSQUEUEFILTER)
39 TAPFILTERS = (TUNFILTER, )
40 ALLFILTERS = (TUNFILTER, CLASSQUEUEFILTER, TOSQUEUEFILTER)
41
42 PL_TESTBED_ID = "planetlab"
43
44
45 ### Custom validation functions ###
46 def is_addrlist(attribute, value):
47     if not validation.is_string(attribute, value):
48         return False
49     
50     if not value:
51         # No empty strings
52         return False
53     
54     components = value.split(',')
55     
56     for component in components:
57         if '/' in component:
58             addr, mask = component.split('/',1)
59         else:
60             addr, mask = component, '32'
61         
62         if mask is not None and not (mask and mask.isdigit()):
63             # No empty or nonnumeric masks
64             return False
65         
66         if not validation.is_ip4_address(attribute, addr):
67             # Address part must be ipv4
68             return False
69         
70     return True
71
72 def is_portlist(attribute, value):
73     if not validation.is_string(attribute, value):
74         return False
75     
76     if not value:
77         # No empty strings
78         return False
79     
80     components = value.split(',')
81     
82     for component in components:
83         if '-' in component:
84             pfrom, pto = component.split('-',1)
85         else:
86             pfrom = pto = component
87         
88         if not pfrom or not pto or not pfrom.isdigit() or not pto.isdigit():
89             # No empty or nonnumeric ports
90             return False
91         
92     return True
93
94
95 ### Connection functions ####
96
97 def connect_node_iface_node(testbed_instance, node_guid, iface_guid):
98     node = testbed_instance._elements[node_guid]
99     iface = testbed_instance._elements[iface_guid]
100     iface.node = node
101
102 def connect_node_iface_inet(testbed_instance, iface_guid, inet_guid):
103     iface = testbed_instance._elements[iface_guid]
104     iface.has_internet = True
105
106 def connect_tun_iface_node(testbed_instance, node_guid, iface_guid):
107     node = testbed_instance._elements[node_guid]
108     iface = testbed_instance._elements[iface_guid]
109     iface.node = node
110     node.required_vsys.update(('fd_tuntap', 'vif_up', 'vif_down'))
111     node.required_packages.update(('python', 'python-crypto', 'python-setuptools', 'gcc'))
112
113 def connect_tun_iface_peer(proto, testbed_instance, iface_guid, peer_iface_guid):
114     iface = testbed_instance._elements[iface_guid]
115     peer_iface = testbed_instance._elements[peer_iface_guid]
116     iface.peer_iface = peer_iface
117     peer_iface.peer_iface = iface
118     iface.peer_proto = \
119     iface.tun_proto = \
120     peer_iface.peer_proto = \
121     peer_iface.tun_proto = proto
122     iface.tun_key = peer_iface.tun_key
123
124 def connect_tun_iface_filter(testbed_instance, iface_guid, filter_guid):
125     iface = testbed_instance._elements[iface_guid]
126     filt = testbed_instance._elements[filter_guid]
127     traces = testbed_instance._get_traces(filter_guid)
128     if 'dropped_stats' in traces: 
129         args = filt.args if filt.args else ""
130         filt.args = ','.join(filt.args.split(',') + ["logdropped=true",])
131     iface.filter_module = filt
132     filt.iface_guid = iface_guid
133     filt.iface = weakref.ref(iface)
134
135     if filt.peer_guid:
136         connect_tun_iface_peer(filt.peer_proto, testbed_instance, filt.iface_guid, filt.peer_guid)
137
138 def connect_filter_peer(proto, testbed_instance, filter_guid, peer_guid):
139     peer = testbed_instance._elements[peer_guid]
140     filt = testbed_instance._elements[filter_guid]
141     filt.peer_proto = proto
142     filt.peer_guid = peer_guid
143     if filt.iface_guid:
144         connect_tun_iface_peer(filt.peer_proto, testbed_instance, filt.iface_guid, filt.peer_guid)
145
146 def connect_filter_filter(proto, testbed_instance, filter_guid, peer_guid):
147     peer = testbed_instance._elements[peer_guid]
148     filt = testbed_instance._elements[filter_guid]
149     filt.peer_proto = proto
150     peer.peer_proto = proto
151     if filt.iface_guid:
152         peer.peer_guid = filt.iface_guid
153     if peer.iface_guid:
154         filt.peer_guid = peer.iface_guid
155     if filt.iface_guid and filt.peer_guid:
156         connect_tun_iface_peer(filt.peer_proto, testbed_instance, filt.iface_guid, filt.peer_guid)
157
158 def crossconnect_tun_iface_peer_init(proto, testbed_instance, iface_guid, peer_iface_data):
159     iface = testbed_instance._elements[iface_guid]
160     iface.peer_iface = None
161     iface.peer_addr = peer_iface_data.get("tun_addr")
162     iface.peer_proto = peer_iface_data.get("tun_proto") or proto
163     iface.peer_port = peer_iface_data.get("tun_port")
164     iface.peer_cipher = peer_iface_data.get("tun_cipher")
165     iface.tun_key = min(iface.tun_key, peer_iface_data.get("tun_key"))
166     iface.tun_proto = proto
167     
168     preconfigure_tuniface(testbed_instance, iface_guid)
169
170 def crossconnect_tun_iface_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data):
171     # refresh (refreshable) attributes for second-phase
172     iface = testbed_instance._elements[iface_guid]
173     iface.peer_addr = peer_iface_data.get("tun_addr")
174     iface.peer_proto = peer_iface_data.get("tun_proto") or proto
175     iface.peer_port = peer_iface_data.get("tun_port")
176     iface.peer_cipher = peer_iface_data.get("tun_cipher")
177     
178     postconfigure_tuniface(testbed_instance, iface_guid)
179
180 def crossconnect_tun_iface_peer_both(proto, testbed_instance, iface_guid, peer_iface_data):
181     crossconnect_tun_iface_peer_init(proto, testbed_instance, iface_guid, peer_iface_data)
182     crossconnect_tun_iface_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data)
183
184 def crossconnect_filter_peer_init(proto, testbed_instance, filter_guid, peer_data):
185     filt = testbed_instance._elements[filter_guid]
186     filt.peer_proto = proto
187     crossconnect_tun_iface_peer_init(filt.peer_proto, testbed_instance, filt.iface_guid, peer_data)
188
189 def crossconnect_filter_peer_compl(proto, testbed_instance, filter_guid, peer_data):
190     filt = testbed_instance._elements[filter_guid]
191     filt.peer_proto = proto
192     crossconnect_tun_iface_peer_compl(filt.peer_proto, testbed_instance, filt.iface_guid, peer_data)
193
194 def crossconnect_filter_peer_both(proto, testbed_instance, filter_guid, peer_data):
195     crossconnect_filter_peer_init(proto, testbed_instance, iface_guid, peer_iface_data)
196     crossconnect_filter_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data)
197
198 def connect_dep(testbed_instance, node_guid, app_guid, node=None, app=None):
199     node = node or testbed_instance._elements[node_guid]
200     app = app or testbed_instance._elements[app_guid]
201     app.node = node
202     
203     if app.depends:
204         node.required_packages.update(set(
205             app.depends.split() ))
206     
207     if app.add_to_path:
208         if app.home_path and app.home_path not in node.pythonpath:
209             node.pythonpath.append(app.home_path)
210     
211     if app.env:
212         for envkey, envval in app.env.iteritems():
213             envval = app._replace_paths(envval)
214             node.env[envkey].append(envval)
215     
216     if app.rpmFusion:
217         node.rpmFusion = True
218
219 def connect_forwarder(testbed_instance, node_guid, fwd_guid):
220     node = testbed_instance._elements[node_guid]
221     fwd = testbed_instance._elements[fwd_guid]
222     node.multicast_forwarder = fwd
223     
224     if fwd.router:
225         connect_dep(testbed_instance, node_guid, None, app=fwd.router)
226
227     connect_dep(testbed_instance, node_guid, fwd_guid)
228
229 def connect_router(testbed_instance, fwd_guid, router_guid):
230     fwd = testbed_instance._elements[fwd_guid]
231     router = testbed_instance._elements[router_guid]
232     fwd.router = router
233     
234     if fwd.node:
235         connect_dep(testbed_instance, None, router_guid, node=fwd.node)
236
237 def connect_node_netpipe(testbed_instance, node_guid, netpipe_guid):
238     node = testbed_instance._elements[node_guid]
239     netpipe = testbed_instance._elements[netpipe_guid]
240     netpipe.node = node
241     node.required_vsys.add('ipfw-be')
242     node.required_packages.add('ipfwslice')
243     
244
245 ### Creation functions ###
246
247 def create_node(testbed_instance, guid):
248     parameters = testbed_instance._get_parameters(guid)
249     
250     # create element with basic attributes
251     element = testbed_instance._make_node(parameters)
252     
253     # add constraint on number of (real) interfaces
254     # by counting connected devices
255     dev_guids = testbed_instance.get_connected(guid, "devs", "node")
256     num_open_ifaces = sum( # count True values
257         NODEIFACE == testbed_instance._get_factory_id(guid)
258         for guid in dev_guids )
259     element.min_num_external_ifaces = num_open_ifaces
260     
261     # require vroute vsys if we have routes to set up
262     routes = testbed_instance._add_route.get(guid)
263     if routes:
264         vsys = element.routing_method(routes,
265             testbed_instance.vsys_vnet)
266         element.required_vsys.add(vsys)
267     
268     testbed_instance.elements[guid] = element
269
270 def create_nodeiface(testbed_instance, guid):
271     parameters = testbed_instance._get_parameters(guid)
272     element = testbed_instance._make_node_iface(parameters)
273     testbed_instance.elements[guid] = element
274
275 def create_tuniface(testbed_instance, guid):
276     parameters = testbed_instance._get_parameters(guid)
277     element = testbed_instance._make_tun_iface(parameters)
278     
279     # Set custom addresses, if there are any already
280     # Setting this early helps set up P2P links
281     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
282         addresses = testbed_instance._add_address[guid]
283         for address in addresses:
284             (address, netprefix, broadcast) = address
285             element.add_address(address, netprefix, broadcast)
286     
287     testbed_instance.elements[guid] = element
288
289 def create_tapiface(testbed_instance, guid):
290     parameters = testbed_instance._get_parameters(guid)
291     element = testbed_instance._make_tap_iface(parameters)
292     
293     # Set custom addresses, if there are any already
294     # Setting this early helps set up P2P links
295     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
296         addresses = testbed_instance._add_address[guid]
297         for address in addresses:
298             (address, netprefix, broadcast) = address
299             element.add_address(address, netprefix, broadcast)
300     
301     testbed_instance.elements[guid] = element
302
303 def create_tunfilter(testbed_instance, guid):
304     parameters = testbed_instance._get_parameters(guid)
305     element = testbed_instance._make_tun_filter(parameters)
306     testbed_instance.elements[guid] = element
307
308 def create_classqueuefilter(testbed_instance, guid):
309     parameters = testbed_instance._get_parameters(guid)
310     element = testbed_instance._make_class_queue_filter(parameters)
311     testbed_instance.elements[guid] = element
312
313 def create_tosqueuefilter(testbed_instance, guid):
314     parameters = testbed_instance._get_parameters(guid)
315     element = testbed_instance._make_tos_queue_filter(parameters)
316     testbed_instance.elements[guid] = element
317
318 def create_application(testbed_instance, guid):
319     parameters = testbed_instance._get_parameters(guid)
320     element = testbed_instance._make_application(parameters)
321     
322     # Just inject configuration stuff
323     element.home_path = "nepi-app-%s" % (guid,)
324     
325     testbed_instance.elements[guid] = element
326
327 def create_dependency(testbed_instance, guid):
328     parameters = testbed_instance._get_parameters(guid)
329     element = testbed_instance._make_dependency(parameters)
330     
331     # Just inject configuration stuff
332     element.home_path = "nepi-dep-%s" % (guid,)
333     
334     testbed_instance.elements[guid] = element
335
336 def create_nepi_dependency(testbed_instance, guid):
337     parameters = testbed_instance._get_parameters(guid)
338     element = testbed_instance._make_nepi_dependency(parameters)
339     
340     # Just inject configuration stuff
341     element.home_path = "nepi-nepi-%s" % (guid,)
342     
343     testbed_instance.elements[guid] = element
344
345 def create_ns3_dependency(testbed_instance, guid):
346     parameters = testbed_instance._get_parameters(guid)
347     element = testbed_instance._make_ns3_dependency(parameters)
348     
349     # Just inject configuration stuff
350     element.home_path = "nepi-ns3-%s" % (guid,)
351     
352     testbed_instance.elements[guid] = element
353
354 def create_multicast_forwarder(testbed_instance, guid):
355     parameters = testbed_instance._get_parameters(guid)
356     element = testbed_instance._make_multicast_forwarder(parameters)
357     
358     # Just inject configuration stuff
359     element.home_path = "nepi-mcfwd-%s" % (guid,)
360     
361     testbed_instance.elements[guid] = element
362
363 def create_multicast_announcer(testbed_instance, guid):
364     parameters = testbed_instance._get_parameters(guid)
365     element = testbed_instance._make_multicast_announcer(parameters)
366     
367     # Just inject configuration stuff
368     element.home_path = "nepi-mcann-%s" % (guid,)
369     
370     testbed_instance.elements[guid] = element
371
372 def create_multicast_router(testbed_instance, guid):
373     parameters = testbed_instance._get_parameters(guid)
374     element = testbed_instance._make_multicast_router(parameters)
375     
376     # Just inject configuration stuff
377     element.home_path = "nepi-mcrt-%s" % (guid,)
378     
379     testbed_instance.elements[guid] = element
380
381 def create_internet(testbed_instance, guid):
382     parameters = testbed_instance._get_parameters(guid)
383     element = testbed_instance._make_internet(parameters)
384     testbed_instance.elements[guid] = element
385
386 def create_netpipe(testbed_instance, guid):
387     parameters = testbed_instance._get_parameters(guid)
388     element = testbed_instance._make_netpipe(parameters)
389     testbed_instance.elements[guid] = element
390
391 ### Start/Stop functions ###
392
393 def start_application(testbed_instance, guid):
394     parameters = testbed_instance._get_parameters(guid)
395     traces = testbed_instance._get_traces(guid)
396     app = testbed_instance.elements[guid]
397     
398     app.stdout = "stdout" in traces
399     app.stderr = "stderr" in traces
400     app.buildlog = "buildlog" in traces
401     app.outout = "output" in traces
402     
403     app.start()
404
405 def stop_application(testbed_instance, guid):
406     app = testbed_instance.elements[guid]
407     app.kill()
408
409 ### Status functions ###
410
411 def status_application(testbed_instance, guid):
412     if guid not in testbed_instance.elements.keys():
413         return AS.STATUS_NOT_STARTED
414     
415     app = testbed_instance.elements[guid]
416     return app.status()
417
418 ### Configure functions ###
419
420 def configure_nodeiface(testbed_instance, guid):
421     element = testbed_instance._elements[guid]
422     
423     # Cannot explicitly configure addresses
424     if guid in testbed_instance._add_address:
425         raise ValueError, "Cannot explicitly set address of public PlanetLab interface"
426     
427     # Get siblings
428     node_guid = testbed_instance.get_connected(guid, "node", "devs")[0]
429     dev_guids = testbed_instance.get_connected(node_guid, "node", "devs")
430     siblings = [ self._element[dev_guid] 
431                  for dev_guid in dev_guids
432                  if dev_guid != guid ]
433     
434     # Fetch address from PLC api
435     element.pick_iface(siblings)
436     
437     # Do some validations
438     element.validate()
439
440 def preconfigure_tuniface(testbed_instance, guid):
441     element = testbed_instance._elements[guid]
442     
443     # Set custom addresses if any, and if not set already
444     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
445         addresses = testbed_instance._add_address[guid]
446         for address in addresses:
447             (address, netprefix, broadcast) = address
448             element.add_address(address, netprefix, broadcast)
449     
450     # Link to external interface, if any
451     for iface in testbed_instance._elements.itervalues():
452         if isinstance(iface, testbed_instance._interfaces.NodeIface) and iface.node is element.node and iface.has_internet:
453             element.external_iface = iface
454             break
455
456     # Set standard TUN attributes
457     if (not element.tun_addr or not element.tun_port) and element.external_iface:
458         element.tun_addr = element.external_iface.address
459         element.tun_port = testbed_instance.tapPortBase + int(guid)
460
461     # Set enabled traces
462     traces = testbed_instance._get_traces(guid)
463     for capmode in ('pcap', 'packets'):
464         if capmode in traces:
465             element.capture = capmode
466             break
467     else:
468         element.capture = False
469     
470     # Do some validations
471     element.validate()
472     
473     # First-phase setup
474     element.prepare('tun-%s' % (guid,))
475
476 def postconfigure_tuniface(testbed_instance, guid):
477     element = testbed_instance._elements[guid]
478     
479     # Second-phase setup
480     element.launch()
481     
482 def prestart_tuniface(testbed_instance, guid):
483     element = testbed_instance._elements[guid]
484     
485     # Second-phase setup
486     element.wait()
487
488 def configure_node(testbed_instance, guid):
489     node = testbed_instance._elements[guid]
490     
491     # Just inject configuration stuff
492     node.home_path = "nepi-node-%s" % (guid,)
493     node.ident_path = testbed_instance.sliceSSHKey
494     node.slicename = testbed_instance.slicename
495     
496     # Do some validations
497     node.validate()
498     
499     # this will be done in parallel in all nodes
500     # this call only spawns the process
501     node.install_dependencies()
502
503 def configure_node_routes(testbed_instance, guid):
504     node = testbed_instance._elements[guid]
505     routes = testbed_instance._add_route.get(guid)
506     
507     if routes:
508         devs = [ dev
509             for dev_guid in testbed_instance.get_connected(guid, "devs", "node")
510             for dev in ( testbed_instance._elements.get(dev_guid) ,)
511             if dev and isinstance(dev, testbed_instance._interfaces.TunIface) ]
512     
513         vsys = testbed_instance.vsys_vnet
514         
515         node.configure_routes(routes, devs, vsys)
516
517 def configure_application(testbed_instance, guid):
518     app = testbed_instance._elements[guid]
519     
520     # Do some validations
521     app.validate()
522     
523     # Wait for dependencies
524     app.node.wait_dependencies()
525     
526     # Install stuff
527     app.async_setup()
528
529 def configure_dependency(testbed_instance, guid):
530     dep = testbed_instance._elements[guid]
531     
532     # Do some validations
533     dep.validate()
534     
535     # Wait for dependencies
536     dep.node.wait_dependencies()
537     
538     # Install stuff
539     dep.async_setup()
540
541 def configure_announcer(testbed_instance, guid):
542     # Link ifaces
543     fwd = testbed_instance._elements[guid]
544     fwd.ifaces = [ dev
545         for node_guid in testbed_instance.get_connected(guid, "node", "apps")
546         for dev_guid in testbed_instance.get_connected(node_guid, "devs", "node")
547         for dev in ( testbed_instance._elements.get(dev_guid) ,)
548         if dev and isinstance(dev, testbed_instance._interfaces.TunIface)
549             and dev.multicast ]
550     
551     # Install stuff
552     configure_dependency(testbed_instance, guid)
553
554 def configure_forwarder(testbed_instance, guid):
555     configure_announcer(testbed_instance, guid)
556     
557     # Link ifaces to forwarder
558     fwd = testbed_instance._elements[guid]
559     for iface in fwd.ifaces:
560         iface.multicast_forwarder = '/var/run/mcastfwd'
561
562 def configure_router(testbed_instance, guid):
563     # Link ifaces
564     rt = testbed_instance._elements[guid]
565     rt.nonifaces = [ dev
566         for fwd_guid in testbed_instance.get_connected(guid, "fwd", "router")
567         for node_guid in testbed_instance.get_connected(fwd_guid, "node", "apps")
568         for dev_guid in testbed_instance.get_connected(node_guid, "devs", "node")
569         for dev in ( testbed_instance._elements.get(dev_guid) ,)
570         if dev and isinstance(dev, testbed_instance._interfaces.TunIface)
571             and not dev.multicast ]
572     
573     # Install stuff
574     configure_dependency(testbed_instance, guid)
575
576 def configure_netpipe(testbed_instance, guid):
577     netpipe = testbed_instance._elements[guid]
578     
579     # Do some validations
580     netpipe.validate()
581     
582     # Wait for dependencies
583     netpipe.node.wait_dependencies()
584     
585     # Install rules
586     netpipe.configure()
587
588 ### Factory information ###
589
590 connector_types = dict({
591     "apps": dict({
592                 "help": "Connector from node to applications", 
593                 "name": "apps",
594                 "max": -1, 
595                 "min": 0
596             }),
597     "devs": dict({
598                 "help": "Connector from node to network interfaces", 
599                 "name": "devs",
600                 "max": -1, 
601                 "min": 0
602             }),
603     "deps": dict({
604                 "help": "Connector from node to application dependencies "
605                         "(packages and applications that need to be installed)", 
606                 "name": "deps",
607                 "max": -1, 
608                 "min": 0
609             }),
610     "inet": dict({
611                 "help": "Connector from network interfaces to the internet", 
612                 "name": "inet",
613                 "max": 1, 
614                 "min": 1
615             }),
616     "node": dict({
617                 "help": "Connector to a Node", 
618                 "name": "node",
619                 "max": 1, 
620                 "min": 1
621             }),
622     "router": dict({
623                 "help": "Connector to a routing daemon", 
624                 "name": "router",
625                 "max": 1, 
626                 "min": 1
627             }),
628     "fwd": dict({
629                 "help": "Forwarder this routing daemon communicates with", 
630                 "name": "fwd",
631                 "max": 1, 
632                 "min": 1
633             }),
634     "pipes": dict({
635                 "help": "Connector to a NetPipe", 
636                 "name": "pipes",
637                 "max": 2, 
638                 "min": 0
639             }),
640     
641     "tcp": dict({
642                 "help": "ip-ip tunneling over TCP link", 
643                 "name": "tcp",
644                 "max": 1, 
645                 "min": 0
646             }),
647     "udp": dict({
648                 "help": "ip-ip tunneling over UDP datagrams", 
649                 "name": "udp",
650                 "max": 1, 
651                 "min": 0
652             }),
653     "gre": dict({
654                 "help": "IP or Ethernet tunneling using the GRE protocol", 
655                 "name": "gre",
656                 "max": 1, 
657                 "min": 0
658             }),
659     "fd->": dict({
660                 "help": "TUN device file descriptor provider", 
661                 "name": "fd->",
662                 "max": 1, 
663                 "min": 0
664             }),
665     "->fd": dict({
666                 "help": "TUN device file descriptor slot", 
667                 "name": "->fd",
668                 "max": 1, 
669                 "min": 0
670             }),
671    })
672
673 connections = [
674     dict({
675         "from": (TESTBED_ID, NODE, "devs"),
676         "to":   (TESTBED_ID, NODEIFACE, "node"),
677         "init_code": connect_node_iface_node,
678         "can_cross": False
679     }),
680     dict({
681         "from": (TESTBED_ID, NODE, "devs"),
682         "to":   (TESTBED_ID, TUNIFACE, "node"),
683         "init_code": connect_tun_iface_node,
684         "can_cross": False
685     }),
686     dict({
687         "from": (TESTBED_ID, NODE, "devs"),
688         "to":   (TESTBED_ID, TAPIFACE, "node"),
689         "init_code": connect_tun_iface_node,
690         "can_cross": False
691     }),
692     dict({
693         "from": (TESTBED_ID, NODEIFACE, "inet"),
694         "to":   (TESTBED_ID, INTERNET, "devs"),
695         "init_code": connect_node_iface_inet,
696         "can_cross": False
697     }),
698     dict({
699         "from": (TESTBED_ID, NODE, "apps"),
700         "to":   (TESTBED_ID, (APPLICATION, MULTICASTANNOUNCER), "node"),
701         "init_code": connect_dep,
702         "can_cross": False
703     }),
704     dict({
705         "from": (TESTBED_ID, NODE, "deps"),
706         "to":   (TESTBED_ID, (DEPENDENCY, NEPIDEPENDENCY, NS3DEPENDENCY), "node"),
707         "init_code": connect_dep,
708         "can_cross": False
709     }),
710     dict({
711         "from": (TESTBED_ID, NODE, "pipes"),
712         "to":   (TESTBED_ID, NETPIPE, "node"),
713         "init_code": connect_node_netpipe,
714         "can_cross": False
715     }),
716     dict({
717         "from": (TESTBED_ID, NODE, "apps"),
718         "to":   (TESTBED_ID, MULTICASTFORWARDER, "node"),
719         "init_code": connect_forwarder,
720         "can_cross": False
721     }),
722     dict({
723         "from": (TESTBED_ID, MULTICASTFORWARDER, "router"),
724         "to":   (TESTBED_ID, MULTICASTROUTER, "fwd"),
725         "init_code": connect_router,
726         "can_cross": False
727     }),
728     dict({
729         "from": (TESTBED_ID, TUNIFACE, "tcp"),
730         "to":   (TESTBED_ID, TUNIFACE, "tcp"),
731         "init_code": functools.partial(connect_tun_iface_peer,"tcp"),
732         "can_cross": False
733     }),
734     dict({
735         "from": (TESTBED_ID, TUNIFACE, "udp"),
736         "to":   (TESTBED_ID, TUNIFACE, "udp"),
737         "init_code": functools.partial(connect_tun_iface_peer,"udp"),
738         "can_cross": False
739     }),
740     dict({
741         "from": (TESTBED_ID, TUNIFACE, "gre"),
742         "to":   (TESTBED_ID, TUNIFACE, "gre"),
743         "init_code": functools.partial(connect_tun_iface_peer,"gre"),
744         "can_cross": False
745     }),
746     dict({
747         "from": (TESTBED_ID, TUNIFACE, "fd->"),
748         "to":   (TESTBED_ID, TUNFILTERS, "->fd"),
749         "init_code": connect_tun_iface_filter,
750         "can_cross": False
751     }),
752     dict({
753         "from": (TESTBED_ID, TUNFILTERS, "tcp"),
754         "to":   (TESTBED_ID, TUNIFACE, "tcp"),
755         "init_code": functools.partial(connect_filter_peer,"tcp"),
756         "can_cross": False
757     }),
758     dict({
759         "from": (TESTBED_ID, TUNFILTERS, "udp"),
760         "to":   (TESTBED_ID, TUNIFACE, "udp"),
761         "init_code": functools.partial(connect_filter_peer,"udp"),
762         "can_cross": False
763     }),
764     dict({
765         "from": (TESTBED_ID, TAPIFACE, "tcp"),
766         "to":   (TESTBED_ID, TAPIFACE, "tcp"),
767         "init_code": functools.partial(connect_tun_iface_peer,"tcp"),
768         "can_cross": False
769     }),
770     dict({
771         "from": (TESTBED_ID, TAPIFACE, "udp"),
772         "to":   (TESTBED_ID, TAPIFACE, "udp"),
773         "init_code": functools.partial(connect_tun_iface_peer,"udp"),
774         "can_cross": False
775     }),
776     dict({
777         "from": (TESTBED_ID, TAPIFACE, "gre"),
778         "to":   (TESTBED_ID, TAPIFACE, "gre"),
779         "init_code": functools.partial(connect_tun_iface_peer,"gre"),
780         "can_cross": False
781     }),
782     dict({
783         "from": (TESTBED_ID, TAPIFACE, "fd->"),
784         "to":   (TESTBED_ID, TAPFILTERS, "->fd"),
785         "init_code": connect_tun_iface_filter,
786         "can_cross": False
787     }),
788     dict({
789         "from": (TESTBED_ID, TAPFILTERS, "tcp"),
790         "to":   (TESTBED_ID, TAPIFACE, "tcp"),
791         "init_code": functools.partial(connect_filter_peer,"tcp"),
792         "can_cross": False
793     }),
794     dict({
795         "from": (TESTBED_ID, TAPFILTERS, "udp"),
796         "to":   (TESTBED_ID, TAPIFACE, "udp"),
797         "init_code": functools.partial(connect_filter_peer,"udp"),
798         "can_cross": False
799     }),
800     dict({
801         "from": (TESTBED_ID, TUNFILTERS, "tcp"),
802         "to":   (TESTBED_ID, TUNFILTERS, "tcp"),
803         "init_code": functools.partial(connect_filter_filter,"tcp"),
804         "can_cross": False
805     }),
806     dict({
807         "from": (TESTBED_ID, TUNFILTERS, "udp"),
808         "to":   (TESTBED_ID, TUNFILTERS, "udp"),
809         "init_code": functools.partial(connect_filter_filter,"udp"),
810         "can_cross": False
811     }),
812     dict({
813         "from": (TESTBED_ID, TAPFILTERS, "tcp"),
814         "to":   (TESTBED_ID, TAPFILTERS, "tcp"),
815         "init_code": functools.partial(connect_filter_filter,"tcp"),
816         "can_cross": False
817     }),
818     dict({
819         "from": (TESTBED_ID, TAPFILTERS, "udp"),
820         "to":   (TESTBED_ID, TAPFILTERS, "udp"),
821         "init_code": functools.partial(connect_filter_filter,"udp"),
822         "can_cross": False
823     }),
824     dict({
825         "from": (TESTBED_ID, TUNIFACE, "tcp"),
826         "to":   (None, None, "tcp"),
827         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"tcp"),
828         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"tcp"),
829         "can_cross": True
830     }),
831     dict({
832         "from": (TESTBED_ID, TUNIFACE, "udp"),
833         "to":   (None, None, "udp"),
834         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"udp"),
835         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"udp"),
836         "can_cross": True
837     }),
838     dict({
839         "from": (TESTBED_ID, TUNIFACE, "fd->"),
840         "to":   (None, None, "->fd"),
841         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"fd"),
842         "can_cross": True
843     }),
844     dict({
845         "from": (TESTBED_ID, TUNIFACE, "gre"),
846         "to":   (None, None, "gre"),
847         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"gre"),
848         "can_cross": True
849     }),
850     dict({
851         "from": (TESTBED_ID, TAPIFACE, "tcp"),
852         "to":   (None, None, "tcp"),
853         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"tcp"),
854         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"tcp"),
855         "can_cross": True
856     }),
857     dict({
858         "from": (TESTBED_ID, TAPIFACE, "udp"),
859         "to":   (None, None, "udp"),
860         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"udp"),
861         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"udp"),
862         "can_cross": True
863     }),
864     dict({
865         "from": (TESTBED_ID, TAPIFACE, "fd->"),
866         "to":   (None, None, "->fd"),
867         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"fd"),
868         "can_cross": True
869     }),
870     # EGRE is an extension of PlanetLab, so we can't connect externally
871     # if the other testbed isn't another PlanetLab
872     dict({
873         "from": (TESTBED_ID, TAPIFACE, "gre"),
874         "to":   (TESTBED_ID, None, "gre"),
875         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"gre"),
876         "can_cross": True
877     }),
878     dict({
879         "from": (TESTBED_ID, ALLFILTERS, "tcp"),
880         "to":   (None, None, "tcp"),
881         "init_code": functools.partial(crossconnect_filter_peer_init,"tcp"),
882         "compl_code": functools.partial(crossconnect_filter_peer_compl,"tcp"),
883         "can_cross": True
884     }),
885     dict({
886         "from": (TESTBED_ID, ALLFILTERS, "udp"),
887         "to":   (None, None, "udp"),
888         "init_code": functools.partial(crossconnect_filter_peer_init,"udp"),
889         "compl_code": functools.partial(crossconnect_filter_peer_compl,"udp"),
890         "can_cross": True
891     }),
892 ]
893
894 attributes = dict({
895     "forward_X11": dict({      
896                 "name": "forward_X11",
897                 "help": "Forward x11 from main namespace to the node",
898                 "type": Attribute.BOOL, 
899                 "value": False,
900                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
901                 "validation_function": validation.is_bool,
902             }),
903     "hostname": dict({      
904                 "name": "hostname",
905                 "help": "Constrain hostname during resource discovery. May use wildcards.",
906                 "type": Attribute.STRING, 
907                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
908                 "validation_function": validation.is_string,
909             }),
910     "city": dict({      
911                 "name": "city",
912                 "help": "Constrain location (city) during resource discovery. May use wildcards.",
913                 "type": Attribute.STRING, 
914                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
915                 "validation_function": validation.is_string,
916             }),
917     "country": dict({      
918                 "name": "hostname",
919                 "help": "Constrain location (country) during resource discovery. May use wildcards.",
920                 "type": Attribute.STRING, 
921                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
922                 "validation_function": validation.is_string,
923             }),
924     "region": dict({      
925                 "name": "hostname",
926                 "help": "Constrain location (region) during resource discovery. May use wildcards.",
927                 "type": Attribute.STRING, 
928                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
929                 "validation_function": validation.is_string,
930             }),
931     "architecture": dict({      
932                 "name": "architecture",
933                 "help": "Constrain architexture during resource discovery.",
934                 "type": Attribute.ENUM, 
935                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
936                 "allowed": ["x86_64",
937                             "i386"],
938                 "validation_function": validation.is_enum,
939             }),
940     "operating_system": dict({      
941                 "name": "operatingSystem",
942                 "help": "Constrain operating system during resource discovery.",
943                 "type": Attribute.ENUM, 
944                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
945                 "allowed": ["f8",
946                             "f12",
947                             "f14",
948                             "centos",
949                             "other"],
950                 "validation_function": validation.is_enum,
951             }),
952     "site": dict({      
953                 "name": "site",
954                 "help": "Constrain the PlanetLab site this node should reside on.",
955                 "type": Attribute.ENUM, 
956                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
957                 "allowed": ["PLE",
958                             "PLC",
959                             "PLJ"],
960                 "validation_function": validation.is_enum,
961             }),
962     "min_reliability": dict({
963                 "name": "minReliability",
964                 "help": "Constrain reliability while picking PlanetLab nodes. Specifies a lower acceptable bound.",
965                 "type": Attribute.DOUBLE,
966                 "range": (0,100),
967                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
968                 "validation_function": validation.is_number,
969             }),
970     "max_reliability": dict({
971                 "name": "maxReliability",
972                 "help": "Constrain reliability while picking PlanetLab nodes. Specifies an upper acceptable bound.",
973                 "type": Attribute.DOUBLE,
974                 "range": (0,100),
975                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
976                 "validation_function": validation.is_number,
977             }),
978     "min_bandwidth": dict({
979                 "name": "minBandwidth",
980                 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies a lower acceptable bound.",
981                 "type": Attribute.DOUBLE,
982                 "range": (0,2**31),
983                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
984                 "validation_function": validation.is_number,
985             }),
986     "max_bandwidth": dict({
987                 "name": "maxBandwidth",
988                 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies an upper acceptable bound.",
989                 "type": Attribute.DOUBLE,
990                 "range": (0,2**31),
991                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
992                 "validation_function": validation.is_number,
993             }),
994     "min_load": dict({
995                 "name": "minLoad",
996                 "help": "Constrain node load average while picking PlanetLab nodes. Specifies a lower acceptable bound.",
997                 "type": Attribute.DOUBLE,
998                 "range": (0,2**31),
999                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1000                 "validation_function": validation.is_number,
1001             }),
1002     "max_load": dict({
1003                 "name": "maxLoad",
1004                 "help": "Constrain node load average while picking PlanetLab nodes. Specifies an upper acceptable bound.",
1005                 "type": Attribute.DOUBLE,
1006                 "range": (0,2**31),
1007                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1008                 "validation_function": validation.is_number,
1009             }),
1010     "min_cpu": dict({
1011                 "name": "minCpu",
1012                 "help": "Constrain available cpu time while picking PlanetLab nodes. Specifies a lower acceptable bound.",
1013                 "type": Attribute.DOUBLE,
1014                 "range": (0,100),
1015                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1016                 "validation_function": validation.is_number,
1017             }),
1018     "max_cpu": dict({
1019                 "name": "maxCpu",
1020                 "help": "Constrain available cpu time while picking PlanetLab nodes. Specifies an upper acceptable bound.",
1021                 "type": Attribute.DOUBLE,
1022                 "range": (0,100),
1023                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1024                 "validation_function": validation.is_number,
1025             }),
1026             
1027     "up": dict({
1028                 "name": "up",
1029                 "help": "Link up",
1030                 "type": Attribute.BOOL,
1031                 "value": False,
1032                 "validation_function": validation.is_bool
1033             }),
1034     "primary": dict({
1035                 "name": "primary",
1036                 "help": "This is the primary interface for the attached node",
1037                 "type": Attribute.BOOL,
1038                 "value": True,
1039                 "validation_function": validation.is_bool
1040             }),
1041     "if_name": dict({
1042                 "name": "if_name",
1043                 "help": "Device name",
1044                 "type": Attribute.STRING,
1045                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1046                 "validation_function": validation.is_string
1047             }),
1048     "mtu":  dict({
1049                 "name": "mtu", 
1050                 "help": "Maximum transmition unit for device",
1051                 "type": Attribute.INTEGER,
1052                 "range": (0,1500),
1053                 "validation_function": validation.is_integer_range(0,1500)
1054             }),
1055     "mask":  dict({
1056                 "name": "mask", 
1057                 "help": "Network mask for the device (eg: 24 for /24 network)",
1058                 "type": Attribute.INTEGER,
1059                 "validation_function": validation.is_integer_range(8,24)
1060             }),
1061     "snat":  dict({
1062                 "name": "snat", 
1063                 "help": "Enable SNAT (source NAT to the internet) no this device",
1064                 "type": Attribute.BOOL,
1065                 "value": False,
1066                 "validation_function": validation.is_bool
1067             }),
1068     "multicast":  dict({
1069                 "name": "multicast", 
1070                 "help": "Enable multicast forwarding on this device. "
1071                         "Note that you still need a multicast routing daemon "
1072                         "in the node.",
1073                 "type": Attribute.BOOL,
1074                 "value": False,
1075                 "validation_function": validation.is_bool
1076             }),
1077     "pointopoint":  dict({
1078                 "name": "pointopoint", 
1079                 "help": "If the interface is a P2P link, the remote endpoint's IP "
1080                         "should be set on this attribute.",
1081                 "type": Attribute.STRING,
1082                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1083                 "validation_function": validation.is_string
1084             }),
1085     "bwlimit":  dict({
1086                 "name": "bwlimit", 
1087                 "help": "Emulated transmission speed (in kbytes per second)",
1088                 "type": Attribute.INTEGER,
1089                 "range" : (1,10*2**20),
1090                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1091                 "validation_function": validation.is_integer
1092             }),
1093     "txqueuelen":  dict({
1094                 "name": "txqueuelen", 
1095                 "help": "Transmission queue length (in packets)",
1096                 "type": Attribute.INTEGER,
1097                 "value": 1000,
1098                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1099                 "range" : (1,10000),
1100                 "validation_function": validation.is_integer
1101             }),
1102             
1103     "command": dict({
1104                 "name": "command",
1105                 "help": "Command line string",
1106                 "type": Attribute.STRING,
1107                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1108                 "validation_function": validation.is_string
1109             }),
1110     "sudo": dict({
1111                 "name": "sudo",
1112                 "help": "Run with root privileges",
1113                 "type": Attribute.BOOL,
1114                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1115                 "value": False,
1116                 "validation_function": validation.is_bool
1117             }),
1118     "stdin": dict({
1119                 "name": "stdin",
1120                 "help": "Standard input",
1121                 "type": Attribute.STRING,
1122                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1123                 "validation_function": validation.is_string
1124             }),
1125             
1126     "depends": dict({
1127                 "name": "depends",
1128                 "help": "Space-separated list of packages required to run the application",
1129                 "type": Attribute.STRING,
1130                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1131                 "validation_function": validation.is_string
1132             }),
1133     "build-depends": dict({
1134                 "name": "buildDepends",
1135                 "help": "Space-separated list of packages required to build the application",
1136                 "type": Attribute.STRING,
1137                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1138                 "validation_function": validation.is_string
1139             }),
1140     "rpm-fusion": dict({
1141                 "name": "rpmFusion",
1142                 "help": "True if required packages can be found in the RpmFusion repository",
1143                 "type": Attribute.BOOL,
1144                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1145                 "value": False,
1146                 "validation_function": validation.is_bool
1147             }),
1148     "sources": dict({
1149                 "name": "sources",
1150                 "help": "Space-separated list of regular files to be deployed in the working path prior to building. "
1151                         "Archives won't be expanded automatically.",
1152                 "type": Attribute.STRING,
1153                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1154                 "validation_function": validation.is_string
1155             }),
1156     "build": dict({
1157                 "name": "build",
1158                 "help": "Build commands to execute after deploying the sources. "
1159                         "Sources will be in the ${SOURCES} folder. "
1160                         "Example: tar xzf ${SOURCES}/my-app.tgz && cd my-app && ./configure && make && make clean.\n"
1161                         "Try to make the commands return with a nonzero exit code on error.\n"
1162                         "Also, do not install any programs here, use the 'install' attribute. This will "
1163                         "help keep the built files constrained to the build folder (which may "
1164                         "not be the home folder), and will result in faster deployment. Also, "
1165                         "make sure to clean up temporary files, to reduce bandwidth usage between "
1166                         "nodes when transferring built packages.",
1167                 "type": Attribute.STRING,
1168                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1169                 "validation_function": validation.is_string
1170             }),
1171     "install": dict({
1172                 "name": "install",
1173                 "help": "Commands to transfer built files to their final destinations. "
1174                         "Sources will be in the initial working folder, and a special "
1175                         "tag ${SOURCES} can be used to reference the experiment's "
1176                         "home folder (where the application commands will run).\n"
1177                         "ALL sources and targets needed for execution must be copied there, "
1178                         "if building has been enabled.\n"
1179                         "That is, 'slave' nodes will not automatically get any source files. "
1180                         "'slave' nodes don't get build dependencies either, so if you need "
1181                         "make and other tools to install, be sure to provide them as "
1182                         "actual dependencies instead.",
1183                 "type": Attribute.STRING,
1184                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1185                 "validation_function": validation.is_string
1186             }),
1187     
1188     "netpipe_mode": dict({      
1189                 "name": "mode",
1190                 "help": "Link mode:\n"
1191                         " * SERVER: applies to incoming connections\n"
1192                         " * CLIENT: applies to outgoing connections\n"
1193                         " * SERVICE: applies to both",
1194                 "type": Attribute.ENUM, 
1195                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1196                 "allowed": ["SERVER",
1197                             "CLIENT",
1198                             "SERVICE"],
1199                 "validation_function": validation.is_enum,
1200             }),
1201     "port_list":  dict({
1202                 "name": "portList", 
1203                 "help": "Port list or range. Eg: '22', '22,23,27', '20-2000'",
1204                 "type": Attribute.STRING,
1205                 "validation_function": is_portlist,
1206             }),
1207     "addr_list":  dict({
1208                 "name": "addrList", 
1209                 "help": "Address list or range. Eg: '127.0.0.1', '127.0.0.1,127.0.1.1', '127.0.0.1/8'",
1210                 "type": Attribute.STRING,
1211                 "validation_function": is_addrlist,
1212             }),
1213     "bw_in":  dict({
1214                 "name": "bwIn", 
1215                 "help": "Inbound bandwidth limit (in Mbit/s)",
1216                 "type": Attribute.DOUBLE,
1217                 "validation_function": validation.is_number,
1218             }),
1219     "bw_out":  dict({
1220                 "name": "bwOut", 
1221                 "help": "Outbound bandwidth limit (in Mbit/s)",
1222                 "type": Attribute.DOUBLE,
1223                 "validation_function": validation.is_number,
1224             }),
1225     "plr_in":  dict({
1226                 "name": "plrIn", 
1227                 "help": "Inbound packet loss rate (0 = no loss, 1 = 100% loss)",
1228                 "type": Attribute.DOUBLE,
1229                 "validation_function": validation.is_number,
1230             }),
1231     "plr_out":  dict({
1232                 "name": "plrOut", 
1233                 "help": "Outbound packet loss rate (0 = no loss, 1 = 100% loss)",
1234                 "type": Attribute.DOUBLE,
1235                 "validation_function": validation.is_number,
1236             }),
1237     "delay_in":  dict({
1238                 "name": "delayIn", 
1239                 "help": "Inbound packet delay (in milliseconds)",
1240                 "type": Attribute.INTEGER,
1241                 "range": (0,60000),
1242                 "validation_function": validation.is_integer,
1243             }),
1244     "delay_out":  dict({
1245                 "name": "delayOut", 
1246                 "help": "Outbound packet delay (in milliseconds)",
1247                 "type": Attribute.INTEGER,
1248                 "range": (0,60000),
1249                 "validation_function": validation.is_integer,
1250             }),
1251     "module": dict({
1252                 "name": "module",
1253                 "help": "Path to a .c or .py source for a filter module, or a binary .so",
1254                 "type": Attribute.STRING,
1255                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1256                 "validation_function": validation.is_string
1257             }),
1258     "args": dict({
1259                 "name": "args",
1260                 "help": "Module arguments - comma-separated list of name=value pairs",
1261                 "type": Attribute.STRING,
1262                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1263                 "validation_function": validation.is_string
1264             }),
1265     "routing_algorithm": dict({      
1266             "name": "algorithm",
1267             "help": "Routing algorithm.",
1268             "value": "dvmrp",
1269             "type": Attribute.ENUM, 
1270             "allowed": ["dvmrp"],
1271             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1272             "validation_function": validation.is_enum,
1273         }),
1274     })
1275
1276 traces = dict({
1277     "stdout": dict({
1278                 "name": "stdout",
1279                 "help": "Standard output stream"
1280               }),
1281     "stderr": dict({
1282                 "name": "stderr",
1283                 "help": "Application standard error",
1284               }),
1285     "buildlog": dict({
1286                 "name": "buildlog",
1287                 "help": "Output of the build process",
1288               }), 
1289     
1290     "netpipe_stats": dict({
1291                 "name": "netpipeStats",
1292                 "help": "Information about rule match counters, packets dropped, etc.",
1293               }),
1294
1295     "packets": dict({
1296                 "name": "packets",
1297                 "help": "Detailled log of all packets going through the interface",
1298               }),
1299     "pcap": dict({
1300                 "name": "pcap",
1301                 "help": "PCAP trace of all packets going through the interface",
1302               }),
1303     "output": dict({
1304                 "name": "output",
1305                 "help": "Extra output trace for applications. When activated this trace can be referenced with wildcard a reference from an Application command line. Ex: command: 'tcpdump -w {#[elemet-label].trace[trace-id].[name|path]#}' ",
1306               }),
1307     "dropped_stats": dict({
1308                 "name": "dropped_stats",
1309                 "help": "Information on dropped packets on a filer or queue associated to a network interface",
1310             }),
1311     })
1312
1313 create_order = [ 
1314     INTERNET, NODE, NODEIFACE, CLASSQUEUEFILTER, TOSQUEUEFILTER, 
1315     MULTICASTANNOUNCER, MULTICASTFORWARDER, MULTICASTROUTER, 
1316     TUNFILTER, TAPIFACE, TUNIFACE, NETPIPE, 
1317     NEPIDEPENDENCY, NS3DEPENDENCY, DEPENDENCY, APPLICATION ]
1318
1319 configure_order = [ 
1320     INTERNET, Parallel(NODE), 
1321     NODEIFACE, 
1322     Parallel(MULTICASTANNOUNCER), Parallel(MULTICASTFORWARDER), Parallel(MULTICASTROUTER), 
1323     Parallel(TAPIFACE), Parallel(TUNIFACE), NETPIPE, 
1324     Parallel(NEPIDEPENDENCY), Parallel(NS3DEPENDENCY), Parallel(DEPENDENCY), Parallel(APPLICATION) ]
1325
1326 # Start (and prestart) node after ifaces, because the node needs the ifaces in order to set up routes
1327 start_order = [ INTERNET, 
1328     NODEIFACE, 
1329     Parallel(TAPIFACE), Parallel(TUNIFACE), 
1330     Parallel(NODE), NETPIPE, 
1331     Parallel(MULTICASTANNOUNCER), Parallel(MULTICASTFORWARDER), Parallel(MULTICASTROUTER), 
1332     Parallel(NEPIDEPENDENCY), Parallel(NS3DEPENDENCY), Parallel(DEPENDENCY), Parallel(APPLICATION) ]
1333
1334 # cleanup order
1335 shutdown_order = [ 
1336     Parallel(APPLICATION), 
1337     Parallel(MULTICASTROUTER), Parallel(MULTICASTFORWARDER), Parallel(MULTICASTANNOUNCER), 
1338     Parallel(TAPIFACE), Parallel(TUNIFACE), Parallel(NETPIPE), 
1339     Parallel(NEPIDEPENDENCY), Parallel(NS3DEPENDENCY), Parallel(DEPENDENCY), 
1340     NODEIFACE, Parallel(NODE) ]
1341
1342 factories_info = dict({
1343     NODE: dict({
1344             "help": "Virtualized Node (V-Server style)",
1345             "category": FC.CATEGORY_NODES,
1346             "create_function": create_node,
1347             "preconfigure_function": configure_node,
1348             "prestart_function": configure_node_routes,
1349             "box_attributes": [
1350                 "forward_X11",
1351                 "hostname",
1352                 "architecture",
1353                 "operating_system",
1354                 "site",
1355                 "min_reliability",
1356                 "max_reliability",
1357                 "min_bandwidth",
1358                 "max_bandwidth",
1359                 
1360                 # NEPI-in-NEPI attributes
1361                 ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP,
1362             ],
1363             "connector_types": ["devs", "apps", "pipes", "deps"],
1364             "tags": [tags.NODE, tags.ALLOW_ROUTES],
1365        }),
1366     NODEIFACE: dict({
1367             "help": "External network interface - they cannot be brought up or down, and they MUST be connected to the internet.",
1368             "category": FC.CATEGORY_DEVICES,
1369             "create_function": create_nodeiface,
1370             "preconfigure_function": configure_nodeiface,
1371             "box_attributes": [ ],
1372             "connector_types": ["node", "inet"],
1373             "tags": [tags.INTERFACE, tags.HAS_ADDRESSES],
1374         }),
1375     TUNIFACE: dict({
1376             "help": "Virtual TUN network interface (layer 3)",
1377             "category": FC.CATEGORY_DEVICES,
1378             "create_function": create_tuniface,
1379             "preconfigure_function": preconfigure_tuniface,
1380             "configure_function": postconfigure_tuniface,
1381             "prestart_function": prestart_tuniface,
1382             "box_attributes": [
1383                 "up", "if_name", "mtu", "snat", "pointopoint", "multicast", "bwlimit",
1384                 "txqueuelen",
1385                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1386             ],
1387             "traces": ["packets", "pcap"],
1388             "connector_types": ["node","udp","tcp","fd->","gre"],
1389             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
1390         }),
1391     TAPIFACE: dict({
1392             "help": "Virtual TAP network interface (layer 2)",
1393             "category": FC.CATEGORY_DEVICES,
1394             "create_function": create_tapiface,
1395             "preconfigure_function": preconfigure_tuniface,
1396             "configure_function": postconfigure_tuniface,
1397             "prestart_function": prestart_tuniface,
1398             "box_attributes": [
1399                 "up", "if_name", "mtu", "snat", "pointopoint", "multicast", "bwlimit",
1400                 "txqueuelen",
1401                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1402             ],
1403             "traces": ["packets", "pcap"],
1404             "connector_types": ["node","udp","tcp","fd->","gre"],
1405             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
1406         }),
1407     TUNFILTER: dict({
1408             "help": "TUN/TAP stream filter\n\n"
1409                     "If specified, it should be either a .py or .so module. "
1410                     "It will be loaded, and all incoming and outgoing packets "
1411                     "will be routed through it. The filter will not be responsible "
1412                     "for buffering, packet queueing is performed in tun_connect "
1413                     "already, so it should not concern itself with it. It should "
1414                     "not, however, block in one direction if the other is congested.\n"
1415                     "\n"
1416                     "Modules are expected to have the following methods:\n"
1417                     "\tinit(**args)\n"
1418                     "\t\tIf arguments are given, this method will be called with the\n"
1419                     "\t\tgiven arguments (as keyword args in python modules, or a single\n"
1420                     "\taccept_packet(packet, direction):\n"
1421                     "\t\tDecide whether to drop the packet. Direction is 0 for packets "
1422                         "coming from the local side to the remote, and 1 is for packets "
1423                         "coming from the remote side to the local. Return a boolean, "
1424                         "true if the packet is not to be dropped.\n"
1425                     "\tfilter_init():\n"
1426                     "\t\tInitializes a filtering pipe (filter_run). It should "
1427                         "return two file descriptors to use as a bidirectional "
1428                         "pipe: local and remote. 'local' is where packets from the "
1429                         "local side will be written to. After filtering, those packets "
1430                         "should be written to 'remote', where tun_connect will read "
1431                         "from, and it will forward them to the remote peer. "
1432                         "Packets from the remote peer will be written to 'remote', "
1433                         "where the filter is expected to read from, and eventually "
1434                         "forward them to the local side. If the file descriptors are "
1435                         "not nonblocking, they will be set to nonblocking. So it's "
1436                         "better to set them from the start like that.\n"
1437                     "\tfilter_run(local, remote):\n"
1438                     "\t\tIf filter_init is provided, it will be called repeatedly, "
1439                         "in a separate thread until the process is killed. It should "
1440                         "sleep at most for a second.\n"
1441                     "\tfilter_close(local, remote):\n"
1442                     "\t\tCalled then the process is killed, if filter_init was provided. "
1443                         "It should, among other things, close the file descriptors.\n"
1444                     "\n"
1445                     "Python modules are expected to return a tuple in filter_init, "
1446                     "either of file descriptors or file objects, while native ones "
1447                     "will receive two int*.\n"
1448                     "\n"
1449                     "Python modules can additionally contain a custom queue class "
1450                     "that will replace the FIFO used by default. The class should "
1451                     "be named 'queueclass' and contain an interface compatible with "
1452                     "collections.deque. That is, indexing (especiall for q[0]), "
1453                     "bool(q), popleft, appendleft, pop (right), append (right), "
1454                     "len(q) and clear.",
1455             "category": FC.CATEGORY_CHANNELS,
1456             "create_function": create_tunfilter,
1457             "box_attributes": [
1458                 "module", "args",
1459                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1460             ],
1461             "connector_types": ["->fd","udp","tcp"],
1462         }),
1463     CLASSQUEUEFILTER : dict({
1464             "help": "TUN classfull queue, uses a separate queue for each user-definable class.\n\n"
1465                     "It takes two arguments, both of which have sensible defaults:\n"
1466                     "\tsize: the base size of each class' queue\n"
1467                     "\tclasses: the class definitions, which follow the following syntax:\n"
1468                     '\t   <CLASSLIST> ::= <CLASS> ":" CLASSLIST\n'
1469                     '\t                |  <CLASS>\n'
1470                     '\t   <CLASS>     ::= <PROTOLIST> "*" <PRIORITYSPEC>\n'
1471                     '\t                |  <DFLTCLASS>\n'
1472                     '\t   <DFLTCLASS> ::= "*" <PRIORITYSPEC>\n'
1473                     '\t   <PROTOLIST> ::= <PROTO> "." <PROTOLIST>\n'
1474                     '\t                |  <PROTO>\n'
1475                     '\t   <PROTO>     ::= <NAME> | <NUMBER>\n'
1476                     '\t   <NAME>      ::= --see http://en.wikipedia.org/wiki/List_of_IP_protocol_numbers --\n'
1477                     '\t                   --only in lowercase, with special characters removed--\n'
1478                     '\t                   --or see below--\n'
1479                     '\t   <NUMBER>    ::= [0-9]+\n'
1480                     '\t   <PRIORITYSPEC> ::= <THOUGHPUT> [ "#" <SIZE> ] [ "p" <PRIORITY> ]\n'
1481                     '\t   <THOUGHPUT> ::= NUMBER -- default 1\n'
1482                     '\t   <PRIORITY>  ::= NUMBER -- default 0\n'
1483                     '\t   <SIZE>      ::= NUMBER -- default 1\n'
1484                     "\n"
1485                     "Size, thoughput and priority are all relative terms. "
1486                     "Sizes are multipliers for the size argument, thoughput "
1487                     "is applied relative to other classes and the same with "
1488                     "priority.",
1489             "category": FC.CATEGORY_CHANNELS,
1490             "create_function": create_classqueuefilter,
1491             "box_attributes": [
1492                 "args",
1493                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1494             ],
1495             "connector_types": ["->fd","udp","tcp"],
1496             "traces": ["dropped_stats"],
1497         }),
1498     TOSQUEUEFILTER : dict({
1499             "help": "TUN classfull queue that classifies according to the TOS (RFC 791) IP field.\n\n"
1500                     "It takes a size argument that specifies the size of each class. As TOS is a "
1501                     "subset of DiffServ, this queue half-implements DiffServ.",
1502             "category": FC.CATEGORY_CHANNELS,
1503             "create_function": create_tosqueuefilter,
1504             "box_attributes": [
1505                 "args",
1506                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1507             ],
1508             "connector_types": ["->fd","udp","tcp"],
1509         }),
1510
1511     APPLICATION: dict({
1512             "help": "Generic executable command line application",
1513             "category": FC.CATEGORY_APPLICATIONS,
1514             "create_function": create_application,
1515             "start_function": start_application,
1516             "status_function": status_application,
1517             "stop_function": stop_application,
1518             "configure_function": configure_application,
1519             "box_attributes": ["command", "sudo", "stdin",
1520                                "depends", "build-depends", "build", "install",
1521                                "sources", "rpm-fusion" ],
1522             "connector_types": ["node"],
1523             "traces": ["stdout", "stderr", "buildlog", "output"],
1524             "tags": [tags.APPLICATION],
1525         }),
1526     DEPENDENCY: dict({
1527             "help": "Requirement for package or application to be installed on some node",
1528             "category": FC.CATEGORY_APPLICATIONS,
1529             "create_function": create_dependency,
1530             "preconfigure_function": configure_dependency,
1531             "box_attributes": ["depends", "build-depends", "build", "install",
1532                                "sources", "rpm-fusion" ],
1533             "connector_types": ["node"],
1534             "traces": ["buildlog"],
1535         }),
1536     NEPIDEPENDENCY: dict({
1537             "help": "Requirement for NEPI inside NEPI - required to run testbed instances inside a node",
1538             "category": FC.CATEGORY_APPLICATIONS,
1539             "create_function": create_nepi_dependency,
1540             "preconfigure_function": configure_dependency,
1541             "box_attributes": [],
1542             "connector_types": ["node"],
1543             "traces": ["buildlog"],
1544         }),
1545     NS3DEPENDENCY: dict({
1546             "help": "Requirement for NS3 inside NEPI - required to run NS3 testbed instances inside a node. It also needs NepiDependency.",
1547             "category": FC.CATEGORY_APPLICATIONS,
1548             "create_function": create_ns3_dependency,
1549             "preconfigure_function": configure_dependency,
1550             "box_attributes": [ ],
1551             "connector_types": ["node"],
1552             "traces": ["buildlog"],
1553         }),
1554     MULTICASTFORWARDER: dict({
1555             "help": "This application installs a userspace packet forwarder "
1556                     "that, when connected to a node, filters all packets "
1557                     "flowing through multicast-capable virtual interfaces "
1558                     "and applies custom-specified routing policies.",
1559             "category": FC.CATEGORY_APPLICATIONS,
1560             "create_function": create_multicast_forwarder,
1561             "preconfigure_function": configure_forwarder,
1562             "start_function": start_application,
1563             "status_function": status_application,
1564             "stop_function": stop_application,
1565             "box_attributes": [ ],
1566             "connector_types": ["node","router"],
1567             "traces": ["buildlog","stderr"],
1568         }),
1569     MULTICASTANNOUNCER: dict({
1570             "help": "This application installs a userspace daemon that "
1571                     "monitors multicast membership and announces it on all "
1572                     "multicast-capable interfaces.\n"
1573                     "This does not usually happen automatically on PlanetLab slivers.",
1574             "category": FC.CATEGORY_APPLICATIONS,
1575             "create_function": create_multicast_announcer,
1576             "preconfigure_function": configure_announcer,
1577             "start_function": start_application,
1578             "status_function": status_application,
1579             "stop_function": stop_application,
1580             "box_attributes": [ ],
1581             "connector_types": ["node"],
1582             "traces": ["buildlog","stderr"],
1583         }),
1584     MULTICASTROUTER: dict({
1585             "help": "This application installs a userspace daemon that "
1586                     "monitors multicast membership and announces it on all "
1587                     "multicast-capable interfaces.\n"
1588                     "This does not usually happen automatically on PlanetLab slivers.",
1589             "category": FC.CATEGORY_APPLICATIONS,
1590             "create_function": create_multicast_router,
1591             "preconfigure_function": configure_router,
1592             "start_function": start_application,
1593             "status_function": status_application,
1594             "stop_function": stop_application,
1595             "box_attributes": ["routing_algorithm"],
1596             "connector_types": ["fwd"],
1597             "traces": ["buildlog","stdout","stderr"],
1598         }),
1599     INTERNET: dict({
1600             "help": "Internet routing",
1601             "category": FC.CATEGORY_CHANNELS,
1602             "create_function": create_internet,
1603             "connector_types": ["devs"],
1604             "tags": [tags.INTERNET],
1605         }),
1606     NETPIPE: dict({
1607             "help": "Link emulation",
1608             "category": FC.CATEGORY_CHANNELS,
1609             "create_function": create_netpipe,
1610             "configure_function": configure_netpipe,
1611             "box_attributes": ["netpipe_mode",
1612                                "addr_list", "port_list",
1613                                "bw_in","plr_in","delay_in",
1614                                "bw_out","plr_out","delay_out"],
1615             "connector_types": ["node"],
1616             "traces": ["netpipe_stats"],
1617         }),
1618 })
1619
1620 testbed_attributes = dict({
1621         "slice": dict({
1622             "name": "slice",
1623             "help": "The name of the PlanetLab slice to use",
1624             "type": Attribute.STRING,
1625             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1626             "validation_function": validation.is_string
1627         }),
1628         "auth_user": dict({
1629             "name": "authUser",
1630             "help": "The name of the PlanetLab user to use for API calls - it must have at least a User role.",
1631             "type": Attribute.STRING,
1632             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1633             "validation_function": validation.is_string
1634         }),
1635         "auth_pass": dict({
1636             "name": "authPass",
1637             "help": "The PlanetLab user's password.",
1638             "type": Attribute.STRING,
1639             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1640             "validation_function": validation.is_string
1641         }),
1642         "plc_host": dict({
1643             "name": "plcHost",
1644             "help": "The PlanetLab PLC API host",
1645             "type": Attribute.STRING,
1646             "value": "www.planet-lab.eu",
1647             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1648             "validation_function": validation.is_string
1649         }),
1650         "plc_url": dict({
1651             "name": "plcUrl",
1652             "help": "The PlanetLab PLC API url pattern - %(hostname)s is replaced by plcHost.",
1653             "type": Attribute.STRING,
1654             "value": "https://%(hostname)s:443/PLCAPI/",
1655             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1656             "validation_function": validation.is_string
1657         }),
1658         "p2p_deployment": dict({
1659             "name": "p2pDeployment",
1660             "help": "Enable peer-to-peer deployment of applications and dependencies. "
1661                     "When enabled, dependency packages and applications are "
1662                     "deployed in a P2P fashion, picking a single node to do "
1663                     "the building or repo download, while all the others "
1664                     "cooperatively exchange resulting binaries or rpms. "
1665                     "When deploying to many nodes, this is a far more efficient "
1666                     "use of resources. It does require re-encrypting and distributing "
1667                     "the slice's private key. Though it is implemented in a secure "
1668                     "fashion, if they key's sole purpose is not PlanetLab, then this "
1669                     "feature should be disabled.",
1670             "type": Attribute.BOOL,
1671             "value": True,
1672             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1673             "validation_function": validation.is_bool
1674         }),
1675         "slice_ssh_key": dict({
1676             "name": "sliceSSHKey",
1677             "help": "The controller-local path to the slice user's ssh private key. "
1678                     "It is the user's responsability to deploy this file where the controller "
1679                     "will run, it won't be done automatically because it's sensitive information. "
1680                     "It is recommended that a NEPI-specific user be created for this purpose and "
1681                     "this purpose alone.",
1682             "type": Attribute.STRING,
1683             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1684             "validation_function": validation.is_string
1685         }),
1686         "pl_log_level": dict({      
1687             "name": "plLogLevel",
1688             "help": "Verbosity of logging of planetlab events.",
1689             "value": "ERROR",
1690             "type": Attribute.ENUM, 
1691             "allowed": ["DEBUG",
1692                         "INFO",
1693                         "WARNING",
1694                         "ERROR",
1695                         "CRITICAL"],
1696             "validation_function": validation.is_enum,
1697         }),
1698         "tap_port_base":  dict({
1699             "name": "tapPortBase", 
1700             "help": "Base port to use when connecting TUN/TAPs. Effective port will be BASE + GUID.",
1701             "type": Attribute.INTEGER,
1702             "value": 15000,
1703             "range": (2000,30000),
1704             "validation_function": validation.is_integer_range(2000,30000)
1705         }),
1706         "dedicated_slice": dict({
1707             "name": "dedicatedSlice",
1708             "help": "Set to True if the slice will be dedicated to this experiment. "
1709                     "NEPI will perform node and slice cleanup, making sure slices are "
1710                     "in a clean, repeatable state before running the experiment.",
1711             "type": Attribute.BOOL,
1712             "value": False,
1713             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1714             "validation_function": validation.is_bool
1715         }),
1716     })
1717
1718 supported_recovery_policies = [
1719         DC.POLICY_FAIL,
1720         DC.POLICY_RESTART,
1721         DC.POLICY_RECOVER,
1722     ]
1723
1724 class MetadataInfo(metadata.MetadataInfo):
1725     @property
1726     def connector_types(self):
1727         return connector_types
1728
1729     @property
1730     def connections(self):
1731         return connections
1732
1733     @property
1734     def attributes(self):
1735         return attributes
1736
1737     @property
1738     def traces(self):
1739         return traces
1740
1741     @property
1742     def create_order(self):
1743         return create_order
1744
1745     @property
1746     def configure_order(self):
1747         return configure_order
1748
1749     @property
1750     def prestart_order(self):
1751         return start_order
1752
1753     @property
1754     def start_order(self):
1755         return start_order
1756
1757     @property
1758     def factories_info(self):
1759         return factories_info
1760
1761     @property
1762     def testbed_attributes(self):
1763         return testbed_attributes
1764
1765     @property
1766     def testbed_id(self):
1767         return TESTBED_ID
1768
1769     @property
1770     def testbed_version(self):
1771         return TESTBED_VERSION
1772
1773     @property
1774     def supported_recovery_policies(self):
1775         return supported_recovery_policies
1776
1777