A
0be02bd1f4
- Fix FileNotFoundError when database.path has no directory component (os.makedirs(os.path.dirname(path)) with path="subscribers.db" raised). Add ensure_parent_dir helper; use it in app.py and scripts/init_db.py. - Open5GS/free5gc adapters now use profile.apn for the core session/DNN name instead of a hardcoded "internet"; apn is threaded through get_adapter. Tests: +4 (ensure_parent_dir, adapter APN); 82 pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import logging
|
|
|
|
from pymongo import MongoClient
|
|
|
|
from .base import CoreAdapter
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Open5GSAdapter(CoreAdapter):
|
|
def __init__(self, mongodb_uri: str, database_name: str = "open5gs",
|
|
apn: str = "internet"):
|
|
# Short timeouts so a down/unreachable core fails the provision fast
|
|
# instead of blocking the request for pymongo's 30s default.
|
|
self.client = MongoClient(
|
|
mongodb_uri,
|
|
serverSelectionTimeoutMS=3000,
|
|
connectTimeoutMS=3000,
|
|
socketTimeoutMS=3000,
|
|
)
|
|
self.db = self.client[database_name]
|
|
self.apn = apn
|
|
|
|
def add_subscriber(self, imsi: str, ki: str, opc: str) -> None:
|
|
"""Upsert a subscriber into the Open5GS MongoDB collection."""
|
|
doc = {
|
|
"imsi": imsi,
|
|
"security": {
|
|
"k": ki,
|
|
"opc": opc,
|
|
"amf": "8000",
|
|
"sqn": 0,
|
|
},
|
|
"ambr": {
|
|
"downlink": {"value": 1, "unit": 3},
|
|
"uplink": {"value": 1, "unit": 3},
|
|
},
|
|
"slice": [
|
|
{
|
|
"sst": 1,
|
|
"default_indicator": True,
|
|
"session": [
|
|
{
|
|
"name": self.apn,
|
|
"type": 3,
|
|
"ambr": {
|
|
"downlink": {"value": 1, "unit": 3},
|
|
"uplink": {"value": 1, "unit": 3},
|
|
},
|
|
}
|
|
],
|
|
}
|
|
],
|
|
}
|
|
self.db.subscribers.replace_one({"imsi": imsi}, doc, upsert=True)
|
|
logger.info(f"Added subscriber {imsi} to Open5GS")
|
|
|
|
def remove_subscriber(self, imsi: str) -> None:
|
|
self.db.subscribers.delete_one({"imsi": imsi})
|
|
logger.info(f"Removed subscriber {imsi} from Open5GS")
|
|
|
|
def list_subscribers(self) -> list:
|
|
return list(self.db.subscribers.find({}, {"_id": 0}))
|