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