Timo Tietavainen | dada846 | 2019-11-27 11:50:01 +0200 | [diff] [blame] | 1 | # Copyright (c) 2019 AT&T Intellectual Property. |
| 2 | # Copyright (c) 2018-2019 Nokia. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # |
| 17 | # This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | # platform project (RICP). |
| 19 | # |
| 20 | |
| 21 | |
| 22 | """The module provides Shared Data Layer (SDL) database backend interface.""" |
| 23 | |
| 24 | from typing import (Dict, Set, List, Union) |
| 25 | from abc import ABC, abstractmethod |
| 26 | |
| 27 | |
| 28 | class DbBackendAbc(ABC): |
| 29 | """An abstract Shared Data Layer (SDL) class providing database backend interface.""" |
| 30 | |
| 31 | @abstractmethod |
Timo Tietavainen | c979c0d | 2020-01-21 21:57:17 +0200 | [diff] [blame] | 32 | def is_connected(self): |
| 33 | """Test database backend connection.""" |
| 34 | pass |
| 35 | |
| 36 | @abstractmethod |
Timo Tietavainen | dada846 | 2019-11-27 11:50:01 +0200 | [diff] [blame] | 37 | def close(self): |
| 38 | """Close database backend connection.""" |
| 39 | pass |
| 40 | |
| 41 | @abstractmethod |
| 42 | def set(self, ns: str, data_map: Dict[str, bytes]) -> None: |
| 43 | """Write key value data mapping to database under a namespace.""" |
| 44 | pass |
| 45 | |
| 46 | @abstractmethod |
| 47 | def set_if(self, ns: str, key: str, old_data: bytes, new_data: bytes) -> bool: |
| 48 | """"Write key value to database under a namespace if the old value is expected one.""" |
| 49 | pass |
| 50 | |
| 51 | @abstractmethod |
| 52 | def set_if_not_exists(self, ns: str, key: str, data: bytes) -> bool: |
| 53 | """"Write key value to database under a namespace if key doesn't exist.""" |
| 54 | pass |
| 55 | |
| 56 | @abstractmethod |
| 57 | def get(self, ns: str, keys: List[str]) -> Dict[str, bytes]: |
| 58 | """"Return values of the keys under a namespace.""" |
| 59 | pass |
| 60 | |
| 61 | @abstractmethod |
Timo Tietavainen | 276ed3c | 2019-12-15 20:16:23 +0200 | [diff] [blame] | 62 | def find_keys(self, ns: str, key_pattern: str) -> List[str]: |
Timo Tietavainen | dada846 | 2019-11-27 11:50:01 +0200 | [diff] [blame] | 63 | """"Return all the keys matching search pattern under a namespace in database.""" |
| 64 | pass |
| 65 | |
| 66 | @abstractmethod |
Timo Tietavainen | 276ed3c | 2019-12-15 20:16:23 +0200 | [diff] [blame] | 67 | def find_and_get(self, ns: str, key_pattern: str) -> Dict[str, bytes]: |
Timo Tietavainen | dada846 | 2019-11-27 11:50:01 +0200 | [diff] [blame] | 68 | """ |
| 69 | Return all the keys with their values matching search pattern under a namespace in |
| 70 | database. |
| 71 | """ |
| 72 | pass |
| 73 | |
| 74 | @abstractmethod |
| 75 | def remove(self, ns: str, keys: List[str]) -> None: |
| 76 | """Remove keys and their data from database.""" |
| 77 | pass |
| 78 | |
| 79 | @abstractmethod |
| 80 | def remove_if(self, ns: str, key: str, data: bytes) -> bool: |
| 81 | """ |
| 82 | Remove key and its data from database if if the current data value is expected |
| 83 | one. |
| 84 | """ |
| 85 | pass |
| 86 | |
| 87 | @abstractmethod |
| 88 | def add_member(self, ns: str, group: str, members: Set[bytes]) -> None: |
| 89 | """Add new members to a group under a namespace in database.""" |
| 90 | pass |
| 91 | |
| 92 | @abstractmethod |
| 93 | def remove_member(self, ns: str, group: str, members: Set[bytes]) -> None: |
| 94 | """Remove members from a group under a namespace in database.""" |
| 95 | pass |
| 96 | |
| 97 | @abstractmethod |
| 98 | def remove_group(self, ns: str, group: str) -> None: |
| 99 | """Remove a group under a namespace in database along with it's members.""" |
| 100 | pass |
| 101 | |
| 102 | @abstractmethod |
| 103 | def get_members(self, ns: str, group: str) -> Set[bytes]: |
| 104 | """Get all the members of a group under a namespace in database.""" |
| 105 | pass |
| 106 | |
| 107 | @abstractmethod |
| 108 | def is_member(self, ns: str, group: str, member: bytes) -> bool: |
| 109 | """Validate if a given member is in the group under a namespace in database.""" |
| 110 | pass |
| 111 | |
| 112 | @abstractmethod |
| 113 | def group_size(self, ns: str, group: str) -> int: |
| 114 | """Return the number of members in a group under a namespace in database.""" |
| 115 | pass |
| 116 | |
| 117 | |
| 118 | class DbBackendLockAbc(ABC): |
| 119 | """ |
| 120 | An abstract Shared Data Layer (SDL) class providing database backend lock interface. |
| 121 | Args: |
| 122 | ns (str): Namespace under which this lock is targeted. |
| 123 | name (str): Lock name, identifies the lock key in a database backend. |
| 124 | """ |
| 125 | def __init__(self, ns: str, name: str) -> None: |
| 126 | self._ns = ns |
| 127 | self._lock_name = name |
| 128 | super().__init__() |
| 129 | |
| 130 | @abstractmethod |
| 131 | def acquire(self, retry_interval: Union[int, float] = 0.1, |
| 132 | retry_timeout: Union[int, float] = 10) -> bool: |
| 133 | """Acquire a database lock.""" |
| 134 | pass |
| 135 | |
| 136 | @abstractmethod |
| 137 | def release(self) -> None: |
| 138 | """Release a database lock.""" |
| 139 | pass |
| 140 | |
| 141 | @abstractmethod |
| 142 | def refresh(self) -> None: |
| 143 | """Refresh the remaining validity time of the database lock back to a initial value.""" |
| 144 | pass |
| 145 | |
| 146 | @abstractmethod |
| 147 | def get_validity_time(self) -> Union[int, float]: |
| 148 | """Return remaining validity time of the lock in seconds.""" |
| 149 | pass |