summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/telemetry/0001-Add-support-for-POST-on-TriggersCollection.patch
blob: b0cf44cb22ffbff5d593b0a1b5b07466e84a2ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
From 008cc1b35ccb1508d3c71ff5c6cfc6c772f1744c Mon Sep 17 00:00:00 2001
From: Szymon Dompke <szymon.dompke@intel.com>
Date: Wed, 17 Nov 2021 18:18:16 +0100
Subject: [PATCH] Add support for POST on TriggersCollection
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Added POST method on /redfish/v1/TelemetryService/Triggers uri, which
creates new trigger in telemetry service, by using dbus call AddTrigger.

By DMTF, most of the properties are not required, and as such are
treated as optional. Some values can be deduced from others (like
'MetricType', depending on 'DiscreteTriggers' or 'NumericThresholds').
All properties provided in POST body by user will be verified against
each other, and errors will be raised. Few examples of such situations:
- 'MetricType' is set to 'Discrete' but 'NumericThresholds' was passed.
- 'MetricType' is set to 'Numeric' but "DiscreteTriggers' or
  'DiscreteTriggerCondition' were passed
- 'DiscreteTriggerCondition' is set to 'Specified' but
  'DiscreteTriggers' is an empty array or was not passed.
- 'DiscreteTriggerCondition' is set to 'Changed' but 'DiscreteTriggers'
  is passed and is not an empty array.

Example 1 – Trigger with discrete values:
{
    "Id": "TestTrigger",
    "MetricType": "Discrete",
    "TriggerActions": [
        "RedfishEvent"
    ],
    "DiscreteTriggerCondition": "Specified",
    "DiscreteTriggers": [
        {
            "Value": "55.88",
            "DwellTime": "PT0.001S",
            "Severity": "Warning"
        },
        {
            "Name": "My discrete trigger",
            "Value": "55.88",
            "DwellTime": "PT0.001S",
            "Severity": "OK"
        },
        {
            "Value": "55.88",
            "DwellTime": "PT0.001S",
            "Severity": "Critical"
        }
    ],
    "MetricProperties": [
        "/redfish/v1/Chassis/AC_Baseboard/Thermal#/Fans/0/Reading"
    ],
    "Links": {
        "MetricReportDefinitions": []
    }
}

Example 2 – trigger with numeric threshold:
{
    "Id": "TestTrigger2",
    "Name": "My Numeric Trigger",
    "MetricType": "Numeric",
    "TriggerActions": [
        "RedfishEvent",
        "RedfishMetricReport"
    ],
    "NumericThresholds": {
        "UpperCritical": {
            "Reading": 50,
            "Activation": "Increasing",
            "DwellTime": "PT0.001S"
        },
        "UpperWarning": {
            "Reading": 48.1,
            "Activation": "Increasing",
            "DwellTime": "PT0.004S"
        }
    },
    "MetricProperties": [
        "/redfish/v1/Chassis/AC_Baseboard/Thermal#/Fans/0/Reading",
        "/redfish/v1/Chassis/AC_Baseboard/Thermal#/Fans/17/Reading"
    ],
    "Links": {
        "MetricReportDefinitions": [
            "/redfish/v1/TelemetryService/MetricReportDefinitions/PowerMetrics",
            "/redfish/v1/TelemetryService/MetricReportDefinitions/PowerMetricStats",
            "/redfish/v1/TelemetryService/MetricReportDefinitions/PlatformPowerUsage"
        ]
    }
}

Tested:
- Triggers were successfully created with above example message bodies.
  This can be checked by calling:
  'busctl tree xyz.openbmc_project.Telemetry'.
- Expected errors were returned for messages with incorrect or mutually
  exclusive properties and incorrect values.
- Redfish service validator is passing.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: Ief8c76de8aa660ae0d2dbe4610c26a28186a290a
---
 redfish-core/include/utils/finalizer.hpp      |  35 ++
 .../include/utils/telemetry_utils.hpp         |  82 +++
 redfish-core/lib/metric_report_definition.hpp |  31 +-
 redfish-core/lib/trigger.hpp                  | 526 +++++++++++++++++-
 4 files changed, 642 insertions(+), 32 deletions(-)
 create mode 100644 redfish-core/include/utils/finalizer.hpp

