buffet: GCD command manager to load command .json files at startup

buffet daemon now loads the base command definition file from
/etc/buffet/gcd.json at startup. Also, static device command
defintions are load from .json file from /etc/buffet/commands
(if found).

BUG=chromium:374861
TEST=USE=buffet P2_TEST_FILTER="buffet::*" FEATURES=test emerge-link platform2
TEST=Manually deployed test image and copied /etc/buffet/gcd.json to
     /etc/buffet/commands/gcd.json and verified in /var/log/buffet/*
     that the static command definition is loaded at startup.

Change-Id: I1e6b13cabe8cc2ba1a941cc05cb1d0cfeedda480
Reviewed-on: https://chromium-review.googlesource.com/209311
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/commands/command_manager.cc b/buffet/commands/command_manager.cc
index d7cf34c..48a323f 100644
--- a/buffet/commands/command_manager.cc
+++ b/buffet/commands/command_manager.cc
@@ -4,6 +4,9 @@
 
 #include "buffet/commands/command_manager.h"
 
+#include <base/at_exit.h>
+#include <base/bind.h>
+#include <base/files/file_enumerator.h>
 #include <base/file_util.h>
 #include <base/values.h>
 #include <base/json/json_reader.h>
@@ -13,6 +16,8 @@
 
 namespace buffet {
 
+CommandManager* CommandManager::instance_ = nullptr;
+
 const CommandDictionary& CommandManager::GetCommandDictionary() const {
   return dictionary_;
 }
@@ -47,6 +52,49 @@
   return LoadCommands(*json, category, error);
 }
 
+CommandManager* CommandManager::GetInstance() {
+  CHECK(instance_) << "CommandManager instance not initialized.";
+  return instance_;
+}
+
+void CommandManager::Startup() {
+  CHECK(!instance_) << "CommandManager instance already initialized.";
+  LOG(INFO) << "Initializing CommandManager.";
+  std::unique_ptr<CommandManager> inst(new CommandManager);
+
+  // Load global standard GCD command dictionary.
+  base::FilePath base_command_file("/etc/buffet/gcd.json");
+  LOG(INFO) << "Loading standard commands from " << base_command_file.value();
+  CHECK(inst->LoadBaseCommands(base_command_file, nullptr))
+      << "Failed to load the standard command definitions.";
+
+  // Load static device command definitions.
+  base::FilePath device_command_dir("/etc/buffet/commands");
+  base::FileEnumerator enumerator(device_command_dir, false,
+                                  base::FileEnumerator::FILES,
+                                  FILE_PATH_LITERAL("*.json"));
+  base::FilePath json_file_path = enumerator.Next();
+  while (!json_file_path.empty()) {
+    LOG(INFO) << "Loading command schema from " << json_file_path.value();
+    CHECK(inst->LoadCommands(json_file_path, nullptr))
+        << "Failed to load the command definition file.";
+    json_file_path = enumerator.Next();
+  }
+
+  // Register a cleanup callback to be executed at shut-down.
+  base::AtExitManager::RegisterTask(base::Bind(&CommandManager::Shutdown));
+  // Transfer the object instance pointer from the smart pointer to
+  // the global pointer.
+  instance_ = inst.release();
+}
+
+void CommandManager::Shutdown() {
+  CHECK(instance_) << "CommandManager instance not initialized.";
+  LOG(INFO) << "Shutting down CommandManager.";
+  delete instance_;
+  instance_ = nullptr;
+}
+
 std::unique_ptr<const base::DictionaryValue> CommandManager::LoadJsonDict(
     const base::FilePath& json_file_path, ErrorPtr* error) {
   std::string json_string;