00a20f9efbc4fd2f4c64b4ba9d3cf18498c6d2c0
[nepi.git] / src / nepi / resources / omf / omf_api_factory.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #         Julien Tribino <julien.tribino@inria.fr>
20
21
22 import time
23 import hashlib
24 import threading
25
26 from nepi.resources.omf.omf5_api import OMF5API
27 from nepi.resources.omf.omf6_api import OMF6API
28
29 class OMFAPIFactory(object):
30     """ 
31     .. note::
32
33         It allows the different RM to use the same xmpp client if they use 
34         the same credentials.  For the moment, it is focused on XMPP.
35
36     """
37     # use lock to avoid concurrent access to the Api list at the same times by 2 
38     # different threads
39     lock = threading.Lock()
40     _apis = dict()
41
42     @classmethod 
43     def get_api(cls, version, server, user, port, password, exp_id = None):
44         """ Get an OMF Api
45
46         :param slice: Xmpp Slice Name
47         :type slice: str
48         :param server: Xmpp Server Adress
49         :type server: str
50         :param port: Xmpp Port (Default : 5222)
51         :type port: str
52         :param password: Xmpp Password
53         :type password: str
54
55         """
56         if version and user and server and port and password:
57             key = cls._make_key(version, server, user, port, password, exp_id)
58             cls.lock.acquire()
59             if key in cls._apis:
60                 #print "Api Counter : " + str(cls._apis[key]['cnt'])
61                 cls._apis[key]['cnt'] += 1
62                 cls.lock.release()
63                 return cls._apis[key]['api']
64             else :
65                 omf_api = cls.create_api(version, server, user, port, password, exp_id)
66                 cls.lock.release()
67                 return omf_api
68         return None
69
70     @classmethod 
71     def create_api(cls, version, server, user, port, password, exp_id):
72         """ Create an OMF API if this one doesn't exist yet with this credentials
73
74         :param slice: Xmpp Slice Name
75         :type slice: str
76         :param server: Xmpp Server Adress
77         :type server: str
78         :param port: Xmpp Port (Default : 5222)
79         :type port: str
80         :param password: Xmpp Password
81         :type password: str
82
83         """
84         key = cls._make_key(version, server, user, port, password, exp_id)
85         if version == "5":
86             omf_api = OMF5API(server, user, port, password, exp_id = exp_id)
87         else :
88             omf_api = OMF6API(server, user = user, port = port, password = password, exp_id = exp_id)
89         cls._apis[key] = {}
90         cls._apis[key]['api'] = omf_api
91         cls._apis[key]['cnt'] = 1
92         return omf_api
93
94     @classmethod 
95     def release_api(cls, version, server, user, port, password, exp_id = None):
96         """ Release an OMF API with this credentials
97
98         :param slice: Xmpp Slice Name
99         :type slice: str
100         :param server: Xmpp Server Adress
101         :type server: str
102         :param port: Xmpp Port (Default : 5222)
103         :type port: str
104         :param password: Xmpp Password
105         :type password: str
106
107         """
108         if version and user and server and port and password:
109             key = cls._make_key(version, server, user, port, password, exp_id)
110             if key in cls._apis:
111                 cls._apis[key]['cnt'] -= 1
112                 #print "Api Counter : " + str(cls._apis[key]['cnt'])
113                 if cls._apis[key]['cnt'] == 0:
114                     omf_api = cls._apis[key]['api']
115                     omf_api.disconnect()
116
117
118     @classmethod 
119     def _make_key(cls, *args):
120         """ Hash the credentials in order to create a key
121
122         :param args: list of arguments used to create the hash (server, user, port, ...)
123         :type args: list of args
124
125         """
126         skey = "".join(map(str, args))
127         return hashlib.md5(skey).hexdigest()
128
129
130