-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathcontrol_protocol_utils.cpp
More file actions
1529 lines (1334 loc) · 90.3 KB
/
control_protocol_utils.cpp
File metadata and controls
1529 lines (1334 loc) · 90.3 KB
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "nmos/control_protocol_utils.h"
#include <list>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/find.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include "bst/regex.h"
#include "cpprest/json_utils.h"
#include "nmos/control_protocol_resource.h"
#include "nmos/control_protocol_state.h"
#include "nmos/json_fields.h"
#include "nmos/query_utils.h"
#include "nmos/resources.h"
namespace nmos
{
namespace nc
{
namespace details
{
bool is_control_class(const nc_class_id& control_class_id, const nc_class_id& class_id_)
{
nc_class_id class_id{class_id_};
if (control_class_id.size() < class_id.size())
{
// truncate test class_id to relevant class_id
class_id.resize(control_class_id.size());
}
return control_class_id == class_id;
}
// get the runtime property constraints of a specific property_id
web::json::value get_runtime_property_constraints(const nc_property_id& property_id, const web::json::value& runtime_property_constraints)
{
using web::json::value;
if (!runtime_property_constraints.is_null())
{
auto& runtime_prop_constraints = runtime_property_constraints.as_array();
auto found_constraints = std::find_if(runtime_prop_constraints.begin(), runtime_prop_constraints.end(), [&property_id](const web::json::value& constraints)
{
return property_id == parse_property_id(nmos::fields::nc::property_id(constraints));
});
if (runtime_prop_constraints.end() != found_constraints)
{
return *found_constraints;
}
}
return value::null();
}
// get the datatype descriptor of a specific type_name
web::json::value get_datatype_descriptor(const web::json::value& type_name, get_control_protocol_datatype_descriptor_handler get_control_protocol_datatype_descriptor)
{
using web::json::value;
if (!type_name.is_null())
{
return get_control_protocol_datatype_descriptor(type_name.as_string()).descriptor;
}
return value::null();
}
// get the datatype property constraints of a specific type_name
web::json::value get_datatype_constraints(const web::json::value& type_name, get_control_protocol_datatype_descriptor_handler get_control_protocol_datatype_descriptor)
{
using web::json::value;
// NcDatatypeDescriptor
const auto& datatype_descriptor = get_datatype_descriptor(type_name, get_control_protocol_datatype_descriptor);
if (!datatype_descriptor.is_null())
{
return nmos::fields::nc::constraints(datatype_descriptor);
}
return value::null();
}
// constraints validation, may throw nmos::control_protocol_exception
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Framework.html#ncparameterconstraintsnumber
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Framework.html#ncparameterconstraintsstring
void constraints_validation(const web::json::value& data, const web::json::value& constraints)
{
auto parameter_constraints_validation = [&constraints](const web::json::value& value)
{
// is numeric constraints
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Framework.html#ncparameterconstraintsnumber
if (constraints.has_field(nmos::fields::nc::step) && !nmos::fields::nc::step(constraints).is_null())
{
if (value.is_null()) { throw control_protocol_exception("value is null"); }
if (!value.is_integer()) { throw control_protocol_exception("value is not an integer"); }
const auto step = nmos::fields::nc::step(constraints).as_double();
if (step <= 0) { throw control_protocol_exception("step is not a positive integer"); }
const auto value_double = value.as_double();
if (constraints.has_field(nmos::fields::nc::minimum) && !nmos::fields::nc::minimum(constraints).is_null())
{
auto min = nmos::fields::nc::minimum(constraints).as_double();
if (0 != std::fmod(value_double - min, step)) { throw control_protocol_exception("value is not divisible by step"); }
}
else if (constraints.has_field(nmos::fields::nc::maximum) && !nmos::fields::nc::maximum(constraints).is_null())
{
auto max = nmos::fields::nc::maximum(constraints).as_double();
if (0 != std::fmod(max - value_double, step)) { throw control_protocol_exception("value is not divisible by step"); }
}
else
{
if (0 != std::fmod(value_double, step)) { throw control_protocol_exception("value is not divisible by step"); }
}
}
if (constraints.has_field(nmos::fields::nc::minimum) && !nmos::fields::nc::minimum(constraints).is_null())
{
if (value.is_null()) { throw control_protocol_exception("value is null"); }
if (!value.is_integer() || value.as_double() < nmos::fields::nc::minimum(constraints).as_double()) { throw control_protocol_exception("value is less than minimum"); }
}
if (constraints.has_field(nmos::fields::nc::maximum) && !nmos::fields::nc::maximum(constraints).is_null())
{
if (value.is_null()) { throw control_protocol_exception("value is null"); }
if (!value.is_integer() || value.as_double() > nmos::fields::nc::maximum(constraints).as_double()) { throw control_protocol_exception("value is greater than maximum"); }
}
// is string constraints
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Framework.html#ncparameterconstraintsstring
if (constraints.has_field(nmos::fields::nc::max_characters) && !constraints.at(nmos::fields::nc::max_characters).is_null())
{
if (value.is_null()) { throw control_protocol_exception("value is null"); }
const size_t max_characters = nmos::fields::nc::max_characters(constraints);
if (!value.is_string() || value.as_string().length() > max_characters) { throw control_protocol_exception("value is longer than maximum characters"); }
}
if (constraints.has_field(nmos::fields::nc::pattern) && !constraints.at(nmos::fields::nc::pattern).is_null())
{
if (value.is_null()) { throw control_protocol_exception("value is null"); }
if (!value.is_string()) { throw control_protocol_exception("value is not a string"); }
const auto value_string = utility::us2s(value.as_string());
bst::regex pattern(utility::us2s(nmos::fields::nc::pattern(constraints)));
if (!bst::regex_match(value_string, pattern)) { throw control_protocol_exception("value dose not match the pattern"); }
}
// reaching here, parameter validation successfully
};
if (data.is_array())
{
for (const auto& value : data.as_array())
{
parameter_constraints_validation(value);
}
}
else
{
parameter_constraints_validation(data);
}
}
// level 0 datatype constraints validation, may throw nmos::control_protocol_exception
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Constraints.html
void datatype_constraints_validation(const web::json::value& data, const datatype_constraints_validation_parameters& params)
{
auto parameter_constraints_validation = [¶ms](const web::json::value& value_)
{
// no constraints validation required
if (params.datatype_descriptor.is_null()) { return; }
const auto& datatype_type = nmos::fields::nc::type(params.datatype_descriptor);
// do NcDatatypeDescriptorPrimitive constraints validation
if (nc_datatype_type::Primitive == datatype_type)
{
// hmm, for the primitive type, it should not have datatype constraints specified via the datatype_descriptor but just in case
const auto& datatype_constraints = nmos::fields::nc::constraints(params.datatype_descriptor);
if (datatype_constraints.is_null())
{
auto primitive_validation = [](const nc_name& name, const web::json::value& value)
{
auto is_int16 = [](int32_t value)
{
return value >= (std::numeric_limits<int16_t>::min)()
&& value <= (std::numeric_limits<int16_t>::max)();
};
auto is_uint16 = [](uint32_t value)
{
return value >= (std::numeric_limits<uint16_t>::min)()
&& value <= (std::numeric_limits<uint16_t>::max)();
};
auto is_float32 = [](double value)
{
return value >= (std::numeric_limits<float_t>::lowest)()
&& value <= (std::numeric_limits<float_t>::max)();
};
if (U("NcBoolean") == name) { return value.is_boolean(); }
if (U("NcInt16") == name && value.is_number()) { return is_int16(value.as_number().to_int32()); }
if (U("NcInt32") == name && value.is_number()) { return value.as_number().is_int32(); }
if (U("NcInt64") == name && value.is_number()) { return value.as_number().is_int64(); }
if (U("NcUint16") == name && value.is_number()) { return is_uint16(value.as_number().to_uint32()); }
if (U("NcUint32") == name && value.is_number()) { return value.as_number().is_uint32(); }
if (U("NcUint64") == name && value.is_number()) { return value.as_number().is_uint64(); }
if (U("NcFloat32") == name && value.is_number()) { return is_float32(value.as_number().to_double()); }
if (U("NcFloat64") == name && value.is_number()) { return !value.as_number().is_integral(); }
if (U("NcString") == name) { return value.is_string(); }
// invalid primitive type
return false;
};
// do primitive type constraints validation
const auto& name = nmos::fields::nc::name(params.datatype_descriptor);
if (!primitive_validation(name, value_))
{
throw control_protocol_exception("value is not a " + utility::us2s(name) + " type");;
}
}
else
{
constraints_validation(value_, datatype_constraints);
}
return;
}
// do NcDatatypeDescriptorTypeDef constraints validation
if (nc_datatype_type::Typedef == datatype_type)
{
// do the datatype constraints specified via the datatype_descriptor if presented
const auto& datatype_constraints = nmos::fields::nc::constraints(params.datatype_descriptor);
if (datatype_constraints.is_null())
{
// do parent typename constraints validation
const auto& type_name = params.datatype_descriptor.at(nmos::fields::nc::parent_type); // parent type_name
datatype_constraints_validation(value_, {details::get_datatype_descriptor(type_name, params.get_control_protocol_datatype_descriptor), params.get_control_protocol_datatype_descriptor});
}
else
{
constraints_validation(value_, datatype_constraints);
}
return;
}
// do NcDatatypeDescriptorEnum constraints validation
if (nc_datatype_type::Enum == datatype_type)
{
const auto& items = nmos::fields::nc::items(params.datatype_descriptor);
if (items.end() == std::find_if(items.begin(), items.end(), [&](const web::json::value& nc_enum_item_descriptor) { return nmos::fields::nc::value(nc_enum_item_descriptor) == value_; }))
{
const auto& name = nmos::fields::nc::name(params.datatype_descriptor);
throw control_protocol_exception("value is not an enum " + utility::us2s(name) + " type");
}
return;
}
// do NcDatatypeDescriptorStruct constraints validation
if (nc_datatype_type::Struct == datatype_type)
{
const auto& datatype_name = nmos::fields::nc::name(params.datatype_descriptor);
const auto& fields = nmos::fields::nc::fields(params.datatype_descriptor);
// NcFieldDescriptor
for (const web::json::value& nc_field_descriptor : fields)
{
const auto& field_name = nmos::fields::nc::name(nc_field_descriptor);
// is field in strurcture
if (!value_.has_field(field_name)) { throw control_protocol_exception("missing " + utility::us2s(field_name) + " in " + utility::us2s(datatype_name)); }
// is field nullable
if (!nmos::fields::nc::is_nullable(nc_field_descriptor) && value_.at(field_name).is_null()) { throw control_protocol_exception(utility::us2s(field_name) + " is not nullable"); }
// if field value is null continue to next field
if (value_.at(field_name).is_null()) continue;
// is field sequenceable
if (nmos::fields::nc::is_sequence(nc_field_descriptor) != value_.at(field_name).is_array()) { throw control_protocol_exception(utility::us2s(field_name) + " is not sequenceable"); }
// check constraints of its typeName
const auto& field_type_name = nc_field_descriptor.at(nmos::fields::nc::type_name);
if (!field_type_name.is_null())
{
auto value = value_.at(field_name);
// do typename constraints validation
datatype_constraints_validation(value, {details::get_datatype_descriptor(field_type_name, params.get_control_protocol_datatype_descriptor), params.get_control_protocol_datatype_descriptor});
}
// check against field constraints if present
const auto& constraints = nmos::fields::nc::constraints(nc_field_descriptor);
if (!constraints.is_null())
{
// do field constraints validation
const auto& value = value_.at(field_name);
constraints_validation(value, constraints);
}
}
// unsupported datatype_type, no validation is required
return;
}
};
if (data.is_array())
{
for (const auto& value : data.as_array())
{
parameter_constraints_validation(value);
}
}
else
{
parameter_constraints_validation(data);
}
}
// multiple levels of constraints validation, may throw nmos::control_protocol_exception
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/Constraints.html
void constraints_validation(const web::json::value& data, const web::json::value& runtime_property_constraints, const web::json::value& property_constraints, const datatype_constraints_validation_parameters& params)
{
// do level 2 runtime property constraints validation
if (!runtime_property_constraints.is_null()) { constraints_validation(data, runtime_property_constraints); return; }
// do level 1 property constraints validation
if (!property_constraints.is_null()) { constraints_validation(data, property_constraints); return; }
// do level 0 datatype constraints validation
datatype_constraints_validation(data, params);
}
// method parameter constraints validation, may throw nmos::control_protocol_exception
void method_parameter_constraints_validation(const web::json::value& data, const web::json::value& property_constraints, const datatype_constraints_validation_parameters& params)
{
using web::json::value;
// do level 1 property constraints & level 0 datatype constraints validation
constraints_validation(data, value::null(), property_constraints, params);
}
web::json::value get_nc_block_member_descriptor(const resources& resources, const nmos::resource& parent_nc_block_resource, web::json::array& role_path_segments)
{
if (parent_nc_block_resource.data.has_field(nmos::fields::nc::members))
{
const auto& members = nmos::fields::nc::members(parent_nc_block_resource.data);
const auto role_path_segement = web::json::front(role_path_segments);
role_path_segments.erase(0);
// find the role_path_segment member
auto member_found = std::find_if(members.begin(), members.end(), [&](const web::json::value& member)
{
return role_path_segement.as_string() == nmos::fields::nc::role(member);
});
if (members.end() != member_found)
{
if (role_path_segments.size() == 0)
{
// NcBlockMemberDescriptor
return *member_found;
}
// get the role_path_segement member resource
if (is_block(parse_class_id(nmos::fields::nc::class_id(*member_found))))
{
// get resource based on the oid
const auto& oid = nmos::fields::nc::oid(*member_found);
const auto found = nmos::find_resource(resources, utility::s2us(std::to_string(oid)));
if (resources.end() != found)
{
return get_nc_block_member_descriptor(resources, *found, role_path_segments);
}
}
}
}
return web::json::value{};
}
web::json::value parse_role_path(const utility::string_t& role_path_)
{
// tokenize the role_path with the '.' delimiter
std::list<utility::string_t> role_path_segments;
boost::algorithm::split(role_path_segments, role_path_, [](utility::char_t c) { return '.' == c; });
return web::json::value_from_elements(role_path_segments);
}
// Set status and status message
bool set_monitor_status(resources& resources, nc_oid oid, int status, const utility::string_t& status_message,
const nc_property_id& status_property_id,
const nc_property_id& status_message_property_id,
const nc_property_id& status_transition_counter_property_id,
const utility::string_t& status_pending_received_time_field_name,
get_control_protocol_class_descriptor_handler get_control_protocol_class_descriptor,
slog::base_gate& gate)
{
auto current_connection_status = get_property(resources, oid, status_property_id, get_control_protocol_class_descriptor, gate);
web::json::value json_status_message = status_message.size() ? web::json::value::string(status_message) : web::json::value::null();
set_property_and_notify(resources, oid, status_property_id, status, get_control_protocol_class_descriptor, gate);
set_property_and_notify(resources, oid, status_message_property_id, json_status_message, get_control_protocol_class_descriptor, gate);
// Cancel any pending status updates
set_property(resources, oid, status_pending_received_time_field_name, web::json::value::number(0), gate);
// if status is "partially unhealthy" (2) or "unhealthy" (3) and less healthy than current state
if (status > 1 && status > current_connection_status.as_integer())
{
// increment transition_counter
auto transition_counter = get_property(resources, oid, status_transition_counter_property_id, get_control_protocol_class_descriptor, gate).as_integer();
set_property_and_notify(resources, oid, status_transition_counter_property_id, ++transition_counter, get_control_protocol_class_descriptor, gate);
}
const auto found = find_resource(resources, utility::s2us(std::to_string(oid)));
if (resources.end() != found)
{
if (nmos::nc::is_sender_monitor(parse_class_id(nmos::fields::nc::class_id(found->data))))
{
return details::update_sender_monitor_overall_status(resources, oid, get_control_protocol_class_descriptor, gate);
}
return details::update_receiver_monitor_overall_status(resources, oid, get_control_protocol_class_descriptor, gate);
}
return false;
}
// Set monitor status and status message
bool set_monitor_status_with_delay(resources& resources, nc_oid oid, int status, const utility::string_t& status_message,
const nc_property_id& status_property_id,
const nc_property_id& status_message_property_id,
const nc_property_id& status_transition_counter_property_id,
const utility::string_t& status_pending_field_name,
const utility::string_t& status_message_pending_time_field_name,
const utility::string_t& status_pending_received_time_field_name,
long long now_time,
monitor_status_pending_handler monitor_status_pending,
get_control_protocol_class_descriptor_handler get_control_protocol_class_descriptor,
slog::base_gate& gate)
{
const auto& current_status = get_property(resources, oid, status_property_id, get_control_protocol_class_descriptor, gate);
if (current_status.is_null())
{
// should never happen, missing receiver/sender monitor status property
slog::log<slog::severities::error>(gate, SLOG_FLF) << U("receiver/sender monitor status property: {level=") << status_property_id.level << U(", index=") << status_property_id.index << U("} not found");
return false;
}
// check whether the the status message changed when the status is unchanged
if (status == current_status.as_integer())
{
// has status message changed?
const auto& current_status_message = get_property(resources, oid, status_message_property_id, get_control_protocol_class_descriptor, gate);
if ((current_status_message.is_null() && status_message.size()) || (!current_status_message.is_null() && current_status_message.as_string() != status_message))
{
// If the status message has changed then update only that
web::json::value json_status_message = status_message.size() ? web::json::value::string(status_message) : web::json::value::null();
return set_property_and_notify(resources, oid, status_message_property_id, json_status_message, get_control_protocol_class_descriptor, gate);
}
// no update required, no changes on status or status message
return true;
}
// status has changed
utility::string_t updated_status_message = status_message;
// if status is "healthy" then preserve the previous status message
if (1 == status && status_message.size() == 0)
{
// Get existing status message and prepend with "Previously: "
const auto& current_status_message = get_property(resources, oid, status_message_property_id, get_control_protocol_class_descriptor, gate);
if (!current_status_message.is_null())
{
utility::ostringstream_t ss;
ss << U("Previously: ") << current_status_message.as_string();
updated_status_message = ss.str();
}
}
// if status or current state is "inactive" (0), update status and status message immediately
if (0 == status || 0 == current_status.as_integer())
{
return set_monitor_status(resources, oid, status, updated_status_message, status_property_id, status_message_property_id, status_transition_counter_property_id, status_pending_received_time_field_name, get_control_protocol_class_descriptor, gate);
}
else
{
const auto& activation_time = get_property(resources, oid, nmos::fields::nc::monitor_activation_time, gate);
const auto& status_reporting_delay = get_property(resources, oid, nc_status_monitor_status_reporting_delay, get_control_protocol_class_descriptor, gate);
if (status > current_status.as_integer() && now_time > (static_cast<long long>(activation_time.as_integer()) + status_reporting_delay.as_integer()))
{
// becoming less health and not in the initial activation state
// immediately set the status
return set_monitor_status(resources, oid, status, updated_status_message, status_property_id, status_message_property_id, status_transition_counter_property_id, status_pending_received_time_field_name, get_control_protocol_class_descriptor, gate);
}
else
{
web::json::value json_status_message = updated_status_message.size() ? web::json::value::string(updated_status_message) : web::json::value::null();
// becoming more health or in the initial activation state
// set the status with delay
const auto& pending_received_time = get_property(resources, oid, status_pending_received_time_field_name, gate);
// only update pending received time if not already set
if (pending_received_time.as_integer() == 0)
{
if (!set_property(resources, oid, status_pending_received_time_field_name, static_cast<int64_t>(now_time), gate))
{
return false;
}
}
if (set_property(resources, oid, status_pending_field_name, status, gate)
&& set_property(resources, oid, status_message_pending_time_field_name, json_status_message, gate))
{
monitor_status_pending();
return true;
}
return false;
}
}
}
web::json::value get_overall_status_message(const resources& resources, nc_oid oid, const std::vector<std::pair<nmos::nc_property_id, nmos::nc_property_id>>& status_property_ids, get_control_protocol_class_descriptor_handler get_control_protocol_class_descriptor, slog::base_gate& gate)
{
// overall status message displays the most unhealthy status message. Priority lowest to highest -> inactive, healthy, partially unhealthy, unhealthy
// overall status message displays root cause of problem. Priority of messages lowest to highest -> sync, payload, transport, link
auto overall_status_message = web::json::value::null();
for (int status_health = 0; status_health < 5; ++status_health) // Iterate through health of statuses
{
for (const auto& property_id_pair : status_property_ids)
{
auto status = get_property(resources, oid, property_id_pair.first, get_control_protocol_class_descriptor, gate);
if (status.as_integer() != status_health) continue;
auto status_message = get_property(resources, oid, property_id_pair.second, get_control_protocol_class_descriptor, gate);
if (!status_message.is_null())
{
overall_status_message = status_message;
}
}
}
return overall_status_message;
}
bool update_receiver_monitor_overall_status(resources& resources, nc_oid oid, get_control_protocol_class_descriptor_handler get_control_protocol_class_descriptor, slog::base_gate& gate)
{
std::vector<std::pair<nmos::nc_property_id, nmos::nc_property_id>> status_message_property_ids = {
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_receiver_monitor_external_synchronization_status_property_id, nc_receiver_monitor_external_synchronization_status_message_property_id),
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_receiver_monitor_stream_status_property_id, nc_receiver_monitor_stream_status_message_property_id),
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_receiver_monitor_connection_status_property_id, nc_receiver_monitor_connection_status_message_property_id),
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_receiver_monitor_link_status_property_id, nc_receiver_monitor_link_status_message_property_id)};
// Update Overall Status
auto connection_status = get_property(resources, oid, nc_receiver_monitor_connection_status_property_id, get_control_protocol_class_descriptor, gate);
auto stream_status = get_property(resources, oid, nc_receiver_monitor_stream_status_property_id, get_control_protocol_class_descriptor, gate);
const auto& overall_status_message = get_overall_status_message(resources, oid, status_message_property_ids, get_control_protocol_class_descriptor, gate);
// if connection or stream status is Inactive
if (nc_connection_status::status::inactive == connection_status.as_integer() || nc_stream_status::status::inactive == stream_status.as_integer())
{
// Overall status is set to Inactive
bool success = set_property_and_notify(resources, oid, nc_status_monitor_overall_status_property_id, nc_overall_status::status::inactive, get_control_protocol_class_descriptor, gate);
return success && set_property_and_notify(resources, oid, nc_status_monitor_overall_status_message_property_id, overall_status_message, get_control_protocol_class_descriptor, gate);
}
auto link_status = get_property(resources, oid, nc_receiver_monitor_link_status_property_id, get_control_protocol_class_descriptor, gate);
auto external_synchronization_status = get_property(resources, oid, nc_receiver_monitor_external_synchronization_status_property_id, get_control_protocol_class_descriptor, gate);
// otherwise take the least healthy status as the overall status
std::vector<int32_t> statuses = {link_status.as_integer(), connection_status.as_integer(), stream_status.as_integer()};
// Ignore external synchronization status if it is not used
if (nc_synchronization_status::status::not_used != external_synchronization_status.as_integer())
{
statuses.push_back(external_synchronization_status.as_integer());
}
// Find most unhealthy status
auto overall_status = *std::max_element(statuses.begin(), statuses.end());
bool success = set_property_and_notify(resources, oid, nc_status_monitor_overall_status_property_id, web::json::value::number(overall_status), get_control_protocol_class_descriptor, gate);
return success && set_property_and_notify(resources, oid, nc_status_monitor_overall_status_message_property_id, overall_status_message, get_control_protocol_class_descriptor, gate);
}
bool update_sender_monitor_overall_status(resources& resources, nc_oid oid, get_control_protocol_class_descriptor_handler get_control_protocol_class_descriptor, slog::base_gate& gate)
{
std::vector<std::pair<nmos::nc_property_id, nmos::nc_property_id>> status_message_property_ids = {
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_sender_monitor_external_synchronization_status_property_id, nc_sender_monitor_external_synchronization_status_message_property_id),
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_sender_monitor_essence_status_property_id, nc_sender_monitor_essence_status_message_property_id),
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_sender_monitor_transmission_status_property_id, nc_sender_monitor_transmission_status_message_property_id),
std::pair<nmos::nc_property_id, nmos::nc_property_id>(nc_sender_monitor_link_status_property_id, nc_sender_monitor_link_status_message_property_id)};
// Update Overall Status
auto transmission_status = get_property(resources, oid, nc_sender_monitor_transmission_status_property_id, get_control_protocol_class_descriptor, gate);
auto essence_status = get_property(resources, oid, nc_sender_monitor_essence_status_property_id, get_control_protocol_class_descriptor, gate);
const auto& overall_status_message = get_overall_status_message(resources, oid, status_message_property_ids, get_control_protocol_class_descriptor, gate);
// if transmission or stream status is Inactive
if (nc_transmission_status::status::inactive == transmission_status.as_integer() || nc_essence_status::status::inactive == essence_status.as_integer())
{
// Overall status is set to Inactive
bool success = set_property_and_notify(resources, oid, nc_status_monitor_overall_status_property_id, nc_overall_status::status::inactive, get_control_protocol_class_descriptor, gate);
return success && set_property_and_notify(resources, oid, nc_status_monitor_overall_status_message_property_id, overall_status_message, get_control_protocol_class_descriptor, gate);
}
auto link_status = get_property(resources, oid, nc_receiver_monitor_link_status_property_id, get_control_protocol_class_descriptor, gate);
auto external_synchronization_status = get_property(resources, oid, nc_receiver_monitor_external_synchronization_status_property_id, get_control_protocol_class_descriptor, gate);
// otherwise take the least healthy status as the overall status
std::vector<int32_t> statuses = {link_status.as_integer(), transmission_status.as_integer(), essence_status.as_integer()};
// Ignore external synchronization status if it is not used
if (nc_synchronization_status::status::not_used != external_synchronization_status.as_integer())
{
statuses.push_back(external_synchronization_status.as_integer());
}
// Find most unhealthy status
auto overall_status = *std::max_element(statuses.begin(), statuses.end());
bool success = set_property_and_notify(resources, oid, nc_status_monitor_overall_status_property_id, web::json::value::number(overall_status), get_control_protocol_class_descriptor, gate);
return success && set_property_and_notify(resources, oid, nc_status_monitor_overall_status_message_property_id, overall_status_message, get_control_protocol_class_descriptor, gate);
}
}
// is the given class_id a NcBlock
bool is_block(const nc_class_id& class_id)
{
return details::is_control_class(nc_block_class_id, class_id);
}
// is the given class_id a NcWorker
bool is_worker(const nc_class_id& class_id)
{
return details::is_control_class(nc_worker_class_id, class_id);
}
// is the given class_id a NcManager
bool is_manager(const nc_class_id& class_id)
{
return details::is_control_class(nc_manager_class_id, class_id);
}
// is the given class_id a NcDeviceManager
bool is_device_manager(const nc_class_id& class_id)
{
return details::is_control_class(nc_device_manager_class_id, class_id);
}
// is the given class_id a NcClassManager
bool is_class_manager(const nc_class_id& class_id)
{
return details::is_control_class(nc_class_manager_class_id, class_id);
}
// is the given class_id a NcStatusMonitor
bool is_status_monitor(const nc_class_id& class_id)
{
return details::is_control_class(nc_status_monitor_class_id, class_id);
}
// is the given class_id a NcStatusMonitor
bool is_sender_monitor(const nc_class_id& class_id)
{
return details::is_control_class(nc_sender_monitor_class_id, class_id);
}
// construct NcClassId
nc_class_id make_class_id(const nc_class_id& prefix, int32_t authority_key, const std::vector<int32_t>& suffix)
{
nc_class_id class_id = prefix;
class_id.push_back(authority_key);
class_id.insert(class_id.end(), suffix.begin(), suffix.end());
return class_id;
}
nc_class_id make_class_id(const nc_class_id& prefix, const std::vector<int32_t>& suffix)
{
return make_class_id(prefix, 0, suffix);
}
// find control class property descriptor (NcPropertyDescriptor)
web::json::value find_property_descriptor(const nc_property_id& property_id, const nc_class_id& class_id_, get_control_protocol_class_descriptor_handler get_control_protocol_class_descriptor)
{
using web::json::value;
auto class_id = class_id_;
while (!class_id.empty())
{
const auto& control_class = get_control_protocol_class_descriptor(class_id);
const auto& property_descriptors = control_class.property_descriptors.as_array();
auto found = std::find_if(property_descriptors.begin(), property_descriptors.end(), [&property_id](const web::json::value& property_descriptor)
{
return (property_id == nc::details::parse_property_id(nmos::fields::nc::id(property_descriptor)));
});
if (property_descriptors.end() != found) { return *found; }
class_id.pop_back();
}
return value::null();
}
// get block member descriptors
void get_member_descriptors(const resources& resources, const resource& resource, bool recurse, web::json::array& descriptors)
{
if (resource.data.has_field(nmos::fields::nc::members))
{
const auto& members = nmos::fields::nc::members(resource.data);
for (const auto& member : members)
{
web::json::push_back(descriptors, member);
}
if (recurse)
{
// get members on all NcBlock(s)
for (const auto& member : members)
{
if (is_block(nc::details::parse_class_id(nmos::fields::nc::class_id(member))))
{
// get resource based on the oid
const auto& oid = nmos::fields::nc::oid(member);
const auto found = find_resource(resources, utility::s2us(std::to_string(oid)));
if (resources.end() != found)
{
nc::get_member_descriptors(resources, *found, recurse, descriptors);
}
}
}
}
}
}
// find members with given role name or fragment
void find_members_by_role(const resources& resources, const resource& resource, const utility::string_t& role, bool match_whole_string, bool case_sensitive, bool recurse, web::json::array& descriptors)
{
auto find_members_by_matching_role = [&](const web::json::array& members)
{
using web::json::value;
auto match = [&](const web::json::value& descriptor)
{
if (match_whole_string)
{
if (case_sensitive) { return role == nmos::fields::nc::role(descriptor); }
else { return boost::algorithm::to_upper_copy(role) == boost::algorithm::to_upper_copy(nmos::fields::nc::role(descriptor)); }
}
else
{
if (case_sensitive) { return !boost::find_first(nmos::fields::nc::role(descriptor), role).empty(); }
else { return !boost::ifind_first(nmos::fields::nc::role(descriptor), role).empty(); }
}
};
return boost::make_iterator_range(boost::make_filter_iterator(match, members.begin(), members.end()), boost::make_filter_iterator(match, members.end(), members.end()));
};
if (resource.data.has_field(nmos::fields::nc::members))
{
const auto& members = nmos::fields::nc::members(resource.data);
auto members_found = find_members_by_matching_role(members);
for (const auto& member : members_found)
{
web::json::push_back(descriptors, member);
}
if (recurse)
{
// do role match on all NcBlock(s)
for (const auto& member : members)
{
if (is_block(nc::details::parse_class_id(nmos::fields::nc::class_id(member))))
{
// get resource based on the oid
const auto& oid = nmos::fields::nc::oid(member);
const auto found = find_resource(resources, utility::s2us(std::to_string(oid)));
if (resources.end() != found)
{
nc::find_members_by_role(resources, *found, role, match_whole_string, case_sensitive, recurse, descriptors);
}
}
}
}
}
}
// find members with given class id
void find_members_by_class_id(const resources& resources, const nmos::resource& resource, const nc_class_id& class_id_, bool include_derived, bool recurse, web::json::array& descriptors)
{
auto find_members_by_matching_class_id = [&](const web::json::array& members)
{
using web::json::value;
auto match = [&](const web::json::value& descriptor)
{
const auto& class_id = nc::details::parse_class_id(nmos::fields::nc::class_id(descriptor));
if (include_derived) { return class_id_.size() <= class_id.size() && std::equal(class_id_.begin(), class_id_.end(), class_id.begin()); }
else { return class_id == class_id_; }
};
return boost::make_iterator_range(boost::make_filter_iterator(match, members.begin(), members.end()), boost::make_filter_iterator(match, members.end(), members.end()));
};
if (resource.data.has_field(nmos::fields::nc::members))
{
auto& members = nmos::fields::nc::members(resource.data);
auto members_found = find_members_by_matching_class_id(members);
for (const auto& member : members_found)
{
web::json::push_back(descriptors, member);
}
if (recurse)
{
// do class_id match on all NcBlock(s)
for (const auto& member : members)
{
if (is_block(nc::details::parse_class_id(nmos::fields::nc::class_id(member))))
{
// get resource based on the oid
const auto& oid = nmos::fields::nc::oid(member);
const auto found = find_resource(resources, utility::s2us(std::to_string(oid)));
if (resources.end() != found)
{
nc::find_members_by_class_id(resources, *found, class_id_, include_derived, recurse, descriptors);
}
}
}
}
}
}
// push a control protocol resource into other control protocol NcBlock resource
void push_back(control_protocol_resource& nc_block_resource, const control_protocol_resource& resource)
{
using web::json::value;
auto& parent = nc_block_resource.data;
const auto& child = resource.data;
if (!is_block(details::parse_class_id(nmos::fields::nc::class_id(parent)))) throw std::logic_error("non-NcBlock cannot be nested");
web::json::push_back(parent[nmos::fields::nc::members],
details::make_block_member_descriptor(nmos::fields::description(child), nmos::fields::nc::role(child), nmos::fields::nc::oid(child), nmos::fields::nc::constant_oid(child), nc::details::parse_class_id(nmos::fields::nc::class_id(child)), nmos::fields::nc::user_label(child), nmos::fields::nc::oid(parent)));
nc_block_resource.resources.push_back(resource);
}
// insert root block and all sub control protocol resources
void insert_root(resources& resources, control_protocol_resource& root)
{
// note, model write lock should already be applied by the outer function, so access to control_protocol_resources is OK...
std::function<void(nmos::resources& resources, nmos::control_protocol_resource& resource)> insert_resources;
insert_resources = [&insert_resources](nmos::resources& resources, nmos::control_protocol_resource& resource)
{
for (auto& r : resource.resources)
{
insert_resources(resources, r);
nmos::nc::insert_resource(resources, std::move(r));
}
resource.resources.clear();
};
insert_resources(resources, root);
nmos::nc::insert_resource(resources, std::move(root));
}
// insert a control protocol resource
std::pair<resources::iterator, bool> insert_resource(resources& resources, resource&& resource)
{
// set the creation and update timestamps, before inserting the resource
resource.updated = resource.created = nmos::strictly_increasing_update(resources);
auto result = resources.insert(std::move(resource));
// replacement of a deleted or expired resource is also allowed
// (currently, with no further checks on api_version, type, etc.)
if (!result.second && !result.first->has_data())
{
// if the insertion was banned, resource has not been moved from
result.second = resources.replace(result.first, std::move(resource));
}
return result;
}
// modify a control protocol resource, and insert notification event to all subscriptions
bool modify_resource(resources& resources, const id& id, std::function<void(resource&)> modifier, const web::json::value& notification_event)
{
// note, model write lock should already be applied by the outer function, so access to control_protocol_resources is OK...
auto found = resources.find(id);
if (resources.end() == found || !found->has_data()) return false;
auto pre = notification_event != web::json::value::null() ? found->data : web::json::value::null();
// "If an exception is thrown by some user-provided operation, then the element pointed to by position is erased."
// This seems too surprising, despite the fact that it means that a modification may have been partially completed,
// so capture and rethrow.
// See https://www.boost.org/doc/libs/1_68_0/libs/multi_index/doc/reference/ord_indices.html#modify
std::exception_ptr modifier_exception;
auto resource_updated = nmos::strictly_increasing_update(resources);
auto result = resources.modify(found, [&resource_updated, &modifier, &modifier_exception](resource& resource)
{
try
{
modifier(resource);
}
catch (...)
{
modifier_exception = std::current_exception();
}
// set the update timestamp
resource.updated = resource_updated;
});
if (web::json::value::null() != notification_event && result)
{
auto& modified = *found;
nc::insert_notification_events(resources, modified.version, modified.downgrade_version, modified.type, pre, modified.data, notification_event);
}
if (modifier_exception)
{
std::rethrow_exception(modifier_exception);
}
return result;
}
// erase a control protocol resource
resources::size_type erase_resource(resources& resources, const id& id)
{
// hmm, may be also erasing all it's member blocks?
resources::size_type count = 0;
auto found = resources.find(id);
if (resources.end() != found && found->has_data())
{
resources.erase(found);
++count;
}
return count;
}
// find the control protocol resource which is assoicated with the given IS-04/IS-05/IS-08 resource id
resources::const_iterator find_resource(resources& resources, type type, const id& resource_id)
{
return find_resource_if(resources, type, [resource_id](const nmos::resource& resource)
{
auto& touchpoints = resource.data.at(nmos::fields::nc::touchpoints);
if (!touchpoints.is_null() && touchpoints.is_array())
{
auto& tps = touchpoints.as_array();
auto found_tp = std::find_if(tps.begin(), tps.end(), [resource_id](const web::json::value& touchpoint)
{
auto& resource = nmos::fields::nc::resource(touchpoint);
return (resource_id == nmos::fields::nc::id(resource).as_string());
});
return (tps.end() != found_tp);
}
return false;
});
}
// method parameters constraints validation
void method_parameters_contraints_validation(const web::json::value& arguments, const web::json::value& nc_method_descriptor, get_control_protocol_datatype_descriptor_handler get_control_protocol_datatype_descriptor)
{
for (const auto& param : nmos::fields::nc::parameters(nc_method_descriptor))
{
const auto& name = nmos::fields::nc::name(param);
const auto& constraints = nmos::fields::nc::constraints(param);
const auto& type_name = param.at(nmos::fields::nc::type_name);
if (arguments.is_null() || !arguments.has_field(name))
{
// missing argument parameter
throw control_protocol_exception("missing argument parameter " + utility::us2s(name));
}
details::method_parameter_constraints_validation(arguments.at(name), constraints, { nmos::nc::details::get_datatype_descriptor(type_name, get_control_protocol_datatype_descriptor), get_control_protocol_datatype_descriptor });
}
}
// Validate that the resource has been correctly constructed according to the class_descriptor
void validate_resource(const resource& resource, const experimental::control_class_descriptor& class_descriptor)
{
// Validate properties
for (const auto& property_descriptor : class_descriptor.property_descriptors.as_array())
{
if ((resource.data.is_null()) || (!resource.data.has_field(nmos::fields::nc::name(property_descriptor))))
{
throw control_protocol_exception("missing control resource property: " + utility::us2s(nmos::fields::nc::name(property_descriptor)));
}
}
// Validate methods