diff --git a/redfish-core/include/utils/finalizer.hpp b/redfish-core/include/utils/finalizer.hpp
new file mode 100644
index 0000000..cb98507
--- /dev/null
+++ b/redfish-core/include/utils/finalizer.hpp
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <functional>
+
+namespace redfish
+{
+
+namespace utils
+{
+
+class Finalizer
+{
+  public:
+    Finalizer() = delete;
+    Finalizer(std::function<void()> finalizer) : finalizer(std::move(finalizer))
+    {}
+
+    Finalizer(const Finalizer&) = delete;
+    Finalizer(Finalizer&&) = delete;
+
+    ~Finalizer()
+    {
+        if (finalizer)
+        {
+            finalizer();
+        }
+    }
+
+  private:
+    std::function<void()> finalizer;
+};
+
+} // namespace utils
+
+} // namespace redfish
\ No newline at end of file
diff --git a/redfish-core/include/utils/telemetry_utils.hpp b/redfish-core/include/utils/telemetry_utils.hpp
index 8aeff0d..c68e40c 100644
--- a/redfish-core/include/utils/telemetry_utils.hpp
+++ b/redfish-core/include/utils/telemetry_utils.hpp
@@ -13,6 +13,9 @@ constexpr const char* metricReportDefinitionUri =
     "/redfish/v1/TelemetryService/MetricReportDefinitions";
 constexpr const char* metricReportUri =
     "/redfish/v1/TelemetryService/MetricReports";
+constexpr const char* triggerInterface =
+    "xyz.openbmc_project.Telemetry.Trigger";
+constexpr const char* triggerUri = "/redfish/v1/TelemetryService/Triggers";
 
 inline std::string getDbusReportPath(const std::string& id)
 {
@@ -28,5 +31,84 @@ inline std::string getDbusTriggerPath(const std::string& id)
     return {triggersPath / id};
 }
 
+inline std::optional<std::string>
+    getReportNameFromReportDefinitionUri(const std::string& uri)
+{
+    constexpr const char* uriPattern =
+        "/redfish/v1/TelemetryService/MetricReportDefinitions/";
+    constexpr size_t idx = std::string_view(uriPattern).length();
+    if (boost::starts_with(uri, uriPattern))
+    {
+        return uri.substr(idx);
+    }
+    return std::nullopt;
+}
+
+inline std::optional<std::string>
+    getTriggerIdFromDbusPath(const std::string& dbusPath)
+{
+    constexpr const char* triggerTree =
+        "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService/";
+    constexpr size_t idx = std::string_view(triggerTree).length();
+    if (boost::starts_with(dbusPath, triggerTree))
+    {
+        return dbusPath.substr(idx);
+    }
+    return std::nullopt;
+}
+
+inline bool getChassisSensorNode(
+    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+    const std::vector<std::string>& uris,
+    boost::container::flat_set<std::pair<std::string, std::string>>& matched)
+{
+    size_t uriIdx = 0;
+    for (const std::string& uri : uris)
+    {
+        std::string chassis;
+        std::string node;
+
+        if (!boost::starts_with(uri, "/redfish/v1/Chassis/") ||
+            !dbus::utility::getNthStringFromPath(uri, 3, chassis) ||
+            !dbus::utility::getNthStringFromPath(uri, 4, node))
+        {
+            BMCWEB_LOG_ERROR << "Failed to get chassis and sensor Node "
+                                "from "
+                             << uri;
+            messages::propertyValueIncorrect(asyncResp->res, uri,
+                                             "MetricProperties/" +
+                                                 std::to_string(uriIdx));
+            return false;
+        }
+
+        if (boost::ends_with(node, "#"))
+        {
+            node.pop_back();
+        }
+
+        matched.emplace(std::move(chassis), std::move(node));
+        uriIdx++;
+    }
+    return true;
+}
+
+inline std::optional<std::string>
+    redfishActionToDbusAction(const std::string& redfishAction)
+{
+    if (redfishAction == "RedfishMetricReport")
+    {
+        return "UpdateReport";
+    }
+    if (redfishAction == "RedfishEvent")
+    {
+        return "RedfishEvent";
+    }
+    if (redfishAction == "LogToLogService")
+    {
+        return "LogToLogService";
+    }
+    return std::nullopt;
+}
+
 } // namespace telemetry
 } // namespace redfish
