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

atlanticwave-sdx / sdx-controller / 18167359805

01 Oct 2025 03:32PM UTC coverage: 55.313%. Remained the same
18167359805

Pull #499

github

web-flow
Merge c460da221 into eaf9bb32a
Pull Request #499: Change domain list to dict

4 of 12 new or added lines in 3 files covered. (33.33%)

1 existing line in 1 file now uncovered.

1218 of 2202 relevant lines covered (55.31%)

1.11 hits per line

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

63.1
/sdx_controller/messaging/rpc_queue_consumer.py
1
#!/usr/bin/env python
2
import json
2✔
3
import logging
2✔
4
import os
2✔
5
import threading
2✔
6
from queue import Queue
2✔
7

8
import pika
2✔
9
from sdx_datamodel.constants import Constants, MessageQueueNames, MongoCollections
2✔
10
from sdx_datamodel.models.topology import SDX_TOPOLOGY_ID_prefix
2✔
11

12
from sdx_controller.handlers.lc_message_handler import LcMessageHandler
2✔
13
from sdx_controller.utils.parse_helper import ParseHelper
2✔
14

15
MQ_HOST = os.getenv("MQ_HOST")
2✔
16
MQ_PORT = os.getenv("MQ_PORT") or 5672
2✔
17
MQ_USER = os.getenv("MQ_USER") or "guest"
2✔
18
MQ_PASS = os.getenv("MQ_PASS") or "guest"
2✔
19

20
# subscribe to the corresponding queue
21
SUB_QUEUE = MessageQueueNames.OXP_UPDATE
2✔
22

23
logger = logging.getLogger(__name__)
2✔
24

25

26
class RpcConsumer(object):
2✔
27
    def __init__(self, thread_queue, exchange_name, te_manager):
2✔
28
        self.logger = logging.getLogger(__name__)
2✔
29

30
        self.logger.info(f"[MQ] Using amqp://{MQ_USER}@{MQ_HOST}:{MQ_PORT}")
2✔
31

32
        self.connection = pika.BlockingConnection(
2✔
33
            pika.ConnectionParameters(
34
                host=MQ_HOST,
35
                port=MQ_PORT,
36
                credentials=pika.PlainCredentials(username=MQ_USER, password=MQ_PASS),
37
            )
38
        )
39

40
        self.channel = self.connection.channel()
2✔
41
        self.exchange_name = exchange_name
2✔
42

43
        self.channel.queue_declare(queue=SUB_QUEUE)
2✔
44
        self._thread_queue = thread_queue
2✔
45

46
        self.te_manager = te_manager
2✔
47

48
        self._exit_event = threading.Event()
2✔
49

50
    def on_request(self, ch, method, props, message_body):
2✔
51
        response = message_body
×
52
        self._thread_queue.put(message_body)
×
53

54
        self.connection = pika.BlockingConnection(
×
55
            pika.ConnectionParameters(
56
                host=MQ_HOST,
57
                port=MQ_PORT,
58
                credentials=pika.PlainCredentials(username=MQ_USER, password=MQ_PASS),
59
            )
60
        )
61
        self.channel = self.connection.channel()
×
62

63
        try:
×
64
            ch.basic_publish(
×
65
                exchange=self.exchange_name,
66
                routing_key=props.reply_to,
67
                properties=pika.BasicProperties(correlation_id=props.correlation_id),
68
                body=str(response),
69
            )
70
            ch.basic_ack(delivery_tag=method.delivery_tag)
×
71
        except Exception as err:
×
72
            self.logger.info(f"[MQ] encountered error when publishing: {err}")
×
73

74
    def start_consumer(self):
2✔
75
        self.channel.basic_qos(prefetch_count=1)
2✔
76
        self.channel.basic_consume(queue=SUB_QUEUE, on_message_callback=self.on_request)
2✔
77

78
        self.logger.info(" [MQ] Awaiting requests from queue: " + SUB_QUEUE)
2✔
79
        self.channel.start_consuming()
2✔
80

81
    def start_sdx_consumer(self, thread_queue, db_instance):
2✔
82
        HEARTBEAT_ID = 0
2✔
83

84
        rpc = RpcConsumer(thread_queue, "", self.te_manager)
2✔
85
        t1 = threading.Thread(target=rpc.start_consumer, args=(), daemon=True)
2✔
86
        t1.start()
2✔
87

88
        lc_message_handler = LcMessageHandler(db_instance, self.te_manager)
2✔
89
        parse_helper = ParseHelper()
2✔
90

91
        latest_topo = {}
2✔
92
        domain_dict = {}
2✔
93

94
        # This part reads from DB when SDX controller initially starts.
95
        # It looks for domain_dict, if already in DB,
96
        # Then use the existing ones from DB.
97
        domain_dict_from_db = db_instance.get_value_from_db(
2✔
98
            MongoCollections.DOMAINS, Constants.DOMAIN_DICT
99
        )
100
        latest_topo_from_db = db_instance.get_value_from_db(
2✔
101
            MongoCollections.TOPOLOGIES, Constants.LATEST_TOPOLOGY
102
        )
103

104
        if domain_dict_from_db:
2✔
NEW
105
            domain_dict = domain_dict_from_db
×
106
            logger.debug("Domain list already exists in db: ")
×
NEW
107
            logger.debug(domain_dict)
×
108

109
        if latest_topo_from_db:
2✔
110
            latest_topo = latest_topo_from_db
2✔
111
            logger.debug("Topology already exists in db: ")
2✔
112
            logger.debug(latest_topo)
2✔
113

114
        # If topologies already saved in db, use them to initialize te_manager
115
        if domain_dict:
2✔
NEW
116
            for domain in domain_dict.keys():
×
UNCOV
117
                topology = db_instance.get_value_from_db(
×
118
                    MongoCollections.TOPOLOGIES, SDX_TOPOLOGY_ID_prefix + domain
119
                )
120

121
                if not topology:
×
122
                    continue
×
123

124
                # Get the actual thing minus the Mongo ObjectID.
125
                self.te_manager.add_topology(topology)
×
126
                logger.debug(f"Read {domain}: {topology}")
×
127

128
        while not self._exit_event.is_set():
2✔
129
            # Queue.get() will block until there's an item in the queue.
130
            msg = thread_queue.get()
2✔
131
            logger.debug("MQ received message:" + str(msg))
×
132

133
            if "Heart Beat" in str(msg):
×
134
                HEARTBEAT_ID += 1
×
135
                logger.debug("Heart beat received. ID: " + str(HEARTBEAT_ID))
×
136
                continue
×
137

138
            if not parse_helper.is_json(msg):
×
139
                continue
×
140

141
            if "version" not in str(msg):
×
142
                logger.info("Got message (NO VERSION) from MQ: " + str(msg))
×
143

144
            lc_message_handler.process_lc_json_msg(
×
145
                msg,
146
                latest_topo,
147
                domain_dict,
148
            )
149

150
    def stop_threads(self):
2✔
151
        """
152
        Signal threads that we're ready to stop.
153
        """
154
        logger.info("[MQ] Stopping threads.")
×
155
        self.channel.stop_consuming()
×
156
        self._exit_event.set()
×
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