version-handler: add version-handler, blob handler

Implement version-handler: a blob handler for retrieving version
information about a blob.

Problem: ipmi-flash(firmware-handler) provides a mechanism to transfer
firmware updates, verify them and perform updates but there is no
mechanism to interrogate the firmware for its version.

Solution: version-handler provides handlers for retrieving information
about firmware blobs. Adding "version" syntax to "/flash/blob" entries
in the json configuration file enables this feature.
The mechanism to retrieve version is identical to the mechanism used by
firmware-handler to perform preparation, verification and updates (kick
off systemd targets).

Signed-off-by: Jason Ling <jasonling@google.com>
Change-Id: I28868ca8dd76d63af668d2e46b9359401d45f0bc
diff --git a/bmc/version-handler/main.cpp b/bmc/version-handler/main.cpp
new file mode 100644
index 0000000..aae27f7
--- /dev/null
+++ b/bmc/version-handler/main.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "config.h"
+
+#include "file_handler.hpp"
+#include "general_systemd.hpp"
+#include "image_handler.hpp"
+#include "status.hpp"
+#include "version_handler.hpp"
+#include "version_handlers_builder.hpp"
+
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+namespace ipmi_flash
+{
+
+namespace
+{
+
+static constexpr const char* jsonConfigurationPath =
+    "/usr/share/phosphor-ipmi-flash/";
+} // namespace
+
+std::unique_ptr<blobs::GenericBlobInterface>
+    createHandlerFromJsons(VersionHandlersBuilder& builder,
+                           const char* configPath)
+{
+    std::vector<HandlerConfig<VersionActionPack>> configsFromJson =
+        builder.buildHandlerConfigs(configPath);
+
+    VersionInfoMap handlerMap;
+    for (auto& config : configsFromJson)
+    {
+        auto [it, inserted] = handlerMap.try_emplace(
+            config.blobId, config.blobId, std::move(config.actions),
+            std::move(config.handler));
+        if (inserted == false)
+        {
+            std::fprintf(stderr, "duplicate blob id %s, discarding\n",
+                         config.blobId.c_str());
+        }
+        else
+        {
+            std::fprintf(stderr, "config loaded: %s\n", config.blobId.c_str());
+        }
+    }
+    auto handler = VersionBlobHandler::create(std::move(handlerMap));
+    if (!handler)
+    {
+        std::fprintf(stderr, "Version Handler has an invalid configuration");
+        return nullptr;
+    }
+
+    return handler;
+}
+} // namespace ipmi_flash
+extern "C"
+{
+    std::unique_ptr<blobs::GenericBlobInterface> createHandler();
+}
+
+std::unique_ptr<blobs::GenericBlobInterface> createHandler()
+{
+    ipmi_flash::VersionHandlersBuilder builder;
+    return ipmi_flash::createHandlerFromJsons(
+        builder, ipmi_flash::jsonConfigurationPath);
+}