064da473a36659642826b18d7848db96da0c4d6e
[nepi.git] / src / neco / 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
4
5 from neco.resources.omf.omf_api import OMFAPIFactory
6
7 import neco
8 import logging
9
10 @clsinit
11 class OMFNode(ResourceManager):
12     _rtype = "OMFNode"
13     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
14
15     @classmethod
16     def _register_attributes(cls):
17         hostname = Attribute("hostname", "Hostname of the machine")
18         cpu = Attribute("cpu", "CPU of the node")
19         ram = Attribute("ram", "RAM of the node")
20         # XXX: flags = "0x02" is not human readable.
21         # instead:
22         # from neco.execution.attribute import Attribute, Flags 
23         # xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
24         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
25         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
26         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
27         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
28         cls._register_attribute(hostname)
29         cls._register_attribute(ram)
30         cls._register_attribute(cpu)
31         cls._register_attribute(xmppSlice)
32         cls._register_attribute(xmppHost)
33         cls._register_attribute(xmppPort)
34         ls._register_attribute(xmppPassword)
35
36     @classmethod
37     def _register_filters(cls):
38         hostname = Attribute("hostname", "Hostname of the machine")
39         gateway = Attribute("gateway", "Gateway")
40         granularity = Attribute("granularity", "Granularity of the reservation time")
41         hardware_type = Attribute("hardware_type", "Hardware type of the machine")
42         cls._register_filter(hostname)
43         cls._register_filter(gateway)
44         cls._register_filter(granularity)
45         cls._register_filter(hardware_type)
46
47     # XXX: We don't necessary need to have the credentials at the 
48     # moment we create the RM
49     # THE OMF API SHOULD BE CREATED ON THE DEPLOY METHOD, NOT NOW
50     # THIS FORCES MORE CONSTRAINES ON THE WAY WE WILL AUTHOMATE DEPLOYMENT!
51     def __init__(self, ec, guid, creds):
52         super(OMFNode, self).__init__(ec, guid)
53         self.set('xmppSlice', creds['xmppSlice'])
54         self.set('xmppHost', creds['xmppHost'])
55         self.set('xmppPort', creds['xmppPort'])
56         self.set('xmppPassword', creds['xmppPassword'])
57
58         # XXX: Lines should not be more than 80 characters!
59         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
60
61         self._logger = logging.getLogger("neco.omf.omfNode   ")
62
63         # XXX: TO DISCUSS
64         self._logger.setLevel(neco.LOGLEVEL)
65
66     def _validate_connection(self, guid):
67         rm = self.ec.resource(guid)
68         if rm.rtype() in self._authorized_connections:
69             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
70             return True
71         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
72         return False
73
74     def discover(self):
75         pass
76      
77     def provision(self, credential):
78         pass
79
80     def start(self):
81         self._omf_api.enroll_host(self.get('hostname'))
82
83     def stop(self):
84         self._omf_api.disconnect()
85
86     def configure(self):
87         #routes = self.tc._add_route.get(self.guid, [])
88         #iface_guids = self.tc.get_connected(self.guid, "devs", "node")
89        
90         for route in routes:
91             (destination, netprefix, nexthop, metric, device) = route
92             netmask = ipaddr2.ipv4_mask2dot(netprefix)
93
94             # Validate that the interface is associated to the node
95             for iface_guid in iface_guids:
96                 iface = self.tc.elements.get(iface_guid)
97                 if iface.devname == device:
98                     self._omf_api.execute(self.get('hostname'), 
99                         "Id#%s" % str(random.getrandbits(128)), 
100                         "add -net %s netmask %s dev %s" % (destination, netmask, iface.devname), 
101                         "/sbin/route", # path
102                         None, # env
103                      )
104                     break