Skip to content

Commit

Permalink
Merge pull request #326 from atlanticwave-sdx/reuse-service-id-for-patch
Browse files Browse the repository at this point in the history
Reuse service id for patch
  • Loading branch information
congwang09 authored Sep 5, 2024
2 parents def2444 + 9ab4984 commit c906388
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
1 change: 1 addition & 0 deletions sdx_controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def create_app(run_listener: bool = True):
else:
logging.basicConfig(level=logging.DEBUG)

logging.getLogger("sdx_pce.topology.temanager").setLevel(logging.INFO)
app = connexion.App(__name__, specification_dir="./swagger/")
app.app.json_encoder = encoder.JSONEncoder
app.add_api(
Expand Down
7 changes: 2 additions & 5 deletions sdx_controller/controllers/l2vpn_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,13 @@ def patch_connection(service_id, body=None): # noqa: E501

logger.info(f"Gathered connexion JSON: {body}")

new_service_id = str(uuid.uuid4())
body["id"] = new_service_id
body["id"] = service_id
logger.info(f"Request has no ID. Generated ID: {service_id}")

try:
logger.info("Removing connection")
connection_handler.remove_connection(current_app.te_manager, service_id)
db_instance.mark_deleted("connections", f"{service_id}")
db_instance.mark_deleted("breakdowns", f"{service_id}")
logger.info("Removed connection: ", service_id)
logger.info(f"Removed connection: {service_id}")
logger.info(
f"Placing new connection {service_id} with te_manager: {current_app.te_manager}"
)
Expand Down
37 changes: 35 additions & 2 deletions sdx_controller/handlers/connection_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import time
import traceback
from typing import Tuple

Expand Down Expand Up @@ -131,8 +132,8 @@ def place_connection(
Note that we can return early if things fail. Return value is
a tuple of the form (reason, HTTP code).
"""
for num, val in enumerate(te_manager.get_topology_map().values()):
logger.debug(f"TE topology #{num}: {val}")
# for num, val in enumerate(te_manager.get_topology_map().values()):
# logger.debug(f"TE topology #{num}: {val}")

graph = te_manager.generate_graph_te()
if graph is None:
Expand Down Expand Up @@ -170,6 +171,36 @@ def place_connection(
logger.error(f"Error when generating/publishing breakdown: {e} - {err}")
return f"Error: {e}", 400

def archive_connection(self, service_id) -> None:
connection_request = self.db_instance.read_from_db("connections", service_id)
if not connection_request:
return

connection_request_str = connection_request[service_id]
self.db_instance.delete_one_entry("connections", service_id)

historical_connections = self.db_instance.read_from_db(
"historical_connections", service_id
)
# Current timestamp in seconds
timestamp = int(time.time())

if historical_connections:
historical_connections_list = historical_connections[service_id]
historical_connections_list.append(
json.dumps({timestamp: json.loads(connection_request_str)})
)
self.db_instance.add_key_value_pair_to_db(
"historical_connections", service_id, historical_connections_list
)
else:
self.db_instance.add_key_value_pair_to_db(
"historical_connections",
service_id,
[json.dumps({timestamp: json.loads(connection_request_str)})],
)
logger.debug(f"Archived connection: {service_id}")

def remove_connection(self, te_manager, service_id) -> Tuple[str, int]:
te_manager.unreserve_vlan(service_id)
connection_request = self.db_instance.read_from_db("connections", service_id)
Expand All @@ -187,6 +218,8 @@ def remove_connection(self, te_manager, service_id) -> Tuple[str, int]:
status, code = self._send_breakdown_to_lc(
breakdown, "delete", json.loads(connection_request)
)
self.db_instance.delete_one_entry("breakdowns", service_id)
self.archive_connection(service_id)
logger.debug(f"Breakdown sent to LC, status: {status}, code: {code}")
return status, code
except Exception as e:
Expand Down
10 changes: 8 additions & 2 deletions sdx_controller/utils/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import json
import logging
import os
from urllib.parse import urlparse

import pymongo

COLLECTION_NAMES = ["topologies", "connections", "breakdowns", "domains", "links"]
COLLECTION_NAMES = [
"topologies",
"connections",
"breakdowns",
"domains",
"links",
"historical_connections",
]

pymongo_logger = logging.getLogger("pymongo")
pymongo_logger.setLevel(logging.INFO)
Expand Down

0 comments on commit c906388

Please sign in to comment.