diff --git a/redfish-core/lib/metric_report_definition.hpp b/redfish-core/lib/metric_report_definition.hpp
index cb38633..4007544 100644
--- a/redfish-core/lib/metric_report_definition.hpp
+++ b/redfish-core/lib/metric_report_definition.hpp
@@ -217,7 +217,7 @@ inline bool getUserParameters(crow::Response& res, const crow::Request& req,
     return true;
 }
 
-inline bool getChassisSensorNode(
+inline bool getChassisSensorNodeFromMetrics(
     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
     const std::vector<std::pair<std::string, std::vector<std::string>>>&
         metrics,
@@ -225,30 +225,9 @@ inline bool getChassisSensorNode(
 {
     for (const auto& [id, uris] : metrics)
     {
-        for (size_t i = 0; i < uris.size(); i++)
+        if (!getChassisSensorNode(asyncResp, uris, matched))
         {
-            const std::string& uri = uris[i];
-            std::string chassis;
-            std::string node;
-
-            if (!boost::starts_with(uri, "/redfish/v1/Chassis/") ||
-                !dbus::utility::getNthStringFromPath(uri, 3, chassis) ||
-                !dbus::utility::getNthStringFromPath(uri, 4, node))
-            {
-                BMCWEB_LOG_ERROR
-                    << "Failed to get chassis and sensor Node from " << uri;
-                messages::propertyValueIncorrect(asyncResp->res, uri,
-                                                 "MetricProperties/" +
-                                                     std::to_string(i));
-                return false;
-            }
-
-            if (boost::ends_with(node, "#"))
-            {
-                node.pop_back();
-            }
-
-            matched.emplace(std::move(chassis), std::move(node));
+            return false;
         }
     }
     return true;
@@ -382,8 +361,8 @@ inline void requestRoutesMetricReportDefinitionCollection(App& app)
 
             boost::container::flat_set<std::pair<std::string, std::string>>
                 chassisSensors;
-            if (!telemetry::getChassisSensorNode(asyncResp, args.metrics,
-                                                 chassisSensors))
+            if (!telemetry::getChassisSensorNodeFromMetrics(
+                    asyncResp, args.metrics, chassisSensors))
             {
                 return;
             }
diff --git a/redfish-core/lib/trigger.hpp b/redfish-core/lib/trigger.hpp
index 210468c..01c150e 100644
--- a/redfish-core/lib/trigger.hpp
+++ b/redfish-core/lib/trigger.hpp
@@ -1,7 +1,9 @@
 #pragma once
 
-#include "utils/collection.hpp"
+#include "sensors.hpp"
+#include "utils/finalizer.hpp"
 #include "utils/telemetry_utils.hpp"
+#include "utils/time_utils.hpp"
 
 #include <app.hpp>
 #include <registries/privilege_registry.hpp>
@@ -14,9 +16,10 @@ namespace redfish
 {
 namespace telemetry
 {
-constexpr const char* triggerInterface =
-    "xyz.openbmc_project.Telemetry.Trigger";
-constexpr const char* triggerUri = "/redfish/v1/TelemetryService/Triggers";
+
+static constexpr std::array<std::string_view, 4>
+    supportedNumericThresholdNames = {"UpperCritical", "LowerCritical",
+                                      "UpperWarning", "LowerWarning"};
 
 using NumericThresholdParams =
     std::tuple<std::string, uint64_t, std::string, double>;
@@ -24,6 +27,10 @@ using NumericThresholdParams =
 using DiscreteThresholdParams =
     std::tuple<std::string, std::string, uint64_t, std::string>;
 
+using TriggerThresholdParams =
+    std::variant<std::vector<NumericThresholdParams>,
+                 std::vector<DiscreteThresholdParams>>;
+
 using TriggerThresholdParamsExt =
     std::variant<std::monostate, std::vector<NumericThresholdParams>,
                  std::vector<DiscreteThresholdParams>>;
@@ -35,6 +42,455 @@ using TriggerGetParamsVariant =
     std::variant<std::monostate, bool, std::string, TriggerThresholdParamsExt,
                  TriggerSensorsParams, std::vector<std::string>>;
 
+namespace add_trigger
+{
+
+enum class MetricType
+{
+    Discrete,
+    Numeric
+};
+
+enum class DiscreteCondition
+{
+    Specified,
+    Changed
+};
+
+struct Context
+{
+    struct
+    {
+        std::string id;
+        std::string name;
+        std::vector<std::string> actions;
+        std::vector<std::pair<sdbusplus::message::object_path, std::string>>
+            sensors;
+        std::vector<std::string> reportNames;
+        TriggerThresholdParams thresholds;
+    } dbusArgs;
+
+    struct
+    {
+        std::optional<DiscreteCondition> discreteCondition;
+        std::optional<MetricType> metricType;
+        std::optional<std::vector<std::string>> metricProperties;
+    } parsedInfo;
+
+    boost::container::flat_map<std::string, std::string> uriToDbusMerged{};
+};
+
+inline std::optional<MetricType> getMetricType(const std::string& metricType)
+{
+    if (metricType == "Discrete")
+    {
+        return MetricType::Discrete;
+    }
+    if (metricType == "Numeric")
+    {
+        return MetricType::Numeric;
+    }
+    return std::nullopt;
+}
+
+inline std::optional<DiscreteCondition>
+    getDiscreteCondition(const std::string& discreteTriggerCondition)
+{
+    if (discreteTriggerCondition == "Specified")
+    {
+        return DiscreteCondition::Specified;
+    }
+    if (discreteTriggerCondition == "Changed")
+    {
+        return DiscreteCondition::Changed;
+    }
+    return std::nullopt;
+}
+
+inline bool parseNumericThresholds(crow::Response& res,
+                                   nlohmann::json& numericThresholds,
+                                   Context& ctx)
+{
+    if (!numericThresholds.is_object())
+    {
+        messages::propertyValueTypeError(res, numericThresholds.dump(),
+                                         "NumericThresholds");
+        return false;
+    }
+
+    std::vector<NumericThresholdParams> parsedParams;
+    parsedParams.reserve(numericThresholds.size());
+
+    for (auto& [thresholdName, thresholdData] : numericThresholds.items())
+    {
+        if (std::find(supportedNumericThresholdNames.begin(),
+                      supportedNumericThresholdNames.end(),
+                      thresholdName) == supportedNumericThresholdNames.end())
+        {
+            messages::propertyUnknown(res, thresholdName);
+            return false;
+        }
+
+        double reading = .0;
+        std::string activation;
+        std::string dwellTimeStr;
+
+        if (!json_util::readJson(thresholdData, res, "Reading", reading,
+                                 "Activation", activation, "DwellTime",
+                                 dwellTimeStr))
+        {
+            return false;
+        }
+
+        std::optional<std::chrono::milliseconds> dwellTime =
+            time_utils::fromDurationString(dwellTimeStr);
+        if (!dwellTime)
+        {
+            messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr);
+            return false;
+        }
+
+        parsedParams.emplace_back(thresholdName,
+                                  static_cast<uint64_t>(dwellTime->count()),
+                                  activation, reading);
+    }
+
+    ctx.dbusArgs.thresholds = std::move(parsedParams);
+    return true;
+}
+
+inline bool parseDiscreteTriggers(
+    crow::Response& res,
+    std::optional<std::vector<nlohmann::json>>& discreteTriggers, Context& ctx)
+{
+    std::vector<DiscreteThresholdParams> parsedParams;
+    if (!discreteTriggers)
+    {
+        ctx.dbusArgs.thresholds = std::move(parsedParams);
+        return true;
+    }
+
+    parsedParams.reserve(discreteTriggers->size());
+    for (nlohmann::json& thresholdInfo : *discreteTriggers)
+    {
+        std::optional<std::string> name;
+        std::string value;
+        std::string dwellTimeStr;
+        std::string severity;
+
+        if (!json_util::readJson(thresholdInfo, res, "Name", name, "Value",
+                                 value, "DwellTime", dwellTimeStr, "Severity",
+                                 severity))
+        {
+            return false;
+        }
+
+        std::optional<std::chrono::milliseconds> dwellTime =
+            time_utils::fromDurationString(dwellTimeStr);
+        if (!dwellTime)
+        {
+            messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr);
+            return false;
+        }
+
+        if (!name)
+        {
+            name = "";
+        }
+
+        parsedParams.emplace_back(
+            *name, severity, static_cast<uint64_t>(dwellTime->count()), value);
+    }
+
+    ctx.dbusArgs.thresholds = std::move(parsedParams);
+    return true;
+}
+
+inline bool parseTriggerThresholds(
+    crow::Response& res,
+    std::optional<std::vector<nlohmann::json>>& discreteTriggers,
+    std::optional<nlohmann::json>& numericThresholds, Context& ctx)
+{
+    if (discreteTriggers && numericThresholds)
+    {
+        messages::mutualExclusiveProperties(res, "DiscreteTriggers",
+                                            "NumericThresholds");
+        return false;
+    }
+
+    if (ctx.parsedInfo.discreteCondition)
+    {
+        if (numericThresholds)
+        {
+            messages::mutualExclusiveProperties(res, "DiscreteTriggerCondition",
+                                                "NumericThresholds");
+            return false;
+        }
+    }
+
+    if (ctx.parsedInfo.metricType)
+    {
+        if (*ctx.parsedInfo.metricType == MetricType::Discrete &&
+            numericThresholds)
+        {
+            messages::propertyValueConflict(res, "NumericThresholds",
+                                            "MetricType");
+            return false;
+        }
+        if (*ctx.parsedInfo.metricType == MetricType::Numeric &&
+            discreteTriggers)
+        {
+            messages::propertyValueConflict(res, "DiscreteTriggers",
+                                            "MetricType");
+            return false;
+        }
+        if (*ctx.parsedInfo.metricType == MetricType::Numeric &&
+            ctx.parsedInfo.discreteCondition)
+        {
+            messages::propertyValueConflict(res, "DiscreteTriggers",
+                                            "DiscreteTriggerCondition");
+            return false;
+        }
+    }
+
+    if (discreteTriggers || ctx.parsedInfo.discreteCondition ||
+        (ctx.parsedInfo.metricType &&
+         *ctx.parsedInfo.metricType == MetricType::Discrete))
+    {
+        if (ctx.parsedInfo.discreteCondition)
+        {
+            if (*ctx.parsedInfo.discreteCondition ==
+                    DiscreteCondition::Specified &&
+                !discreteTriggers)
+            {
+                messages::createFailedMissingReqProperties(res,
+                                                           "DiscreteTriggers");
+                return false;
+            }
+            if (discreteTriggers && ((*ctx.parsedInfo.discreteCondition ==
+                                          DiscreteCondition::Specified &&
+                                      discreteTriggers->empty()) ||
+                                     (*ctx.parsedInfo.discreteCondition ==
+                                          DiscreteCondition::Changed &&
+                                      !discreteTriggers->empty())))
+            {
+                messages::propertyValueConflict(res, "DiscreteTriggers",
+                                                "DiscreteTriggerCondition");
+                return false;
+            }
+        }
+        if (!parseDiscreteTriggers(res, discreteTriggers, ctx))
+        {
+            return false;
+        }
+    }
+    else if (numericThresholds)
+    {
+        if (!parseNumericThresholds(res, *numericThresholds, ctx))
+        {
+            return false;
+        }
+    }
+    else
+    {
+        messages::createFailedMissingReqProperties(
+            res, "'DiscreteTriggers', 'NumericThresholds', "
+                 "'DiscreteTriggerCondition' or 'MetricType'");
+        return false;
+    }
+    return true;
+}
+
+inline bool parseLinks(crow::Response& res, nlohmann::json& links, Context& ctx)
+{
+    if (links.empty())
+    {
+        return true;
+    }
+
+    std::optional<std::vector<std::string>> metricReportDefinitions;
+    if (!json_util::readJson(links, res, "MetricReportDefinitions",
+                             metricReportDefinitions))
+    {
+        return false;
+    }
+
+    if (metricReportDefinitions)
+    {
+        ctx.dbusArgs.reportNames.reserve(metricReportDefinitions->size());
+        for (std::string& reportDefinionUri : *metricReportDefinitions)
+        {
+            std::optional<std::string> reportName =
+                getReportNameFromReportDefinitionUri(reportDefinionUri);
+            if (!reportName)
+            {
+                messages::propertyValueIncorrect(res, "MetricReportDefinitions",
+                                                 reportDefinionUri);
+                return false;
+            }
+            ctx.dbusArgs.reportNames.push_back(*reportName);
+        }
+    }
+    return true;
+}
+
+inline bool parseMetricProperties(crow::Response& res, Context& ctx)
+{
+    if (!ctx.parsedInfo.metricProperties)
+    {
+        return true;
+    }
+
+    ctx.dbusArgs.sensors.reserve(ctx.parsedInfo.metricProperties->size());
+
+    size_t uriIdx = 0;
+    for (const std::string& uri : *ctx.parsedInfo.metricProperties)
+    {
+        auto el = ctx.uriToDbusMerged.find(uri);
+        if (el == ctx.uriToDbusMerged.end())
+        {
+            BMCWEB_LOG_ERROR << "Failed to find DBus sensor "
+                                "corsresponding to URI "
+                             << uri;
+            messages::propertyValueNotInList(
+                res, uri, "MetricProperties/" + std::to_string(uriIdx));
+            return false;
+        }
+
+        const std::string& dbusPath = el->second;
+        ctx.dbusArgs.sensors.emplace_back(dbusPath, uri);
+        uriIdx++;
+    }
+    return true;
+}
+
+inline bool parsePostTriggerParams(crow::Response& res,
+                                   const crow::Request& req, Context& ctx)
+{
+    std::optional<std::string> id;
+    std::optional<std::string> name;
+    std::optional<std::string> metricType;
+    std::optional<std::vector<std::string>> triggerActions;
+    std::optional<std::string> discreteTriggerCondition;
+    std::optional<std::vector<nlohmann::json>> discreteTriggers;
+    std::optional<nlohmann::json> numericThresholds;
+    std::optional<nlohmann::json> links;
+    if (!json_util::readJson(
+            req, res, "Id", id, "Name", name, "MetricType", metricType,
+            "TriggerActions", triggerActions, "DiscreteTriggerCondition",
+            discreteTriggerCondition, "DiscreteTriggers", discreteTriggers,
+            "NumericThresholds", numericThresholds, "MetricProperties",
+            ctx.parsedInfo.metricProperties, "Links", links))
+    {
+        return false;
+    }
+
+    ctx.dbusArgs.id = id.value_or("");
+    ctx.dbusArgs.name = name.value_or("");
+
+    if (metricType)
+    {
+        if (!(ctx.parsedInfo.metricType = getMetricType(*metricType)))
+        {
+            messages::propertyValueIncorrect(res, "MetricType", *metricType);
+            return false;
+        }
+    }
+
+    if (discreteTriggerCondition)
+    {
+        if (!(ctx.parsedInfo.discreteCondition =
+                  getDiscreteCondition(*discreteTriggerCondition)))
+        {
+            messages::propertyValueIncorrect(res, "DiscreteTriggerCondition",
+                                             *discreteTriggerCondition);
+            return false;
+        }
+    }
+
+    if (triggerActions)
+    {
+        ctx.dbusArgs.actions.reserve(triggerActions->size());
+        for (const std::string& action : *triggerActions)
+        {
+            if (const std::optional<std::string>& dbusAction =
+                    redfishActionToDbusAction(action))
+            {
+                ctx.dbusArgs.actions.emplace_back(*dbusAction);
+            }
+            else
+            {
+                messages::propertyValueNotInList(res, action, "TriggerActions");
+                return false;
+            }
+        }
+    }
+
+    if (!parseTriggerThresholds(res, discreteTriggers, numericThresholds, ctx))
+    {
+        return false;
+    }
+
+    if (links)
+    {
+        if (!parseLinks(res, *links, ctx))
+        {
+            return false;
+        }
+    }
+    return true;
+}
+
+inline void createTrigger(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                          Context& ctx)
+{
+    crow::connections::systemBus->async_method_call(
+        [aResp = asyncResp, id = ctx.dbusArgs.id](
+            const boost::system::error_code ec, const std::string& dbusPath) {
+            if (ec == boost::system::errc::file_exists)
+            {
+                messages::resourceAlreadyExists(aResp->res, "Trigger", "Id",
+                                                id);
+                return;
+            }
+            if (ec == boost::system::errc::too_many_files_open)
+            {
+                messages::createLimitReachedForResource(aResp->res);
+                return;
+            }
+            if (ec)
+            {
+                messages::internalError(aResp->res);
+                BMCWEB_LOG_ERROR << "respHandler DBus error " << ec;
+                return;
+            }
+
+            const std::optional<std::string>& triggerId =
+                getTriggerIdFromDbusPath(dbusPath);
+            if (!triggerId)
+            {
+                messages::internalError(aResp->res);
+                BMCWEB_LOG_ERROR << "Unknown data returned by "
+                                    "AddTrigger DBus method";
+                return;
+            }
+
+            messages::created(aResp->res);
+            aResp->res.addHeader("Location",
+                                 triggerUri + std::string("/") + *triggerId);
+        },
+        service, "/xyz/openbmc_project/Telemetry/Triggers",
+        "xyz.openbmc_project.Telemetry.TriggerManager", "AddTrigger",
+        "TelemetryService/" + ctx.dbusArgs.id, ctx.dbusArgs.name,
+        ctx.dbusArgs.actions, ctx.dbusArgs.sensors, ctx.dbusArgs.reportNames,
+        ctx.dbusArgs.thresholds);
+}
+
+} // namespace add_trigger
+
+namespace get_trigger
+{
+
 inline std::optional<std::string>
     getRedfishFromDbusAction(const std::string& dbusAction)
 {
@@ -270,6 +726,8 @@ inline bool fillTrigger(
     return true;
 }
 
+} // namespace get_trigger
+
 } // namespace telemetry
 
 inline void requestRoutesTriggerCollection(App& app)
@@ -290,6 +748,62 @@ inline void requestRoutesTriggerCollection(App& app)
                     asyncResp, telemetry::triggerUri, interfaces,
                     "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService");
             });
+
+    BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/")
+        .privileges(redfish::privileges::postTriggersCollection)
+        .methods(boost::beast::http::verb::post)(
+            [](const crow::Request& req,
+               const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
+                const auto ctx =
+                    std::make_shared<telemetry::add_trigger::Context>();
+                if (!telemetry::add_trigger::parsePostTriggerParams(
+                        asyncResp->res, req, *ctx))
+                {
+                    return;
+                }
+
+                if (!ctx->parsedInfo.metricProperties ||
+                    ctx->parsedInfo.metricProperties->empty())
+                {
+                    telemetry::add_trigger::createTrigger(asyncResp, *ctx);
+                    return;
+                }
+
+                boost::container::flat_set<std::pair<std::string, std::string>>
+                    chassisSensors;
+                if (!telemetry::getChassisSensorNode(
+                        asyncResp, *ctx->parsedInfo.metricProperties,
+                        chassisSensors))
+                {
+                    return;
+                }
+
+                const auto finalizer =
+                    std::make_shared<utils::Finalizer>([asyncResp, ctx] {
+                        if (!telemetry::add_trigger::parseMetricProperties(
+                                asyncResp->res, *ctx))
+                        {
+                            return;
+                        }
+                        telemetry::add_trigger::createTrigger(asyncResp, *ctx);
+                    });
+
+                for (const auto& [chassis, sensorType] : chassisSensors)
+                {
+                    retrieveUriToDbusMap(
+                        chassis, sensorType,
+                        [asyncResp, ctx,
+                         finalizer](const boost::beast::http::status status,
+                                    const boost::container::flat_map<
+                                        std::string, std::string>& uriToDbus) {
+                            if (status == boost::beast::http::status::ok)
+                            {
+                                ctx->uriToDbusMerged.insert(uriToDbus.begin(),
+                                                            uriToDbus.end());
+                            }
+                        });
+                }
+            });
 }
 
 inline void requestRoutesTrigger(App& app)
@@ -320,8 +834,8 @@ inline void requestRoutesTrigger(App& app)
                             return;
                         }
 
-                        if (!telemetry::fillTrigger(asyncResp->res.jsonValue,
-                                                    id, ret))
+                        if (!telemetry::get_trigger::fillTrigger(
+                                asyncResp->res.jsonValue, id, ret))
                         {
                             messages::internalError(asyncResp->res);
                         }
-- 
2.25.1