root / kamaki / clients / commissioning / callpoint.py @ 6764f588
History | View | Annotate | Download (8.2 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
|
35 |
from kamaki.clients.commissioning.specificator import CanonifyException |
36 |
from kamaki.clients.commissioning.exception import CorruptedError |
37 |
from kamaki.clients.commissioning.exception import InvalidDataError |
38 |
from kamaki.clients.commissioning.exception import ReturnButFail |
39 |
from kamaki.clients.commissioning.importing import imp_module |
40 |
|
41 |
from re import compile as re_compile, sub as re_sub |
42 |
|
43 |
|
44 |
class Callpoint(object): |
45 |
|
46 |
api_spec = None
|
47 |
|
48 |
CorruptedError = CorruptedError |
49 |
InvalidDataError = InvalidDataError |
50 |
|
51 |
original_calls = None
|
52 |
|
53 |
def __init__(self, connection=None): |
54 |
from json import loads, dumps |
55 |
|
56 |
self.json_loads = loads
|
57 |
self.json_dumps = dumps
|
58 |
self.init_connection(connection)
|
59 |
original_calls = {} |
60 |
self.original_calls = original_calls
|
61 |
canonifier = self.api_spec
|
62 |
|
63 |
if canonifier is None: |
64 |
m = "No api spec given to '%s'" % (type(self).__name__,) |
65 |
raise NotImplementedError(m) |
66 |
|
67 |
for call_name, call_doc in canonifier.call_docs(): |
68 |
if hasattr(self, call_name): |
69 |
# don't crash: wrap the function instead
|
70 |
#m = ( "Method '%s' defined both in natively "
|
71 |
# "in callpoint '%s' and in api spec '%s'" %
|
72 |
# (call_name,
|
73 |
# type(self).__name__,
|
74 |
# type(canonifier).__name__) )
|
75 |
|
76 |
#raise ValueError(m)
|
77 |
call_func = getattr(self, call_name) |
78 |
if not callable(call_func): |
79 |
m = ("api spec '%s', method '%s' is not a "\
|
80 |
"callable attribute in callpoint '%s'" %\
|
81 |
(type(canonifier).__name__,
|
82 |
call_name, |
83 |
type(self).__name)) |
84 |
raise ValueError(m) |
85 |
|
86 |
original_calls[call_name] = call_func |
87 |
|
88 |
def mk_call_func(): |
89 |
local_call_name = call_name |
90 |
|
91 |
def call_func(**data): |
92 |
return self.make_call(local_call_name, data) |
93 |
|
94 |
call_func.__name__ = call_name |
95 |
call_func.__doc__ = call_doc |
96 |
return call_func
|
97 |
|
98 |
setattr(self, call_name, mk_call_func()) |
99 |
|
100 |
def init_connection(self, connection): |
101 |
pass
|
102 |
|
103 |
def commit(self): |
104 |
pass
|
105 |
|
106 |
def rollback(self): |
107 |
pass
|
108 |
|
109 |
def do_make_call(self, call_name, data): |
110 |
raise NotImplementedError |
111 |
|
112 |
def validate_call(self, call_name): |
113 |
return hasattr(self, call_name) |
114 |
|
115 |
def make_call_from_json_description(self, json_description): |
116 |
try:
|
117 |
description = self.json_loads(json_description)
|
118 |
except ValueError: |
119 |
m = "Cannot load json description"
|
120 |
raise self.InvalidDataError(m) |
121 |
|
122 |
data = self.make_call_from_description(description)
|
123 |
json_data = self.json_dumps(data) if data is not None else None |
124 |
return json_data
|
125 |
|
126 |
def make_call_from_description(self, description): |
127 |
try:
|
128 |
call_name = description['call_name']
|
129 |
call_data = description['call_data']
|
130 |
except (TypeError, KeyError), e: |
131 |
m = "Invalid description"
|
132 |
raise self.InvalidDataError(m, e) |
133 |
|
134 |
return self.make_call(call_name, call_data) |
135 |
|
136 |
def make_call_from_json(self, call_name, json_data): |
137 |
if json_data:
|
138 |
try:
|
139 |
data = self.json_loads(json_data)
|
140 |
except ValueError, e: |
141 |
m = "Cannot load json data"
|
142 |
raise self.InvalidDataError(m, e) |
143 |
else:
|
144 |
data = None
|
145 |
|
146 |
data = self.make_call(call_name, data)
|
147 |
json_data = self.json_dumps(data)
|
148 |
return json_data
|
149 |
|
150 |
def make_call(self, call_name, data): |
151 |
if call_name.startswith('_'): |
152 |
m = "Invalid call '%s'" % (call_name,)
|
153 |
raise self.InvalidDataError(m) |
154 |
|
155 |
canonifier = self.api_spec
|
156 |
try:
|
157 |
data = canonifier.canonify_input(call_name, data) |
158 |
except CanonifyException, e:
|
159 |
m = "Invalid input to call '%s'" % (call_name,)
|
160 |
raise self.InvalidDataError(m, e) |
161 |
|
162 |
if not self.validate_call(call_name): |
163 |
m = "Cannot find specified call '%s'" % (call_name,)
|
164 |
raise self.CorruptedError(m) |
165 |
|
166 |
call_func = self.original_calls.get(call_name, None) |
167 |
try:
|
168 |
if call_func is None: |
169 |
data = self.do_make_call(call_name, data)
|
170 |
else:
|
171 |
data = call_func(**data) |
172 |
self.commit()
|
173 |
except ReturnButFail, e:
|
174 |
self.rollback()
|
175 |
data = e.data |
176 |
except Exception, e: |
177 |
self.rollback()
|
178 |
raise
|
179 |
|
180 |
try:
|
181 |
data = canonifier.canonify_output(call_name, data) |
182 |
except CanonifyException, e:
|
183 |
m = "Invalid output from call '%s'" % (call_name,)
|
184 |
raise self.CorruptedError(m, e) |
185 |
|
186 |
return data
|
187 |
|
188 |
|
189 |
def mkcallargs(**kw): |
190 |
return kw
|
191 |
|
192 |
|
193 |
versiontag_pattern = re_compile('[^a-zA-Z0-9_-]')
|
194 |
|
195 |
|
196 |
def mk_versiontag(version): |
197 |
if not version or version == 'v': |
198 |
return '' |
199 |
|
200 |
return '_' + re_sub(versiontag_pattern, '_', version) |
201 |
|
202 |
|
203 |
def get_callpoint(pointname, version=None, automake=None, **kw): |
204 |
|
205 |
versiontag = mk_versiontag(version) |
206 |
components = pointname.split('.')
|
207 |
|
208 |
appname = components[0]
|
209 |
if len(components) < 2: |
210 |
raise ValueError("invalid pointname '%s'" % (pointname,)) |
211 |
|
212 |
category = components[1]
|
213 |
if not category or category not in ['clients', 'servers']: |
214 |
raise ValueError("invalid pointname '%s'" % (pointname,)) |
215 |
|
216 |
modname = ('%s.callpoint.API_Callpoint%s' % (pointname, versiontag))
|
217 |
|
218 |
try:
|
219 |
API_Callpoint = imp_module(modname) |
220 |
return API_Callpoint
|
221 |
except ImportError: |
222 |
if not automake: |
223 |
raise
|
224 |
|
225 |
if category != 'clients': |
226 |
m = ( |
227 |
"Can only auto-make callpoint in 'clients' not '%s'" % (category))
|
228 |
raise ValueError(m) |
229 |
|
230 |
components = components[1:]
|
231 |
if not components: |
232 |
raise ValueError("invalid pointname '%s'" % (pointname)) |
233 |
|
234 |
pointname = '.'.join(components)
|
235 |
if pointname == 'quotaholder': |
236 |
apiname = 'quotaholder.api.QuotaholderAPI'
|
237 |
else:
|
238 |
apiname = '%s.api.API_Spec%s' % (pointname, versiontag)
|
239 |
|
240 |
API_Spec = imp_module(apiname) |
241 |
|
242 |
basename = 'commissioning.clients.%s.API_Callpoint' % (automake,)
|
243 |
BaseCallpoint = imp_module(basename) |
244 |
|
245 |
stupidpython = (appname, |
246 |
version if version is not None else 'v', |
247 |
pointname, |
248 |
automake) |
249 |
|
250 |
class AutoCallpoint(BaseCallpoint): |
251 |
appname, version, pointname, automake = stupidpython |
252 |
api_spec = API_Spec() |
253 |
|
254 |
return AutoCallpoint
|