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

atlanticwave-sdx / sdx-controller / 10473608051

20 Aug 2024 02:30PM UTC coverage: 57.931% (+4.4%) from 53.513%
10473608051

push

github

web-flow
Merge pull request #318 from atlanticwave-sdx/299-sdx-controller-must-have-new-endpoints-according-to-specification-20

299 sdx controller must have new endpoints according to specification 20

463 of 733 branches covered (63.17%)

Branch coverage included in aggregate %.

251 of 355 new or added lines in 9 files covered. (70.7%)

1038 of 1858 relevant lines covered (55.87%)

2.23 hits per line

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

81.05
/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
    connection_id = service_id
4✔
40

41
    logger.info(
4✔
42
        f"Handling delete (service id: {connection_id}) "
43
        f"with te_manager: {current_app.te_manager}"
44
    )
45

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

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

69
    return "OK", 200
4✔
70

71

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

76
    :param service_id: ID of connection that needs to be fetched
77
    :type service_id: str
78

79
    :rtype: Connection
80
    """
81

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

88

89
def getconnections():  # noqa: E501
4✔
90
    """List all connections
91

92
    connection details # noqa: E501
93

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

105

106
def place_connection(body):
4✔
107
    """
108
    Place an connection request from the SDX-Controller.
109

110
    :param body: order placed for creating a connection
111
    :type body: dict | bytes
112

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

119
    body = connexion.request.get_json()
4✔
120
    logger.info(f"Gathered connexion JSON: {body}")
4✔
121

122
    logger.info("Placing connection. Saving to database.")
4✔
123

124
    connection_id = body.get("id")
4✔
125

126
    if connection_id is None:
4✔
127
        connection_id = str(uuid.uuid4())
4✔
128
        body["id"] = connection_id
4✔
129
        logger.info(f"Request has no ID. Generated ID: {connection_id}")
4✔
130

131
    logger.info("Saving to database complete.")
4✔
132

133
    logger.info(
4✔
134
        f"Handling request {connection_id} with te_manager: {current_app.te_manager}"
135
    )
136

137
    reason, code = connection_handler.place_connection(current_app.te_manager, body)
4✔
138

139
    if code == 200:
4✔
140
        db_instance.add_key_value_pair_to_db(
4✔
141
            "connections", connection_id, json.dumps(body)
142
        )
143

144
    logger.info(
4✔
145
        f"place_connection result: ID: {connection_id} reason='{reason}', code={code}"
146
    )
147

148
    response = {
4✔
149
        "service_id": connection_id,
150
        "status": "OK" if code == 200 else "Failure",
151
        "reason": reason,
152
    }
153

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

161
    # response["id"] = connection_id
162
    # response["status"] = "success" if code == 200 else "failure"
163
    # response["reason"] = reason # `reason` is not present in schema though.
164

165
    return response, code
4✔
166

167

168
def patch_connection(connection_id, body=None):  # noqa: E501
4✔
169
    """Edit and change an existing L2vpn connection by ID from the SDX-Controller
170

171
     # noqa: E501
172

173
    :param service_id: ID of l2vpn connection that needs to be changed
174
    :type service_id: dict | bytes'
175
    :param body:
176
    :type body: dict | bytes
177

178
    :rtype: Connection
179
    """
NEW
180
    value = db_instance.read_from_db("connections", f"{connection_id}")
×
NEW
181
    if not value:
×
NEW
182
        return "Connection not found", 404
×
183

NEW
184
    logger.info(f"Changed connection: {body}")
×
NEW
185
    if not connexion.request.is_json:
×
NEW
186
        return "Request body must be JSON", 400
×
187

NEW
188
    body = L2vpnServiceIdBody.from_dict(connexion.request.get_json())  # noqa: E501
×
189

NEW
190
    logger.info(f"Gathered connexion JSON: {body}")
×
191

NEW
192
    return json.loads(value[connection_id])
×
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