open5gs/misc/mongodb/python/Open5GS.py
Nick 85dbeb05cc
Added editing subscribers with PythonAPI Interface
Using the UpdateSubscriber call. Input variables are the IMSI and the subscriber data, which is the same as the outputted data of GetSubscriber.
2020-03-15 17:28:17 +11:00

64 lines
2.1 KiB
Python

import mongo
import pymongo
import random
import bson
class Open5GS:
def __init__(self, server, port):
self.server = server
self.port = port
def GetSubscribers(self):
myclient = pymongo.MongoClient("mongodb://" + str(self.server) + ":" + str(self.port) + "/")
mydb = myclient["open5gs"]
mycol = mydb["subscribers"]
subs_list = []
for x in mycol.find():
print(x)
subs_list.append(x)
pass
return subs_list
def GetSubscriber(self, imsi):
myclient = pymongo.MongoClient("mongodb://" + str(self.server) + ":" + str(self.port) + "/")
mydb = myclient["open5gs"]
mycol = mydb["subscribers"]
myquery = { "imsi": str(imsi)}
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
return x
def AddSubscriber(self, sub_data):
myclient = pymongo.MongoClient("mongodb://" + str(self.server) + ":" + str(self.port) + "/")
mydb = myclient["open5gs"]
mycol = mydb["subscribers"]
x = mycol.insert_one(sub_data)
print("Added subscriber with Inserted ID : " + str(x.inserted_id))
return x.inserted_id
def UpdateSubscriber(self, imsi, sub_data):
myclient = pymongo.MongoClient("mongodb://" + str(self.server) + ":" + str(self.port) + "/")
mydb = myclient["open5gs"]
mycol = mydb["subscribers"]
print("Attempting to update IMSI " + str(imsi))
newvalues = { "$set": sub_data }
myquery = { "imsi": str(imsi)}
x = mycol.update_one(myquery, newvalues)
print(x)
return True
def DeleteSubscriber(self, imsi):
myclient = pymongo.MongoClient("mongodb://" + str(self.server) + ":" + str(self.port) + "/")
mydb = myclient["open5gs"]
mycol = mydb["subscribers"]
myquery = { "imsi": str(imsi)}
x = mycol.delete_many(myquery)
print(x.deleted_count, " subscribers deleted.")
return x.deleted_count