Skip to content

Commit aa14cad

Browse files
committed
feat(zettabgp): add docstrings
1 parent e2c521e commit aa14cad

File tree

16 files changed

+552
-22
lines changed

16 files changed

+552
-22
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Benedikt Schwering
3+
Copyright (c) 2024 Benedikt Schwering and Sebastian Forstner
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/adapters/mongodb.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
ZettaBGP - Advanced Anomaly Detection in Internet Routing
4+
Copyright (c) 2024 Benedikt Schwering and Sebastian Forstner
5+
6+
This work is licensed under the terms of the MIT license.
7+
For a copy, see LICENSE in the project root.
8+
9+
Author:
10+
Benedikt Schwering <bes9584@thi.de>
11+
Sebastian Forstner <sef9869@thi.de>
12+
'''
113
from src.parsers.route_update import RouteUpdateParser
214
from src.models.route_update import RouteUpdate
315
from src.models.route_update import ChangeType
@@ -7,10 +19,28 @@
719
import os
820

921
class MongoDBAdapter:
10-
'''This class is responsible for receiving the parsed messages and forwarding them to both MongoDB databases'''
22+
'''
23+
This class is responsible for receiving the parsed messages and forwarding them to both MongoDB databases.
24+
25+
Author:
26+
Sebastian Forstner <sef9869@thi.de>
27+
'''
1128
def __init__(self, parser: RouteUpdateParser, no_mongodb_log: bool, no_mongodb_state: bool, no_mongodb_statistics: bool, clear_mongodb: bool):
29+
'''
30+
Initializes the MongoDBAdapter.
31+
32+
Author:
33+
Sebastian Forstner <sef9869@thi.de>
34+
35+
Args:
36+
parser (RouteUpdateParser): The parser to receive the parsed messages from.
37+
no_mongodb_log (bool): Whether to disable the log storage.
38+
no_mongodb_state (bool): Whether to disable the state storage.
39+
no_mongodb_statistics (bool): Whether to disable the statistics storage.
40+
clear_mongodb (bool): Whether to clear the MongoDB databases.
41+
'''
1242
try:
13-
'''Connects to MongoDB-Container running with Docker'''
43+
# Connects to MongoDB-Container running with Docker
1444
database_client = MongoClient(
1545
host=os.getenv('MONGO_DB_HOST', 'localhost'),
1646
port=int(os.getenv('MONGO_DB_PORT', 27017)),
@@ -21,21 +51,22 @@ def __init__(self, parser: RouteUpdateParser, no_mongodb_log: bool, no_mongodb_s
2151
log_flag = no_mongodb_log
2252
state_flag = no_mongodb_state
2353
statistics_flag = no_mongodb_statistics
24-
'''Creates database and collection for log storrage'''
54+
55+
# Creates database and collection for log storage
2556
if not log_flag:
2657
log_db = database_client.message_log
2758
log_collection = log_db.storage
2859
if clear_mongodb:
2960
log_collection.delete_many({})
3061

31-
'''Creates database and collection for state storrage'''
62+
# Creates database and collection for state storage
3263
if not state_flag:
3364
state_db = database_client.message_state
3465
state_collection = state_db.storage
3566
if clear_mongodb:
3667
state_collection.delete_many({})
3768

38-
'''Creates database and collection for statistics storrage'''
69+
# Creates database and collection for statistics storage
3970
if not statistics_flag:
4071
statistics_db = database_client.message_statistics
4172
statistics_collection = statistics_db.storage
@@ -45,7 +76,7 @@ def __init__(self, parser: RouteUpdateParser, no_mongodb_log: bool, no_mongodb_s
4576
@parser.on_update
4677
def on_update(message: RouteUpdate):
4778

48-
'''saves optional, non-base-type attributes for later use; required to guarantee save use of mongodb'''
79+
# Saves optional, non-base-type attributes for later use; required to guarantee save use of mongodb
4980
if message.path_attributes.origin:
5081
origins = message.path_attributes.origin.value
5182
else:
@@ -75,7 +106,7 @@ def on_update(message: RouteUpdate):
75106
else:
76107
extended_community.append(str(ext_com))
77108

78-
'''creates dict for message with _id and other unique attributes, that dont change'''
109+
# Creates dict for message with _id and other unique attributes, that don't change
79110
new_message_id = {
80111
'timestamp' : message.timestamp,
81112
'peer_ip' : message.peer_ip,
@@ -103,7 +134,8 @@ def on_update(message: RouteUpdate):
103134
},
104135
'_id' : ObjectId(),
105136
}
106-
'''creates dict used for collection updates, MUST NOT contain _id and should not contain other non changing attributes'''
137+
138+
# Creates dict used for collection updates, MUST NOT contain _id and should not contain other non changing attributes
107139
set_message = {
108140
'$set': {
109141
'timestamp' : message.timestamp,
@@ -127,7 +159,7 @@ def on_update(message: RouteUpdate):
127159
}
128160
}
129161

130-
'''route got withdrawn, db actions accordingly'''
162+
# Route got withdrawn, db actions accordingly
131163
if message.change_type == ChangeType.WITHDRAW:
132164
if not log_flag:
133165
log_announce = log_collection.insert_one(new_message_id)
@@ -159,7 +191,7 @@ def on_update(message: RouteUpdate):
159191
}
160192
statistics_announce = statistics_collection.update_one(statistics_filter, new_values, upsert=True)
161193

162-
'''route got announced, db actions accordingly'''
194+
# Route got announced, db actions accordingly
163195
if message.change_type == ChangeType.ANNOUNCE:
164196
if not log_flag:
165197
log_announce = log_collection.insert_one(new_message_id)

src/adapters/rabbitmq.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
ZettaBGP - Advanced Anomaly Detection in Internet Routing
4+
Copyright (c) 2024 Benedikt Schwering and Sebastian Forstner
5+
6+
This work is licensed under the terms of the MIT license.
7+
For a copy, see LICENSE in the project root.
8+
9+
Author:
10+
Benedikt Schwering <bes9584@thi.de>
11+
Sebastian Forstner <sef9869@thi.de>
12+
'''
113
from src.parsers.route_update import RouteUpdateParser
214
from src.models.route_update import RouteUpdate
315
from datetime import timedelta, datetime
416
import pika, json, os
517

