Add length limit for user-defined values

Previously, the only limit for most user-defined values like id or name
was the one enforced by dbus, which was fairly big. In order to save
space used by persistent data, new meson options were added, with length
limit for those fields.

This directly impacts following dbus interfaces:
- TriggerManager.AddTrigger:
  - Id
  - Name
  - Reports
  - Thresholds (only name of discrete threshold)
- Trigger.Name
- Trigger.Reports
- Trigger.Thresholds (only name of discrete threshold)
- ReportManager.AddReport(FutureVersion):
  - Id
  - Name
  - MetricParameters (metricId)
- Report.Name
- Report.ReadingParameters (metricId)

For Id fields we support 'prefixes', which also are limited, but those
limit are separate. So if limit for prefix is set to 5 and limit for
id/name is set to 5, following Ids are fine:
- 12345/12345
- 12345
and following are not:
- 123456/1234
- 1/123456

Testing done:
- UTs are passing.
- new limits are reflected when calling mentioned dbus interfaces.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I29291a1cc56a344d92fb65491c9186fdb90a8529
diff --git a/src/interfaces/report_factory.hpp b/src/interfaces/report_factory.hpp
index d8e076a..9448598 100644
--- a/src/interfaces/report_factory.hpp
+++ b/src/interfaces/report_factory.hpp
@@ -30,10 +30,10 @@
     virtual std::vector<LabeledMetricParameters>
         convertMetricParams(const ReadingParameters& metricParams) const = 0;
 
