b452b93322bf8b3a832e53d89f5978a5f16448a9
[ric-plt/e2mgr.git] / Automation / Tests / Scripts / e2mdbscripts.py
1 ##############################################################################
2 #
3 #   Copyright (c) 2019 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 ##############################################################################
18
19 import config
20 import redis
21 import cleanup_db
22 import json
23
24 def getRedisClientDecodeResponse():
25     c = config.redis_ip_address
26     p = config.redis_ip_port
27     return redis.Redis(host=c, port=p, db=0, decode_responses=True)
28
29 def verify_ran_is_associated_with_e2t_instance(ranName, e2tAddress):
30     r = getRedisClientDecodeResponse()
31     e2tInstanceJson = r.get("{e2Manager},E2TInstance:"+e2tAddress)
32     e2tInstanceDic = json.loads(e2tInstanceJson)
33     assocRanList = e2tInstanceDic.get("associatedRanList")
34     return ranName in assocRanList
35
36 def verify_e2t_instance_has_no_associated_rans(e2tAddress):
37     r = getRedisClientDecodeResponse()
38     e2tInstanceJson = r.get("{e2Manager},E2TInstance:"+e2tAddress)
39     e2tInstanceDic = json.loads(e2tInstanceJson)
40     assocRanList = e2tInstanceDic.get("associatedRanList")
41     return not assocRanList
42
43 def verify_e2t_instance_exists_in_addresses(e2tAddress):
44     r = getRedisClientDecodeResponse()
45     e2tAddressesJson = r.get("{e2Manager},E2TAddresses")
46     e2tAddresses = json.loads(e2tAddressesJson)
47     return e2tAddress in e2tAddresses
48
49 def verify_e2t_instance_key_exists(e2tAddress):
50     r = getRedisClientDecodeResponse()
51     return r.exists("{e2Manager},E2TInstance:"+e2tAddress)
52
53 def populate_e2t_instances_in_e2m_db_for_get_e2t_instances_tc():
54     r = getRedisClientDecodeResponse()
55     r.set("{e2Manager},E2TAddresses", "[\"e2t.att.com:38000\",\"e2t.att.com:38001\"]")
56     r.set("{e2Manager},E2TInstance:e2t.att.com:38000", "{\"address\":\"e2t.att.com:38000\",\"associatedRanList\":[\"test1\",\"test2\",\"test3\"],\"keepAliveTimestamp\":1577619310484022369,\"state\":\"ACTIVE\"}")
57     r.set("{e2Manager},E2TInstance:e2t.att.com:38001", "{\"address\":\"e2t.att.com:38001\",\"associatedRanList\":[],\"keepAliveTimestamp\":1577619310484022369,\"state\":\"ACTIVE\"}")
58     return True
59
60 # def dissociate_ran_from_e2tInstance(ranName, e2tAddress):
61 #     r = getRedisClientDecodeResponse()
62 #     e2tInstanceJson = r.get("{e2Manager},E2TInstance:"+e2tAddress)
63 #     e2tInstanceDic = json.loads(e2tInstanceJson)
64 #     assocRanList = e2tInstanceDic.get("associatedRanList")
65 #     print(assocRanList)
66 #     assocRanList.remove(ranName)
67 #     updatedE2tInstanceJson = json.dumps(e2tInstanceDic)
68 #     print(updatedE2tInstanceJson)
69 #     r.set("{e2Manager},E2TInstance:"+e2tAddress, updatedE2tInstanceJson)
70 #     nodebBytes = r.get("{e2Manager},RAN:"+ranName)
71 #     encoded = nodebBytes.decode().replace(e2tAddress,"").encode()
72 #     r.set("{e2Manager},RAN:"+ranName, encoded)
73