beef up the AI/ML framework tests by adding InfluxDB as data source and populate...
[it/test.git] / ric_robot_suite / ric-python-utils / ricutils / E2SimUtils.py
1 #   Copyright (c) 2019 AT&T Intellectual Property.
2 #   Copyright (c) 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 from redis import Redis
17 import string
18 import random
19
20 class E2SimUtils(object):
21  def __init__(self, dbaas, port=int(6379)):
22
23   self._db = Redis(host=dbaas, port=port)
24
25  def sdlKey(self, namespace, val):
26   return "{%s},%s" % (namespace, val)
27
28  def sdlKeyNamespace(self, k):
29    return k.split(",")[0][1:-1]
30
31  def sdlKeyVal(self, k):
32    return k.split(",")[1]
33
34  def e2RANKey(self, val):
35   return self.sdlKey("e2Manager", "RAN:" + val)
36
37  def e2RANKeyVal(self, k):
38   return self.sdlKeyVal(k).split(':')[-1]
39
40  def gNodeB(self, ranName="*"):
41   # return one or (by default) all e2 records
42   gnbs={}
43   for k in self._db.keys(self.e2RANKey(ranName)):
44    gnbs[self.e2RANKeyVal(k.decode('utf-8'))]=self._db.get(k)
45   return gnbs
46
47  def gNodeBDelete(self, ranName="*"):
48   # delete one or (by default) all e2 records
49   # returns the deleted records to avoid
50   # buyer's remorse.
51   gnbs = self.gNodeB(ranName)
52   for k in gnbs:
53    self._db.delete(self.e2RANKey(k))
54   return gnbs
55
56  def randomRANName(self, prefix=""):
57   prefix = prefix + ''.join(random.choice(string.ascii_uppercase) for _ in range(4-(min(4, len(prefix)))))
58   return prefix[0:4].upper() + ''.join(random.choice(string.digits) for _ in range(6))
59
60  def TranslategNodeBID(self, prefix, plmn, bits):
61   # given a gNodeB type prefix, a plmn (as a string, no spaces), and a bitstring
62   # return a gNodeB ID
63   plmn = ''.join(filter(lambda c: c in list(string.hexdigits), plmn))
64   bits = ''.join(filter(lambda c: c in ['0', '1'], bits))
65   mnc3 = (int(plmn[2:4],16) & 0xf0) >> 4
66   if mnc3 == 15:
67     return '%s:%d%d%d-0%d%d-%x' % \
68      (prefix,\
69       int(plmn[0:2],16) & 0xf, (int(plmn[0:2],16) & 0xf0) >> 4, int(plmn[2:4],16) & 0xf,\
70       int(plmn[4:6],16) & 0x0f, (int(plmn[4:6],16) & 0xf0) >> 4, \
71       int((bits + "0" * (len(bits)%4)),2))
72   else:
73     return '%s:%d%d%d-%d%d%d-%x' % \
74      (prefix,\
75       int(plmn[0:2],16) & 0xf, (int(plmn[0:2],16) & 0xf0) >> 4, int(plmn[2:4],16) & 0xf,\
76       int(plmn[4:6],16) & 0x0f, (int(plmn[4:6],16) & 0xf0) >> 4, mnc3,\
77       int((bits + "0" * (len(bits)%4)),2))