Added constrains to id generator

This commit prevents id with more than one '/'. Slash can be used to
create namespaces to avoid id conflicts between multiple users.

Changed generator logic to take provided id as a name if no name was
provided.

Tested:
- Tested that id and name generates correctly
- It is no longer possible to add id with more than one '/'

Signed-off-by: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
Change-Id: Ieef32ddb71b5a4870117aab0d624cbd46b5893e6
diff --git a/src/trigger_manager.cpp b/src/trigger_manager.cpp
index 358ee78..c0b20d7 100644
--- a/src/trigger_manager.cpp
+++ b/src/trigger_manager.cpp
@@ -61,7 +61,6 @@
             "Reached maximal trigger count");
     }
 
-    verifyTriggerIdLength(triggerId);
     utils::verifyIdCharacters(triggerId);
 
     for (const auto& trigger : triggers)
@@ -74,26 +73,6 @@
     }
 }
 
-void TriggerManager::verifyTriggerIdLength(const std::string& triggerId)
-{
-    if (triggerId.length() > maxTriggerIdLength)
-    {
-        throw sdbusplus::exception::SdBusError(
-            static_cast<int>(std::errc::invalid_argument),
-            "Trigger id exceeds maximum length");
-    }
-}
-
-std::string TriggerManager::generateId(const std::string& prefix,
-                                       const std::string& triggerName) const
-{
-    const auto existingTriggerIds = utils::transform(
-        triggers, [](const auto& trigger) { return trigger->getId(); });
-
-    return utils::generateId(prefix, triggerName, existingTriggerIds,
-                             maxTriggerIdLength);
-}
-
 interfaces::Trigger& TriggerManager::addTrigger(
     const std::string& triggerIdIn, const std::string& triggerNameIn,
     const std::vector<std::string>& triggerActions,
@@ -101,19 +80,18 @@
     const std::vector<std::string>& reportIds,
     const LabeledTriggerThresholdParams& labeledThresholdParams)
 {
-    std::string triggerName = triggerNameIn;
-    if (triggerName.empty())
-    {
-        triggerName = triggerNameDefault;
-    }
+    const auto existingTriggerIds = utils::transform(
+        triggers, [](const auto& trigger) { return trigger->getId(); });
 
-    std::string triggerId = generateId(triggerIdIn, triggerName);
+    auto [id, name] =
+        utils::generateId(triggerIdIn, triggerNameIn, triggerNameDefault,
+                          existingTriggerIds, maxTriggerIdLength);
 
-    verifyAddTrigger(triggerId, triggerName);
+    verifyAddTrigger(id, name);
 
     triggers.emplace_back(triggerFactory->make(
-        triggerId, triggerName, triggerActions, reportIds, *this,
-        *triggerStorage, labeledThresholdParams, labeledSensorsInfo));
+        id, name, triggerActions, reportIds, *this, *triggerStorage,
+        labeledThresholdParams, labeledSensorsInfo));
 
     return *triggers.back();
 }