Merge with comments
[nepi.git] / src / neco / resources / omf / omf_channel.py
1 #!/usr/bin/env python
2 from neco.execution.resource import Resource, 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 OMFChannel(Resource):
12     """
13     .. class:: Class Args :
14       
15         :param ec: The Experiment controller
16         :type ec: ExperimentController
17         :param guid: guid of the RM
18         :type guid: int
19         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
20         :type creds: dict
21
22     .. note::
23
24        This class is used only by the Experiment Controller through the Resource Factory
25
26     """
27     _rtype = "OMFChannel"
28     _authorized_connections = ["OMFWifiInterface"]
29
30     @classmethod
31     def _register_attributes(cls):
32         """Register the attributes of an OMF channel
33         """
34         channel = Attribute("channel", "Name of the application")
35         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
36         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
37         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
38         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
39         cls._register_attribute(channel)
40         cls._register_attribute(xmppSlice)
41         cls._register_attribute(xmppHost)
42         cls._register_attribute(xmppPort)
43         cls._register_attribute(xmppPassword)
44
45     def __init__(self, ec, guid, creds):
46         """
47         :param ec: The Experiment controller
48         :type ec: ExperimentController
49         :param guid: guid of the RM
50         :type guid: int
51         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
52         :type creds: dict
53
54         """
55         super(OMFChannel, self).__init__(ec, guid)
56         self.set('xmppSlice', creds['xmppSlice'])
57         self.set('xmppHost', creds['xmppHost'])
58         self.set('xmppPort', creds['xmppPort'])
59         self.set('xmppPassword', creds['xmppPassword'])
60
61         self._nodes_guid = list()
62
63         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
64
65         self._logger = logging.getLogger("neco.omf.omfChannel")
66         self._logger.setLevel(neco.LOGLEVEL)
67
68     def _validate_connection(self, guid):
69         """Check if the connection is available.
70
71         :param guid: Guid of the current RM
72         :type guid: int
73         :rtype:  Boolean
74
75         """
76         rm = self.ec.resource(guid)
77         if rm.rtype() in self._authorized_connections:
78             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
79             return True
80         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
81         return False
82
83     def _get_target(self, conn_set):
84         """
85         Get the couples (host, interface) that used this channel
86
87         :param conn_set: Connections of the current Guid
88         :type conn_set: set
89         :rtype: list
90         :return: self._nodes_guid
91         """
92         for elt in conn_set:
93             rm_iface = self.ec.resource(elt)
94             for conn in rm_iface._connections:
95                 rm_node = self.ec.resource(conn)
96                 if rm_node.rtype() == "OMFNode":
97                     couple = [rm_node.get('hostname'), rm_iface.get('alias')]
98                     #print couple
99                     self._nodes_guid.append(couple)
100         return self._nodes_guid
101
102     def discover(self):
103         """ Discover the availables channels
104
105         """
106         pass
107      
108     def provision(self, credential):
109         """ Provision some availables channels
110
111         """
112         pass
113
114     def start(self):
115         """Send Xmpp Message Using OMF protocol to configure Channel
116
117         """
118         if self.get('channel'):
119             set_nodes = self._get_target(self._connections) 
120             #print set_nodes
121             for couple in set_nodes:
122                 #print "Couple node/alias : " + couple[0] + "  ,  " + couple[1]
123                 attrval = self.get('channel')
124                 attrname = "net/%s/%s" % (couple[1], 'channel')
125                 #print "Send the configure message"
126                 self._omf_api.configure(couple[0], attrname, attrval)
127
128     def xstart(self):
129         try:
130             if self.get('channel'):
131                 node = self.tc.elements.get(self._node_guid)    
132                 attrval = self.get('channel')
133                 attrname = "net/%s/%s" % (self._alias, 'channel')
134                 self._omf_api.configure('omf.plexus.wlab17', attrname, attrval)
135         except AttributeError:
136             # If the attribute is not yet defined, ignore the error
137             pass
138
139