2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 # Julien Tribino <julien.tribino@inria.fr>
26 from nepi.resources.omf.omf5_api import OMF5API
27 from nepi.resources.omf.omf6_api import OMF6API
29 class OMFAPIFactory(object):
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.
37 # use lock to avoid concurrent access to the Api list at the same times by 2
39 lock = threading.Lock()
43 def get_api(cls, version, server, user, port, password, exp_id = None):
46 :param slice: Xmpp Slice Name
48 :param server: Xmpp Server Adress
50 :param port: Xmpp Port (Default : 5222)
52 :param password: Xmpp Password
56 if version and user and server and port and password:
57 key = cls._make_key(version, server, user, port, password, exp_id)
60 #print "Api Counter : " + str(cls._apis[key]['cnt'])
61 cls._apis[key]['cnt'] += 1
63 return cls._apis[key]['api']
65 omf_api = cls.create_api(version, server, user, port, password, exp_id)
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
74 :param slice: Xmpp Slice Name
76 :param server: Xmpp Server Adress
78 :param port: Xmpp Port (Default : 5222)
80 :param password: Xmpp Password
84 key = cls._make_key(version, server, user, port, password, exp_id)
86 omf_api = OMF5API(server, user, port, password, exp_id = exp_id)
88 omf_api = OMF6API(server, user = user, port = port, password = password, exp_id = exp_id)
90 cls._apis[key]['api'] = omf_api
91 cls._apis[key]['cnt'] = 1
95 def release_api(cls, version, server, user, port, password, exp_id = None):
96 """ Release an OMF API with this credentials
98 :param slice: Xmpp Slice Name
100 :param server: Xmpp Server Adress
102 :param port: Xmpp Port (Default : 5222)
104 :param password: Xmpp Password
108 if version and user and server and port and password:
109 key = cls._make_key(version, server, user, port, password, exp_id)
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']
119 def _make_key(cls, *args):
120 """ Hash the credentials in order to create a key
122 :param args: list of arguments used to create the hash (server, user, port, ...)
123 :type args: list of args
126 skey = "".join(map(str, args))
127 return hashlib.md5(skey).hexdigest()