forked from mit-ll/LL-DLEP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocolConfigImpl.cpp
1541 lines (1349 loc) · 43.7 KB
/
ProtocolConfigImpl.cpp
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
/*
* Dynamic Link Exchange Protocol (DLEP)
*
* Copyright (C) 2015, 2016, 2018 Massachusetts Institute of Technology
*/
#include <boost/lexical_cast.hpp>
#include <iomanip>
#include <libxml/parser.h>
#include <libxml/tree.h>
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#else
#error "libxml2 was not built with required XInclude processing enabled."
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/xmlschemas.h>
#else
#error "libxml2 was not built with required Schema processing enabled."
#endif
#include "ProtocolConfigImpl.h"
namespace LLDLEP
{
namespace ProtocolStrings
{
// signal/message strings
const std::string Peer_Discovery = "Peer_Discovery";
const std::string Peer_Offer = "Peer_Offer";
const std::string Session_Initialization = "Session_Initialization";
const std::string Session_Initialization_Response =
"Session_Initialization_Response";
const std::string Session_Termination = "Session_Termination";
const std::string Session_Termination_Response = "Session_Termination_Response";
const std::string Session_Update = "Session_Update";
const std::string Session_Update_Response = "Session_Update_Response";
const std::string Destination_Up = "Destination_Up";
const std::string Destination_Up_Response = "Destination_Up_Response";
const std::string Destination_Down = "Destination_Down";
const std::string Destination_Down_Response = "Destination_Down_Response";
const std::string Destination_Update = "Destination_Update";
const std::string Link_Characteristics_Request = "Link_Characteristics_Request";
const std::string Link_Characteristics_Response = "Link_Characteristics_Response";
const std::string Heartbeat = "Heartbeat";
const std::string Destination_Announce = "Destination_Announce";
const std::string Destination_Announce_Response = "Destination_Announce_Response";
// data item strings
const std::string Version = "Version";
const std::string Port = "Port";
const std::string Peer_Type = "Peer_Type";
const std::string MAC_Address = "MAC_Address";
const std::string IPv4_Address = "IPv4_Address";
const std::string IPv6_Address = "IPv6_Address";
const std::string Status = "Status";
const std::string Heartbeat_Interval = "Heartbeat_Interval";
const std::string Link_Characteristics_Response_Timer = "Link_Characteristics_Response_Timer";
const std::string IPv4_Attached_Subnet = "IPv4_Attached_Subnet";
const std::string IPv6_Attached_Subnet = "IPv6_Attached_Subnet";
const std::string Extensions_Supported = "Extensions_Supported";
const std::string Experimental_Definition = "Experimental_Definition";
const std::string IPv4_Connection_Point = "IPv4_Connection_Point";
const std::string IPv6_Connection_Point = "IPv6_Connection_Point";
// required metrics strings
const std::string Maximum_Data_Rate_Receive = "Maximum_Data_Rate_Receive";
const std::string Maximum_Data_Rate_Transmit = "Maximum_Data_Rate_Transmit";
const std::string Current_Data_Rate_Receive = "Current_Data_Rate_Receive";
const std::string Current_Data_Rate_Transmit = "Current_Data_Rate_Transmit";
const std::string Latency = "Latency";
const std::string Resources = "Resources";
const std::string Resources_Receive = "Resources_Receive";
const std::string Resources_Transmit = "Resources_Transmit";
const std::string Relative_Link_Quality_Receive = "Relative_Link_Quality_Receive";
const std::string Relative_Link_Quality_Transmit = "Relative_Link_Quality_Transmit";
const std::string Maximum_Transmission_Unit = "Maximum_Transmission_Unit";
// status code strings
const std::string Success = "Success";
const std::string Unknown_Message = "Unknown_Message";
const std::string Invalid_Message = "Invalid_Message";
const std::string Unexpected_Message = "Unexpected_Message";
const std::string Request_Denied = "Request_Denied";
const std::string Timed_Out = "Timed_Out";
const std::string Invalid_Data = "Invalid_Data";
const std::string Invalid_Destination = "Invalid_Destination";
const std::string Not_Interested = "Not_Interested";
const std::string Inconsistent_Data = "Inconsistent_Data";
} // namespace ProtocolStrings
} // namespace LLDLEP
using namespace LLDLEP;
using namespace LLDLEP::internal;
ProtocolConfigImpl::ProtocolConfigImpl(
const std::string & proto_config_schema,
const std::string & proto_config_file,
DlepLoggerPtr logger) :
version { {0, 0} },
logger(logger)
{
std::string err = load_protocol_config(proto_config_schema,
proto_config_file);
if (err != "")
{
throw BadProtocolConfig(err);
}
}
DlepLoggerPtr
ProtocolConfigImpl::get_logger() const
{
return logger;
}
//-----------------------------------------------------------------------------
//
// support for loading and validating the XML protocol config file
static void
xml_error_handler(void * ctx, const char * msg, ...)
{
ProtocolConfigImpl * protocfg = static_cast<ProtocolConfigImpl *>(ctx);
DlepLoggerPtr logger = protocfg->get_logger();
va_list ap;
va_start(ap, msg);
char buf[1024];
vsnprintf(buf, sizeof(buf), msg, ap);
va_end(ap);
LOG(DLEP_LOG_ERROR, std::ostringstream(buf));
}
static void
xml_warning_handler(void * ctx, const char * msg, ...)
{
ProtocolConfigImpl * protocfg = static_cast<ProtocolConfigImpl *>(ctx);
DlepLoggerPtr logger = protocfg->get_logger();
va_list ap;
va_start(ap, msg);
char buf[1024];
vsnprintf(buf, sizeof(buf), msg, ap);
va_end(ap);
LOG(DLEP_LOG_NOTICE, std::ostringstream(buf));
}
std::string
ProtocolConfigImpl::load_protocol_config(const std::string &
proto_config_schema,
const std::string & proto_config_file)
{
LIBXML_TEST_VERSION
std::ostringstream msg;
std::string err = "";
xmlDocPtr doc = nullptr;
xmlParserCtxtPtr parser_ctx = nullptr;
xmlSchemaValidCtxtPtr schema_validate_ctx = nullptr;
xmlSchemaParserCtxtPtr schema_parser_ctx = nullptr;
xmlSchemaPtr schema = nullptr;
int ret = 0;
int xmlopts = 0;
msg << "schema=" << proto_config_schema << " config=" << proto_config_file;
LOG(DLEP_LOG_INFO, msg);
// Parse the XML config file and store the resulting tree in doc.
// This will check that it is well-formed XML, but does not
// validate against the schema yet.
parser_ctx = xmlNewParserCtxt();
if (parser_ctx == nullptr)
{
err = "XML parser context creation failed";
goto bailout;
}
msg << "parsing " << proto_config_file;
LOG(DLEP_LOG_DEBUG, msg);
xmlopts =
XML_PARSE_NOBLANKS | // remove useless blank nodes
XML_PARSE_XINCLUDE | // do XInclude processing
XML_PARSE_NONET; // forbid network access; everything is local
doc = xmlCtxtReadFile(parser_ctx, proto_config_file.c_str(), nullptr, xmlopts);
if (doc == nullptr)
{
err = "XML parsing failed for " + proto_config_file;
goto bailout;
}
// Do XInclude processing
ret = xmlXIncludeProcessFlags(doc, xmlopts);
if (ret < 0)
{
err = "XML include processing failed for " + proto_config_file;
goto bailout;
}
msg << "XInclude made " << ret << " substitutions";
LOG(DLEP_LOG_DEBUG, msg);
// Load the schema to use to validate the document (doc) parsed above.
msg << "loading schema " << proto_config_schema;
LOG(DLEP_LOG_DEBUG, msg);
schema_parser_ctx = xmlSchemaNewParserCtxt(proto_config_schema.c_str());
if (schema_parser_ctx == nullptr)
{
err = "XML schema parser context creation failed";
goto bailout;
}
xmlSchemaSetParserErrors(schema_parser_ctx,
(xmlSchemaValidityErrorFunc)xml_error_handler,
(xmlSchemaValidityWarningFunc)xml_warning_handler,
this);
schema = xmlSchemaParse(schema_parser_ctx);
if (schema == nullptr)
{
err = "XML schema parsing failed for " + proto_config_schema;
goto bailout;
}
// Validate the document (doc) with the schema.
msg << "validating the config against the schema";
LOG(DLEP_LOG_DEBUG, msg);
schema_validate_ctx = xmlSchemaNewValidCtxt(schema);
if (schema_validate_ctx == nullptr)
{
err = "XML schema validate context creation failed";
goto bailout;
}
xmlSchemaSetValidErrors(schema_validate_ctx,
(xmlSchemaValidityErrorFunc)xml_error_handler,
(xmlSchemaValidityWarningFunc)xml_warning_handler,
this);
ret = xmlSchemaValidateDoc(schema_validate_ctx, doc);
if (ret)
{
err = "XML validation failed for " + proto_config_file
+ " err=" + std::to_string(ret);
goto bailout;
}
err = extract_protocol_config(doc);
bailout:
msg << "cleaning up";
LOG(DLEP_LOG_DEBUG, msg);
if (parser_ctx != nullptr)
{
xmlFreeParserCtxt(parser_ctx);
}
if (doc != nullptr)
{
xmlFreeDoc(doc);
}
if (schema_parser_ctx != nullptr)
{
xmlSchemaFreeParserCtxt(schema_parser_ctx);
}
if (schema != nullptr)
{
xmlSchemaFree(schema);
}
if (schema_validate_ctx != nullptr)
{
xmlSchemaFreeValidCtxt(schema_validate_ctx);
}
xmlCleanupParser();
if (err != "")
{
LOG(DLEP_LOG_ERROR, std::ostringstream(err));
}
return err;
}
void
ProtocolConfigImpl::log_xml_tree(xmlNodePtr start_node, int level)
{
if (start_node == nullptr)
{
return;
}
for (xmlNodePtr cur_node = start_node; cur_node; cur_node = cur_node->next)
{
std::ostringstream msg;
xmlChar * content = xmlNodeGetContent(cur_node);
msg << std::setw(4 * level) << " " << cur_node->name
<< " type: " << cur_node->type
<< " value: " << (char *)content;
LOG(DLEP_LOG_DEBUG, msg);
xmlFree(content);
log_xml_tree(cur_node->children, level + 1);
}
}
//-----------------------------------------------------------------------------
//
// support for extracing information from the XML tree once it has
// been loaded from the file
std::string
ProtocolConfigImpl::extract_protocol_config(xmlDocPtr doc)
{
std::string err = "";
xmlNodePtr root = xmlDocGetRootElement(doc);
assert(root != nullptr);
// log_xml_tree(root, 0);
for (xmlNodePtr node = root->children; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "version")
{
extract_version(node->children);
}
else if (node_name == "signal_prefix")
{
extract_signal_prefix(node->children);
}
else if (node_name == "field_sizes")
{
extract_field_sizes(node->children);
}
else if (node_name == "module")
{
extract_module(node->children);
}
else
{
std::ostringstream msg;
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
}
return err;
}
template<typename T>
void
ProtocolConfigImpl::log_node_path_and_value(xmlNodePtr node, T val)
{
long lineno = xmlGetLineNo(node);
// Build up the pathname to this value for logging. This is not
// efficient, but it is simple, and it only happens at startup.
std::string path(std::string((const char *)node->name));
while (node->parent != nullptr)
{
node = node->parent;
if (node->name != nullptr)
{
path = std::string((const char *)node->name) + "/" + path;
}
}
std::ostringstream msg;
// XXX I'd like to log the name of the file here too, but
// node->doc->name is null.
msg << lineno << ": " << path << " = " << std::boolalpha << val;
LOG(DLEP_LOG_DEBUG, msg);
}
template<typename T>
T
ProtocolConfigImpl::extract_node_value(xmlNodePtr node)
{
xmlChar * str = xmlNodeGetContent(node);
T val;
// Use boolalpha in case T is bool because the XML string will be
// "true" or "false", not 0/1. For other data types, this has no
// effect.
std::stringstream((const char *)str) >> std::boolalpha >> val;
xmlFree(str);
log_node_path_and_value(node, val);
return val;
}
// If I don't wrap the specialization of extract_node_value below
// inside its namespace LLDLEP::internal, g++ gives the following
// error:
//
// error: specialization of ‘template<class T> T
// LLDLEP::internal::ProtocolConfigImpl::extract_node_value(xmlNodePtr)’
// in different namespace [-fpermissive]
//
// The template is already in this namespace in the class declaration,
// so what's the problem? clang accepts it without being wrapped.
// Not sure who is right. Both compilers accept the code below, so
// we'll go with that.
namespace LLDLEP
{
namespace internal
{
// Specialization for strings. We don't want to use the generic
// version of this method (above) for strings because when you read a
// string from a stringstream, it stops at the first space it
// encounters. This would prevent us from having strings with spaces
// in them, and that's too limiting.
template<> std::string
ProtocolConfigImpl::extract_node_value(xmlNodePtr node)
{
xmlChar * str = xmlNodeGetContent(node);
std::string val {(const char *)str};
xmlFree(str);
log_node_path_and_value(node, val);
return val;
}
} // namespace internal
} // namespace LLDLEP
void
ProtocolConfigImpl::extract_version(xmlNodePtr node)
{
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "major")
{
version[0] = extract_node_value<std::uint16_t>(node);
}
else if (node_name == "minor")
{
version[1] = extract_node_value<std::uint16_t>(node);
}
else
{
std::ostringstream msg;
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
}
}
void
ProtocolConfigImpl::extract_signal_prefix(xmlNodePtr node)
{
signal_prefix = extract_node_value<std::string>(node);
}
void
ProtocolConfigImpl::extract_field_sizes(xmlNodePtr node)
{
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "signal_length")
{
signal_length_size = extract_node_value<std::size_t>(node);
}
else if (node_name == "signal_id")
{
signal_id_size = extract_node_value<std::size_t>(node);
}
else if (node_name == "data_item_length")
{
data_item_length_size = extract_node_value<std::size_t>(node);
}
else if (node_name == "data_item_id")
{
data_item_id_size = extract_node_value<std::size_t>(node);
}
else if (node_name == "extension_id")
{
extension_id_size = extract_node_value<std::size_t>(node);
}
else if (node_name == "status_code")
{
status_code_size = extract_node_value<std::size_t>(node);
}
else
{
std::ostringstream msg;
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
}
}
void
ProtocolConfigImpl::extract_module(xmlNodePtr node)
{
ModuleInfo modinfo;
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "name")
{
modinfo.name = extract_node_value<std::string>(node);
}
else if (node_name == "draft")
{
modinfo.draft = extract_node_value<std::string>(node);
}
else if (node_name == "experiment_name")
{
modinfo.experiment_name = extract_node_value<std::string>(node);
}
else if (node_name == "extension_id")
{
modinfo.extension_id = extract_node_value<ExtensionIdType>(node);
}
else if (node_name == "signal")
{
extract_module_signal(node->children, modinfo);
}
else if (node_name == "data_item")
{
extract_module_data_item(node->children, modinfo);
}
else if (node_name == "status_code")
{
extract_module_status_code(node->children, modinfo);
}
else
{
std::ostringstream msg;
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
}
auto it = module_info_map.find(modinfo.name);
if (it != module_info_map.end())
{
throw BadProtocolConfig("redefinition of module " + modinfo.name);
}
module_info_map[modinfo.name] = modinfo;
}
template<typename MapType, typename IdType>
void
ProtocolConfigImpl::insert_id_name_mapping(MapType & id_name_map, IdType id,
const std::string & name,
const std::string & mapname)
{
auto itl = id_name_map.left.find(id);
if ((itl != id_name_map.left.end()) && (itl->second != name))
{
throw BadProtocolConfig(
mapname + " id " + std::to_string(id) + " has multiple names: " +
itl->second + ", " + name);
}
auto itr = id_name_map.right.find(name);
if ((itr != id_name_map.right.end()) && (itr->second != id))
{
throw BadProtocolConfig(
mapname + " name " + name + " has multiple ids: " +
std::to_string(id) + ", " + std::to_string(itr->second));
}
std::ostringstream msg;
msg << mapname << " mapping: " << name << " <--> " << id;
LOG(DLEP_LOG_DEBUG, msg);
id_name_map.insert({id, name});
}
void
ProtocolConfigImpl::extract_module_signal(xmlNodePtr node,
ModuleInfo & modinfo)
{
std::ostringstream msg;
SignalInfo siginfo;
// Was id specified? If so, this is the signal's definition, else
// it is a reference to an existing signal.
bool have_id = false;
// Which signal flags were specified? This records which flags we have a
// value (0 or 1) for. The actual flag values will be in siginfo.flags.
std::uint32_t have_flags = 0;
// shorthand for both send flags
const std::uint32_t send_flags = SignalInfo::Flags::modem_sends |
SignalInfo::Flags::router_sends;
// Is this a message as opposed to a signal?
bool is_message = false;
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "name")
{
siginfo.name = extract_node_value<std::string>(node);
}
else if (node_name == "id")
{
have_id = true;
siginfo.id = extract_node_value<SignalIdType>(node);
}
else if (node_name == "message")
{
have_flags |= SignalInfo::Flags::message;
is_message = extract_node_value<bool>(node);
if (is_message)
{
siginfo.flags |= SignalInfo::Flags::message;
}
else
{
siginfo.flags &= ~SignalInfo::Flags::message;
}
}
else if (node_name == "sender")
{
have_flags |= send_flags;
std::string sender = extract_node_value<std::string>(node);
if (sender == "modem")
{
siginfo.flags |= SignalInfo::Flags::modem_sends;
}
else if (sender == "router")
{
siginfo.flags |= SignalInfo::Flags::router_sends;
}
else // has to be both
{
siginfo.flags |= send_flags;
}
}
else if (node_name == "data_item")
{
DataItemForSignal difs =
extract_module_data_item_ref(siginfo.name, node->children);
siginfo.data_items.push_back(difs);
}
else if (node_name == "response")
{
std::string response = extract_node_value<std::string>(node);
try
{
siginfo.response_id = get_signal_id(response);
}
catch (const BadSignalName &)
{
// this signal hasn't been defined yet
throw BadProtocolConfig(
"undefined response " + response +
" for signal " + siginfo.name);
}
}
else
{
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
} // end for each xml node
// Now we have extracted all of the inforamtion about this
// signal/message from the xml tree, and we have to store it. To
// begin, we'll assume we are dealing with a signal, and
// initialize id_name_map/id_info_map accordingly. If it turns
// out to be a message, later we'll change these to point to the
// message maps.
auto * id_name_map = &signal_map;
auto * id_info_map = &signal_info_map;
// If have_id is set, this is the signal's definition. Check that
// the message and sender fields were also specified. I didn't
// find a way to express the constraint "if this field is present, then
// these other fields must also be present" in the XML schema, so
// it's enforced here instead.
if (have_id)
{
if (!(have_flags & SignalInfo::Flags::message))
{
throw BadProtocolConfig("definition of signal/message " +
siginfo.name +
" must specify a message element");
}
if (!(have_flags & send_flags))
{
throw BadProtocolConfig("definition of signal/message " +
siginfo.name +
" must specify a sender element");
}
// Enter the id/name mapping into the appropriate table.
if (is_message)
{
id_name_map = &message_map;
id_info_map = &message_info_map;
}
insert_id_name_mapping(*id_name_map, siginfo.id, siginfo.name,
(is_message ? "message" : "signal"));
}
else
{
// This is a reference to what should be an already-defined signal,
// probably by an extension. Find the id for the signal being
// referenced.
try
{
bool is_signal = false;
siginfo.id = get_signal_id(siginfo.name, &is_signal);
if (! is_signal)
{
is_message = true;
id_name_map = &message_map;
id_info_map = &message_info_map;
}
}
catch (const BadSignalName &)
{
// this signal hasn't been defined yet
throw BadProtocolConfig("undefined signal/message " + siginfo.name);
}
}
// Look up the signal info for this signal id. If we're just defining the
// signal now, it won't be found.
// XXX_20 does this handle redefinition, which should be an error?
auto it = id_info_map->find(siginfo.id);
if (it == id_info_map->end())
{
// New signal definition
assert(have_id);
siginfo.module = modinfo.name;
// Put the id in the right vector, messages vs. signals
if (is_message)
{
modinfo.messages.push_back(siginfo.id);
}
else
{
modinfo.signals.push_back(siginfo.id);
}
(*id_info_map)[siginfo.id] = siginfo;
}
else
{
// An extension is modifying this signal. Incorporate the changes.
SignalInfo & existing_siginfo = it->second;
// If flags are being changed, store the new flag values,
// being careful to not modify any flags that were not
// specified above.
if (have_flags)
{
std::uint32_t previous_flags = existing_siginfo.flags;
// clear old flag values
existing_siginfo.flags &= ~have_flags;
// store new flag values
existing_siginfo.flags |= siginfo.flags;
if (previous_flags != existing_siginfo.flags)
{
msg << "module " << modinfo.name
<< " changed signal " << siginfo.name
<< " flags from " << previous_flags
<< " to " << existing_siginfo.flags;
LOG(DLEP_LOG_INFO, msg);
}
}
// Append any new data items to the existing signal
// configuration info.
if (!siginfo.data_items.empty())
{
existing_siginfo.data_items.insert(
existing_siginfo.data_items.end(),
siginfo.data_items.begin(),
siginfo.data_items.end());
msg << "module " << modinfo.name
<< " added " << siginfo.data_items.size()
<< " data items to signal " << siginfo.name;
LOG(DLEP_LOG_INFO, msg);
}
} // end else modifying an existing signal
}
// Extract a reference to a previously defined data item.
// Signals/messages contain these references to specify which data
// items are allowed in the signal. Similarly, data items that
// contain sub data items contain these references to specify which
// data items are allowed as sub data items.
SubDataItem
ProtocolConfigImpl::extract_module_data_item_ref(const std::string & parent_name,
_xmlNode * node)
{
SubDataItem sdi;
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "name")
{
sdi.name = extract_node_value<std::string>(node);
// Looking up the data item's info here to ensure
// that the data item has already been defined.
try
{
DataItemInfo di_info = get_data_item_info(sdi.name);
}
catch (const std::invalid_argument &)
{
throw BadProtocolConfig("In " + parent_name +
", undefined reference to data item " +
sdi.name);
}
// If this data item has an id defined at the top level,
// copy it here. The copied id may be overridden if an id
// is configured for this data item reference.
try
{
sdi.id = get_data_item_id(sdi.name);
}
catch (const BadDataItemName &)
{
// id doesn't have to be defined at the top level
}
}
else if (node_name == "id")
{
// The schema doesn't allow an id field for data item
// references in signals, so we should only come here for
// sub data items.
sdi.id = extract_node_value<DataItemIdType>(node);
}
else if (node_name == "occurs")
{
sdi.occurs = extract_node_value<std::string>(node);
}
else
{
std::ostringstream msg;
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
}
// Error if the id wasn't defined anywhere.
if (sdi.id == IdUndefined)
{
throw BadProtocolConfig("In " + parent_name +
", id is undefined for data item reference " + sdi.name);
}
return sdi;
}
void
ProtocolConfigImpl::extract_module_data_item(xmlNodePtr node,
ModuleInfo & modinfo)
{
DataItemInfo di_info;
di_info.module = modinfo.name;
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "name")
{
di_info.name = extract_node_value<std::string>(node);
}
else if (node_name == "id")
{
di_info.id = extract_node_value<DataItemIdType>(node);
insert_id_name_mapping(data_item_map, di_info.id,
di_info.name, "data_item");
}
else if (node_name == "type")
{
std::string type_str = extract_node_value<std::string>(node);
try
{
di_info.value_type = from_string(type_str);
}
catch (const std::exception & e)
{
throw BadProtocolConfig(
" unrecognized data item type " + type_str);
}
}
else if (node_name == "metric")
{
bool is_metric = extract_node_value<bool>(node);
if (is_metric)
{
di_info.flags |= DataItemInfo::Flags::metric;
}
else
{
di_info.flags &= ~DataItemInfo::Flags::metric;
}
}
else if (node_name == "units")
{
di_info.units = extract_node_value<std::string>(node);
}
else if (node_name == "sub_data_item")
{
SubDataItem sdi =
extract_module_data_item_ref(di_info.name, node->children);
di_info.sub_data_items.push_back(sdi);
}
else
{
std::ostringstream msg;
msg << "ignoring unrecognized xml node " << node_name;
LOG(DLEP_LOG_INFO, msg);
}
}
auto it = data_item_info_map.find(di_info.name);
if (it != data_item_info_map.end())
{
throw BadProtocolConfig("redefinition of data item " + di_info.name);
}
modinfo.data_items.push_back(di_info.name);
data_item_info_map[di_info.name] = di_info;
}
void
ProtocolConfigImpl::extract_module_status_code(xmlNodePtr node,
ModuleInfo & modinfo)
{
StatusCodeInfo sc_info;
sc_info.module = modinfo.name;
for (; node; node = node->next)
{
std::string node_name((const char *)node->name);
if (node_name == "name")
{
sc_info.name = extract_node_value<std::string>(node);
}
else if (node_name == "id")
{
sc_info.id = extract_node_value<StatusCodeIdType>(node);
insert_id_name_mapping(status_code_map, sc_info.id,
sc_info.name, "status_code");
}
else if (node_name == "failure_mode")
{
sc_info.failure_mode = extract_node_value<std::string>(node);
}
else
{
std::ostringstream msg;