Large updates to webserver

Do not merge yet

Change-Id: I38c56844c1b0e3e8e5493c2705e62e6db7ee2102
diff --git a/include/redfish_v1.hpp b/include/redfish_v1.hpp
index c302a68..73ed84e 100644
--- a/include/redfish_v1.hpp
+++ b/include/redfish_v1.hpp
@@ -1,9 +1,24 @@
+#pragma once
+
 #include <crow/app.h>
+
+#include <dbus/connection.hpp>
+#include <dbus/endpoint.hpp>
+#include <dbus/filter.hpp>
+#include <dbus/match.hpp>
+#include <dbus/message.hpp>
+#include <fstream>
+
 namespace crow {
 namespace redfish {
 
 template <typename... Middlewares>
 void request_routes(Crow<Middlewares...>& app) {
+  
+  // noop for now
+  return;
+
+
   CROW_ROUTE(app, "/redfish/").methods("GET"_method)([]() {
     return nlohmann::json{{"v1", "/redfish/v1/"}};
   });
@@ -28,6 +43,28 @@
     };
   });
 
+  CROW_ROUTE(app, "/redfish/v1/Chassis").methods("GET"_method)([]() {
+    std::vector<std::string> entities;
+    std::ifstream f("~/system.json");
+    nlohmann::json input;
+    input << f;
+    for (auto it = input.begin(); it != input.end(); it++) {
+      auto value = it.value();
+      if (value["type"] == "Chassis") {
+         std::string str = value["name"];
+         entities.emplace_back(str);
+      }
+    }
+    auto ret = nlohmann::json{
+        {"@odata.context",
+         "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"},
+        {"@odata.id", "/redfish/v1/Chassis"},
+        {"@odata.type", "#ChassisCollection.ChassisCollection"},
+        {"Name", "Chassis Collection"},
+        {"Members@odata.count", entities.size()}};
+    return ret;
+  });
+
   CROW_ROUTE(app, "/redfish/v1/AccountService").methods("GET"_method)([]() {
     return nlohmann::json{
         {"@odata.context",
@@ -38,6 +75,7 @@
         {"Name", "Account Service"},
         {"Description", "BMC User Accounts"},
         {"Status",
+         // TODO(ed) health rollup
          {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
         {"ServiceEnabled", true},
         {"MinPasswordLength", 1},
@@ -47,22 +85,41 @@
     };
   });
 
-  CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
-      .methods("GET"_method)([]() {
-        return nlohmann::json{
-            {"@odata.context",
-             "/redfish/v1/"
-             "$metadata#ManagerAccountCollection.ManagerAccountCollection"},
-            {"@odata.id", "/redfish/v1/AccountService/Accounts"},
-            {"@odata.type",
-             "#ManagerAccountCollection.ManagerAccountCollection"},
-            {"Name", "Accounts Collection"},
-            {"Description", "BMC User Accounts"},
-            {"Members@odata.count", 3},
-            {"Members",
-             {{{"@odata.id", "/redfish/v1/AccountService/Accounts/1"}},
-              {{"@odata.id", "/redfish/v1/AccountService/Accounts/2"}},
-              {{"@odata.id", "/redfish/v1/AccountService/Accounts/3"}}}}};
+  CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts")
+      .methods("GET"_method)([](const crow::request& req, crow::response& res) {
+        boost::asio::io_service io;
+        auto bus = std::make_shared<dbus::connection>(io, dbus::bus::session);
+        dbus::endpoint user_list("org.openbmc.UserManager",
+                                 "/org/openbmc/UserManager/Users",
+                                 "org.openbmc.Enrol", "UserList");
+        bus->async_method_call(
+            [&](const boost::system::error_code ec,
+                           const std::vector<std::string>& users) {
+              if (ec) {
+                res.code = 500;
+              } else {
+                nlohmann::json return_json{
+                    {"@odata.context",
+                     "/redfish/v1/"
+                     "$metadata#ManagerAccountCollection."
+                     "ManagerAccountCollection"},
+                    {"@odata.id", "/redfish/v1/AccountService/Accounts"},
+                    {"@odata.type",
+                     "#ManagerAccountCollection.ManagerAccountCollection"},
+                    {"Name", "Accounts Collection"},
+                    {"Description", "BMC User Accounts"},
+                    {"Members@odata.count", users.size()}};
+                nlohmann::json member_array;
+                int user_index = 0;
+                for (auto& user : users) {
+                  member_array.push_back(
+                      {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
+                                         std::to_string(++user_index)}});
+                }
+                return_json["Members"] = member_array;
+              }
+              res.end();
+            }, user_list);
       });
 
   CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<int>/")
@@ -84,5 +141,5 @@
                {{"@odata.id", "/redfish/v1/AccountService/Roles/NoAccess"}}}}}};
       });
 }
-}
-}
\ No newline at end of file
+}  // namespace redfish
+}  // namespace crow