Cross connections (initial planetlab-side implementation following the tun protocol)
[nepi.git] / src / nepi / core / connector.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Common connector base classes
6 """
7
8 import sys
9
10 class ConnectorTypeBase(object):
11     def __init__(self, testbed_id, factory_id, name, max = -1, min = 0):
12         super(ConnectorTypeBase, self).__init__()
13         if max == -1:
14             max = sys.maxint
15         elif max <= 0:
16             raise RuntimeError, "The maximum number of connections allowed need to be more than 0"
17         if min < 0:
18             raise RuntimeError, "The minimum number of connections allowed needs to be at least 0"
19         # connector_type_id -- univoquely identifies a connector type 
20         # across testbeds
21         self._connector_type_id = self.make_connector_type_id(
22             testbed_id, factory_id, name)
23         # name -- display name for the connector type
24         self._name = name
25         # max -- maximum amount of connections that this type support, 
26         # -1 for no limit
27         self._max = max
28         # min -- minimum amount of connections required by this type of connector
29         self._min = min
30
31     @property
32     def connector_type_id(self):
33         return self._connector_type_id
34
35     @property
36     def name(self):
37         return self._name
38
39     @property
40     def max(self):
41         return self._max
42
43     @property
44     def min(self):
45         return self._min
46     
47     @staticmethod
48     def make_connector_type_id(testbed_id, factory_id, name):
49         testbed_id = testbed_id.lower() if testbed_id else None
50         factory_id = factory_id.lower() if factory_id else None
51         name = name.lower() if name else None
52         return (testbed_id, factory_id, name)
53     
54     @staticmethod
55     def _type_resolution_order(connector_type_id):
56         testbed_id, factory_id, name = connector_type_id
57         
58         # the key is always a candidate
59         yield connector_type_id
60         
61         # Try wildcard combinations
62         if (testbed_id, None, name) != connector_type_id:
63             yield (testbed_id, None, name)
64         if (None, factory_id, name) != connector_type_id:
65             yield (None, factory_id, name)
66         if (None, None, name) != connector_type_id:
67             yield (None, None, name)
68
69