953b76fb71e0462e1d50c83ba9af90a11da11efe
[ric-plt/xapp-frame-py.git] / ricxappframe / rmr / rmrclib / rmrclib.py
1 # ==================================================================================
2 #       Copyright (c) 2019-2020 Nokia
3 #       Copyright (c) 2018-2020 AT&T Intellectual Property.
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 implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # ==================================================================================
17 import ctypes
18 import json
19 from contextlib import contextmanager
20
21 # Subpackage that creates and publishes a reference to the C shared library.
22 # Intended to be private; RMR-python and xapp-frame-py users should not need this.
23 # Sphinx and autodoc mock this module to run without the .so file present.
24
25 # https://docs.python.org/3.7/library/ctypes.html
26 # https://stackoverflow.com/questions/2327344/ctypes-loading-a-c-shared-library-that-has-dependencies/30845750#30845750
27 # make sure you do a set -x LD_LIBRARY_PATH /usr/local/lib/;
28 rmr_c_lib = ctypes.CDLL("librmr_si.so", mode=ctypes.RTLD_GLOBAL)
29 _rmr_get_consts = rmr_c_lib.rmr_get_consts
30 _rmr_get_consts.argtypes = []
31 _rmr_get_consts.restype = ctypes.POINTER(ctypes.c_char)
32
33 _rmr_free_consts = rmr_c_lib.rmr_free_consts
34 _rmr_free_consts.argtypes = [ctypes.POINTER(ctypes.c_char)]
35 _rmr_free_consts.restype = None
36
37
38 @contextmanager
39 def _rmr_get_consts_decorator():
40     ptr = _rmr_get_consts()
41     try:
42         yield ptr
43     finally:
44         _rmr_free_consts(ptr)
45
46
47 def get_constants(cache={}):
48     """
49     Gets constants published by RMR and caches for subsequent calls.
50     TODO: are there constants that end user applications need?
51     """
52     if cache:
53         return cache
54
55     # read pointer to json data
56     with _rmr_get_consts_decorator() as ptr:
57         js = ctypes.cast(ptr, ctypes.c_char_p).value  # cast it to python string
58         cache = json.loads(str(js.decode()))  # create constants value object as a hash
59
60     return cache
61
62
63 def get_mapping_dict(cache={}):
64     """
65     Builds a state mapping dict from constants and caches for subsequent calls.
66     Relevant constants at this writing include:
67
68     RMR_OK              0   state is good
69     RMR_ERR_BADARG      1   argument passd to function was unusable
70     RMR_ERR_NOENDPT     2   send/call could not find an endpoint based on msg type
71     RMR_ERR_EMPTY       3   msg received had no payload; attempt to send an empty message
72     RMR_ERR_NOHDR       4   message didn't contain a valid header
73     RMR_ERR_SENDFAILED  5   send failed; errno has nano reason
74     RMR_ERR_CALLFAILED  6   unable to send call() message
75     RMR_ERR_NOWHOPEN    7   no wormholes are open
76     RMR_ERR_WHID        8   wormhole id was invalid
77     RMR_ERR_OVERFLOW    9   operation would have busted through a buffer/field size
78     RMR_ERR_RETRY       10  request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)
79     RMR_ERR_RCVFAILED   11  receive failed (hard error)
80     RMR_ERR_TIMEOUT     12  message processing call timed out
81     RMR_ERR_UNSET       13  the message hasn't been populated with a transport buffer
82     RMR_ERR_TRUNC       14  received message likely truncated
83     RMR_ERR_INITFAILED  15  initialization of something (probably message) failed
84
85     """
86     if cache:
87         return cache
88
89     rmr_consts = get_constants()
90     for key in rmr_consts:  # build the state mapping dict
91         if key[:7] in ["RMR_ERR", "RMR_OK"]:
92             en = int(rmr_consts[key])
93             cache[en] = key
94
95     return cache
96
97
98 def state_to_status(stateno):
99     """
100     Converts a msg state integer to a status string.
101
102     Returns "UNKNOWN STATE" if the int value is not known.
103     """
104     sdict = get_mapping_dict()
105     return sdict.get(stateno, "UNKNOWN STATE")