-    virtual void
-        updateMetrics(std::vector<std::shared_ptr<interfaces::Metric>>& metrics,
-                      bool enabled,
-                      const ReadingParameters& metricParams) const = 0;
+    virtual void updateMetrics(
+        std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled,
+        const std::vector<LabeledMetricParameters>& labeledMetricParams)
+        const = 0;
 
     virtual std::unique_ptr<interfaces::Report>
         make(const std::string& id, const std::string& name,
diff --git a/src/report.cpp b/src/report.cpp
index cf2b389..9ee60a4 100644
--- a/src/report.cpp
+++ b/src/report.cpp
@@ -331,8 +331,12 @@
         "ReadingParametersFutureVersion", readingParameters,
         sdbusplus::vtable::property_::emits_change,
         [this, &reportFactory](auto newVal, auto& oldVal) {
-            reportFactory.updateMetrics(
-                metrics, state.get<ReportFlags::enabled>(), newVal);
+            auto labeledMetricParams =
+                reportFactory.convertMetricParams(newVal);
+            ReportManager::verifyMetricParameters(labeledMetricParams);
+            reportFactory.updateMetrics(metrics,
+                                        state.get<ReportFlags::enabled>(),
+                                        labeledMetricParams);
             readingParameters = toReadingParameters(
                 utils::transform(metrics, [](const auto& metric) {
                     return metric->dumpConfiguration();
diff --git a/src/report_factory.cpp b/src/report_factory.cpp
index 1c530f3..cb096cf 100644
--- a/src/report_factory.cpp
+++ b/src/report_factory.cpp
@@ -49,9 +49,8 @@
 
 void ReportFactory::updateMetrics(
     std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled,
-    const ReadingParameters& metricParams) const
+    const std::vector<LabeledMetricParameters>& labeledMetricParams) const
 {
-    auto labeledMetricParams = convertMetricParams(metricParams);
     std::vector<std::shared_ptr<interfaces::Metric>> oldMetrics = metrics;
     std::vector<std::shared_ptr<interfaces::Metric>> newMetrics;
 
diff --git a/src/report_factory.hpp b/src/report_factory.hpp
index e8729b1..f49be6d 100644
--- a/src/report_factory.hpp
+++ b/src/report_factory.hpp
@@ -24,10 +24,10 @@
     std::vector<LabeledMetricParameters> convertMetricParams(
         const ReadingParameters& metricParams) const override;
 
-    void
-        updateMetrics(std::vector<std::shared_ptr<interfaces::Metric>>& metrics,
-                      bool enabled,
-                      const ReadingParameters& metricParams) const override;
+    void updateMetrics(
+        std::vector<std::shared_ptr<interfaces::Metric>>& metrics, bool enabled,
+        const std::vector<LabeledMetricParameters>& labeledMetricParams)
+        const override;
 
     std::unique_ptr<interfaces::Report>
         make(const std::string& reportId, const std::string& name,
diff --git a/src/report_manager.cpp b/src/report_manager.cpp
index 738dd6d..1677bb3 100644
--- a/src/report_manager.cpp
+++ b/src/report_manager.cpp
@@ -3,7 +3,8 @@
 #include "report.hpp"
 #include "types/report_types.hpp"
 #include "utils/conversion.hpp"
-#include "utils/generate_id.hpp"
+#include "utils/dbus_path_utils.hpp"
+#include "utils/make_id_name.hpp"
 #include "utils/transform.hpp"
 
 #include <phosphor-logging/log.hpp>
@@ -46,10 +47,6 @@
                 "MaxReports", size_t{}, sdbusplus::vtable::property_::const_,
                 [](const auto&) { return maxReports; });
             dbusIface.register_property_r(
-                "MaxReportIdLength", size_t{},
-                sdbusplus::vtable::property_::const_,
-                [](const auto&) { return maxReportIdLength; });
-            dbusIface.register_property_r(
                 "MinInterval", uint64_t{}, sdbusplus::vtable::property_::const_,
                 [](const auto&) -> uint64_t { return minInterval.count(); });
             dbusIface.register_property_r(
@@ -133,12 +130,31 @@
         reports.end());
 }
 
+void ReportManager::verifyMetricParameters(
+    const std::vector<LabeledMetricParameters>& readingParams)
+{
+    namespace ts = utils::tstring;
+
+    for (auto readingParam : readingParams)
+    {
+        if (readingParam.at_label<ts::Id>().length() >
+            utils::constants::maxIdNameLength)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "MetricId too long");
+        }
+    }
+}
+
 void ReportManager::verifyAddReport(
     const std::string& reportId, const std::string& reportName,
     const ReportingType reportingType, Milliseconds interval,
     const ReportUpdates reportUpdates, const uint64_t appendLimit,
     const std::vector<LabeledMetricParameters>& readingParams)
 {
+    namespace ts = utils::tstring;
+
     if (reports.size() >= maxReports)
     {
         throw sdbusplus::exception::SdBusError(
@@ -178,10 +194,10 @@
             "Too many reading parameters");
     }
 
+    verifyMetricParameters(readingParams);
+
     try
     {
-        namespace ts = utils::tstring;
-
         for (const LabeledMetricParameters& item : readingParams)
         {
             utils::toOperationType(
@@ -221,8 +237,8 @@
     const auto existingReportIds = utils::transform(
         reports, [](const auto& report) { return report->getId(); });
 
-    auto [id, name] = utils::generateId(reportId, reportName, reportNameDefault,
-                                        existingReportIds, maxReportIdLength);
+    auto [id, name] = utils::makeIdName(reportId, reportName, reportNameDefault,
+                                        existingReportIds);
 
     verifyAddReport(id, name, reportingType, interval, reportUpdates,
                     appendLimit, labeledMetricParams);
diff --git a/src/report_manager.hpp b/src/report_manager.hpp
index 88dc3e0..4882419 100644
--- a/src/report_manager.hpp
+++ b/src/report_manager.hpp
@@ -33,6 +33,9 @@
 
     void removeReport(const interfaces::Report* report) override;
 
+    static void verifyMetricParameters(
+        const std::vector<LabeledMetricParameters>& readingParams);
+
   private:
     std::unique_ptr<interfaces::ReportFactory> reportFactory;
     std::unique_ptr<interfaces::JsonStorage> reportStorage;
@@ -64,14 +67,14 @@
   public:
     static constexpr size_t maxReports{TELEMETRY_MAX_REPORTS};
     static constexpr size_t maxNumberMetrics{TELEMETRY_MAX_READING_PARAMS};
-    static constexpr size_t maxReportIdLength{
-        TELEMETRY_MAX_DBUS_PATH_LENGTH -
-        utils::constants::reportDirStr.length()};
     static constexpr Milliseconds minInterval{TELEMETRY_MIN_INTERVAL};
     static constexpr size_t maxAppendLimit{TELEMETRY_MAX_APPEND_LIMIT};
     static constexpr const char* reportManagerIfaceName =
         "xyz.openbmc_project.Telemetry.ReportManager";
     static constexpr const char* reportManagerPath =
         "/xyz/openbmc_project/Telemetry/Reports";
-    static constexpr const char* reportNameDefault = "Report";
+    static constexpr std::string_view reportNameDefault = "Report";
+
+    static_assert(!reportNameDefault.empty(),
+                  "Default report name cannot be empty.");
 };
diff --git a/src/trigger.cpp b/src/trigger.cpp
index c0935c3..c11ea42 100644
--- a/src/trigger.cpp
+++ b/src/trigger.cpp
@@ -75,6 +75,7 @@
                 [this, &triggerFactory](auto newVal, auto& oldVal) {
                     auto newThresholdParams = std::visit(
                         utils::ToLabeledThresholdParamConversion(), newVal);
+                    TriggerManager::verifyThresholdParams(newThresholdParams);
                     triggerFactory.updateThresholds(
                         thresholds, *id, triggerActions, reportIds, sensors,
                         newThresholdParams);
@@ -134,6 +135,12 @@
             dbusIface.register_property_rw(
                 "Name", name, sdbusplus::vtable::property_::emits_change,
                 [this](auto newVal, auto& oldVal) {
+                    if (newVal.length() > utils::constants::maxIdNameLength)
+                    {
+                        throw sdbusplus::exception::SdBusError(
+                            static_cast<int>(std::errc::invalid_argument),
+                            "Name is too long");
+                    }
                     name = oldVal = newVal;
                     return 1;
                 },
diff --git a/src/trigger_manager.cpp b/src/trigger_manager.cpp
index df8dea9..e337bca 100644
--- a/src/trigger_manager.cpp
+++ b/src/trigger_manager.cpp
@@ -4,8 +4,9 @@
 #include "types/trigger_types.hpp"
 #include "utils/conversion_trigger.hpp"
 #include "utils/dbus_path_utils.hpp"
-#include "utils/generate_id.hpp"
+#include "utils/make_id_name.hpp"
 #include "utils/transform.hpp"
+#include "utils/tstring.hpp"
 
 #include <phosphor-logging/log.hpp>
 
@@ -72,9 +73,32 @@
     }
 }
 
+void TriggerManager::verifyThresholdParams(
+    const LabeledTriggerThresholdParams& thresholdParams)
+{
+    namespace ts = utils::tstring;
+
+    if (auto discreteParams =
+            std::get_if<std::vector<discrete::LabeledThresholdParam>>(
+                &thresholdParams);
+        discreteParams != nullptr)
+    {
+        for (auto discreteParam : *discreteParams)
+        {
+            if (discreteParam.at_label<ts::UserId>().length() >
+                utils::constants::maxIdNameLength)
+            {
+                throw sdbusplus::exception::SdBusError(
+                    static_cast<int>(std::errc::invalid_argument),
+                    "UserId too long");
+            }
+        }
+    }
+}
+
 void TriggerManager::verifyAddTrigger(
-    const std::string& triggerId, const std::string& triggerName,
-    const std::vector<std::string>& newReportIds) const
+    const std::vector<std::string>& reportIds,
+    const LabeledTriggerThresholdParams& thresholdParams) const
 {
     if (triggers.size() >= maxTriggers)
     {
@@ -83,18 +107,8 @@
             "Reached maximal trigger count");
     }
 
-    utils::verifyIdCharacters(triggerId);
-
-    for (const auto& trigger : triggers)
-    {
-        if (trigger->getId() == triggerId)
-        {
-            throw sdbusplus::exception::SdBusError(
-                static_cast<int>(std::errc::file_exists), "Duplicate trigger");
-        }
-    }
-
-    verifyReportIds(newReportIds);
+    verifyReportIds(reportIds);
+    verifyThresholdParams(thresholdParams);
 }
 
 interfaces::Trigger& TriggerManager::addTrigger(
@@ -107,11 +121,10 @@
     const auto existingTriggerIds = utils::transform(
         triggers, [](const auto& trigger) { return trigger->getId(); });
 
-    auto [id, name] =
-        utils::generateId(triggerIdIn, triggerNameIn, triggerNameDefault,
-                          existingTriggerIds, maxTriggerIdLength);
+    auto [id, name] = utils::makeIdName(triggerIdIn, triggerNameIn,
+                                        triggerNameDefault, existingTriggerIds);
 
-    verifyAddTrigger(id, name, reportIds);
+    verifyAddTrigger(reportIds, labeledThresholdParams);
 
     triggers.emplace_back(triggerFactory->make(
         id, name, triggerActions, reportIds, *this, *triggerStorage,
diff --git a/src/trigger_manager.hpp b/src/trigger_manager.hpp
index 4db6941..65ee2af 100644
--- a/src/trigger_manager.hpp
+++ b/src/trigger_manager.hpp
@@ -27,6 +27,8 @@
     void removeTrigger(const interfaces::Trigger* trigger) override;
 
     static void verifyReportIds(const std::vector<std::string>& newReportIds);
+    static void verifyThresholdParams(
+        const LabeledTriggerThresholdParams& thresholdParams);
 
   private:
     std::unique_ptr<interfaces::TriggerFactory> triggerFactory;
@@ -34,9 +36,9 @@
     std::unique_ptr<sdbusplus::asio::dbus_interface> managerIface;
     std::vector<std::unique_ptr<interfaces::Trigger>> triggers;
 
-    void verifyAddTrigger(const std::string& triggerId,
-                          const std::string& triggerName,
-                          const std::vector<std::string>& newReportIds) const;
+    void verifyAddTrigger(
+        const std::vector<std::string>& reportIds,
+        const LabeledTriggerThresholdParams& thresholdParams) const;
 
     interfaces::Trigger&
         addTrigger(const std::string& triggerId, const std::string& triggerName,
@@ -48,12 +50,12 @@
 
   public:
     static constexpr size_t maxTriggers{TELEMETRY_MAX_TRIGGERS};
-    static constexpr size_t maxTriggerIdLength{
-        TELEMETRY_MAX_DBUS_PATH_LENGTH -
-        utils::constants::triggerDirStr.length()};
     static constexpr const char* triggerManagerIfaceName =
         "xyz.openbmc_project.Telemetry.TriggerManager";
     static constexpr const char* triggerManagerPath =
         "/xyz/openbmc_project/Telemetry/Triggers";
-    static constexpr const char* triggerNameDefault = "Trigger";
+    static constexpr std::string_view triggerNameDefault = "Trigger";
+
+    static_assert(!triggerNameDefault.empty(),
+                  "Default trigger name cannot be empty.");
 };
diff --git a/src/utils/dbus_path_utils.cpp b/src/utils/dbus_path_utils.cpp
index 9d37cfc..5c1fddb 100644
--- a/src/utils/dbus_path_utils.cpp
+++ b/src/utils/dbus_path_utils.cpp
@@ -35,16 +35,48 @@
     if (path.str.starts_with(constants::reportDirStr))
     {
         auto id = path.str.substr(constants::reportDirStr.length());
-        if (std::cmp_greater(std::count(id.begin(), id.end(), '/'),
-                             constants::maxPrefixesInId))
-        {
-            throw sdbusplus::exception::SdBusError(
-                static_cast<int>(std::errc::invalid_argument),
-                "Too many prefixes in id");
-        }
+        verifyIdPrefixes(id);
         return id;
     }
     throw sdbusplus::exception::SdBusError(
         static_cast<int>(std::errc::invalid_argument), "Invalid path prefix");
 }
+
+void verifyIdPrefixes(std::string_view id)
+{
+    size_t pos_start = 0;
+    size_t pos_end = 0;
+    size_t prefix_cnt = 0;
+    while ((pos_end = id.find('/', pos_start)) != std::string::npos)
+    {
+        if (pos_start == pos_end)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "Invalid prefixes in id");
+        }
+
+        if (++prefix_cnt > constants::maxPrefixesInId)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "Too many prefixes");
+        }
+
+        if (pos_end - pos_start > constants::maxPrefixLength)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::invalid_argument),
+                "Prefix too long");
+        }
+
+        pos_start = pos_end + 1;
+    }
+
+    if (id.length() - pos_start > constants::maxIdNameLength)
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument), "Id too long");
+    }
+}
 } // namespace utils
