Create cerificate object during startup for existing certificate

During service start check if certificate file already exist if so
load the certificate file, validate and create certificate object

Change-Id: If0d62cc52fa34b8992b63fc49ed8014280b3e469
Signed-off-by: Marri Devender Rao <devenrao@in.ibm.com>
diff --git a/certs_manager.cpp b/certs_manager.cpp
index dbc1e37..7170605 100644
--- a/certs_manager.cpp
+++ b/certs_manager.cpp
@@ -3,14 +3,14 @@
 #include <phosphor-logging/elog-errors.hpp>
 #include <xyz/openbmc_project/Certs/Install/error.hpp>
 #include <xyz/openbmc_project/Common/error.hpp>
-
 namespace phosphor
 {
 namespace certs
 {
 
-using InternalFailure =
-    sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+using InvalidCertificate =
+    sdbusplus::xyz::openbmc_project::Certs::Install::Error::InvalidCertificate;
 using Reason = xyz::openbmc_project::Certs::Install::InvalidCertificate::REASON;
 
 /** @brief Constructor to put object onto bus at a dbus path.
@@ -27,6 +27,30 @@
     bus(bus), objectPath(path), certType(type), unitToRestart(std::move(unit)),
     certInstallPath(std::move(installPath))
 {
+    if (fs::exists(certInstallPath))
+    {
+        try
+        {
+            // TODO: Issue#3 At present supporting only one certificate to be
+            // uploaded this need to be revisited to support multiple
+            // certificates
+            auto certObjectPath = objectPath + '/' + '1';
+            certificatePtr = std::make_unique<Certificate>(
+                bus, certObjectPath, certType, unitToRestart, certInstallPath,
+                certInstallPath);
+        }
+        catch (const InternalFailure& e)
+        {
+            certificatePtr.reset(nullptr);
+            report<InternalFailure>();
+        }
+        catch (const InvalidCertificate& e)
+        {
+            certificatePtr.reset(nullptr);
+            report<InvalidCertificate>(
+                Reason("Existing certificate file is corrupted"));
+        }
+    }
 }
 
 void Manager::install(const std::string filePath)