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

atlanticwave-sdx / sdx-controller / 13334477933

14 Feb 2025 05:42PM UTC coverage: 55.391% (+0.02%) from 55.374%
13334477933

Pull #415

github

web-flow
Merge b0a3d35cc into 5c5ba2915
Pull Request #415: Adding log level configuration from env variable

5 of 9 new or added lines in 5 files covered. (55.56%)

3 existing lines in 2 files now uncovered.

1120 of 2022 relevant lines covered (55.39%)

1.11 hits per line

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

94.44
/sdx_controller/__init__.py
1
import json
2✔
2
import logging
2✔
3
import os
2✔
4
import threading
2✔
5
from queue import Queue
2✔
6

7
import connexion
2✔
8
from sdx_pce.topology.temanager import TEManager
2✔
9

10
from sdx_controller import encoder
2✔
11
from sdx_controller.messaging.rpc_queue_consumer import RpcConsumer
2✔
12
from sdx_controller.utils.db_utils import DbUtils
2✔
13

14
logger = logging.getLogger(__name__)
2✔
15
logging.getLogger("pika").setLevel(logging.WARNING)
2✔
16
LOG_FILE = os.environ.get("LOG_FILE")
2✔
17
LOG_LEVEL = os.getenv("LOG_LEVEL", "DEBUG")
2✔
18

19

20
def create_rpc_thread(app):
2✔
21
    """
22
    Start a thread to get items off the message queue.
23
    """
24
    thread_queue = Queue()
2✔
25

26
    app.rpc_consumer = RpcConsumer(thread_queue, "", app.te_manager)
2✔
27
    rpc_thread = threading.Thread(
2✔
28
        target=app.rpc_consumer.start_sdx_consumer,
29
        kwargs={"thread_queue": thread_queue, "db_instance": app.db_instance},
30
        daemon=True,
31
    )
32

33
    rpc_thread.start()
2✔
34

35

36
def create_app(run_listener: bool = True):
2✔
37
    """
38
    Create a connexion app.
39

40
    The object returned is a Connexion App, which in turn contains a
41
    Flask app, that we can run either with Flask or an ASGI server
42
    such as uvicorn::
43

44
        $ flask run sdx_server.app:app
45
        $ uvicorn run sdx_server.app:asgi_app
46

47
    We also create a thread that subscribes to our message queue.
48
    Occasionally it might be useful not to start the thread (such as
49
    when running the test suite, because currently our tests do not
50
    use the message queue), and we might want to disable those
51
    threads, which is when run_listener param might be useful.
52
    """
53
    if LOG_FILE:
2✔
NEW
54
        logging.basicConfig(filename=LOG_FILE, level=logging.getLevelName(LOG_LEVEL))
×
55
    else:
56
        logging.basicConfig(level=logging.getLevelName(LOG_LEVEL))
2✔
57

58
    logging.getLogger("sdx_pce.topology.temanager").setLevel(logging.INFO)
2✔
59
    app = connexion.App(__name__, specification_dir="./swagger/")
2✔
60
    app.app.json_encoder = encoder.JSONEncoder
2✔
61
    app.add_api(
2✔
62
        "swagger.yaml", arguments={"title": "SDX-Controller"}, pythonic_params=True
63
    )
64

65
    # Get DB connection and tables set up.
66
    app.db_instance = DbUtils()
2✔
67
    app.db_instance.initialize_db()
2✔
68

69
    topo_val = app.db_instance.read_from_db("topologies", "latest_topo")
2✔
70

71
    # Get a handle to PCE.
72
    app.te_manager = (
2✔
73
        TEManager(topology_data=json.loads(topo_val["latest_topo"]))
74
        if topo_val
75
        else TEManager(topology_data=None)
76
    )
77

78
    # TODO: This is a hack, until we find a better way to get a handle
79
    # to TEManager from Flask current_app, which are typically
80
    # available to request handlers.  There must be a better way to
81
    # pass this around.
82
    app.app.te_manager = app.te_manager
2✔
83

84
    if run_listener:
2✔
85
        create_rpc_thread(app)
2✔
86
    else:
87
        app.rpc_consumer = None
×
88

89
    return app
2✔
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