diff --git a/src/utils/dbus_path_utils.hpp b/src/utils/dbus_path_utils.hpp
index a1b5709..3798d98 100644
--- a/src/utils/dbus_path_utils.hpp
+++ b/src/utils/dbus_path_utils.hpp
@@ -19,6 +19,21 @@
 constexpr std::string_view allowedCharactersInPath =
     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/";
 constexpr size_t maxPrefixesInId = 1;
+constexpr size_t maxPrefixLength{TELEMETRY_MAX_PREFIX_LENGTH};
+constexpr size_t maxIdNameLength{TELEMETRY_MAX_ID_NAME_LENGTH};
+constexpr size_t maxDbusPathLength{TELEMETRY_MAX_DBUS_PATH_LENGTH};
+
+constexpr size_t maxTriggeFullIdLength{maxDbusPathLength -
+                                       triggerDirStr.length()};
+constexpr size_t maxReportFullIdLength{maxDbusPathLength -
+                                       reportDirStr.length()};
+
+static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <=
+                  maxTriggeFullIdLength,
+              "Misconfigured prefix/id/name lengths.");
+static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <=
+                  maxReportFullIdLength,
+              "Misconfigured prefix/id/name lengths.");
 
 const sdbusplus::message::object_path triggerDirPath =
     sdbusplus::message::object_path(std::string(triggerDirStr));
