| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_PERSISTENT_STORAGE_H_ |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_PERSISTENT_STORAGE_H_ |
| |
| #include "state_persistence.pb.h" |
| #include "absl/status/status.h" |
| #include "absl/status/statusor.h" |
| namespace safepower_agent { |
| |
| // This class is responsible for reading and writing saved state from |
| // persistent storage. It should manage a single persistent instance of |
| // SavedActions consisting of all saved actions. |
| class PersistentStorageManager { |
| public: |
| virtual ~PersistentStorageManager() = default; |
| |
| // Writes a delta to the saved actions to persistent storage. |
| virtual absl::Status WriteSavedActionsChange( |
| const safepower_agent_persistence_proto::SavedActions& actions) = 0; |
| |
| // Reads all saved actions from persistent storage. |
| virtual absl::StatusOr<safepower_agent_persistence_proto::SavedActions> |
| ReadSavedActions() = 0; |
| |
| virtual absl::Status InitializeSavedActions() = 0; |
| }; |
| |
| // This is a wrapper around PersistentStorageManager that can be used to |
| // prevent writing to persistent storage. |
| class ReadOnlyPersistentStorageManager : public PersistentStorageManager { |
| public: |
| explicit ReadOnlyPersistentStorageManager(PersistentStorageManager& delegate) |
| : delegate_(delegate) {} |
| |
| absl::Status InitializeSavedActions() override { |
| return absl::FailedPreconditionError( |
| "Cannot initialize saved actions in read-only mode"); |
| } |
| |
| absl::Status WriteSavedActionsChange( |
| const safepower_agent_persistence_proto::SavedActions& /*actions*/) |
| override { |
| return absl::FailedPreconditionError( |
| "Cannot write saved actions in read-only mode"); |
| } |
| |
| absl::StatusOr<safepower_agent_persistence_proto::SavedActions> |
| ReadSavedActions() override { |
| return delegate_.ReadSavedActions(); |
| } |
| |
| private: |
| PersistentStorageManager& delegate_; |
| }; |
| |
| } // namespace safepower_agent |
| |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_PERSISTENT_STORAGE_H_ |