1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2019 Nokia.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 # platform project (RICP).
22 """The module provides Shared Data Layer (SDL) database backend interface."""
24 from typing import (Dict, Set, List, Union)
25 from abc import ABC, abstractmethod
28 class DbBackendAbc(ABC):
29 """An abstract Shared Data Layer (SDL) class providing database backend interface."""
32 def is_connected(self):
33 """Test database backend connection."""
38 """Close database backend connection."""
42 def set(self, ns: str, data_map: Dict[str, bytes]) -> None:
43 """Write key value data mapping to database under a namespace."""
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."""
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."""
57 def get(self, ns: str, keys: List[str]) -> Dict[str, bytes]:
58 """"Return values of the keys under a namespace."""
62 def find_keys(self, ns: str, key_pattern: str) -> List[str]:
63 """"Return all the keys matching search pattern under a namespace in database."""
67 def find_and_get(self, ns: str, key_pattern: str) -> Dict[str, bytes]:
69 Return all the keys with their values matching search pattern under a namespace in
75 def remove(self, ns: str, keys: List[str]) -> None:
76 """Remove keys and their data from database."""
80 def remove_if(self, ns: str, key: str, data: bytes) -> bool:
82 Remove key and its data from database if if the current data value is expected
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."""
93 def remove_member(self, ns: str, group: str, members: Set[bytes]) -> None:
94 """Remove members from a group under a namespace in database."""
98 def remove_group(self, ns: str, group: str) -> None:
99 """Remove a group under a namespace in database along with it's members."""
103 def get_members(self, ns: str, group: str) -> Set[bytes]:
104 """Get all the members of a group under a namespace in database."""
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."""
113 def group_size(self, ns: str, group: str) -> int:
114 """Return the number of members in a group under a namespace in database."""
118 class DbBackendLockAbc(ABC):
120 An abstract Shared Data Layer (SDL) class providing database backend lock interface.
122 ns (str): Namespace under which this lock is targeted.
123 name (str): Lock name, identifies the lock key in a database backend.
125 def __init__(self, ns: str, name: str) -> None:
127 self._lock_name = name
131 def acquire(self, retry_interval: Union[int, float] = 0.1,
132 retry_timeout: Union[int, float] = 10) -> bool:
133 """Acquire a database lock."""
137 def release(self) -> None:
138 """Release a database lock."""
142 def refresh(self) -> None:
143 """Refresh the remaining validity time of the database lock back to a initial value."""
147 def get_validity_time(self) -> Union[int, float]:
148 """Return remaining validity time of the lock in seconds."""