• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlanticwave-sdx / sdx-controller / 9500593558

13 Jun 2024 01:29PM UTC coverage: 52.322% (+0.09%) from 52.235%
9500593558

push

github

web-flow
Merge pull request #288 from atlanticwave-sdx/remove-connection

Delete connection

300 of 547 branches covered (54.84%)

Branch coverage included in aggregate %.

19 of 23 new or added lines in 3 files covered. (82.61%)

1 existing line in 1 file now uncovered.

748 of 1456 relevant lines covered (51.37%)

2.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.19
/sdx_controller/controllers/connection_controller.py
1
import json
4✔
2
import logging
4✔
3

4
import connexion
4✔
5
from flask import current_app
4✔
6

7
from sdx_controller.handlers.connection_handler import ConnectionHandler
4✔
8
from sdx_controller.utils.db_utils import DbUtils
4✔
9

10
LOG_FORMAT = (
4✔
11
    "%(levelname) -10s %(asctime)s %(name) -30s %(funcName) "
12
    "-35s %(lineno) -5d: %(message)s"
13
)
14
logger = logging.getLogger(__name__)
4✔
15
logging.getLogger("pika").setLevel(logging.WARNING)
4✔
16
logger.setLevel(logging.DEBUG)
4✔
17

18
# Get DB connection and tables set up.
19
db_instance = DbUtils()
4✔
20
db_instance.initialize_db()
4✔
21
connection_handler = ConnectionHandler(db_instance)
4✔
22

23

24
def delete_connection(connection_id):
4✔
25
    """
26
    Delete connection order by ID.
27

28
    :param connection_id: ID of the connection that needs to be
29
        deleted
30
    :type connection_id: int
31

32
    :rtype: None
33
    """
34
    logger.info(
4✔
35
        f"Handling delete (connecton id: {connection_id}) "
36
        f"with te_manager: {current_app.te_manager}"
37
    )
38

39
    # # Looking up by UUID do not seem work yet.  Will address in
40
    # # https://github.com/atlanticwave-sdx/sdx-controller/issues/252.
41
    #
42
    # value = db_instance.read_from_db(f"{connection_id}")
43
    # print(f"value: {value}")
44
    # if not value:
45
    #     return "Not found", 404
46

47
    try:
4✔
48
        # TODO: pce's unreserve_vlan() method silently returns even if the
49
        # connection_id is not found.  This should in fact be an error.
50
        #
51
        # https://github.com/atlanticwave-sdx/pce/issues/180
52
        connection = db_instance.read_from_db("connections", f"{connection_id}")
4✔
53
        if not connection:
4✔
54
            return "Did not find connection", 404
4✔
55
        connection_handler.remove_connection(current_app, connection_id)
4✔
56
        db_instance.mark_deleted("connections", f"{connection_id}")
4✔
57
    except Exception as e:
×
58
        logger.info(f"Delete failed (connection id: {connection_id}): {e}")
×
NEW
59
        return f"Failed, reason: {e}", 500
×
60

61
    return "OK", 200
4✔
62

63

64
def getconnection_by_id(connection_id):
4✔
65
    """
66
    Find connection by ID.
67

68
    :param connection_id: ID of connection that needs to be fetched
69
    :type connection_id: int
70

71
    :rtype: Connection
72
    """
73
    value = db_instance.read_from_db("connections", f"{connection_id}")
4✔
74
    if not value:
4✔
75
        return "Connection not found", 404
4✔
76
    return json.loads(value[connection_id])
4✔
77

78

79
def getconnections():  # noqa: E501
4✔
80
    """List all connections
81

82
    connection details # noqa: E501
83

84
    :rtype: Connection
85
    """
86
    values = db_instance.get_all_entries_in_collection("connections")
4✔
87
    if not values:
4✔
88
        return "No connection was found", 404
4✔
89
    return_values = {}
4✔
90
    for connection in values:
4✔
91
        connection_id = next(iter(connection))
4✔
92
        return_values[connection_id] = json.loads(connection[connection_id])
4✔
93
    return return_values
4✔
94

95

96
def place_connection(body):
4✔
97
    """
98
    Place an connection request from the SDX-Controller.
99

100
    :param body: order placed for creating a connection
101
    :type body: dict | bytes
102

103
    :rtype: Connection
104
    """
105
    logger.info(f"Placing connection: {body}")
4✔
106
    if not connexion.request.is_json:
4✔
107
        return "Request body must be JSON", 400
×
108

109
    body = connexion.request.get_json()
4✔
110
    logger.info(f"Gathered connexion JSON: {body}")
4✔
111

112
    logger.info("Placing connection. Saving to database.")
4✔
113

114
    connection_id = body["id"]
4✔
115

116
    db_instance.add_key_value_pair_to_db("connections", connection_id, json.dumps(body))
4✔
117
    logger.info("Saving to database complete.")
4✔
118

119
    logger.info(
4✔
120
        f"Handling request {connection_id} with te_manager: {current_app.te_manager}"
121
    )
122

123
    reason, code = connection_handler.place_connection(current_app.te_manager, body)
4✔
124
    logger.info(
4✔
125
        f"place_connection result: ID: {connection_id} reason='{reason}', code={code}"
126
    )
127

128
    response = {
4✔
129
        "connection_id": connection_id,
130
        "status": "OK" if code == 200 else "Failure",
131
        "reason": reason,
132
    }
133

134
    # # TODO: our response is supposed to be shaped just like request
135
    # # ('#/components/schemas/connection'), and in that case the below
136
    # # code would be a quick implementation.
137
    # #
138
    # # https://github.com/atlanticwave-sdx/sdx-controller/issues/251
139
    # response = body
140

141
    # response["id"] = connection_id
142
    # response["status"] = "success" if code == 200 else "failure"
143
    # response["reason"] = reason # `reason` is not present in schema though.
144

145
    return response, code
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc