buffet: Update configuration reading for new fields moved from privetd

Removed default_ prefix from default_name and default_description.
Added fields location, model_id, oem_name, model_name.

BUG=brillo:377,brillo:342
TEST=FEATURE=test emerge-gizmo buffet
CQ-DEPEND=CL:266085

Change-Id: I133ac5e41db0d7a7fc67fa0d9773dd1179a90032
Reviewed-on: https://chromium-review.googlesource.com/266028
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/buffet_config.cc b/buffet/buffet_config.cc
index 1ed7842..d8e0324 100644
--- a/buffet/buffet_config.cc
+++ b/buffet/buffet_config.cc
@@ -7,19 +7,56 @@
 #include <base/logging.h>
 #include <base/strings/string_number_conversions.h>
 
+namespace {
+
+// TODO(vitalybuka): Remove this when deviceKind is gone from server.
+std::string GetDeviceKind(const std::string& manifest_id) {
+  CHECK_EQ(5u, manifest_id.size());
+  std::string kind = manifest_id.substr(0, 2);
+  if (kind == "AC")
+    return "accessPoint";
+  if (kind == "AK")
+    return "aggregator";
+  if (kind == "AM")
+    return "camera";
+  if (kind == "AB")
+    return "developmentBoard";
+  if (kind == "AE")
+    return "printer";
+  if (kind == "AF")
+    return "scanner";
+  if (kind == "AD")
+    return "speaker";
+  if (kind == "AL")
+    return "storage";
+  if (kind == "AJ")
+    return "toy";
+  if (kind == "AA")
+    return "vendor";
+  if (kind == "AN")
+    return "video";
+  LOG(FATAL) << "Invalid model id: " << manifest_id;
+  return std::string();
+}
+
+}  // namespace
+
 namespace buffet {
+
 namespace config_keys {
 
-const char kClientId[]             = "client_id";
-const char kClientSecret[]         = "client_secret";
-const char kApiKey[]               = "api_key";
-const char kOAuthURL[]             = "oauth_url";
-const char kServiceURL[]           = "service_url";
-const char kDeviceKind[]           = "device_kind";
-const char kName[]                 = "name";
-const char kDefaultDescription[]   = "default_description";
-const char kDefaultLocation[]      = "default_location";
-const char kModelId[]              = "model_id";
+const char kClientId[] = "client_id";
+const char kClientSecret[] = "client_secret";
+const char kApiKey[] = "api_key";
+const char kOAuthURL[] = "oauth_url";
+const char kServiceURL[] = "service_url";
+const char kName[] = "name";
+const char kDescription[] = "description";
+const char kLocation[] = "location";
+const char kOemName[] = "oem_name";
+const char kModelName[] = "model_name";
+const char kModelId[] = "model_id";
+
 const char kPollingPeriodMs[]      = "polling_period_ms";
 
 }  // namespace config_keys
@@ -33,22 +70,43 @@
 
 void BuffetConfig::Load(const chromeos::KeyValueStore& store) {
   store.GetString(config_keys::kClientId, &client_id_);
+  CHECK(!client_id_.empty());
+
   store.GetString(config_keys::kClientSecret, &client_secret_);
+  CHECK(!client_secret_.empty());
+
   store.GetString(config_keys::kApiKey, &api_key_);
+  CHECK(!api_key_.empty());
+
   store.GetString(config_keys::kOAuthURL, &oauth_url_);
+  CHECK(!oauth_url_.empty());
+
   store.GetString(config_keys::kServiceURL, &service_url_);
-  store.GetString(config_keys::kDeviceKind, &device_kind_);
-  store.GetString(config_keys::kName, &name_);
-  store.GetString(config_keys::kDefaultDescription, &default_description_);
-  store.GetString(config_keys::kDefaultLocation, &default_location_);
+  CHECK(!service_url_.empty());
+
+  store.GetString(config_keys::kOemName, &oem_name_);
+  CHECK(!oem_name_.empty());
+
+  store.GetString(config_keys::kModelName, &model_name_);
+  CHECK(!model_name_.empty());
+
   store.GetString(config_keys::kModelId, &model_id_);
+  device_kind_ = GetDeviceKind(model_id_);
+
   std::string polling_period_str;
-  if (store.GetString(config_keys::kPollingPeriodMs, &polling_period_str)) {
-    CHECK(base::StringToUint64(polling_period_str, &polling_period_ms_))
-        << "Failed to parse polling period in configuration file.";
-    VLOG(1) << "Using a polling period of " << polling_period_ms_
-            << " milliseconds.";
-  }
+  if (store.GetString(config_keys::kPollingPeriodMs, &polling_period_str))
+    CHECK(base::StringToUint64(polling_period_str, &polling_period_ms_));
+
+  store.GetString(config_keys::kName, &name_);
+  CHECK(!name_.empty());
+
+  store.GetString(config_keys::kDescription, &description_);
+  store.GetString(config_keys::kLocation, &location_);
+}
+
+void BuffetConfig::set_name(const std::string& name) {
+  CHECK(!name.empty());
+  name_ = name;
 }
 
 }  // namespace buffet
diff --git a/buffet/buffet_config.h b/buffet/buffet_config.h
index af6c849..05545e0 100644
--- a/buffet/buffet_config.h
+++ b/buffet/buffet_config.h
@@ -19,29 +19,40 @@
   void Load(const base::FilePath& config_path);
   void Load(const chromeos::KeyValueStore& store);
 
-  std::string client_id() const { return client_id_; }
-  std::string client_secret() const { return client_secret_; }
-  std::string api_key() const { return api_key_; }
-  std::string oauth_url() const { return oauth_url_; }
-  std::string service_url() const { return service_url_; }
-  std::string device_kind() const { return device_kind_; }
-  std::string name() const { return name_; }
-  std::string default_description() const { return default_description_; }
-  std::string default_location() const { return default_location_; }
-  std::string model_id() const { return model_id_; }
+  const std::string& client_id() const { return client_id_; }
+  const std::string& client_secret() const { return client_secret_; }
+  const std::string& api_key() const { return api_key_; }
+  const std::string& oauth_url() const { return oauth_url_; }
+  const std::string& service_url() const { return service_url_; }
+  const std::string& oem_name() const { return oem_name_; }
+  const std::string& model_name() const { return model_name_; }
+  const std::string& model_id() const { return model_id_; }
+  const std::string& device_kind() const { return device_kind_; }
   uint64_t polling_period_ms() const { return polling_period_ms_; }
 
+  const std::string& name() const { return name_; }
+  const std::string& description() const { return description_; }
+  const std::string& location() const { return location_; }
+
+  void set_name(const std::string& name);
+  void set_description(const std::string& description) {
+    description_ = description;
+  }
+  void set_location(const std::string& location) { location_ = location; }
+
  private:
   std::string client_id_{"58855907228.apps.googleusercontent.com"};
   std::string client_secret_{"eHSAREAHrIqPsHBxCE9zPPBi"};
   std::string api_key_{"AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA"};
   std::string oauth_url_{"https://accounts.google.com/o/oauth2/"};
   std::string service_url_{"https://www.googleapis.com/clouddevices/v1/"};
-  std::string device_kind_{"vendor"};
   std::string name_{"Developer device"};
-  std::string default_description_{"A development device"};
-  std::string default_location_{"my desk"};
-  std::string model_id_{"AAA"};
+  std::string description_;
+  std::string location_;
+  std::string oem_name_{"Chromium"};
+  std::string model_name_{"Brillo"};
+  std::string model_id_{"AAAAA"};
+  std::string device_kind_{"AA"};
   uint64_t polling_period_ms_{7000};
 
   DISALLOW_COPY_AND_ASSIGN(BuffetConfig);
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index 9cf3579..ff8db89 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -123,7 +123,7 @@
 DeviceRegistrationInfo::DeviceRegistrationInfo(
     const std::shared_ptr<CommandManager>& command_manager,
     const std::shared_ptr<StateManager>& state_manager,
-    std::unique_ptr<const BuffetConfig> config,
+    std::unique_ptr<BuffetConfig> config,
     const std::shared_ptr<chromeos::http::Transport>& transport,
     const std::shared_ptr<StorageInterface>& state_store,
     bool xmpp_enabled,
@@ -178,29 +178,25 @@
   if (!value || !value->GetAsDictionary(&dict))
     return false;
 
-  // Get the values into temp variables first to make sure we can get
-  // all the data correctly before changing the state of this object.
-  std::string refresh_token;
-  std::string device_id;
-  std::string device_robot_account;
+  // Read all available data before failing.
   std::string name;
-  std::string description;
-  std::string location;
-  if (!dict->GetString(storage_keys::kRefreshToken, &refresh_token) ||
-      !dict->GetString(storage_keys::kDeviceId, &device_id) ||
-      !dict->GetString(storage_keys::kRobotAccount, &device_robot_account) ||
-      !dict->GetString(storage_keys::kName, &name) ||
-      !dict->GetString(storage_keys::kDescription, &description) ||
-      !dict->GetString(storage_keys::kLocation, &location)) {
-    return false;
-  }
-  refresh_token_        = refresh_token;
-  device_robot_account_ = device_robot_account;
-  name_                 = name;
-  description_          = description;
-  location_             = location;
+  if (dict->GetString(storage_keys::kName, &name) && !name.empty())
+    config_->set_name(name);
 
-  SetDeviceId(device_id);
+  std::string description;
+  if (dict->GetString(storage_keys::kDescription, &description))
+    config_->set_description(description);
+
+  std::string location;
+  if (dict->GetString(storage_keys::kLocation, &location))
+    config_->set_location(location);
+
+  dict->GetString(storage_keys::kRefreshToken, &refresh_token_);
+  dict->GetString(storage_keys::kRobotAccount, &device_robot_account_);
+
+  std::string device_id;
+  if (dict->GetString(storage_keys::kDeviceId, &device_id))
+    SetDeviceId(device_id);
 
   if (HaveRegistrationCredentials(nullptr)) {
     // Wait a significant amount of time for local daemons to publish their
@@ -211,7 +207,6 @@
     //             we need not wait for them.
     ScheduleStartDevice(base::TimeDelta::FromSeconds(5));
   }
-
   return true;
 }
 
@@ -220,9 +215,9 @@
   dict.SetString(storage_keys::kRefreshToken, refresh_token_);
   dict.SetString(storage_keys::kDeviceId,     device_id_);
   dict.SetString(storage_keys::kRobotAccount, device_robot_account_);
-  dict.SetString(storage_keys::kName,         name_);
-  dict.SetString(storage_keys::kDescription,  description_);
-  dict.SetString(storage_keys::kLocation,     location_);
+  dict.SetString(storage_keys::kName, config_->name());
+  dict.SetString(storage_keys::kDescription, config_->description());
+  dict.SetString(storage_keys::kLocation, config_->location());
 
   return storage_->Save(&dict);
 }
@@ -400,14 +395,13 @@
   std::unique_ptr<base::DictionaryValue> resource{new base::DictionaryValue};
   if (!device_id_.empty())
     resource->SetString("id", device_id_);
+  resource->SetString("name", config_->name());
+  if (!config_->description().empty())
+    resource->SetString("description", config_->description());
+  if (!config_->location().empty())
+    resource->SetString("location", config_->location());
+  resource->SetString("modelManifestId", config_->model_id());
   resource->SetString("deviceKind", config_->device_kind());
-  resource->SetString("name", name_.empty() ? config_->name() : name_);
-  if (!description_.empty())
-    resource->SetString("description", description_);
-  if (!location_.empty())
-    resource->SetString("location", location_);
-  if (!config_->model_id().empty())
-    resource->SetString("modelManifestId", config_->model_id());
   resource->SetString("channel.supportedType", "xmpp");
   resource->Set("commandDefs", commands.release());
   resource->Set("state", state.release());
@@ -466,11 +460,20 @@
   }
   // These fields are optional, and will default to values from the manufacturer
   // supplied config.
-  GetWithDefault(params, storage_keys::kName, config_->name(), &name_);
-  GetWithDefault(params, storage_keys::kDescription,
-                 config_->default_description(), &description_);
-  GetWithDefault(params, storage_keys::kLocation,
-                 config_->default_location(), &location_);
+  std::string name;
+  GetWithDefault(params, storage_keys::kName, config_->name(), &name);
+  if (!name.empty())
+    config_->set_name(name);
+
+  std::string description;
+  GetWithDefault(params, storage_keys::kDescription, config_->description(),
+                 &description);
+  config_->set_description(description);
+
+  std::string location;
+  GetWithDefault(params, storage_keys::kLocation, config_->location(),
+                 &location);
+  config_->set_location(location);
 
   std::unique_ptr<base::DictionaryValue> device_draft =
       BuildDeviceResource(error);
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h
index 1f4c05e..11b8e37 100644
--- a/buffet/device_registration_info.h
+++ b/buffet/device_registration_info.h
@@ -51,7 +51,7 @@
   DeviceRegistrationInfo(
       const std::shared_ptr<CommandManager>& command_manager,
       const std::shared_ptr<StateManager>& state_manager,
-      std::unique_ptr<const BuffetConfig> config,
+      std::unique_ptr<BuffetConfig> config,
       const std::shared_ptr<chromeos::http::Transport>& transport,
       const std::shared_ptr<StorageInterface>& state_store,
       bool xmpp_enabled,
@@ -215,11 +215,6 @@
   std::string device_id_;
   std::string device_robot_account_;
 
-  // These fields are user settable and stored in the state store.
-  std::string name_;
-  std::string description_;
-  std::string location_;
-
   // Transient data
   std::string access_token_;
   base::Time access_token_expiration_;
@@ -233,7 +228,7 @@
   // Device state manager.
   std::shared_ptr<StateManager> state_manager_;
 
-  std::unique_ptr<const BuffetConfig> config_;
+  std::unique_ptr<BuffetConfig> config_;
 
   const bool xmpp_enabled_;
   std::unique_ptr<XmppClient> xmpp_client_;
diff --git a/buffet/device_registration_info_unittest.cc b/buffet/device_registration_info_unittest.cc
index c800052..2f25d7a 100644
--- a/buffet/device_registration_info_unittest.cc
+++ b/buffet/device_registration_info_unittest.cc
@@ -58,16 +58,6 @@
 
 }  // namespace test_data
 
-// Fill in the storage with default environment information (URLs, etc).
-void InitDefaultStorage(base::DictionaryValue* data) {
-  data->SetString(storage_keys::kRefreshToken, "");
-  data->SetString(storage_keys::kDeviceId, "");
-  data->SetString(storage_keys::kRobotAccount, "");
-  data->SetString(storage_keys::kName, "");
-  data->SetString(storage_keys::kDescription, "");
-  data->SetString(storage_keys::kLocation, "");
-}
-
 // Add the test device registration information.
 void SetDefaultDeviceRegistration(base::DictionaryValue* data) {
   data->SetString(storage_keys::kRefreshToken, test_data::kRefreshToken);
@@ -189,7 +179,6 @@
 class DeviceRegistrationInfoTest : public ::testing::Test {
  protected:
   void SetUp() override {
-    InitDefaultStorage(&data_);
     storage_ = std::make_shared<MemStorage>();
     storage_->Save(&data_);
     transport_ = std::make_shared<chromeos::http::fake::Transport>();
@@ -201,9 +190,9 @@
     config_store.SetString("api_key", test_data::kApiKey);
     config_store.SetString("device_kind",  "vendor");
     config_store.SetString("name",  "Coffee Pot");
-    config_store.SetString("default_description",  "Easy to clean");
-    config_store.SetString("default_location",  "Kitchen");
-    config_store.SetString("model_id", "AAA");
+    config_store.SetString("description", "Easy to clean");
+    config_store.SetString("location", "Kitchen");
+    config_store.SetString("model_id", "AAAAA");
     config_store.SetString("oauth_url", test_data::kOAuthURL);
     config_store.SetString("service_url", test_data::kServiceURL);
     std::unique_ptr<BuffetConfig> config{new BuffetConfig};
@@ -402,7 +391,7 @@
     EXPECT_TRUE(json->GetString("deviceDraft.location", &value));
     EXPECT_EQ("Kitchen", value);
     EXPECT_TRUE(json->GetString("deviceDraft.modelManifestId", &value));
-    EXPECT_EQ("AAA", value);
+    EXPECT_EQ("AAAAA", value);
     EXPECT_TRUE(json->GetString("deviceDraft.name", &value));
     EXPECT_EQ("Coffee Pot", value);
     base::DictionaryValue* commandDefs = nullptr;