diff --git a/orchagent/orch.cpp b/orchagent/orch.cpp index 235b4661d1d..44f8c73f48d 100644 --- a/orchagent/orch.cpp +++ b/orchagent/orch.cpp @@ -66,13 +66,10 @@ vector Orch::getSelectables() return selectables; } -void Consumer::execute() +void Consumer::addToSync(std::deque &entries) { SWSS_LOG_ENTER(); - std::deque entries; - getConsumerTable()->pops(entries); - /* Nothing popped */ if (entries.empty()) { @@ -123,6 +120,16 @@ void Consumer::execute() m_toSync[key] = KeyOpFieldsValuesTuple(key, op, existing_values); } } +} + +void Consumer::execute() +{ + SWSS_LOG_ENTER(); + + std::deque entries; + getConsumerTable()->pops(entries); + + addToSync(entries); drain(); } @@ -133,6 +140,51 @@ void Consumer::drain() m_orch->doTask(*this); } +/* Fetch existing table data for consumer of the specified table */ +void Orch::addExistingData(DBConnector *db, string tableName) +{ + Consumer* consumer; + auto it = m_consumerMap.begin(); + + while (it != m_consumerMap.end()) + { + // Executor (not Consumer) may be in m_consumerMap, don't cast + if (tableName == (it->second.get())->getName()) + { + break; + } + it++; + } + + if (it == m_consumerMap.end()) + { + return; + } + // Now we are sure that it is a Consumer + consumer = (Consumer*)(it->second.get()); + + std::deque entries; + Table table = Table(db, tableName); + vector keys; + + table.getKeys(keys); + for (const auto &key: keys) + { + KeyOpFieldsValuesTuple kco; + + kfvKey(kco) = key; + kfvOp(kco) = SET_COMMAND; + + if (!table.get(key, kfvFieldsValues(kco))) + { + continue; + } + entries.push_back(kco); + } + + consumer->addToSync(entries); +} + /* - Validates reference has proper format which is [table_name:object_name] - validates table_name exists diff --git a/orchagent/orch.h b/orchagent/orch.h index 475e6b58e21..3965f32c380 100644 --- a/orchagent/orch.h +++ b/orchagent/orch.h @@ -91,10 +91,21 @@ class Executor : public Selectable virtual void execute() { } virtual void drain() { } + virtual string getName() const + { + return m_name; + } + virtual void setName(string name) + { + m_name = name; + } protected: Selectable *m_selectable; Orch *m_orch; + // Name for Executor + string m_name; + // Get the underlying selectable Selectable *getSelectable() const { return m_selectable; } }; @@ -116,8 +127,14 @@ class Consumer : public Executor { return getConsumerTable()->getTableName(); } + string getName() const + { + return getConsumerTable()->getTableName(); + } + void execute(); void drain(); + void addToSync(std::deque &entries); /* Store the latest 'golden' status */ // TODO: hide? @@ -149,6 +166,9 @@ class Orch vector getSelectables(); + // add the existing data to the consumer todo task list. + void addExistingData(DBConnector *db, string tableName); + /* Iterate all consumers in m_consumerMap and run doTask(Consumer) */ void doTask();