2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 import nepi.util.ipaddr2 as ipaddr2
8 class NodeIface(object):
9 def __init__(self, api=None):
17 # These get initialized at configuration time
22 self._interface_id = None
24 # These get initialized when the iface is connected to its node
27 # These get initialized when the iface is connected to the internet
28 self.has_internet = False
30 def add_address(self, address, netprefix, broadcast):
31 raise RuntimeError, "Cannot add explicit addresses to public interface"
33 def pick_iface(self, siblings):
35 Picks an interface using the PLCAPI to query information about the node.
37 Needs an assigned node.
40 siblings: other NodeIface elements attached to the same node
43 if (self.node or self.node._node_id) is None:
44 raise RuntimeError, "Cannot pick interface without an assigned node"
46 avail = self._api.GetInterfaces(
47 node_id=self.node._node_id,
48 is_primary=self.primary,
49 fields=('interface_id','mac','netmask','ip') ))
51 used = set([sibling._interface_id for sibling in siblings
52 if sibling._interface_id is not None])
54 for candidate in avail:
55 candidate_id = candidate['interface_id']
56 if candidate_id not in used:
58 self._interface_id = candidate_id
59 self.address = candidate['ip']
60 self.lladdr = candidate['mac']
61 self.netprefix = candidate['netmask']
64 raise RuntimeError, "Cannot configure interface: cannot find suitable interface in PlanetLab node"
67 if not element.has_internet:
68 raise RuntimeError, "All external interface devices must be connected to the Internet"
71 class TunIface(object):
72 def __init__(self, api=None):
83 self.device_name = None
87 # These get initialized when the iface is connected to its node
90 def add_address(self, address, netprefix, broadcast):
91 if (self.address or self.netprefix or self.netmask) is not None:
92 raise RuntimeError, "Cannot add more than one address to TUN interfaces"
94 raise ValueError, "TUN interfaces cannot broadcast in PlanetLab"
96 self.address = address
97 self.netprefix = netprefix
98 self.netmask = ipaddr2.ipv4_dot2mask(netprefix)
104 # Yep, it does nothing - yet
105 class Internet(object):
106 def __init__(self, api=None):
108 api = plcapi.PLCAPI()