blob: 7c27f644395a12a849bc293d10a8121987ce0101 [file] [log] [blame]
tierno87858ca2018-10-08 16:30:15 +02001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Telefonica S.A.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
tierno5c012612018-04-19 16:01:59 +020017
tierno1e9a3292018-11-05 18:18:45 +010018import logging
tierno5c012612018-04-19 16:01:59 +020019from http import HTTPStatus
tierno1e9a3292018-11-05 18:18:45 +010020from osm_common.common_utils import FakeLock
21from threading import Lock
tierno5c012612018-04-19 16:01:59 +020022
23__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
24
25
26class MsgException(Exception):
27 """
28 Base Exception class for all msgXXXX exceptions
29 """
30
tiernoa4eaefe2018-06-20 17:37:17 +020031 def __init__(self, message, http_code=HTTPStatus.SERVICE_UNAVAILABLE):
tierno5c012612018-04-19 16:01:59 +020032 """
33 General exception
34 :param message: descriptive text
35 :param http_code: <http.HTTPStatus> type. It contains ".value" (http error code) and ".name" (http error name
36 """
37 self.http_code = http_code
38 Exception.__init__(self, "messaging exception " + message)
39
40
41class MsgBase(object):
42 """
43 Base class for all msgXXXX classes
44 """
45
tierno1e9a3292018-11-05 18:18:45 +010046 def __init__(self, logger_name='msg', lock=False):
47 """
48 Constructor of FsBase
49 :param logger_name: logging name
50 :param lock: Used to protect simultaneous access to the same instance class by several threads:
51 False, None: Do not protect, this object will only be accessed by one thread
52 True: This object needs to be protected by several threads accessing.
53 Lock object. Use thi Lock for the threads access protection
54 """
55 self.logger = logging.getLogger(logger_name)
56 if not lock:
57 self.lock = FakeLock()
58 elif lock is True:
59 self.lock = Lock()
60 elif isinstance(lock, Lock):
61 self.lock = lock
62 else:
63 raise ValueError("lock parameter must be a Lock class or boolean")
tierno5c012612018-04-19 16:01:59 +020064
65 def connect(self, config):
66 pass
67
68 def disconnect(self):
69 pass
70
71 def write(self, topic, key, msg):
tierno136f2952018-10-19 13:01:03 +020072 raise MsgException("Method 'write' not implemented", http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
tierno5c012612018-04-19 16:01:59 +020073
74 def read(self, topic):
tierno136f2952018-10-19 13:01:03 +020075 raise MsgException("Method 'read' not implemented", http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
tierno5c012612018-04-19 16:01:59 +020076
Benjamin Diaz48b78e12018-10-18 17:55:12 -030077 async def aiowrite(self, topic, key, msg, loop=None):
tierno136f2952018-10-19 13:01:03 +020078 raise MsgException("Method 'aiowrite' not implemented", http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
tierno5c012612018-04-19 16:01:59 +020079
tierno10602af2019-02-18 14:53:54 +000080 async def aioread(self, topic, loop=None, callback=None, aiocallback=None, group_id=None, **kwargs):
tierno136f2952018-10-19 13:01:03 +020081 raise MsgException("Method 'aioread' not implemented", http_code=HTTPStatus.INTERNAL_SERVER_ERROR)