Debian Codegraph @

networking_bagpipe/bagpipe_bgp/common/looking_glass.py

raw
1 # Copyright 2014 Orange
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging as python_logging
16 import re
17
18 from oslo_log import log as logging
19 from urllib.parse import quote
20
21
22 LOG = logging.getLogger(__name__)
23
24 VALUE = 'VALUE'
25 SUBITEM = 'SUBITEM'
26 SUBTREE = 'SUBTREE'
27 FORWARD = 'FORWARD'
28 COLLECTION = 'COLLECTION'
29 DELEGATE = 'DELEGATE'
30
31
32 def _split_lg_path(path_prefix, path):
33 if len(path) == 0:
34 return (None, None, path_prefix)
35 else:
36 return (path[0], path[1:],
37 # pylint: disable=no-member
38 "{}/{}".format(path_prefix, quote(path[0])))
39
40
41 def _get_lg_local_info_recurse(obj, cls, path_prefix):
42
43 if cls == LookingGlassMixin:
44 return {}
45
46 result = cls.get_lg_local_info(obj, path_prefix)
47
48 assert isinstance(result, dict)
49
50 for base in cls.__bases__:
51 if issubclass(base, LookingGlassMixin):
52 result.update(
53 _get_lg_local_info_recurse(obj, base, path_prefix))
54
55 return result
56
57
58 def _get_lg_map_recurse(obj, cls):
59
60 if cls == LookingGlassMixin:
61 return {}
62
63 result = cls.get_lg_map(obj)
64
65 for base in cls.__bases__:
66 if issubclass(base, LookingGlassMixin):
67 result.update(_get_lg_map_recurse(obj, base))
68 else:
69 LOG.debug("not recursing into %s", base)
70
71 return result
72
73
74 def _lookup_path(my_dict, path):
75 '''lookup path in dict'''
76 assert isinstance(path, (list, tuple))
77
78 if len(path) == 0:
79 return my_dict
80
81 # len(path)>0
82 if not isinstance(my_dict, dict):
83 raise KeyError(path[0])
84 else:
85 return _lookup_path(my_dict[path[0]], path[1:])
86
87
88 def get_lg_prefixed_path(path_prefix, path_items):
89 fmt = "%s" + ('/%s' * len(path_items))
90 # pylint: disable=no-member
91 quoted_path_items = [quote(path_item) for path_item in path_items]
92 quoted_path_items.insert(0, path_prefix)
93 return fmt % tuple(quoted_path_items)
94
95
96 class LookingGlassMixin:
97
98 def _get_lg_map(self):
99 # not to be overridden: calls get_lg_map, on each of the super classes
100 # and merge the result in a dict
101 return _get_lg_map_recurse(self, self.__class__)
102
103 def get_lg_map(self):
104 """Main looking glass hook for LG objects
105
106 This can be overridden by looking glass objects to indicate looking
107 glass information items for this objects.
108
109 :returns: a dict mapping a <subpath> to a (<lg_map_type>,<hook>) tuple
110
111 if lg_map_type is VALUE, then the looking glass information for
112 <subpath> will be <hook>
113 if lg_map_type is SUBITEM, then <hook> is supposed to be a function
114 and the looking glass information for <subpath> will be the result
115 of calling hook()
116 if lg_map_type is SUBTREE, then <hook> is supposed to be a function and
117 the looking glass information for <subpath> will be the result of
118 calling hook(path_prefix), but this information will only be
119 produced if the <subpath> is queried (not produced if the full
120 object is queried)
121 if lg_map_type is FORWARD, then <hook> is supposed to be a looking
122 glass object and the looking glass information for <subpath>
123 will be the looking glass information for <subpath> of object
124 <hook>
125 if lg_map_type is DELEGATE, then <hook> is supposed to be a looking
126 glass object and the looking glass information for <subpath> will
127 be the full looking glass information for object <hook>
128 if lg_map_type is COLLECTION, then <hook> is supposed to be a tuple of
129 functions (list_callback,target_callback). list_callback() is
130 expected
131 to return a list of string, each string identifying a looking
132 glass object target_callback(string) is expected to return the
133 looking glass object corresponding to <string>
134
135 if *self* is directly queried, the information returned is just a
136 list of dict containing "href" values pointing to each object in
137 the collection
138 if a <subpath> is queried, the information returned is the
139 looking glass information for the object corresponding to
140 <subpath>
141 """
142 return {}
143
144 def _get_lg_local_info(self, path_prefix):
145 # not to be overridden: calls get_lg_local_info, on each of
146 # the super classes and merge the result in a dict
147 return _get_lg_local_info_recurse(self, self.__class__, path_prefix)
148
149 def get_lg_local_info(self, path_prefix):
150 """Deeper hook for LG objects
151
152 Can be overridden by looking glass objects instead of get_lg_map
153
154 :param path_prefix: the URL prefix that was used to reach *self*
155 through the looking glass
156 :returns: a dict that will be serialized as JSON and passed to the
157 looking glass client, either as is, or if a sub path was queried,
158 the dict value corresponding to the first item of the path
159 """
160 return {}
161
162 def get_looking_glass_info(self, path_prefix="", path=None):
163 """Internal method to build the looking glass information
164
165 Builds the LG information for *self* based on the looking glass map.
166
167 *not* to be overridden by looking glass objects
168 """
169 if path is None:
170 path = []
171
172 (first_segment,
173 path_reminder,
174 new_path_prefix) = _split_lg_path(path_prefix, path)
175
176 lg_map = self._get_lg_map()
177
178 if first_segment in lg_map:
179 (mapping_type, mapping_target) = lg_map[first_segment]
180 LOG.debug("Delegation for path_item '%s': %s:%s ",
181 first_segment, mapping_type, mapping_target)
182
183 if mapping_type == VALUE:
184 return mapping_target
185
186 if mapping_type == FORWARD:
187 LOG.debug(
188 " Forwarded '%s' to target %s...", path, mapping_target)
189 if not isinstance(mapping_target, LookingGlassMixin):
190 LOG.error("Delegation target for '%s' at '%s' does not "
191 "implement LookingGlassMixin!",
192 first_segment, new_path_prefix)
193 raise NoSuchLookingGlassObject(new_path_prefix,
194 first_segment)
195 return mapping_target.get_looking_glass_info(path_prefix, path)
196
197 if mapping_type == FORWARD:
198 LOG.debug(
199 " Forwarded '%s' to target %s...", path, mapping_target)
200 if not isinstance(mapping_target, LookingGlassMixin):
201 LOG.error("Delegation target for '%s' at '%s' does not "
202 "implement LookingGlassMixin!",
203 first_segment, new_path_prefix)
204 raise NoSuchLookingGlassObject(new_path_prefix,
205 first_segment)
206 return mapping_target.get_looking_glass_info(path_prefix, path)
207
208 elif mapping_type == DELEGATE:
209 LOG.debug(
210 " Delegated '%s' to delegation target %s ...",
211 path, mapping_target)
212 if not isinstance(mapping_target, LookingGlassMixin):
213 LOG.error("Delegation target for '%s' at '%s' does not "
214 "implement LookingGlassMixin!",
215 first_segment, new_path_prefix)
216 raise NoSuchLookingGlassObject(new_path_prefix,
217 first_segment)
218 return mapping_target.get_looking_glass_info(new_path_prefix,
219 path_reminder)
220
221 elif mapping_type == SUBITEM:
222 LOG.debug(" Sub-item callback: %s", first_segment)
223 try:
224 return _lookup_path(mapping_target(), path_reminder)
225 except KeyError as e:
226 raise NoSuchLookingGlassObject(new_path_prefix, str(e))
227
228 elif mapping_type == SUBTREE:
229 LOG.debug(" Subtree callback: %s(...)", first_segment)
230 try:
231 return _lookup_path(mapping_target(new_path_prefix),
232 path_reminder)
233 except KeyError as e:
234 raise NoSuchLookingGlassObject(new_path_prefix, str(e))
235
236 elif mapping_type == COLLECTION:
237 LOG.debug(" Collection callback...")
238
239 (list_callback, target_callback) = mapping_target
240
241 (second_segment, path_reminder, newer_path_prefix) = \
242 _split_lg_path(new_path_prefix, path_reminder)
243 if second_segment is None:
244 LOG.debug(" Getting list elements: %s", list_callback)
245 result = []
246 for x in list_callback():
247 x["href"] = get_lg_prefixed_path(path_prefix,
248 [first_segment,
249 x["id"]])
250 result.append(x)
251 return result
252 else:
253 LOG.debug(" Callback -> resolve subItem '%s' with %s "
254 "and follow up get_looking_glass_info(...'%s')",
255 second_segment, target_callback, path_reminder)
256 try:
257 # TODO(tmorin): catch errors
258 target = target_callback(second_segment)
259 if target is None:
260 LOG.error("No delegation target for '%s' at '%s' ",
261 second_segment, new_path_prefix)
262 raise NoSuchLookingGlassObject(new_path_prefix,
263 second_segment)
264 if not isinstance(target, LookingGlassMixin):
265 LOG.error("Delegation target for '%s' at '%s' does"
266 " not implement LookingGlassMixin (%s)!",
267 second_segment, new_path_prefix,
268 type(target))
269 raise NoSuchLookingGlassObject(new_path_prefix,
270 second_segment)
271 return target.get_looking_glass_info(newer_path_prefix,
272 path_reminder)
273 except KeyError:
274 raise NoSuchLookingGlassObject(new_path_prefix,
275 second_segment)
276
277 # firt_segment is None or is not in our map
278 # let's build LookingGlassLocalInfo
279 info = self._get_lg_local_info(path_prefix)
280 for (path_item, (mapping_type, mapping_target)) in lg_map.items():
281 if path_item in info:
282 LOG.warning("overriding '%s', present both in "
283 "LookingGlassLocalInfo and LookingGlassMixin map",
284 path_item)
285 if mapping_type in (FORWARD, DELEGATE, SUBTREE, COLLECTION):
286 info[path_item] = {"href": get_lg_prefixed_path(path_prefix,
287 [path_item])
288 }
289 elif mapping_type == SUBITEM:
290 LOG.debug(" Subitem => callback %s(...)", mapping_target)
291 # TODO(tmorin): catch errors
292 info[path_item] = mapping_target()
293 elif mapping_type == VALUE:
294 info[path_item] = mapping_target
295 else:
296 LOG.warning("LGMap not processed for %s", path_item)
297
298 if first_segment is None:
299 return info
300 else:
301 try:
302 return _lookup_path(info, path)
303 except KeyError as e:
304 raise NoSuchLookingGlassObject(new_path_prefix, str(e))
305
306 LOG.warning("Looking glass did not found a looking-glass object for"
307 " this path...")
308
309 return None
310
311
312 class NoSuchLookingGlassObject(Exception):
313
314 def __init__(self, path_prefix, path):
315 super().__init__()
316 assert isinstance(path_prefix, str)
317 self.path_prefix = path_prefix
318
319 assert isinstance(path, str)
320 self.path = path
321
322 def __repr__(self):
323 return "No such looking glass object: {} at {}".format(
324 self.path, self.path_prefix)
325
326
327 # Looking glass reference URLs
328 root = ""
329 references = {}
330
331
332 def set_references_root(url_prefix):
333 global root
334 root = url_prefix
335
336
337 def set_reference_path(reference, path):
338 references[reference] = path
339
340
341 def get_absolute_path(reference, path_prefix, path=None):
342 if path is None:
343 path = []
344 index = path_prefix.find(root)
345 absolute_base_url = path_prefix[:index + len(root)]
346 return get_lg_prefixed_path(absolute_base_url,
347 references[reference] + path)
348
349
350 class LookingGlassLogHandler(python_logging.Handler):
351
352 """Looking Glass LogHandler storing logs to make them available in LG
353
354 This log handler simply stores the last <max_size> messages of importance
355 above <level>. These messages can be retrieved with .get_records().
356 """
357
358 def __init__(self, level=logging.WARNING, max_size=100):
359 super().__init__(level)
360 self.records = []
361 self.max_size = max_size
362 self.setFormatter(
363 python_logging.Formatter('%(asctime)s - %(levelname)s - '
364 '%(message)s'))
365
366 def emit(self, record):
367 # expand the log message now and free references to the arguments
368 record.msg = record.getMessage().replace('"', "'")
369 record.args = []
370 self.records.insert(0, record)
371 del self.records[self.max_size:]
372
373 def __len__(self):
374 return len(self.records)
375
376 def get_records(self):
377 return self.records
378
379 def reset_local_lg_logs(self):
380 del self.records[:]
381
382
383 class LookingGlassLocalLogger(LookingGlassMixin):
384
385 """LookingGlass Mixin making self.log a LookingGlass log catcher
386
387 For objects subclassing this class, self.log will be a logger derived from
388 <name> based on the existing logging configuration, but with an additional
389 logger using LookingGlassLogHandler.
390
391 This additional logger is used to make the last <n> records (above WARNING)
392 available through the looking glass
393 """
394
395 def __init__(self, append_to_name=""):
396 try:
397 self.lg_log_handler
398 except AttributeError:
399 self.lg_log_handler = LookingGlassLogHandler()
400 name = self.__module__
401 if append_to_name:
402 name += "." + append_to_name
403 elif hasattr(self, 'instance_id'):
404 # pylint: disable=no-member
405 name += ".%d" % self.instance_id
406 elif hasattr(self, 'name'):
407 # pylint: disable=no-member
408 name += ".%s" % re.sub("[. ]", "-", self.name).lower()
409 self.log = logging.getLogger(name)
410 self.log.logger.addHandler(self.lg_log_handler)
411
412 def get_lg_map(self):
413 return {"logs": (SUBTREE, self.get_logs)}
414
415 def get_logs(self, path_prefix):
416 return [{'level': record.levelname,
417 'time': self.lg_log_handler.formatter.formatTime(record),
418 'message': record.msg}
419 for record in self.lg_log_handler.get_records()]
420
421 def _reset_local_lg_logs(self):
422 self.lg_log_handler.reset_local_lg_logs()