4 # ==================================================================================
5 # Copyright (c) 2020 Nokia
6 # Copyright (c) 2020 AT&T Intellectual Property.
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ==================================================================================
23 from ricsdl.syncstorage import SyncStorage
28 This is a wrapper around the SDL Python interface.
30 We do not embed the below directly in the Xapp classes because this SDL wrapper is useful for other python apps, for example A1 Mediator uses this verbatim.
31 Therefore, we leave this here as a seperate instantiable object so it can be used outside of xapps too
32 One could argue this get moved into *sdl itself*.
34 We currently use msgpack for binary (de)serialization: https://msgpack.org/index.html
37 def __init__(self, use_fake_sdl=False):
44 if this is True (default: False), then SDLs "fake dict backend" is used, which is very useful for testing since it allows you to use SDL without any SDL or Redis deployed at all.
45 This can be used while developing your xapp, and also for monkeypatching during unit testing (e.g., the xapp framework unit tests do this)
48 self._sdl = SyncStorage(fake_db_backend="dict")
50 self._sdl = SyncStorage()
52 def set(self, ns, key, value, usemsgpack=True):
56 NOTE: I am down for a discussion about whether usemsgpack should *default* to True or False here. This seems like a usage statistic question (that we don't have enough data for yet). Are more uses for an xapp to write/read their own data, or will more xapps end up reading data written by some other thing? I think it's too early to know this. So we go with True as the very first user of this, a1, does this. I'm open to changing this default to False later with evidence.
65 if usemsgpack is True, value can be anything serializable by msgpack
66 if usemsgpack is False, value must be bytes
67 usemsgpack: boolean (optional)
68 determines whether the value is serialized using msgpack
71 self._sdl.set(ns, {key: msgpack.packb(value, use_bin_type=True)})
73 self._sdl.set(ns, {key: value})
75 def get(self, ns, key, usemsgpack=True):
85 usemsgpack: boolean (optional)
86 if usemsgpack is True, the value is deserialized using msgpack
87 if usemsgpack is False, the value is returned as raw bytes
91 None (if not exist) or see above; depends on usemsgpack
93 ret_dict = self._sdl.get(ns, {key})
96 return msgpack.unpackb(ret_dict[key], raw=False)
101 def find_and_get(self, ns, prefix, usemsgpack=True):
103 get all k v pairs that start with prefix
113 usemsgpack: boolean (optional)
114 if usemsgpack is True, the value returned is a dict where each value has been deserialized using msgpack
115 if usemsgpack is False, the value returned is as a dict mapping keys to raw bytes
119 {} (if no keys match) or see above; depends on usemsgpack
122 # note: SDL "*" usage is inconsistent with real python regex, where it would be ".*"
123 ret_dict = self._sdl.find_and_get(ns, "{0}*".format(prefix))
125 return {k: msgpack.unpackb(v, raw=False) for k, v in ret_dict.items()}
128 def delete(self, ns, key):
139 self._sdl.remove(ns, {key})
141 def healthcheck(self):
143 checks if the sdl connection is healthy
149 return self._sdl.is_active()