618
class RabbitMQAdapter:
7-
'''This class is responsible for receiving the parsed messages and forwarding them to the RabbitMQ message broker'''
19+
'''
20+
This class is responsible for receiving the parsed messages and forwarding them to the RabbitMQ message broker.
21+
22+
Author:
23+
Benedikt Schwering <bes9584@thi.de>
24+
'''
825
def __init__(self, parser: RouteUpdateParser, no_direct: bool, queue_interval: int):
26+
'''
27+
Initializes the RabbitMQAdapter.
28+
29+
Author:
30+
Benedikt Schwering <bes9584@thi.de>
31+
32+
Args:
33+
parser (RouteUpdateParser): The parser to receive the parsed messages from.
34+
no_direct (bool): Whether to disable the direct route updates.
35+
queue_interval (int): The interval in minutes to group the route updates.
36+
'''
937
connection = pika.BlockingConnection(
1038
pika.ConnectionParameters(
1139
host=os.getenv('RABBIT_MQ_HOST', 'localhost'),
1240
)
1341
)
1442

15-
'''Creates a channel for the connection and declares the zettabgp exchange'''
43+
# Creates a channel for the connection and declares the zettabgp exchange
1644
channel = connection.channel()
1745
channel.exchange_declare(
1846
exchange='zettabgp',
1947
exchange_type='direct',
2048
)
2149

