blob: 08f46003221b93f890537a5bfc435f425b363235 [file] [log] [blame]
Timo Tietavainendada8462019-11-27 11:50:01 +02001# 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
24from typing import (Dict, Set, List, Union)
25from abc import ABC, abstractmethod
26
27
28class DbBackendAbc(ABC):
29 """An abstract Shared Data Layer (SDL) class providing database backend interface."""
30
31 @abstractmethod
32 def close(self):
33 """Close database backend connection."""
34 pass
35
36 @abstractmethod
37 def set(self, ns: str, data_map: Dict[str, bytes]) -> None:
38 """Write key value data mapping to database under a namespace."""
39 pass
40
41 @abstractmethod
42 def set_if(self, ns: str, key: str, old_data: bytes, new_data: bytes) -> bool:
43 """"Write key value to database under a namespace if the old value is expected one."""
44 pass
45
46 @abstractmethod
47 def set_if_not_exists(self, ns: str, key: str, data: bytes) -> bool:
48 """"Write key value to database under a namespace if key doesn't exist."""
49 pass
50
51 @abstractmethod
52 def get(self, ns: str, keys: List[str]) -> Dict[str, bytes]:
53 """"Return values of the keys under a namespace."""
54 pass
55
56 @abstractmethod
Timo Tietavainen276ed3c2019-12-15 20:16:23 +020057 def find_keys(self, ns: str, key_pattern: str) -> List[str]:
Timo Tietavainendada8462019-11-27 11:50:01 +020058 """"Return all the keys matching search pattern under a namespace in database."""
59 pass
60
61 @abstractmethod
Timo Tietavainen276ed3c2019-12-15 20:16:23 +020062 def find_and_get(self, ns: str, key_pattern: str) -> Dict[str, bytes]:
Timo Tietavainendada8462019-11-27 11:50:01 +020063 """
64 Return all the keys with their values matching search pattern under a namespace in
65 database.
66 """
67 pass
68
69 @abstractmethod
70 def remove(self, ns: str, keys: List[str]) -> None:
71 """Remove keys and their data from database."""
72 pass
73
74 @abstractmethod
75 def remove_if(self, ns: str, key: str, data: bytes) -> bool:
76 """
77 Remove key and its data from database if if the current data value is expected
78 one.
79 """
80 pass
81
82 @abstractmethod
83 def add_member(self, ns: str, group: str, members: Set[bytes]) -> None:
84 """Add new members to a group under a namespace in database."""
85 pass
86
87 @abstractmethod
88 def remove_member(self, ns: str, group: str, members: Set[bytes]) -> None:
89 """Remove members from a group under a namespace in database."""
90 pass
91
92 @abstractmethod
93 def remove_group(self, ns: str, group: str) -> None:
94 """Remove a group under a namespace in database along with it's members."""
95 pass
96
97 @abstractmethod
98 def get_members(self, ns: str, group: str) -> Set[bytes]:
99 """Get all the members of a group under a namespace in database."""
100 pass
101
102 @abstractmethod
103 def is_member(self, ns: str, group: str, member: bytes) -> bool:
104 """Validate if a given member is in the group under a namespace in database."""
105 pass
106
107 @abstractmethod
108 def group_size(self, ns: str, group: str) -> int:
109 """Return the number of members in a group under a namespace in database."""
110 pass
111
112
113class DbBackendLockAbc(ABC):
114 """
115 An abstract Shared Data Layer (SDL) class providing database backend lock interface.
116 Args:
117 ns (str): Namespace under which this lock is targeted.
118 name (str): Lock name, identifies the lock key in a database backend.
119 """
120 def __init__(self, ns: str, name: str) -> None:
121 self._ns = ns
122 self._lock_name = name
123 super().__init__()
124
125 @abstractmethod
126 def acquire(self, retry_interval: Union[int, float] = 0.1,
127 retry_timeout: Union[int, float] = 10) -> bool:
128 """Acquire a database lock."""
129 pass
130
131 @abstractmethod
132 def release(self) -> None:
133 """Release a database lock."""
134 pass
135
136 @abstractmethod
137 def refresh(self) -> None:
138 """Refresh the remaining validity time of the database lock back to a initial value."""
139 pass
140
141 @abstractmethod
142 def get_validity_time(self) -> Union[int, float]:
143 """Return remaining validity time of the lock in seconds."""
144 pass