Skip to content

Commit

Permalink
Form a minimal response to GET /l2vpn/1.0/:service_id
Browse files Browse the repository at this point in the history
  • Loading branch information
sajith committed Sep 19, 2024
1 parent 8fe3f4a commit b801c7c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
11 changes: 8 additions & 3 deletions sdx_controller/controllers/l2vpn_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from flask import current_app

from sdx_controller import util
from sdx_controller.handlers.connection_handler import ConnectionHandler
from sdx_controller.handlers.connection_handler import (
ConnectionHandler,
get_connection_status,
)
from sdx_controller.models.connection import Connection # noqa: E501
from sdx_controller.models.l2vpn_body import L2vpnBody # noqa: E501
from sdx_controller.models.l2vpn_service_id_body import L2vpnServiceIdBody # noqa: E501
Expand Down Expand Up @@ -77,10 +80,12 @@ def getconnection_by_id(service_id):
:rtype: Connection
"""

value = db_instance.read_from_db("connections", f"{service_id}")
value = get_connection_status(db_instance, service_id)

if not value:
return "Connection not found", 404
return json.loads(value[service_id])

return value


def getconnections(): # noqa: E501
Expand Down
74 changes: 74 additions & 0 deletions sdx_controller/handlers/connection_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,77 @@ def handle_link_failure(self, te_manager, failed_links):
logger.debug("Removed connection:")
logger.debug(connection)
self.place_connection(te_manager, connection)


def get_connection_status(db, service_id: str):
"""
Form a response to `GET /l2vpn/1.0/{service_id}`.
"""
assert db is not None
assert service_id is not None

breakdown = db.read_from_db("breakdowns", service_id)
if not breakdown:
logger.info(f"Could not find breakdown for {service_id}")
return None

print(f"breakdown for {service_id}: {breakdown}")

# The breakdown we read from DB is in this shape:
#
# {
# "_id": ObjectId("66ec71770c7022eb0922f41a"),
# "5b7df397-2269-489b-8e03-f256461265a0": {
# "urn:sdx:topology:amlight.net": {
# "name": "AMLIGHT_vlan_1000_10001",
# "dynamic_backup_path": True,
# "uni_a": {
# "tag": {"value": 1000, "tag_type": 1},
# "port_id": "urn:sdx:port:amlight.net:A1:1",
# },
# "uni_z": {
# "tag": {"value": 10001, "tag_type": 1},
# "port_id": "urn:sdx:port:amlight.net:B1:3",
# },
# }
# },
# }

response = {}

domains = breakdown.get(service_id)
logger.info(f"domains for {service_id}: {domains.keys()}")

endpoints = list()

for domain, breakdown in domains.items():
uni_a_port = breakdown.get("uni_a").get("port_id")
uni_a_vlan = breakdown.get("uni_a").get("tag").get("value")

endpoint_a = {
"port_id": uni_a_port,
"vlan": str(uni_a_vlan),
}

endpoints.append(endpoint_a)

uni_z_port = breakdown.get("uni_z").get("port_id")
uni_z_vlan = breakdown.get("uni_z").get("tag").get("value")

endpoint_z = {
"port_id": uni_z_port,
"vlan": str(uni_z_vlan),
}

endpoints.append(endpoint_z)

response[service_id] = {
"service_id": service_id,
# TODO: use the real name here.
"name": "Fake Name",
"endpoints": endpoints,
}

logger.info(f"Formed a response: {response}")

return response

0 comments on commit b801c7c

Please sign in to comment.