-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '15-zettabgp-webapp' into 'main'
Resolve "ZettaBGP WebApp" Closes #15 See merge request imprj/01-bgp-testbed/zettabgp!13
- Loading branch information
Showing
62 changed files
with
15,557 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src/ui/ | ||
ui/dist/ | ||
ui/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
mrt_library/ | ||
src/ui/ | ||
local/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
mrtparse | ||
pydantic | ||
pymongo | ||
uvicorn | ||
fastapi | ||
exabgp | ||
click | ||
rich | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from src.models.mrt_library import MRTScenarioRequest, MRTScenarioResult, MRTScenario, MRTLibrary | ||
from src.parsers.mrt_bgp4mp import MrtBgp4MpParser | ||
from src.adapters.rabbitmq import RabbitMQAdapter | ||
from src.adapters.mongodb import MongoDBAdapter | ||
from src.models.route_update import ChangeType | ||
from fastapi.exceptions import HTTPException | ||
from datetime import datetime | ||
from fastapi import APIRouter | ||
from mrtparse import Reader | ||
from pathlib import Path | ||
import json, time, os | ||
|
||
mrt_library_router = APIRouter() | ||
|
||
def _get_mrt_library() -> MRTLibrary: | ||
mrt_library = MRTLibrary( | ||
scenarios=[], | ||
) | ||
|
||
for scenario_file in Path( | ||
os.getenv('ZETTABGP_WEBAPP_MRT_LIBRARY_PATH', 'mrt_library') | ||
).glob('**/scenario.json'): | ||
with open(scenario_file, 'r') as file: | ||
scenario = json.loads(file.read()) | ||
scenario['id'] = str(scenario_file.parent.absolute()).replace('/', '-') | ||
scenario['path'] = str(scenario_file.parent.absolute()) | ||
|
||
mrt_library.scenarios.append( | ||
MRTScenario.model_validate( | ||
obj=scenario, | ||
) | ||
) | ||
|
||
return mrt_library | ||
|
||
def _get_mrt_scenario(id: str) -> MRTScenario: | ||
for scenario in _get_mrt_library().scenarios: | ||
if scenario.id == id: | ||
return scenario | ||
|
||
@mrt_library_router.get('/') | ||
def get_mrt_library() -> MRTLibrary: | ||
return _get_mrt_library() | ||
|
||
@mrt_library_router.post('/') | ||
def start_mrt_scenario(mrt_scenario_request: MRTScenarioRequest) -> MRTScenarioResult: | ||
scenario = _get_mrt_scenario( | ||
id=mrt_scenario_request.id, | ||
) | ||
|
||
if not scenario: | ||
return HTTPException( | ||
status_code=400, | ||
detail='Scenario not found.', | ||
) | ||
|
||
mrt_scenario_result = MRTScenarioResult( | ||
count_announce=0, | ||
count_withdraw=0, | ||
) | ||
|
||
parser = MrtBgp4MpParser() | ||
|
||
if not scenario.no_rabbitmq_direct or scenario.rabbitmq_grouped: | ||
RabbitMQAdapter( | ||
parser=parser, | ||
no_direct=scenario.no_rabbitmq_direct, | ||
queue_interval=scenario.rabbitmq_grouped, | ||
) | ||
|
||
if not scenario.no_mongodb_log or not scenario.no_mongodb_state or not scenario.no_mongodb_statistics: | ||
MongoDBAdapter( | ||
parser=parser, | ||
no_mongodb_log=scenario.no_mongodb_log, | ||
no_mongodb_state=scenario.no_mongodb_state, | ||
no_mongodb_statistics=scenario.no_mongodb_statistics, | ||
clear_mongodb=scenario.clear_mongodb, | ||
) | ||
|
||
playback_speed_reference: datetime = None | ||
|
||
for mrt_file in scenario.mrt_files: | ||
mrt_file = str(Path(scenario.path) / Path(mrt_file)) | ||
|
||
for message in Reader(mrt_file): | ||
if message.data['type'] != {16: 'BGP4MP'}: | ||
print('[dark_orange]\[WARN][/] Skipping unsupported MRT type: ', end='') | ||
print(message.data['type']) | ||
continue | ||
|
||
current_timestamp: datetime = datetime.fromtimestamp( | ||
timestamp=list(message.data['timestamp'].keys())[0], | ||
) | ||
|
||
if scenario.playback_speed: | ||
if playback_speed_reference: | ||
time.sleep((current_timestamp - playback_speed_reference).seconds / scenario.playback_speed) | ||
|
||
playback_speed_reference = current_timestamp | ||
|
||
updates = parser.parse( | ||
bgp4mp_message=message, | ||
) | ||
|
||
if updates: | ||
for update in updates: | ||
match update.change_type: | ||
case ChangeType.ANNOUNCE: | ||
mrt_scenario_result.count_announce += 1 | ||
case ChangeType.WITHDRAW: | ||
mrt_scenario_result.count_withdraw += 1 | ||
|
||
return mrt_scenario_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from fastapi import APIRouter | ||
import pkg_resources | ||
|
||
version_router = APIRouter() | ||
|
||
@version_router.get('/') | ||
def get_version(): | ||
return pkg_resources.get_distribution('zettabgp').version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
|
||
class MRTScenarioRequest(BaseModel): | ||
id: str | ||
|
||
class MRTScenarioResult(BaseModel): | ||
count_announce: int | ||
count_withdraw: int | ||
|
||
class MRTScenario(BaseModel): | ||
id: str | ||
path: str | ||
name: str | ||
description: str | ||
no_rabbitmq_direct: bool | ||
rabbitmq_grouped: Optional[int] | ||
no_mongodb_log: bool | ||
no_mongodb_state: bool | ||
no_mongodb_statistics: bool | ||
clear_mongodb: bool | ||
playback_speed: Optional[int] | ||
mrt_files: list[str] | ||
|
||
class MRTLibrary(BaseModel): | ||
scenarios: list[MRTScenario] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from src.controllers.mrt_library import mrt_library_router | ||
from src.controllers.version import version_router | ||
from fastapi.staticfiles import StaticFiles | ||
from fastapi.responses import FileResponse | ||
from fastapi import FastAPI | ||
import uvicorn, os | ||
|
||
app = FastAPI() | ||
|
||
app.include_router( | ||
router=mrt_library_router, | ||
prefix='/api/mrt-library' | ||
) | ||
app.include_router( | ||
router=version_router, | ||
prefix='/api/version' | ||
) | ||
|
||
app.mount( | ||
path='/ui', | ||
app=StaticFiles( | ||
directory=os.getenv('ZETTABGP_WEBAPP_UI_PATH', 'src/ui'), | ||
), | ||
name="ui", | ||
) | ||
|
||
@app.get('/') | ||
def serve_angular_root(): | ||
return FileResponse(os.getenv('ZETTABGP_WEBAPP_UI_PATH', 'src/ui') + '/index.html') | ||
|
||
@app.exception_handler(404) | ||
def serve_angular_root(_, __): | ||
return FileResponse(os.getenv('ZETTABGP_WEBAPP_UI_PATH', 'src/ui') + '/index.html') | ||
|
||
def start_webapp(reload: bool): | ||
uvicorn.run( | ||
app=os.getenv('ZETTABGP_WEBAPP_APP', 'src.webapp:app'), | ||
host='0.0.0.0', | ||
port=8000, | ||
reload=reload, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Editor configuration, see https://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.ts] | ||
quote_type = single | ||
ij_typescript_use_double_quotes = false | ||
|
||
[*.md] | ||
max_line_length = off | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files. | ||
|
||
# Compiled output | ||
/dist | ||
/tmp | ||
/out-tsc | ||
/bazel-out | ||
|
||
# Node | ||
/node_modules | ||
npm-debug.log | ||
yarn-error.log | ||
|
||
# IDEs and editors | ||
.idea/ | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# Visual Studio Code | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
.history/* | ||
|
||
# Miscellaneous | ||
/.angular/cache | ||
.sass-cache/ | ||
/connect.lock | ||
/coverage | ||
/libpeerconnection.log | ||
testem.log | ||
/typings | ||
|
||
# System files | ||
.DS_Store | ||
Thumbs.db |
Oops, something went wrong.