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

atlanticwave-sdx / sdx-controller / 10729550655

05 Sep 2024 11:12PM UTC coverage: 57.757% (+0.1%) from 57.645%
10729550655

push

github

web-flow
Merge pull request #326 from atlanticwave-sdx/reuse-service-id-for-patch

Reuse service id for patch

467 of 745 branches covered (62.68%)

Branch coverage included in aggregate %.

15 of 21 new or added lines in 4 files covered. (71.43%)

4 existing lines in 1 file now uncovered.

1052 of 1885 relevant lines covered (55.81%)

2.23 hits per line

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

68.81
/sdx_controller/controllers/l2vpn_controller.py
1
import json
4✔
2
import logging
4✔
3
import uuid
4✔
4

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

8
from sdx_controller import util
4✔
9
from sdx_controller.handlers.connection_handler import ConnectionHandler
4✔
10
from sdx_controller.models.connection import Connection  # noqa: E501
4✔
11
from sdx_controller.models.l2vpn_body import L2vpnBody  # noqa: E501
4✔
12
from sdx_controller.models.l2vpn_service_id_body import L2vpnServiceIdBody  # noqa: E501
4✔
13
from sdx_controller.utils.db_utils import DbUtils
4✔
14

15
LOG_FORMAT = (
4✔
16
    "%(levelname) -10s %(asctime)s %(name) -30s %(funcName) "
17
    "-35s %(lineno) -5d: %(message)s"
18
)
19
logger = logging.getLogger(__name__)
4✔
20
logging.getLogger("pika").setLevel(logging.WARNING)
4✔
21
logger.setLevel(logging.DEBUG)
4✔
22

23
# Get DB connection and tables set up.
24
db_instance = DbUtils()
4✔
25
db_instance.initialize_db()
4✔
26
connection_handler = ConnectionHandler(db_instance)
4✔
27

28

29
def delete_connection(service_id):
4✔
30
    """
31
    Delete connection order by ID.
32

33
    :param service_id: ID of the connection that needs to be
34
        deleted
35
    :type service_id: str
36

37
    :rtype: None
38
    """
39
    logger.info(
4✔
40
        f"Handling delete (service id: {service_id}) "
41
        f"with te_manager: {current_app.te_manager}"
42
    )
43

44
    # # Looking up by UUID do not seem work yet.  Will address in
45
    # # https://github.com/atlanticwave-sdx/sdx-controller/issues/252.
46
    #
47
    # value = db_instance.read_from_db(f"{service_id}")
48
    # print(f"value: {value}")
49
    # if not value:
50
    #     return "Not found", 404
51

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

67
    return "OK", 200
4✔
68

69

70
def getconnection_by_id(service_id):
4✔
71
    """
72
    Find connection by ID.
73

74
    :param service_id: ID of connection that needs to be fetched
75
    :type service_id: str
76

77
    :rtype: Connection
78
    """
79

80
    value = db_instance.read_from_db("connections", f"{service_id}")
4✔
81
    if not value:
4✔
82
        return "Connection not found", 404
4✔
83
    return json.loads(value[service_id])
4✔
84

85

86
def getconnections():  # noqa: E501
4✔
87
    """List all connections
88

89
    connection details # noqa: E501
90

91
    :rtype: Connection
92
    """
93
    values = db_instance.get_all_entries_in_collection("connections")
4✔
94
    if not values:
4✔
95
        return "No connection was found", 404
4✔
96
    return_values = {}
4✔
97
    for connection in values:
4✔
98
        service_id = next(iter(connection))
4✔
99
        return_values[service_id] = json.loads(connection[service_id])
4✔
100
    return return_values
4✔
101

102

103
def place_connection(body):
4✔
104
    """
105
    Place an connection request from the SDX-Controller.
106

107
    :param body: order placed for creating a connection
108
    :type body: dict | bytes
109

110
    :rtype: Connection
111
    """
