Blacklist nodes that are not so healthy
[nepi.git] / src / nepi / testbeds / planetlab / metadata_v01.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import time
5
6 from constants import TESTBED_ID
7 from nepi.core import metadata
8 from nepi.core.attributes import Attribute
9 from nepi.util import validation
10 from nepi.util.constants import STATUS_NOT_STARTED, STATUS_RUNNING, \
11         STATUS_FINISHED, ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP
12
13 import functools
14 import os
15 import os.path
16
17 NODE = "Node"
18 NODEIFACE = "NodeInterface"
19 TUNIFACE = "TunInterface"
20 TAPIFACE = "TapInterface"
21 APPLICATION = "Application"
22 DEPENDENCY = "Dependency"
23 NEPIDEPENDENCY = "NepiDependency"
24 NS3DEPENDENCY = "NS3Dependency"
25 INTERNET = "Internet"
26 NETPIPE = "NetPipe"
27
28 PL_TESTBED_ID = "planetlab"
29
30
31 ### Custom validation functions ###
32 def is_addrlist(attribute, value):
33     if not validation.is_string(attribute, value):
34         return False
35     
36     if not value:
37         # No empty strings
38         return False
39     
40     components = value.split(',')
41     
42     for component in components:
43         if '/' in component:
44             addr, mask = component.split('/',1)
45         else:
46             addr, mask = component, '32'
47         
48         if mask is not None and not (mask and mask.isdigit()):
49             # No empty or nonnumeric masks
50             return False
51         
52         if not validation.is_ip4_address(attribute, addr):
53             # Address part must be ipv4
54             return False
55         
56     return True
57
58 def is_portlist(attribute, value):
59     if not validation.is_string(attribute, value):
60         return False
61     
62     if not value:
63         # No empty strings
64         return False
65     
66     components = value.split(',')
67     
68     for component in components:
69         if '-' in component:
70             pfrom, pto = component.split('-',1)
71         else:
72             pfrom = pto = component
73         
74         if not pfrom or not pto or not pfrom.isdigit() or not pto.isdigit():
75             # No empty or nonnumeric ports
76             return False
77         
78     return True
79
80
81 ### Connection functions ####
82
83 def connect_node_iface_node(testbed_instance, node_guid, iface_guid):
84     node = testbed_instance._elements[node_guid]
85     iface = testbed_instance._elements[iface_guid]
86     iface.node = node
87
88 def connect_node_iface_inet(testbed_instance, iface_guid, inet_guid):
89     iface = testbed_instance._elements[iface_guid]
90     iface.has_internet = True
91
92 def connect_tun_iface_node(testbed_instance, node_guid, iface_guid):
93     node = testbed_instance._elements[node_guid]
94     iface = testbed_instance._elements[iface_guid]
95     if not node.emulation:
96         raise RuntimeError, "Use of TUN interfaces requires emulation"
97     iface.node = node
98     node.required_vsys.update(('fd_tuntap', 'vif_up'))
99     node.required_packages.update(('python', 'python-crypto', 'python-setuptools', 'gcc'))
100
101 def connect_tun_iface_peer(proto, testbed_instance, iface_guid, peer_iface_guid):
102     iface = testbed_instance._elements[iface_guid]
103     peer_iface = testbed_instance._elements[peer_iface_guid]
104     iface.peer_iface = peer_iface
105     iface.peer_proto = \
106     iface.tun_proto = proto
107     iface.tun_key = peer_iface.tun_key
108
109 def crossconnect_tun_iface_peer_init(proto, testbed_instance, iface_guid, peer_iface_data):
110     iface = testbed_instance._elements[iface_guid]
111     iface.peer_iface = None
112     iface.peer_addr = peer_iface_data.get("tun_addr")
113     iface.peer_proto = peer_iface_data.get("tun_proto") or proto
114     iface.peer_port = peer_iface_data.get("tun_port")
115     iface.tun_key = min(iface.tun_key, peer_iface_data.get("tun_key"))
116     iface.tun_proto = proto
117     
118     preconfigure_tuniface(testbed_instance, iface_guid)
119
120 def crossconnect_tun_iface_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data):
121     # refresh (refreshable) attributes for second-phase
122     iface = testbed_instance._elements[iface_guid]
123     iface.peer_addr = peer_iface_data.get("tun_addr")
124     iface.peer_proto = peer_iface_data.get("tun_proto") or proto
125     iface.peer_port = peer_iface_data.get("tun_port")
126     
127     postconfigure_tuniface(testbed_instance, iface_guid)
128
129 def crossconnect_tun_iface_peer_both(proto, testbed_instance, iface_guid, peer_iface_data):
130     crossconnect_tun_iface_peer_init(proto, testbed_instance, iface_guid, peer_iface_data)
131     crossconnect_tun_iface_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data)
132
133 def connect_dep(testbed_instance, node_guid, app_guid):
134     node = testbed_instance._elements[node_guid]
135     app = testbed_instance._elements[app_guid]
136     app.node = node
137     
138     if app.depends:
139         node.required_packages.update(set(
140             app.depends.split() ))
141     
142     if app.add_to_path:
143         if app.home_path and app.home_path not in node.pythonpath:
144             node.pythonpath.append(app.home_path)
145     
146     if app.env:
147         for envkey, envval in app.env.iteritems():
148             envval = app._replace_paths(envval)
149             node.env[envkey].append(envval)
150
151 def connect_node_netpipe(testbed_instance, node_guid, netpipe_guid):
152     node = testbed_instance._elements[node_guid]
153     netpipe = testbed_instance._elements[netpipe_guid]
154     if not node.emulation:
155         raise RuntimeError, "Use of NetPipes requires emulation"
156     netpipe.node = node
157     
158
159 ### Creation functions ###
160
161 def create_node(testbed_instance, guid):
162     parameters = testbed_instance._get_parameters(guid)
163     
164     # create element with basic attributes
165     element = testbed_instance._make_node(parameters)
166     
167     # add constraint on number of (real) interfaces
168     # by counting connected devices
169     dev_guids = testbed_instance.get_connected(guid, "devs", "node")
170     num_open_ifaces = sum( # count True values
171         NODEIFACE == testbed_instance._get_factory_id(guid)
172         for guid in dev_guids )
173     element.min_num_external_ifaces = num_open_ifaces
174     
175     # require vroute vsys if we have routes to set up
176     routes = testbed_instance._add_route.get(guid)
177     if routes:
178         element.required_vsys.add("vroute")
179     
180     testbed_instance.elements[guid] = element
181
182 def create_nodeiface(testbed_instance, guid):
183     parameters = testbed_instance._get_parameters(guid)
184     element = testbed_instance._make_node_iface(parameters)
185     testbed_instance.elements[guid] = element
186
187 def create_tuniface(testbed_instance, guid):
188     parameters = testbed_instance._get_parameters(guid)
189     element = testbed_instance._make_tun_iface(parameters)
190     
191     # Set custom addresses, if there are any already
192     # Setting this early helps set up P2P links
193     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
194         addresses = testbed_instance._add_address[guid]
195         for address in addresses:
196             (address, netprefix, broadcast) = address
197             element.add_address(address, netprefix, broadcast)
198     
199     testbed_instance.elements[guid] = element
200
201 def create_tapiface(testbed_instance, guid):
202     parameters = testbed_instance._get_parameters(guid)
203     element = testbed_instance._make_tap_iface(parameters)
204     
205     # Set custom addresses, if there are any already
206     # Setting this early helps set up P2P links
207     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
208         addresses = testbed_instance._add_address[guid]
209         for address in addresses:
210             (address, netprefix, broadcast) = address
211             element.add_address(address, netprefix, broadcast)
212     
213     testbed_instance.elements[guid] = element
214
215 def create_application(testbed_instance, guid):
216     parameters = testbed_instance._get_parameters(guid)
217     element = testbed_instance._make_application(parameters)
218     
219     # Just inject configuration stuff
220     element.home_path = "nepi-app-%s" % (guid,)
221     
222     testbed_instance.elements[guid] = element
223
224 def create_dependency(testbed_instance, guid):
225     parameters = testbed_instance._get_parameters(guid)
226     element = testbed_instance._make_dependency(parameters)
227     
228     # Just inject configuration stuff
229     element.home_path = "nepi-dep-%s" % (guid,)
230     
231     testbed_instance.elements[guid] = element
232
233 def create_nepi_dependency(testbed_instance, guid):
234     parameters = testbed_instance._get_parameters(guid)
235     element = testbed_instance._make_nepi_dependency(parameters)
236     
237     # Just inject configuration stuff
238     element.home_path = "nepi-nepi-%s" % (guid,)
239     
240     testbed_instance.elements[guid] = element
241
242 def create_ns3_dependency(testbed_instance, guid):
243     parameters = testbed_instance._get_parameters(guid)
244     element = testbed_instance._make_ns3_dependency(parameters)
245     
246     # Just inject configuration stuff
247     element.home_path = "nepi-ns3-%s" % (guid,)
248     
249     testbed_instance.elements[guid] = element
250
251 def create_internet(testbed_instance, guid):
252     parameters = testbed_instance._get_parameters(guid)
253     element = testbed_instance._make_internet(parameters)
254     testbed_instance.elements[guid] = element
255
256 def create_netpipe(testbed_instance, guid):
257     parameters = testbed_instance._get_parameters(guid)
258     element = testbed_instance._make_netpipe(parameters)
259     testbed_instance.elements[guid] = element
260
261 ### Start/Stop functions ###
262
263 def start_application(testbed_instance, guid):
264     parameters = testbed_instance._get_parameters(guid)
265     traces = testbed_instance._get_traces(guid)
266     app = testbed_instance.elements[guid]
267     
268     app.stdout = "stdout" in traces
269     app.stderr = "stderr" in traces
270     app.buildlog = "buildlog" in traces
271     
272     app.start()
273
274 def stop_application(testbed_instance, guid):
275     app = testbed_instance.elements[guid]
276     app.kill()
277
278 ### Status functions ###
279
280 def status_application(testbed_instance, guid):
281     if guid not in testbed_instance.elements.keys():
282         return STATUS_NOT_STARTED
283     
284     app = testbed_instance.elements[guid]
285     return app.status()
286
287 ### Configure functions ###
288
289 def configure_nodeiface(testbed_instance, guid):
290     element = testbed_instance._elements[guid]
291     
292     # Cannot explicitly configure addresses
293     if guid in testbed_instance._add_address:
294         raise ValueError, "Cannot explicitly set address of public PlanetLab interface"
295     
296     # Get siblings
297     node_guid = testbed_instance.get_connected(guid, "node", "devs")[0]
298     dev_guids = testbed_instance.get_connected(node_guid, "node", "devs")
299     siblings = [ self._element[dev_guid] 
300                  for dev_guid in dev_guids
301                  if dev_guid != guid ]
302     
303     # Fetch address from PLC api
304     element.pick_iface(siblings)
305     
306     # Do some validations
307     element.validate()
308
309 def preconfigure_tuniface(testbed_instance, guid):
310     element = testbed_instance._elements[guid]
311     
312     # Set custom addresses if any, and if not set already
313     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
314         addresses = testbed_instance._add_address[guid]
315         for address in addresses:
316             (address, netprefix, broadcast) = address
317             element.add_address(address, netprefix, broadcast)
318     
319     # Link to external interface, if any
320     for iface in testbed_instance._elements.itervalues():
321         if isinstance(iface, testbed_instance._interfaces.NodeIface) and iface.node is element.node and iface.has_internet:
322             element.external_iface = iface
323             break
324
325     # Set standard TUN attributes
326     if (not element.tun_addr or not element.tun_port) and element.external_iface:
327         element.tun_addr = element.external_iface.address
328         element.tun_port = 15000 + int(guid)
329
330     # Set enabled traces
331     traces = testbed_instance._get_traces(guid)
332     element.capture = 'packets' in traces
333     
334     # Do some validations
335     element.validate()
336     
337     # First-phase setup
338     if element.peer_proto:
339         if element.peer_iface and isinstance(element.peer_iface, testbed_instance._interfaces.TunIface):
340             # intra tun
341             listening = id(element) < id(element.peer_iface)
342         else:
343             # cross tun
344             if not element.tun_addr or not element.tun_port:
345                 listening = True
346             elif not element.peer_addr or not element.peer_port:
347                 listening = True
348             else:
349                 # both have addresses...
350                 # ...the one with the lesser address listens
351                 listening = element.tun_addr < element.peer_addr
352         element.prepare( 
353             'tun-%s' % (guid,),
354              listening)
355
356 def postconfigure_tuniface(testbed_instance, guid):
357     element = testbed_instance._elements[guid]
358     
359     # Second-phase setup
360     element.setup()
361     
362 def wait_tuniface(testbed_instance, guid):
363     element = testbed_instance._elements[guid]
364     
365     # Second-phase setup
366     element.async_launch_wait()
367     
368
369 def configure_node(testbed_instance, guid):
370     node = testbed_instance._elements[guid]
371     
372     # Just inject configuration stuff
373     node.home_path = "nepi-node-%s" % (guid,)
374     node.ident_path = testbed_instance.sliceSSHKey
375     node.slicename = testbed_instance.slicename
376     
377     # Do some validations
378     node.validate()
379     
380     # this will be done in parallel in all nodes
381     # this call only spawns the process
382     node.install_dependencies()
383
384 def configure_node_routes(testbed_instance, guid):
385     node = testbed_instance._elements[guid]
386     routes = testbed_instance._add_route.get(guid)
387     
388     if routes:
389         devs = [ dev
390             for dev_guid in testbed_instance.get_connected(guid, "devs", "node")
391             for dev in ( testbed_instance._elements.get(dev_guid) ,)
392             if dev and isinstance(dev, testbed_instance._interfaces.TunIface) ]
393         
394         node.configure_routes(routes, devs)
395
396 def configure_application(testbed_instance, guid):
397     app = testbed_instance._elements[guid]
398     
399     # Do some validations
400     app.validate()
401     
402     # Wait for dependencies
403     app.node.wait_dependencies()
404     
405     # Install stuff
406     app.async_setup()
407
408 def configure_dependency(testbed_instance, guid):
409     dep = testbed_instance._elements[guid]
410     
411     # Do some validations
412     dep.validate()
413     
414     # Wait for dependencies
415     dep.node.wait_dependencies()
416     
417     # Install stuff
418     dep.async_setup()
419
420 def configure_netpipe(testbed_instance, guid):
421     netpipe = testbed_instance._elements[guid]
422     
423     # Do some validations
424     netpipe.validate()
425     
426     # Wait for dependencies
427     netpipe.node.wait_dependencies()
428     
429     # Install rules
430     netpipe.configure()
431
432 ### Factory information ###
433
434 connector_types = dict({
435     "apps": dict({
436                 "help": "Connector from node to applications", 
437                 "name": "apps",
438                 "max": -1, 
439                 "min": 0
440             }),
441     "devs": dict({
442                 "help": "Connector from node to network interfaces", 
443                 "name": "devs",
444                 "max": -1, 
445                 "min": 0
446             }),
447     "deps": dict({
448                 "help": "Connector from node to application dependencies "
449                         "(packages and applications that need to be installed)", 
450                 "name": "deps",
451                 "max": -1, 
452                 "min": 0
453             }),
454     "inet": dict({
455                 "help": "Connector from network interfaces to the internet", 
456                 "name": "inet",
457                 "max": 1, 
458                 "min": 1
459             }),
460     "node": dict({
461                 "help": "Connector to a Node", 
462                 "name": "node",
463                 "max": 1, 
464                 "min": 1
465             }),
466     "pipes": dict({
467                 "help": "Connector to a NetPipe", 
468                 "name": "pipes",
469                 "max": 2, 
470                 "min": 0
471             }),
472     
473     "tcp": dict({
474                 "help": "ip-ip tunneling over TCP link", 
475                 "name": "tcp",
476                 "max": 1, 
477                 "min": 0
478             }),
479     "udp": dict({
480                 "help": "ip-ip tunneling over UDP datagrams", 
481                 "name": "udp",
482                 "max": 1, 
483                 "min": 0
484             }),
485     "fd->": dict({
486                 "help": "TUN device file descriptor provider", 
487                 "name": "fd->",
488                 "max": 1, 
489                 "min": 0
490             }),
491    })
492
493 connections = [
494     dict({
495         "from": (TESTBED_ID, NODE, "devs"),
496         "to":   (TESTBED_ID, NODEIFACE, "node"),
497         "init_code": connect_node_iface_node,
498         "can_cross": False
499     }),
500     dict({
501         "from": (TESTBED_ID, NODE, "devs"),
502         "to":   (TESTBED_ID, TUNIFACE, "node"),
503         "init_code": connect_tun_iface_node,
504         "can_cross": False
505     }),
506     dict({
507         "from": (TESTBED_ID, NODE, "devs"),
508         "to":   (TESTBED_ID, TAPIFACE, "node"),
509         "init_code": connect_tun_iface_node,
510         "can_cross": False
511     }),
512     dict({
513         "from": (TESTBED_ID, NODEIFACE, "inet"),
514         "to":   (TESTBED_ID, INTERNET, "devs"),
515         "init_code": connect_node_iface_inet,
516         "can_cross": False
517     }),
518     dict({
519         "from": (TESTBED_ID, NODE, "apps"),
520         "to":   (TESTBED_ID, APPLICATION, "node"),
521         "init_code": connect_dep,
522         "can_cross": False
523     }),
524     dict({
525         "from": (TESTBED_ID, NODE, "deps"),
526         "to":   (TESTBED_ID, DEPENDENCY, "node"),
527         "init_code": connect_dep,
528         "can_cross": False
529     }),
530     dict({
531         "from": (TESTBED_ID, NODE, "deps"),
532         "to":   (TESTBED_ID, NEPIDEPENDENCY, "node"),
533         "init_code": connect_dep,
534         "can_cross": False
535     }),
536     dict({
537         "from": (TESTBED_ID, NODE, "deps"),
538         "to":   (TESTBED_ID, NS3DEPENDENCY, "node"),
539         "init_code": connect_dep,
540         "can_cross": False
541     }),
542     dict({
543         "from": (TESTBED_ID, NODE, "pipes"),
544         "to":   (TESTBED_ID, NETPIPE, "node"),
545         "init_code": connect_node_netpipe,
546         "can_cross": False
547     }),
548     dict({
549         "from": (TESTBED_ID, TUNIFACE, "tcp"),
550         "to":   (TESTBED_ID, TUNIFACE, "tcp"),
551         "init_code": functools.partial(connect_tun_iface_peer,"tcp"),
552         "can_cross": False
553     }),
554     dict({
555         "from": (TESTBED_ID, TUNIFACE, "udp"),
556         "to":   (TESTBED_ID, TUNIFACE, "udp"),
557         "init_code": functools.partial(connect_tun_iface_peer,"udp"),
558         "can_cross": False
559     }),
560     dict({
561         "from": (TESTBED_ID, TAPIFACE, "tcp"),
562         "to":   (TESTBED_ID, TAPIFACE, "tcp"),
563         "init_code": functools.partial(connect_tun_iface_peer,"tcp"),
564         "can_cross": False
565     }),
566     dict({
567         "from": (TESTBED_ID, TAPIFACE, "udp"),
568         "to":   (TESTBED_ID, TAPIFACE, "udp"),
569         "init_code": functools.partial(connect_tun_iface_peer,"udp"),
570         "can_cross": False
571     }),
572     dict({
573         "from": (TESTBED_ID, TUNIFACE, "tcp"),
574         "to":   (None, None, "tcp"),
575         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"tcp"),
576         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"tcp"),
577         "can_cross": True
578     }),
579     dict({
580         "from": (TESTBED_ID, TUNIFACE, "udp"),
581         "to":   (None, None, "udp"),
582         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"udp"),
583         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"udp"),
584         "can_cross": True
585     }),
586     dict({
587         "from": (TESTBED_ID, TUNIFACE, "fd->"),
588         "to":   (None, None, "->fd"),
589         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"fd"),
590         "can_cross": True
591     }),
592     dict({
593         "from": (TESTBED_ID, TAPIFACE, "tcp"),
594         "to":   (None, None, "tcp"),
595         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"tcp"),
596         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"tcp"),
597         "can_cross": True
598     }),
599     dict({
600         "from": (TESTBED_ID, TAPIFACE, "udp"),
601         "to":   (None, None, "udp"),
602         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"udp"),
603         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"udp"),
604         "can_cross": True
605     }),
606     dict({
607         "from": (TESTBED_ID, TAPIFACE, "fd->"),
608         "to":   (None, None, "->fd"),
609         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"fd"),
610         "can_cross": True
611     }),
612 ]
613
614 attributes = dict({
615     "forward_X11": dict({      
616                 "name": "forward_X11",
617                 "help": "Forward x11 from main namespace to the node",
618                 "type": Attribute.BOOL, 
619                 "value": False,
620                 "flags": Attribute.DesignOnly,
621                 "validation_function": validation.is_bool,
622             }),
623     "hostname": dict({      
624                 "name": "hostname",
625                 "help": "Constrain hostname during resource discovery. May use wildcards.",
626                 "type": Attribute.STRING, 
627                 "flags": Attribute.DesignOnly,
628                 "validation_function": validation.is_string,
629             }),
630     "architecture": dict({      
631                 "name": "architecture",
632                 "help": "Constrain architexture during resource discovery.",
633                 "type": Attribute.ENUM, 
634                 "flags": Attribute.DesignOnly,
635                 "allowed": ["x86_64",
636                             "i386"],
637                 "validation_function": validation.is_enum,
638             }),
639     "operating_system": dict({      
640                 "name": "operatingSystem",
641                 "help": "Constrain operating system during resource discovery.",
642                 "type": Attribute.ENUM, 
643                 "flags": Attribute.DesignOnly,
644                 "allowed": ["f8",
645                             "f12",
646                             "f14",
647                             "centos",
648                             "other"],
649                 "validation_function": validation.is_enum,
650             }),
651     "site": dict({      
652                 "name": "site",
653                 "help": "Constrain the PlanetLab site this node should reside on.",
654                 "type": Attribute.ENUM, 
655                 "flags": Attribute.DesignOnly,
656                 "allowed": ["PLE",
657                             "PLC",
658                             "PLJ"],
659                 "validation_function": validation.is_enum,
660             }),
661     "emulation": dict({      
662                 "name": "emulation",
663                 "help": "Enable emulation on this node. Enables NetfilterRoutes, bridges, and a host of other functionality.",
664                 "type": Attribute.BOOL,
665                 "value": False, 
666                 "flags": Attribute.DesignOnly,
667                 "validation_function": validation.is_bool,
668             }),
669     "min_reliability": dict({
670                 "name": "minReliability",
671                 "help": "Constrain reliability while picking PlanetLab nodes. Specifies a lower acceptable bound.",
672                 "type": Attribute.DOUBLE,
673                 "range": (0,100),
674                 "flags": Attribute.DesignOnly,
675                 "validation_function": validation.is_double,
676             }),
677     "max_reliability": dict({
678                 "name": "maxReliability",
679                 "help": "Constrain reliability while picking PlanetLab nodes. Specifies an upper acceptable bound.",
680                 "type": Attribute.DOUBLE,
681                 "range": (0,100),
682                 "flags": Attribute.DesignOnly,
683                 "validation_function": validation.is_double,
684             }),
685     "min_bandwidth": dict({
686                 "name": "minBandwidth",
687                 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies a lower acceptable bound.",
688                 "type": Attribute.DOUBLE,
689                 "range": (0,2**31),
690                 "flags": Attribute.DesignOnly,
691                 "validation_function": validation.is_double,
692             }),
693     "max_bandwidth": dict({
694                 "name": "maxBandwidth",
695                 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies an upper acceptable bound.",
696                 "type": Attribute.DOUBLE,
697                 "range": (0,2**31),
698                 "flags": Attribute.DesignOnly,
699                 "validation_function": validation.is_double,
700             }),
701             
702     "up": dict({
703                 "name": "up",
704                 "help": "Link up",
705                 "type": Attribute.BOOL,
706                 "value": False,
707                 "validation_function": validation.is_bool
708             }),
709     "primary": dict({
710                 "name": "primary",
711                 "help": "This is the primary interface for the attached node",
712                 "type": Attribute.BOOL,
713                 "value": True,
714                 "validation_function": validation.is_bool
715             }),
716     "device_name": dict({
717                 "name": "name",
718                 "help": "Device name",
719                 "type": Attribute.STRING,
720                 "flags": Attribute.DesignOnly,
721                 "validation_function": validation.is_string
722             }),
723     "mtu":  dict({
724                 "name": "mtu", 
725                 "help": "Maximum transmition unit for device",
726                 "type": Attribute.INTEGER,
727                 "range": (0,1500),
728                 "validation_function": validation.is_integer_range(0,1500)
729             }),
730     "mask":  dict({
731                 "name": "mask", 
732                 "help": "Network mask for the device (eg: 24 for /24 network)",
733                 "type": Attribute.INTEGER,
734                 "validation_function": validation.is_integer_range(8,24)
735             }),
736     "snat":  dict({
737                 "name": "snat", 
738                 "help": "Enable SNAT (source NAT to the internet) no this device",
739                 "type": Attribute.BOOL,
740                 "value": False,
741                 "validation_function": validation.is_bool
742             }),
743     "pointopoint":  dict({
744                 "name": "pointopoint", 
745                 "help": "If the interface is a P2P link, the remote endpoint's IP "
746                         "should be set on this attribute.",
747                 "type": Attribute.STRING,
748                 "flags": Attribute.DesignOnly,
749                 "validation_function": validation.is_string
750             }),
751     "txqueuelen":  dict({
752                 "name": "mask", 
753                 "help": "Transmission queue length (in packets)",
754                 "type": Attribute.INTEGER,
755                 "flags": Attribute.DesignOnly,
756                 "range" : (1,10000),
757                 "validation_function": validation.is_integer
758             }),
759             
760     "command": dict({
761                 "name": "command",
762                 "help": "Command line string",
763                 "type": Attribute.STRING,
764                 "flags": Attribute.DesignOnly,
765                 "validation_function": validation.is_string
766             }),
767     "sudo": dict({
768                 "name": "sudo",
769                 "help": "Run with root privileges",
770                 "type": Attribute.BOOL,
771                 "flags": Attribute.DesignOnly,
772                 "value": False,
773                 "validation_function": validation.is_bool
774             }),
775     "stdin": dict({
776                 "name": "stdin",
777                 "help": "Standard input",
778                 "type": Attribute.STRING,
779                 "flags": Attribute.DesignOnly,
780                 "validation_function": validation.is_string
781             }),
782             
783     "depends": dict({
784                 "name": "depends",
785                 "help": "Space-separated list of packages required to run the application",
786                 "type": Attribute.STRING,
787                 "flags": Attribute.DesignOnly,
788                 "validation_function": validation.is_string
789             }),
790     "build-depends": dict({
791                 "name": "buildDepends",
792                 "help": "Space-separated list of packages required to build the application",
793                 "type": Attribute.STRING,
794                 "flags": Attribute.DesignOnly,
795                 "validation_function": validation.is_string
796             }),
797     "sources": dict({
798                 "name": "sources",
799                 "help": "Space-separated list of regular files to be deployed in the working path prior to building. "
800                         "Archives won't be expanded automatically.",
801                 "type": Attribute.STRING,
802                 "flags": Attribute.DesignOnly,
803                 "validation_function": validation.is_string
804             }),
805     "build": dict({
806                 "name": "build",
807                 "help": "Build commands to execute after deploying the sources. "
808                         "Sources will be in the ${SOURCES} folder. "
809                         "Example: tar xzf ${SOURCES}/my-app.tgz && cd my-app && ./configure && make && make clean.\n"
810                         "Try to make the commands return with a nonzero exit code on error.\n"
811                         "Also, do not install any programs here, use the 'install' attribute. This will "
812                         "help keep the built files constrained to the build folder (which may "
813                         "not be the home folder), and will result in faster deployment. Also, "
814                         "make sure to clean up temporary files, to reduce bandwidth usage between "
815                         "nodes when transferring built packages.",
816                 "type": Attribute.STRING,
817                 "flags": Attribute.DesignOnly,
818                 "validation_function": validation.is_string
819             }),
820     "install": dict({
821                 "name": "install",
822                 "help": "Commands to transfer built files to their final destinations. "
823                         "Sources will be in the initial working folder, and a special "
824                         "tag ${SOURCES} can be used to reference the experiment's "
825                         "home folder (where the application commands will run).\n"
826                         "ALL sources and targets needed for execution must be copied there, "
827                         "if building has been enabled.\n"
828                         "That is, 'slave' nodes will not automatically get any source files. "
829                         "'slave' nodes don't get build dependencies either, so if you need "
830                         "make and other tools to install, be sure to provide them as "
831                         "actual dependencies instead.",
832                 "type": Attribute.STRING,
833                 "flags": Attribute.DesignOnly,
834                 "validation_function": validation.is_string
835             }),
836     
837     "netpipe_mode": dict({      
838                 "name": "mode",
839                 "help": "Link mode:\n"
840                         " * SERVER: applies to incoming connections\n"
841                         " * CLIENT: applies to outgoing connections\n"
842                         " * SERVICE: applies to both",
843                 "type": Attribute.ENUM, 
844                 "flags": Attribute.DesignOnly,
845                 "allowed": ["SERVER",
846                             "CLIENT",
847                             "SERVICE"],
848                 "validation_function": validation.is_enum,
849             }),
850     "port_list":  dict({
851                 "name": "portList", 
852                 "help": "Port list or range. Eg: '22', '22,23,27', '20-2000'",
853                 "type": Attribute.STRING,
854                 "validation_function": is_portlist,
855             }),
856     "addr_list":  dict({
857                 "name": "addrList", 
858                 "help": "Address list or range. Eg: '127.0.0.1', '127.0.0.1,127.0.1.1', '127.0.0.1/8'",
859                 "type": Attribute.STRING,
860                 "validation_function": is_addrlist,
861             }),
862     "bw_in":  dict({
863                 "name": "bwIn", 
864                 "help": "Inbound bandwidth limit (in Mbit/s)",
865                 "type": Attribute.DOUBLE,
866                 "validation_function": validation.is_double,
867             }),
868     "bw_out":  dict({
869                 "name": "bwOut", 
870                 "help": "Outbound bandwidth limit (in Mbit/s)",
871                 "type": Attribute.DOUBLE,
872                 "validation_function": validation.is_double,
873             }),
874     "plr_in":  dict({
875                 "name": "plrIn", 
876                 "help": "Inbound packet loss rate (0 = no loss, 1 = 100% loss)",
877                 "type": Attribute.DOUBLE,
878                 "validation_function": validation.is_double,
879             }),
880     "plr_out":  dict({
881                 "name": "plrOut", 
882                 "help": "Outbound packet loss rate (0 = no loss, 1 = 100% loss)",
883                 "type": Attribute.DOUBLE,
884                 "validation_function": validation.is_double,
885             }),
886     "delay_in":  dict({
887                 "name": "delayIn", 
888                 "help": "Inbound packet delay (in milliseconds)",
889                 "type": Attribute.INTEGER,
890                 "range": (0,60000),
891                 "validation_function": validation.is_integer,
892             }),
893     "delay_out":  dict({
894                 "name": "delayOut", 
895                 "help": "Outbound packet delay (in milliseconds)",
896                 "type": Attribute.INTEGER,
897                 "range": (0,60000),
898                 "validation_function": validation.is_integer,
899             }),
900     })
901
902 traces = dict({
903     "stdout": dict({
904                 "name": "stdout",
905                 "help": "Standard output stream"
906               }),
907     "stderr": dict({
908                 "name": "stderr",
909                 "help": "Application standard error",
910               }),
911     "buildlog": dict({
912                 "name": "buildlog",
913                 "help": "Output of the build process",
914               }), 
915     
916     "netpipe_stats": dict({
917                 "name": "netpipeStats",
918                 "help": "Information about rule match counters, packets dropped, etc.",
919               }),
920
921     "packets": dict({
922                 "name": "packets",
923                 "help": "Detailled log of all packets going through the interface",
924               }),
925     })
926
927 create_order = [ INTERNET, NODE, NODEIFACE, TAPIFACE, TUNIFACE, NETPIPE, NEPIDEPENDENCY, NS3DEPENDENCY, DEPENDENCY, APPLICATION ]
928
929 configure_order = [ INTERNET, NODE, NODEIFACE, TAPIFACE, TUNIFACE, NETPIPE, NEPIDEPENDENCY, NS3DEPENDENCY, DEPENDENCY, APPLICATION ]
930
931 # Start (and prestart) node after ifaces, because the node needs the ifaces in order to set up routes
932 start_order = [ INTERNET, NODEIFACE, TAPIFACE, TUNIFACE, NODE, NETPIPE, NEPIDEPENDENCY, NS3DEPENDENCY, DEPENDENCY, APPLICATION ]
933
934 factories_info = dict({
935     NODE: dict({
936             "allow_routes": True,
937             "help": "Virtualized Node (V-Server style)",
938             "category": "topology",
939             "create_function": create_node,
940             "preconfigure_function": configure_node,
941             "prestart_function": configure_node_routes,
942             "box_attributes": [
943                 "forward_X11",
944                 "hostname",
945                 "architecture",
946                 "operating_system",
947                 "site",
948                 "emulation",
949                 "min_reliability",
950                 "max_reliability",
951                 "min_bandwidth",
952                 "max_bandwidth",
953                 
954                 # NEPI-in-NEPI attributes
955                 ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP,
956             ],
957             "connector_types": ["devs", "apps", "pipes", "deps"]
958        }),
959     NODEIFACE: dict({
960             "has_addresses": True,
961             "help": "External network interface - they cannot be brought up or down, and they MUST be connected to the internet.",
962             "category": "devices",
963             "create_function": create_nodeiface,
964             "preconfigure_function": configure_nodeiface,
965             "box_attributes": [ ],
966             "connector_types": ["node", "inet"]
967         }),
968     TUNIFACE: dict({
969             "allow_addresses": True,
970             "help": "Virtual TUN network interface (layer 3)",
971             "category": "devices",
972             "create_function": create_tuniface,
973             "preconfigure_function": preconfigure_tuniface,
974             "configure_function": postconfigure_tuniface,
975             "prestart_function": wait_tuniface,
976             "box_attributes": [
977                 "up", "device_name", "mtu", "snat", "pointopoint",
978                 "txqueuelen",
979                 "tun_proto", "tun_addr", "tun_port", "tun_key"
980             ],
981             "traces": ["packets"],
982             "connector_types": ["node","udp","tcp","fd->"]
983         }),
984     TAPIFACE: dict({
985             "allow_addresses": True,
986             "help": "Virtual TAP network interface (layer 2)",
987             "category": "devices",
988             "create_function": create_tapiface,
989             "preconfigure_function": preconfigure_tuniface,
990             "configure_function": postconfigure_tuniface,
991             "prestart_function": wait_tuniface,
992             "box_attributes": [
993                 "up", "device_name", "mtu", "snat", "pointopoint",
994                 "txqueuelen",
995                 "tun_proto", "tun_addr", "tun_port", "tun_key"
996             ],
997             "traces": ["packets"],
998             "connector_types": ["node","udp","tcp","fd->"]
999         }),
1000     APPLICATION: dict({
1001             "help": "Generic executable command line application",
1002             "category": "applications",
1003             "create_function": create_application,
1004             "start_function": start_application,
1005             "status_function": status_application,
1006             "stop_function": stop_application,
1007             "configure_function": configure_application,
1008             "box_attributes": ["command", "sudo", "stdin",
1009                                "depends", "build-depends", "build", "install",
1010                                "sources" ],
1011             "connector_types": ["node"],
1012             "traces": ["stdout", "stderr", "buildlog"]
1013         }),
1014     DEPENDENCY: dict({
1015             "help": "Requirement for package or application to be installed on some node",
1016             "category": "applications",
1017             "create_function": create_dependency,
1018             "preconfigure_function": configure_dependency,
1019             "box_attributes": ["depends", "build-depends", "build", "install",
1020                                "sources" ],
1021             "connector_types": ["node"],
1022             "traces": ["buildlog"]
1023         }),
1024     NEPIDEPENDENCY: dict({
1025             "help": "Requirement for NEPI inside NEPI - required to run testbed instances inside a node",
1026             "category": "applications",
1027             "create_function": create_nepi_dependency,
1028             "preconfigure_function": configure_dependency,
1029             "box_attributes": [ ],
1030             "connector_types": ["node"],
1031             "traces": ["buildlog"]
1032         }),
1033     NS3DEPENDENCY: dict({
1034             "help": "Requirement for NS3 inside NEPI - required to run NS3 testbed instances inside a node. It also needs NepiDependency.",
1035             "category": "applications",
1036             "create_function": create_ns3_dependency,
1037             "preconfigure_function": configure_dependency,
1038             "box_attributes": [ ],
1039             "connector_types": ["node"],
1040             "traces": ["buildlog"]
1041         }),
1042     INTERNET: dict({
1043             "help": "Internet routing",
1044             "category": "topology",
1045             "create_function": create_internet,
1046             "connector_types": ["devs"],
1047         }),
1048     NETPIPE: dict({
1049             "help": "Link emulation",
1050             "category": "topology",
1051             "create_function": create_netpipe,
1052             "configure_function": configure_netpipe,
1053             "box_attributes": ["netpipe_mode",
1054                                "addr_list", "port_list",
1055                                "bw_in","plr_in","delay_in",
1056                                "bw_out","plr_out","delay_out"],
1057             "connector_types": ["node"],
1058             "traces": ["netpipe_stats"]
1059         }),
1060 })
1061
1062 testbed_attributes = dict({
1063         "slice": dict({
1064             "name": "slice",
1065             "help": "The name of the PlanetLab slice to use",
1066             "type": Attribute.STRING,
1067             "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
1068             "validation_function": validation.is_string
1069         }),
1070         "auth_user": dict({
1071             "name": "authUser",
1072             "help": "The name of the PlanetLab user to use for API calls - it must have at least a User role.",
1073             "type": Attribute.STRING,
1074             "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
1075             "validation_function": validation.is_string
1076         }),
1077         "auth_pass": dict({
1078             "name": "authPass",
1079             "help": "The PlanetLab user's password.",
1080             "type": Attribute.STRING,
1081             "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
1082             "validation_function": validation.is_string
1083         }),
1084         "plc_host": dict({
1085             "name": "plcHost",
1086             "help": "The PlanetLab PLC API host",
1087             "type": Attribute.STRING,
1088             "value": "www.planet-lab.eu",
1089             "flags": Attribute.DesignOnly,
1090             "validation_function": validation.is_string
1091         }),
1092         "plc_url": dict({
1093             "name": "plcUrl",
1094             "help": "The PlanetLab PLC API url pattern - %(hostname)s is replaced by plcHost.",
1095             "type": Attribute.STRING,
1096             "value": "https://%(hostname)s:443/PLCAPI/",
1097             "flags": Attribute.DesignOnly,
1098             "validation_function": validation.is_string
1099         }),
1100         "slice_ssh_key": dict({
1101             "name": "sliceSSHKey",
1102             "help": "The controller-local path to the slice user's ssh private key. "
1103                     "It is the user's responsability to deploy this file where the controller "
1104                     "will run, it won't be done automatically because it's sensitive information. "
1105                     "It is recommended that a NEPI-specific user be created for this purpose and "
1106                     "this purpose alone.",
1107             "type": Attribute.STRING,
1108             "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
1109             "validation_function": validation.is_string
1110         }),
1111     })
1112
1113 class VersionedMetadataInfo(metadata.VersionedMetadataInfo):
1114     @property
1115     def connector_types(self):
1116         return connector_types
1117
1118     @property
1119     def connections(self):
1120         return connections
1121
1122     @property
1123     def attributes(self):
1124         return attributes
1125
1126     @property
1127     def traces(self):
1128         return traces
1129
1130     @property
1131     def create_order(self):
1132         return create_order
1133
1134     @property
1135     def configure_order(self):
1136         return configure_order
1137
1138     @property
1139     def prestart_order(self):
1140         return start_order
1141
1142     @property
1143     def start_order(self):
1144         return start_order
1145
1146     @property
1147     def factories_info(self):
1148         return factories_info
1149
1150     @property
1151     def testbed_attributes(self):
1152         return testbed_attributes
1153