From bab9c4b86631619580319ac3d529774dc7f482bf Mon Sep 17 00:00:00 2001
From: Jorge Camarero Vera <jorge.camarero@cern.ch>
Date: Fri, 23 Sep 2022 15:03:32 +0200
Subject: [PATCH] Remove getDriveStates form TapeDrivesCatalogueState

---
 catalogue/DummyCatalogue.hpp           | 11 +++++++++--
 catalogue/TapeDrivesCatalogueState.cpp | 12 ------------
 catalogue/TapeDrivesCatalogueState.hpp |  1 -
 scheduler/OStoreDB/OStoreDB.cpp        |  2 +-
 scheduler/Scheduler.cpp                |  8 ++++----
 5 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/catalogue/DummyCatalogue.hpp b/catalogue/DummyCatalogue.hpp
index 83417a9828..daa1559a29 100644
--- a/catalogue/DummyCatalogue.hpp
+++ b/catalogue/DummyCatalogue.hpp
@@ -18,6 +18,7 @@
 #pragma once
 
 #include <iostream>
+#include <list>
 
 #include "Catalogue.hpp"
 
@@ -93,7 +94,6 @@ public:
   void deleteFileFromRecycleBin(const uint64_t archiveFileId, log::LogContext &lc) {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
   void deleteFilesFromRecycleLog(const std::string & vid, log::LogContext & lc) {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
   void createTapeDrive(const common::dataStructures::TapeDrive &tapeDrive) {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
-  std::list<common::dataStructures::TapeDrive> getTapeDrives() const {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
   void deleteTapeDrive(const std::string &tapeDriveName) {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
   void createTapeDriveConfig(const std::string &tapeDriveName, const std::string &category, const std::string &keyName, const std::string &value, const std::string &source) {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
   std::list<cta::catalogue::Catalogue::DriveConfig> getTapeDriveConfigs() const {throw exception::Exception(std::string("In ")+__PRETTY_FUNCTION__+": not implemented");}
@@ -266,7 +266,7 @@ public:
     return {m_tapeDriveStatus.driveName};
   }
 
-  std::optional<common::dataStructures::TapeDrive> getTapeDrive(const std::string &tapeDriveName) const {
+  std::optional<common::dataStructures::TapeDrive> getTapeDrive(const std::string &tapeDriveName) const override {
     if (m_tapeDriveStatus.driveName != "") return m_tapeDriveStatus;
     common::dataStructures::TapeDrive tapeDriveStatus;
     const time_t reportTime = time(nullptr);
@@ -290,6 +290,13 @@ public:
     return tapeDriveStatus;
   }
 
+  std::list<common::dataStructures::TapeDrive> getTapeDrives() const override {
+    std::list<common::dataStructures::TapeDrive> tapeDrives;
+    const auto tapeDrive = getTapeDrive(m_tapeDriveStatus.driveName);
+    if (tapeDrive.has_value()) tapeDrives.push_back(tapeDrive.value());
+    return tapeDrives;
+  }
+
   void setDesiredTapeDriveState(const std::string&,
       const common::dataStructures::DesiredDriveState &desiredState) override {
     m_tapeDriveStatus.desiredUp = desiredState.up;
diff --git a/catalogue/TapeDrivesCatalogueState.cpp b/catalogue/TapeDrivesCatalogueState.cpp
index e91aafce44..972ac56ffa 100644
--- a/catalogue/TapeDrivesCatalogueState.cpp
+++ b/catalogue/TapeDrivesCatalogueState.cpp
@@ -66,18 +66,6 @@ void TapeDrivesCatalogueState::checkDriveCanBeCreated(const cta::common::dataStr
   }
 }
 
-std::list<cta::common::dataStructures::TapeDrive> TapeDrivesCatalogueState::getDriveStates(
-  log::LogContext & lc) const {
-  std::list<cta::common::dataStructures::TapeDrive> tapeDrivesList;
-  const auto driveNames = m_catalogue.getTapeDriveNames();
-  for (const auto& driveName : driveNames) {
-    const auto tapeDrive = m_catalogue.getTapeDrive(driveName);
-    if (!tapeDrive) continue;
-    tapeDrivesList.push_back(tapeDrive.value());
-  }
-  return tapeDrivesList;
-}
-
 void TapeDrivesCatalogueState::removeDrive(const std::string& drive, log::LogContext &lc) {
   try {
     m_catalogue.deleteTapeDrive(drive);
diff --git a/catalogue/TapeDrivesCatalogueState.hpp b/catalogue/TapeDrivesCatalogueState.hpp
index 4d53954e2f..e935d28d36 100644
--- a/catalogue/TapeDrivesCatalogueState.hpp
+++ b/catalogue/TapeDrivesCatalogueState.hpp
@@ -71,7 +71,6 @@ public:
     const common::dataStructures::SecurityIdentity& identity, log::LogContext & lc);
   CTA_GENERATE_EXCEPTION_CLASS(DriveAlreadyExistsException);
   void checkDriveCanBeCreated(const cta::common::dataStructures::DriveInfo & driveInfo);
-  std::list<cta::common::dataStructures::TapeDrive> getDriveStates(log::LogContext & lc) const;
   void removeDrive(const std::string& drive, log::LogContext &lc);
   void setDesiredDriveState(const std::string& drive, const common::dataStructures::DesiredDriveState & desiredState,
     log::LogContext &lc);
diff --git a/scheduler/OStoreDB/OStoreDB.cpp b/scheduler/OStoreDB/OStoreDB.cpp
index a1cd9a08e4..88595b5017 100644
--- a/scheduler/OStoreDB/OStoreDB.cpp
+++ b/scheduler/OStoreDB/OStoreDB.cpp
@@ -483,7 +483,7 @@ void OStoreDB::fetchMountInfo(SchedulerDatabase::TapeMountDecisionInfo& tmdi, Ro
   // If a next mount exists the drive "counts double", but the corresponding drive
   // is either about to mount, or about to replace its current mount.
   double registerFetchTime = 0;
-  auto driveStates = m_tapeDrivesState->getDriveStates(logContext);
+  const auto driveStates = m_catalogue.getTapeDrives();
   registerFetchTime = t.secs(utils::Timer::resetCounter);
   using common::dataStructures::DriveStatus;
   std::set<int> activeDriveStatuses = {
diff --git a/scheduler/Scheduler.cpp b/scheduler/Scheduler.cpp
index 0b0917d904..133c180117 100644
--- a/scheduler/Scheduler.cpp
+++ b/scheduler/Scheduler.cpp
@@ -757,10 +757,10 @@ void Scheduler::RepackReportBatch::report(log::LogContext& lc) {
 //------------------------------------------------------------------------------
 common::dataStructures::DesiredDriveState Scheduler::getDesiredDriveState(const std::string& driveName, log::LogContext & lc) {
   utils::Timer t;
-  auto driveStates = m_tapeDrivesState->getDriveStates(lc);
-  for (auto & driveState : driveStates) {
+  const auto driveStates = m_catalogue.getTapeDrives();
+  for (const auto & driveState : driveStates) {
     if (driveState.driveName == driveName) {
-      auto schedulerDbTime = t.secs();
+      const auto schedulerDbTime = t.secs();
       if (schedulerDbTime > 1) {
         log::ScopedParamContainer spc(lc);
         spc.add("drive", driveName)
@@ -940,7 +940,7 @@ std::optional<cta::common::dataStructures::TapeDrive> Scheduler::getDriveState(c
 //------------------------------------------------------------------------------
 std::list<common::dataStructures::TapeDrive> Scheduler::getDriveStates(const common::dataStructures::SecurityIdentity &cliIdentity, log::LogContext & lc) const {
   utils::Timer t;
-  auto ret = m_tapeDrivesState->getDriveStates(lc);
+  const auto ret = m_catalogue.getTapeDrives();
   auto schedulerDbTime = t.secs();
   log::ScopedParamContainer spc(lc);
   spc.add("schedulerDbTime", schedulerDbTime);
-- 
GitLab