Minor typos
[nepi.git] / src / nepi / resources / omf / omf_node.py
1 #!/usr/bin/env python
2 from neco.execution.resource import ResourceManager, clsinit
3 from neco.execution.attribute import Attribute, Flags 
4
5 from neco.resources.omf.omf_api import OMFAPIFactory
6
7 import neco
8 import logging
9 import time
10
11 @clsinit
12 class OMFNode(ResourceManager):
13     """
14     .. class:: Class Args :
15       
16         :param ec: The Experiment controller
17         :type ec: ExperimentController
18         :param guid: guid of the RM
19         :type guid: int
20         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
21         :type creds: dict
22
23     .. note::
24
25        This class is used only by the Experiment Controller through the Resource Factory
26
27     """
28     _rtype = "OMFNode"
29     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
30     _waiters = []
31
32     @classmethod
33     def _register_attributes(cls):
34         """Register the attributes of an OMF Node
35
36         """
37         hostname = Attribute("hostname", "Hostname of the machine")
38         cpu = Attribute("cpu", "CPU of the node")
39         ram = Attribute("ram", "RAM of the node")
40         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
41         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
42         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
43         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
44         cls._register_attribute(hostname)
45         cls._register_attribute(ram)
46         cls._register_attribute(cpu)
47         cls._register_attribute(xmppSlice)
48         cls._register_attribute(xmppHost)
49         cls._register_attribute(xmppPort)
50         cls._register_attribute(xmppPassword)
51
52     @classmethod
53     def _register_filters(cls):
54         """Register the filters of an OMF Node
55
56         """
57         hostname = Attribute("hostname", "Hostname of the machine")
58         gateway = Attribute("gateway", "Gateway")
59         granularity = Attribute("granularity", "Granularity of the reservation time")
60         hardware_type = Attribute("hardware_type", "Hardware type of the machine")
61         cls._register_filter(hostname)
62         cls._register_filter(gateway)
63         cls._register_filter(granularity)
64         cls._register_filter(hardware_type)
65
66     # XXX: We don't necessary need to have the credentials at the 
67     # moment we create the RM
68     def __init__(self, ec, guid):
69         """
70         :param ec: The Experiment controller
71         :type ec: ExperimentController
72         :param guid: guid of the RM
73         :type guid: int
74         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
75         :type creds: dict
76
77         """
78         super(OMFNode, self).__init__(ec, guid)
79
80         self._omf_api = None 
81
82         self._logger = logging.getLogger("neco.omf.omfNode   ")
83
84         # XXX: TO DISCUSS
85         self._logger.setLevel(neco.LOGLEVEL)
86
87     def _validate_connection(self, guid):
88         """Check if the connection is available.
89
90         :param guid: Guid of the current RM
91         :type guid: int
92         :rtype:  Boolean
93
94         """
95         rm = self.ec.get_resource(guid)
96         if rm.rtype() in self._authorized_connections:
97             self._logger.debug("Connection between %s %s and %s %s accepted" %
98                 (self.rtype(), self._guid, rm.rtype(), guid))
99             return True
100         self._logger.debug("Connection between %s %s and %s %s refused" %
101             (self.rtype(), self._guid, rm.rtype(), guid))
102         return False
103
104     def deploy_action(self):
105         """Deploy the RM
106
107         """ 
108         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
109             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
110         self._omf_api.enroll_host(self.get('hostname'))
111
112         super(OMFNode, self).deploy_action()
113
114     def discover(self):
115         """ Discover the availables nodes
116
117         """
118         pass
119      
120     def provision(self):
121         """ Provision some availables nodes
122
123         """
124         pass
125
126     def start(self):
127         """Send Xmpp Message Using OMF protocol to enroll the node into the experiment
128
129         """
130         super(OMFNode, self).start()
131
132
133     def stop(self):
134         """Send Xmpp Message Using OMF protocol to disconnect the node
135
136         """
137         super(OMFNode, self).stop()
138
139     def release(self):
140         """Clean the RM at the end of the experiment
141
142         """
143         self._omf_api.release(self.get('hostname'))
144         OMFAPIFactory.release_api(self.get('xmppSlice'), 
145             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
146
147
148     def configure(self):
149         #routes = self.tc._add_route.get(self.guid, [])
150         #iface_guids = self.tc.get_connected(self.guid, "devs", "node")
151        
152         for route in routes:
153             (destination, netprefix, nexthop, metric, device) = route
154             netmask = ipaddr2.ipv4_mask2dot(netprefix)
155
156             # Validate that the interface is associated to the node
157             for iface_guid in iface_guids:
158                 iface = self.tc.elements.get(iface_guid)
159                 if iface.devname == device:
160                     self._omf_api.execute(self.get('hostname'), 
161                         "Id#%s" % str(random.getrandbits(128)), 
162                         "add -net %s netmask %s dev %s" % (destination, netmask, iface.devname), 
163                         "/sbin/route", # path
164                         None, # env
165                      )
166                     break