@@ -33,9 +48,22 @@
            !path.ends_with('/');
 }
 
+inline void verifyIdCharacters(std::string_view id)
+{
+    if (id.find_first_not_of(utils::constants::allowedCharactersInPath) !=
+        std::string::npos)
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument),
+            "Invalid character in id");
+    }
+}
+
 sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path,
                                            const std::string& appended);
 
 std::string reportPathToId(const sdbusplus::message::object_path& path);
 
+void verifyIdPrefixes(std::string_view id);
+
 } // namespace utils
diff --git a/src/utils/generate_id.cpp b/src/utils/generate_id.cpp
deleted file mode 100644
index 42c9155..0000000
--- a/src/utils/generate_id.cpp
+++ /dev/null
@@ -1,135 +0,0 @@
-#include "utils/generate_id.hpp"
-
-#include "utils/dbus_path_utils.hpp"
-
-#include <sdbusplus/exception.hpp>
-
-#include <algorithm>
-#include <system_error>
-
-namespace utils
-{
-namespace details
-{
-
-size_t countDigits(size_t value)
-{
-    size_t result = 1;
-    while (value >= 10)
-    {
-        ++result;
-        value /= 10;
-    }
-    return result;
-}
-
-std::string generateId(std::string_view id, std::string_view name,
-                       const std::vector<std::string>& conflictIds,
-                       size_t maxLength)
-{
-    verifyIdCharacters(id);
-
-    if (id.starts_with('/'))
-    {
-        id = id.substr(1);
-    }
-
-    if ((id.length() > maxLength) ||
-        (id.ends_with('/') && id.length() >= maxLength))
-    {
-        throw sdbusplus::exception::SdBusError(
-            static_cast<int>(std::errc::invalid_argument), "Id too long");
-    }
-
-    if (!id.empty() && !id.ends_with('/'))
-    {
-        if (std::find(conflictIds.begin(), conflictIds.end(), id) !=
-            conflictIds.end())
-        {
-            throw sdbusplus::exception::SdBusError(
-                static_cast<int>(std::errc::file_exists), "Duplicated id");
-        }
-        return std::string(id);
-    }
-
-    std::string strippedId(name);
-    strippedId.erase(
-        std::remove_if(
-            strippedId.begin(), strippedId.end(),
-            [](char c) {
-                return c == '/' ||
-                       utils::constants::allowedCharactersInPath.find(c) ==
-                           std::string_view::npos;
-            }),
-        strippedId.end());
-    strippedId = std::string(id) + strippedId;
-
-    size_t idx = 0;
-    std::string tmpId = strippedId.substr(0, maxLength);
-
-    while (std::find(conflictIds.begin(), conflictIds.end(), tmpId) !=
-               conflictIds.end() ||
-           tmpId.empty())
-    {
-        size_t digitsInIdx = countDigits(idx);
-
-        if (digitsInIdx > maxLength)
-        {
-            throw sdbusplus::exception::SdBusError(
-                static_cast<int>(std::errc::file_exists),
-                "Unique indices are depleted");
-        }
-
-        tmpId =
-            strippedId.substr(0, maxLength - digitsInIdx) + std::to_string(idx);
-        ++idx;
-    }
-
-    return tmpId;
-}
-
-} // namespace details
-
-void verifyIdCharacters(std::string_view id)
-{
-    if (id.find_first_not_of(utils::constants::allowedCharactersInPath) !=
-        std::string::npos)
-    {
-        throw sdbusplus::exception::SdBusError(
-            static_cast<int>(std::errc::invalid_argument),
-            "Invalid character in id");
-    }
-
-    if (auto pos = id.find_first_of("/");
-        pos != std::string::npos && pos != id.find_last_of("/"))
-    {
-        throw sdbusplus::exception::SdBusError(
-            static_cast<int>(std::errc::invalid_argument),
-            "Too many '/' in id");
-    }
-}
-
-std::pair<std::string, std::string> generateId(
-    std::string_view id, std::string_view name, std::string_view defaultName,
-    const std::vector<std::string>& conflictIds, const size_t maxLength)
-{
-    if (name.empty() && !id.ends_with('/'))
-    {
-        name = id;
-
-        if (auto pos = name.find_last_of("/"); pos != std::string::npos)
-        {
-            name = name.substr(pos + 1);
-        }
-    }
-
-    if (name.empty())
-    {
-        name = defaultName;
-    }
-
-    return std::make_pair(details::generateId(id, name, conflictIds, maxLength),
-                          std::string{name});
-}
-
-} // namespace utils
diff --git a/src/utils/generate_id.hpp b/src/utils/generate_id.hpp
deleted file mode 100644
index 94952dc..0000000
--- a/src/utils/generate_id.hpp
+++ /dev/null
@@ -1,15 +0,0 @@
-#pragma once
-
-#include <string>
-#include <string_view>
-#include <vector>
-
-namespace utils
-{
-
-void verifyIdCharacters(std::string_view id);
-std::pair<std::string, std::string> generateId(
-    std::string_view id, std::string_view name, std::string_view defaultName,
-    const std::vector<std::string>& conflictIds, const size_t maxLength);
-
-} // namespace utils
diff --git a/src/utils/make_id_name.cpp b/src/utils/make_id_name.cpp
new file mode 100644
index 0000000..0df6c17
--- /dev/null
+++ b/src/utils/make_id_name.cpp
@@ -0,0 +1,122 @@
+#include "utils/make_id_name.hpp"
+
+#include "utils/dbus_path_utils.hpp"
+
+#include <sdbusplus/exception.hpp>
+
+#include <algorithm>
+#include <system_error>
+
+namespace utils
+{
+namespace details
+{
+
+size_t countDigits(size_t value)
+{
+    size_t result = 1;
+    while (value >= 10)
+    {
+        ++result;
+        value /= 10;
+    }
+    return result;
+}
+
+std::string generateId(std::string_view idIn, std::string_view nameIn,
+                       std::string_view defaultName,
+                       const std::vector<std::string>& conflictIds)
+{
+    verifyIdCharacters(idIn);
+    verifyIdPrefixes(idIn);
+
+    if (!idIn.empty() && !idIn.ends_with('/'))
+    {
+        if (std::find(conflictIds.begin(), conflictIds.end(), idIn) !=
+            conflictIds.end())
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::file_exists), "Duplicated id");
+        }
+        return std::string(idIn);
+    }
+
+    const std::string prefixes(idIn);
+
+    std::string strippedId(nameIn);
+    if (strippedId.find_first_of(utils::constants::allowedCharactersInPath) ==
+        std::string::npos)
+    {
+        strippedId = defaultName;
+    }
+    strippedId.erase(
+        std::remove_if(
+            strippedId.begin(), strippedId.end(),
+            [](char c) {
+                return c == '/' ||
+                       utils::constants::allowedCharactersInPath.find(c) ==
+                           std::string_view::npos;
+            }),
+        strippedId.end());
+
+    size_t idx = 0;
+    std::string tmpId =
+        prefixes + strippedId.substr(0, constants::maxIdNameLength);
+
+    while (std::find(conflictIds.begin(), conflictIds.end(), tmpId) !=
+           conflictIds.end())
+    {
+        size_t digitsInIdx = countDigits(idx);
+
+        if (digitsInIdx > constants::maxIdNameLength)
+        {
+            throw sdbusplus::exception::SdBusError(
+                static_cast<int>(std::errc::file_exists),
+                "Unique indices are depleted");
+        }
+
+        tmpId = prefixes +
+                strippedId.substr(0, constants::maxIdNameLength - digitsInIdx) +
+                std::to_string(idx);
+        ++idx;
+    }
+
+    return tmpId;
+}
+
+} // namespace details
+
+std::pair<std::string, std::string>
+    makeIdName(std::string_view id, std::string_view name,
+               std::string_view defaultName,
+               const std::vector<std::string>& conflictIds)
+{
+    if (name.length() > constants::maxIdNameLength)
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument), "Name too long");
+    }
+
+    if (name.empty() && !id.ends_with('/'))
+    {
+        name = id;
+
+        if (auto pos = name.find_last_of("/"); pos != std::string::npos)
+        {
+            name = name.substr(pos + 1);
+        }
+
+        name = name.substr(0, constants::maxIdNameLength);
+    }
+
+    if (name.empty())
+    {
+        name = defaultName;
+    }
+
+    return std::make_pair(
+        details::generateId(id, name, defaultName, conflictIds),
+        std::string{name});
+}
+
+} // namespace utils
diff --git a/src/utils/make_id_name.hpp b/src/utils/make_id_name.hpp
new file mode 100644
index 0000000..2c4ce75
--- /dev/null
+++ b/src/utils/make_id_name.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <string>
+#include <string_view>
+#include <vector>
+
+namespace utils
+{
+
+std::pair<std::string, std::string>
+    makeIdName(std::string_view id, std::string_view name,
+               std::string_view defaultName,
+               const std::vector<std::string>& conflictIds);
+
+} // namespace utils