112
    logger.info(f"Placing connection: {body}")
4✔
113
    if not connexion.request.is_json:
4✔
114
        return "Request body must be JSON", 400
×
115

116
    body = connexion.request.get_json()
4✔
117
    logger.info(f"Gathered connexion JSON: {body}")
4✔
118

119
    logger.info("Placing connection. Saving to database.")
4✔
120

121
    service_id = body.get("id")
4✔
122

123
    if service_id is None:
4✔
124
        service_id = str(uuid.uuid4())
4✔
125
        body["id"] = service_id
4✔
126
        logger.info(f"Request has no ID. Generated ID: {service_id}")
4✔
127

128
    logger.info("Saving to database complete.")
4✔
129

130
    logger.info(
4✔
131
        f"Handling request {service_id} with te_manager: {current_app.te_manager}"
132
    )
133

134
    reason, code = connection_handler.place_connection(current_app.te_manager, body)
4✔
135

136
    if code == 200:
4✔
137
        db_instance.add_key_value_pair_to_db(
4✔
138
            "connections", service_id, json.dumps(body)
139
        )
140

141
    logger.info(
4✔
142
        f"place_connection result: ID: {service_id} reason='{reason}', code={code}"
143
    )
144

145
    response = {
4✔
146
        "service_id": service_id,
147
        "status": "OK" if code == 200 else "Failure",
148
        "reason": reason,
149
    }
150

151
    # # TODO: our response is supposed to be shaped just like request
152
    # # ('#/components/schemas/connection'), and in that case the below
153
    # # code would be a quick implementation.
154
    # #
155
    # # https://github.com/atlanticwave-sdx/sdx-controller/issues/251
156
    # response = body
157

158
    # response["id"] = service_id
159
    # response["status"] = "success" if code == 200 else "failure"
160
    # response["reason"] = reason # `reason` is not present in schema though.
161

162
    return response, code
4✔
163

164

165
def patch_connection(service_id, body=None):  # noqa: E501
4✔
166
    """Edit and change an existing L2vpn connection by ID from the SDX-Controller
167

168
     # noqa: E501
169

170
    :param service_id: ID of l2vpn connection that needs to be changed
171
    :type service_id: dict | bytes'
172
    :param body:
173
    :type body: dict | bytes
174

175
    :rtype: Connection
176
    """
177
    value = db_instance.read_from_db("connections", f"{service_id}")
×
178
    if not value:
×
179
        return "Connection not found", 404
×
180

181
    if not connexion.request.is_json:
×
182
        return "Request body must be JSON", 400
×
183

184
    body = L2vpnServiceIdBody.from_dict(connexion.request.get_json())  # noqa: E501
×
185

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

NEW
188
    body["id"] = service_id
×
189
    logger.info(f"Request has no ID. Generated ID: {service_id}")
×
190

191
    try:
×
192
        logger.info("Removing connection")
×
193
        connection_handler.remove_connection(current_app.te_manager, service_id)
×
NEW
194
        logger.info(f"Removed connection: {service_id}")
×
195
        logger.info(
×
196
            f"Placing new connection {service_id} with te_manager: {current_app.te_manager}"
197
        )
198
        reason, code = connection_handler.place_connection(current_app.te_manager, body)
×
199
        if code == 200:
×
200
            db_instance.add_key_value_pair_to_db(
×
201
                "connections", service_id, json.dumps(body)
202
            )
203
        logger.info(
×
204
            f"place_connection result: ID: {service_id} reason='{reason}', code={code}"
205
        )
206
        response = {
×
207
            "service_id": service_id,
208
            "status": "OK" if code == 200 else "Failure",
209
            "reason": reason,
210
        }
211
    except Exception as e:
×
212
        logger.info(f"Delete failed (connection id: {service_id}): {e}")
×
213
        return f"Failed, reason: {e}", 500
×
214

215
    return response, code
×
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

© 2026 Coveralls, Inc