22-
'''Declares the test_bgp_updates queue and binds it to the zettabgp exchange'''
50+
# Declares the test_bgp_updates queue and binds it to the zettabgp exchange
2351
def _declare_test_queue(queue_name: str, routing_key: str):
2452
channel.queue_declare(
2553
queue=queue_name,

src/controllers/mrt_library.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
ZettaBGP - Advanced Anomaly Detection in Internet Routing
4+
Copyright (c) 2024 Benedikt Schwering and Sebastian Forstner
5+
6+
This work is licensed under the terms of the MIT license.
7+
For a copy, see LICENSE in the project root.
8+
9+
Author:
10+
Benedikt Schwering <bes9584@thi.de>
11+
Sebastian Forstner <sef9869@thi.de>
12+
'''
113
from src.models.mrt_library import MRTScenarioRequest, MRTScenarioResult, MRTScenario, MRTLibrary
214
from src.services.mrt_simulation import mrt_simulation
315
from fastapi import APIRouter, HTTPException
@@ -7,6 +19,15 @@
719
mrt_library_router = APIRouter()
820

921
def _get_mrt_library() -> MRTLibrary:
22+
'''
23+
This function returns the MRT library.
24+
25+
Author:
26+
Benedikt Schwering <bes9584@thi.de>
27+
28+
Returns:
29+
MRTLibrary: The MRT library.
30+
'''
1031
mrt_library = MRTLibrary(
1132
scenarios=[],
1233
)
@@ -28,16 +49,49 @@ def _get_mrt_library() -> MRTLibrary:
2849
return mrt_library
2950

3051
def _get_mrt_scenario(id: str) -> MRTScenario:
52+
'''
53+
This function returns an MRT scenario by its ID.
54+
55+
Author:
56+
Benedikt Schwering <bes9584@thi.de>
57+
58+
Args:
59+
id (str): The ID of the MRT scenario.
60+
61+
Returns:
62+
MRTScenario: The MRT scenario.
63+
'''
3164
for scenario in _get_mrt_library().scenarios:
3265
if scenario.id == id:
3366
return scenario
3467

3568
@mrt_library_router.get('/')
3669
def get_mrt_library() -> MRTLibrary:
70+
'''
71+
This function returns the MRT library.
72+
73+
Author:
74+
Benedikt Schwering <bes9584@thi.de>
75+
76+
Returns:
77+
MRTLibrary: The MRT library.
78+
'''
3779
return _get_mrt_library()
3880

3981
@mrt_library_router.post('/')
4082
def start_mrt_scenario(mrt_scenario_request: MRTScenarioRequest) -> MRTScenarioResult:
83+
'''
84+
This function starts an MRT scenario.
85+
86+
Author:
87+
Benedikt Schwering <bes9584@thi.de>
88+
89+
Args:
90+
mrt_scenario_request (MRTScenarioRequest): The MRT scenario request.
91+
92+
Returns:
93+
MRTScenarioResult: The MRT scenario result.
94+
'''
4195
scenario = _get_mrt_scenario(
4296
id=mrt_scenario_request.id,
4397
)

src/controllers/version.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
ZettaBGP - Advanced Anomaly Detection in Internet Routing
4+
Copyright (c) 2024 Benedikt Schwering and Sebastian Forstner
5+
6+
This work is licensed under the terms of the MIT license.
7+
For a copy, see LICENSE in the project root.
8+
9+
Author:
10+
Benedikt Schwering <bes9584@thi.de>
11+
Sebastian Forstner <sef9869@thi.de>
12+
'''
113
from fastapi import APIRouter
214
import pkg_resources
315

416
version_router = APIRouter()
517

618
@version_router.get('/')
7-
def get_version():
19+
def get_version() -> str:
20+
'''
21+
This function returns the version of the ZettaBGP package.
22+
23+
Author:
24+
Benedikt Schwering <bes9584@thi.de>
25+
26+
Returns:
27+
str: The version of the ZettaBGP package.
28+
'''
829
return pkg_resources.get_distribution('zettabgp').version

src/main.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
ZettaBGP - Advanced Anomaly Detection in Internet Routing
4+
Copyright (c) 2024 Benedikt Schwering and Sebastian Forstner
5+
6+
This work is licensed under the terms of the MIT license.
7+
For a copy, see LICENSE in the project root.
8+
9+
Author:
10+
Benedikt Schwering <bes9584@thi.de>
11+
Sebastian Forstner <sef9869@thi.de>
12+
'''
113
import src.services.mrt_simulation as mrt_simulation_service
214
import src.services.exabgp as exabgp_service
315
from src.webapp import start_webapp
@@ -47,6 +59,21 @@ def cli():
4759
is_flag=True,
4860
)
4961
def exabgp(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_log: bool, no_mongodb_state: bool, no_mongodb_statistics: bool, clear_mongodb: bool):
62+
'''
63+
ExaBGP command for retrieving BGP messages from ExaBGP via stdin and processing them.
64+
65+
Author:
66+
Benedikt Schwering <bes9584@thi.de>
67+
Sebastian Forstner <sef9869@thi.de>
68+
69+
Args:
70+
no_rabbitmq_direct (bool): Disable direct RabbitMQ direct queue..
71+
rabbitmq_grouped (int): Queue group interval in minutes.
72+
no_mongodb_log (bool): Disable logging to MongoDB.
73+
no_mongodb_state (bool): Disable state storage to MongoDB.
74+
no_mongodb_statistics (bool): Disable statistics storage to MongoDB.
75+
clear_mongodb (bool): Clear MongoDB collections.
76+
'''
5077
exabgp_service.exabgp(
5178
no_rabbitmq_direct=no_rabbitmq_direct,
5279
rabbitmq_grouped=rabbitmq_grouped,
@@ -124,7 +151,25 @@ def exabgp(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_log: bool
124151
required=True,
125152
nargs=-1,
126153
)
127-
def mrt_simulation(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_log: bool, no_mongodb_state: bool, no_mongodb_statistics: bool, clear_mongodb: bool, playback_speed: int, playback_interval: int, mrt_files: tuple[str]):
154+
def mrt_simulation(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_log: bool, no_mongodb_state: bool, no_mongodb_statistics: bool, clear_mongodb: bool, playback_speed: int, playback_interval: int, mrt_files: tuple[str, ...]):
155+
'''
156+
MRT Simulation command for retrieving BGP messages from MRT files and processing them.
157+
158+
Author:
159+
Benedikt Schwering <bes9584@thi.de>
160+
Sebastian Forstner <sef9869@thi.de>
161+
162+
Args:
163+
no_rabbitmq_direct (bool): Disable direct RabbitMQ direct queue..
164+
rabbitmq_grouped (int): Queue group interval in minutes.
165+
no_mongodb_log (bool): Disable logging to MongoDB.
166+
no_mongodb_state (bool): Disable state storage to MongoDB.
167+
no_mongodb_statistics (bool): Disable statistics storage to MongoDB.
168+
clear_mongodb (bool): Clear MongoDB collections.
169+
playback_speed (int): Playback speed in multiples of real time.
170+
playback_interval (int): Playback interval in minutes.
171+
mrt_files (tuple[str, ...]): MRT files to process.
172+
'''
128173
mrt_simulation_service.mrt_simulation(
129174
no_rabbitmq_direct=no_rabbitmq_direct,
130175
rabbitmq_grouped=rabbitmq_grouped,
@@ -147,6 +192,15 @@ def mrt_simulation(no_rabbitmq_direct: bool, rabbitmq_grouped: int, no_mongodb_l
147192
is_flag=True,
148193
)
149194
def webapp(reload: bool):
195+
'''
196+
WebApp command for launching the admin WebApp.
197+
198+
Author:
199+
Benedikt Schwering <bes9584@thi.de>
200+
201+
Args:
202+
reload (bool): Reload the WebApp on changes.
203+
'''
150204
start_webapp(
151205
reload=reload,
152206
)

0 commit comments

Comments
 (0)