00001 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
00002 #include "otsdaq-core/Macros/ConfigurationPluginMacros.h"
00003 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00004
00005 #include <iostream>
00006 #include <fstream>
00007 #include <stdio.h>
00008
00009 using namespace ots;
00010
00011
00012 #define XDAQ_RUN_FILE std::string(getenv("XDAQ_CONFIGURATION_DATA_PATH")) + "/"+ std::string(getenv("XDAQ_CONFIGURATION_XML")) + ".xml"
00013
00014
00015
00016
00017
00018 XDAQContextConfiguration::XDAQContextConfiguration(void)
00019 : ConfigurationBase("XDAQContextConfiguration")
00020 {
00022
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 }
00043
00044
00045 XDAQContextConfiguration::~XDAQContextConfiguration(void)
00046 {}
00047
00048
00049 void XDAQContextConfiguration::init(ConfigurationManager* configManager)
00050 {
00051 extractContexts(configManager);
00052
00054
00055 std::fstream fs;
00056 fs.open(XDAQ_RUN_FILE, std::fstream::out | std::fstream::trunc);
00057 if(fs.fail())
00058 {
00059 __SS__ << "Failed to open XDAQ run file: " << XDAQ_RUN_FILE << std::endl;
00060 throw std::runtime_error(ss.str());
00061 }
00062 outputXDAQXML((std::ostream &)fs);
00063 fs.close();
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 }
00087
00088
00089
00090 bool XDAQContextConfiguration::isARTDAQContext(const std::string &contextUID)
00091 {
00092 return (contextUID.find("ART") == 0 ||
00093 contextUID.find("ARTDAQ") == 0);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 unsigned int XDAQContextConfiguration::getARTDAQAppRank(const std::string &contextUID) const
00104 {
00105
00106
00107
00108 unsigned int rank = 0;
00109
00110 for(auto &i : artdaqBoardReaders_)
00111 {
00112 if(contexts_[i].contextUID_ == contextUID)
00113 return rank;
00114 ++rank;
00115 }
00116 for(auto &i : artdaqEventBuilders_)
00117 {
00118 if(contexts_[i].contextUID_ == contextUID)
00119 return rank;
00120 ++rank;
00121 }
00122 for(auto &i : artdaqAggregators_)
00123 {
00124 if(contexts_[i].contextUID_ == contextUID)
00125 return rank;
00126 ++rank;
00127 }
00128
00129 if(contextUID == "X")
00130 return rank;
00131
00132 if(!rank)
00133 {
00134 __COUT__ << "Assuming since there are 0 active ARTDAQ context UID(s), we can ignore rank failure." << std::endl;
00135 return -1;
00136 }
00137 __SS__ << "ARTDAQ rank could not be found for context UID '" <<
00138 contextUID << "' - there were " << rank
00139 << " active ARTDAQ context UID(s) checked." << std::endl;
00140 __COUT_ERR__ << "\n" << ss.str();
00141 throw std::runtime_error(ss.str());
00142 return -1;
00143 }
00144
00145
00146 std::vector<const XDAQContextConfiguration::XDAQContext *> XDAQContextConfiguration::getBoardReaderContexts() const
00147 {
00148 std::vector<const XDAQContext *> retVec;
00149 for(auto &i : artdaqBoardReaders_)
00150 retVec.push_back(&contexts_[i]);
00151 return retVec;
00152 }
00153
00154 std::vector<const XDAQContextConfiguration::XDAQContext *> XDAQContextConfiguration::getEventBuilderContexts() const
00155 {
00156 std::vector<const XDAQContext *> retVec;
00157 for(auto &i : artdaqEventBuilders_)
00158 retVec.push_back(&contexts_[i]);
00159 return retVec;
00160
00161 }
00162
00163 std::vector<const XDAQContextConfiguration::XDAQContext *> XDAQContextConfiguration::getAggregatorContexts() const
00164 {
00165 std::vector<const XDAQContext *> retVec;
00166 for(auto &i : artdaqAggregators_)
00167 retVec.push_back(&contexts_[i]);
00168 return retVec;
00169 }
00170
00171
00172 ConfigurationTree XDAQContextConfiguration::getSupervisorConfigNode(ConfigurationManager *configManager,
00173 const std::string &contextUID, const std::string &appUID) const
00174 {
00175 return configManager->__SELF_NODE__.getNode(
00176 contextUID + "/" +
00177 colContext_.colLinkToApplicationConfiguration_ + "/" +
00178 appUID + "/" +
00179 colApplication_.colLinkToSupervisorConfiguration_);
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 }
00202
00203
00204
00205
00206
00207 void XDAQContextConfiguration::extractContexts(ConfigurationManager* configManager)
00208 {
00209
00210
00211
00212
00213
00214
00215 auto children = configManager->__SELF_NODE__.getChildren();
00216
00217 contexts_.clear();
00218 artdaqContexts_.clear();
00219
00220 artdaqBoardReaders_.clear();
00221 artdaqEventBuilders_.clear();
00222 artdaqAggregators_.clear();
00223
00224 for(auto &child:children)
00225 {
00226 contexts_.push_back(XDAQContext());
00227
00228
00229
00230 contexts_.back().contextUID_ = child.first;
00231
00232 contexts_.back().sourceConfig_ = child.second.getConfigurationName() + "_v" +
00233 child.second.getConfigurationVersion().toString() + " @ " +
00234 std::to_string(child.second.getConfigurationCreationTime());
00235 child.second.getNode(colContext_.colContextUID_ ).getValue(contexts_.back().contextUID_);
00236 child.second.getNode(colContext_.colStatus_ ).getValue(contexts_.back().status_);
00237 child.second.getNode(colContext_.colId_ ).getValue(contexts_.back().id_);
00238 child.second.getNode(colContext_.colAddress_ ).getValue(contexts_.back().address_);
00239 child.second.getNode(colContext_.colPort_ ).getValue(contexts_.back().port_);
00240
00241
00242 auto appLink = child.second.getNode(colContext_.colLinkToApplicationConfiguration_);
00243 if(appLink.isDisconnected())
00244 {
00245 __SS__ << "Application link is disconnected!" << std::endl;
00246 throw std::runtime_error(ss.str());
00247 }
00248
00249
00250 auto appChildren = appLink.getChildren();
00251 for(auto appChild:appChildren)
00252 {
00253
00254
00255 contexts_.back().applications_.push_back(XDAQApplication());
00256
00257 contexts_.back().applications_.back().applicationGroupID_ = child.first;
00258 contexts_.back().applications_.back().sourceConfig_ = appChild.second.getConfigurationName() + "_v" +
00259 appChild.second.getConfigurationVersion().toString() + " @ " +
00260 std::to_string(appChild.second.getConfigurationCreationTime());
00261
00262 appChild.second.getNode(colApplication_.colApplicationUID_ ).getValue(contexts_.back().applications_.back().applicationUID_);
00263 appChild.second.getNode(colApplication_.colStatus_ ).getValue(contexts_.back().applications_.back().status_);
00264 appChild.second.getNode(colApplication_.colClass_ ).getValue(contexts_.back().applications_.back().class_);
00265 appChild.second.getNode(colApplication_.colId_ ).getValue(contexts_.back().applications_.back().id_);
00266
00267
00268 if(appChild.second.getNode(colApplication_.colInstance_ ).isDefaultValue())
00269 contexts_.back().applications_.back().instance_ = 1;
00270 else
00271 appChild.second.getNode(colApplication_.colInstance_ ).getValue(contexts_.back().applications_.back().instance_);
00272
00273 if(appChild.second.getNode(colApplication_.colNetwork_ ).isDefaultValue())
00274 contexts_.back().applications_.back().network_ = "local";
00275 else
00276 appChild.second.getNode(colApplication_.colNetwork_ ).getValue(contexts_.back().applications_.back().network_);
00277
00278 if(appChild.second.getNode(colApplication_.colGroup_ ).isDefaultValue())
00279 contexts_.back().applications_.back().group_ = "daq";
00280 else
00281 appChild.second.getNode(colApplication_.colGroup_ ).getValue(contexts_.back().applications_.back().group_);
00282
00283 appChild.second.getNode(colApplication_.colModule_ ).getValue(contexts_.back().applications_.back().module_);
00284
00285
00286 auto appPropertyLink = appChild.second.getNode(colApplication_.colLinkToPropertyConfiguration_);
00287 if(!appPropertyLink.isDisconnected())
00288 {
00289
00290
00291 auto appPropertyChildren = appPropertyLink.getChildren();
00292
00293
00294
00295 for(auto appPropertyChild:appPropertyChildren)
00296 {
00297 contexts_.back().applications_.back().properties_.push_back(XDAQApplicationProperty());
00298 contexts_.back().applications_.back().properties_.back().status_ =
00299 appPropertyChild.second.getNode(colAppProperty_.colStatus_).getValue<bool>();
00300 contexts_.back().applications_.back().properties_.back().name_ =
00301 appPropertyChild.second.getNode(colAppProperty_.colPropertyName_).getValue<std::string>();
00302 contexts_.back().applications_.back().properties_.back().type_ =
00303 appPropertyChild.second.getNode(colAppProperty_.colPropertyType_).getValue<std::string>();
00304 contexts_.back().applications_.back().properties_.back().value_ =
00305 appPropertyChild.second.getNode(colAppProperty_.colPropertyValue_).getValue<std::string>();
00306
00307
00308 }
00309 }
00310
00311
00312 }
00313
00314
00315 if(isARTDAQContext(contexts_.back().contextUID_))
00316 {
00317 artdaqContexts_.push_back(contexts_.size()-1);
00318
00319 if(contexts_.back().applications_.size() != 1)
00320 {
00321 __SS__ << "ARTDAQ Context '" << contexts_.back().contextUID_ <<
00322 "' must have one Application! " <<
00323 contexts_.back().applications_.size() << " were found. " << std::endl;
00324 throw std::runtime_error(ss.str());
00325 }
00326
00327 if(!contexts_.back().status_) continue;
00328
00329 if(contexts_.back().applications_[0].class_ ==
00330 "ots::ARTDAQDataManagerSupervisor")
00331 artdaqBoardReaders_.push_back(contexts_.size()-1);
00332 else if(contexts_.back().applications_[0].class_ ==
00333 "ots::EventBuilderApp")
00334 artdaqEventBuilders_.push_back(contexts_.size()-1);
00335 else if(contexts_.back().applications_[0].class_ ==
00336 "ots::AggregatorApp")
00337 artdaqAggregators_.push_back(contexts_.size()-1);
00338 else
00339 {
00340 __SS__ << "ARTDAQ Context must be have Application of an allowed class type:\n " <<
00341 "\tots::ARTDAQDataManagerSupervisor\n" <<
00342 "\tots::EventBuilderApp\n" <<
00343 "\tots::AggregatorApp\n" << "\nClass found was " <<
00344 contexts_.back().applications_[0].class_ << std::endl;
00345 throw std::runtime_error(ss.str());
00346 }
00347
00348 }
00349
00350 }
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 void XDAQContextConfiguration::outputXDAQXML(std::ostream &out)
00554 {
00555
00556
00559
00560
00561
00562 out << "<?xml version='1.0'?>\n" <<
00563 "<xc:Partition \txmlns:xsi\t= \"http://www.w3.org/2001/XMLSchema-instance\"\n" <<
00564 "\t\txmlns:soapenc\t= \"http://schemas.xmlsoap.org/soap/encoding/\"\n" <<
00565 "\t\txmlns:xc\t= \"http://xdaq.web.cern.ch/xdaq/xsd/2004/XMLConfiguration-30\">\n\n";
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 char tmp[200];
00577 for(XDAQContext &context:contexts_)
00578 {
00579
00580
00581
00582 sprintf(tmp,"\t<!-- ContextUID='%s' sourceConfig='%s' -->",
00583 context.contextUID_.c_str(), context.sourceConfig_.c_str());
00584 out << tmp << "\n";
00585
00586 if(!context.status_)
00587 out << "\t<!--\n";
00588
00589 sprintf(tmp,"\t<xc:Context id=\"%u\" url=\"%s:%u\">", context.id_, context.address_.c_str(), context.port_);
00590 out << tmp << "\n\n";
00591
00592 for(XDAQApplication &app:context.applications_)
00593 {
00594
00595
00596
00597 if(context.status_)
00598 {
00599 sprintf(tmp,"\t\t<!-- Application GroupID = '%s' UID='%s' sourceConfig='%s' -->",
00600 app.applicationGroupID_.c_str(), app.applicationUID_.c_str(), app.sourceConfig_.c_str());
00601 out << tmp << "\n";
00602
00603 if(!app.status_)
00604 out << "\t\t<!--\n";
00605 }
00606
00607 sprintf(tmp,"\t\t<xc:Application class=\"%s\" id=\"%u\" instance=\"%u\" network=\"%s\" group=\"%s\">\n",
00608 app.class_.c_str(), app.id_, app.instance_, app.network_.c_str(), app.group_.c_str());
00609 out << tmp;
00610
00612 int foundColon = app.class_.rfind(':');
00613 if(foundColon >= 0) ++foundColon;
00614 out << "\t\t\t<properties xmlns=\"urn:xdaq-application:" <<
00615 app.class_.substr(foundColon) <<
00616 "\" xsi:type=\"soapenc:Struct\">\n";
00617
00618
00619 for(XDAQApplicationProperty &appProperty:app.properties_)
00620 {
00621
00622 if(!appProperty.status_)
00623 out << "\t\t\t\t<!--\n";
00624
00625 sprintf(tmp,"\t\t\t\t<%s xsi:type=\"%s\">%s</%s>\n",
00626 appProperty.name_.c_str(),
00627 appProperty.type_.c_str(),
00628 appProperty.value_.c_str(),
00629 appProperty.name_.c_str());
00630 out << tmp;
00631
00632 if(!appProperty.status_)
00633 out << "\t\t\t\t-->\n";
00634 }
00635 out << "\t\t\t</properties>\n";
00637
00638 out << "\t\t</xc:Application>\n";
00639
00640
00641 sprintf(tmp,"\t\t<xc:Module>%s</xc:Module>\n", app.module_.c_str());
00642 out << tmp;
00643
00644 if(context.status_ && !app.status_)
00645 out << "\t\t-->\n";
00646 out << "\n";
00647 }
00648
00649 out << "\t</xc:Context>\n";
00650 if(!context.status_)
00651 out << "\t-->\n";
00652 out << "\n";
00653 }
00654
00655 out << "</xc:Partition>\n\n\n";
00656
00657 }
00658
00659
00660 std::string XDAQContextConfiguration::getContextUID(const std::string &url) const
00661 {
00662 for(auto context: contexts_)
00663 {
00664 if(!context.status_) continue;
00665
00666 if(url == context.address_ + ":" + std::to_string(context.port_))
00667 return context.contextUID_;
00668 }
00669 return "";
00670 }
00671
00672
00673 std::string XDAQContextConfiguration::getApplicationUID(const std::string &url, unsigned int id) const
00674 {
00675 for(auto context: contexts_)
00676 {
00677 if(!context.status_) continue;
00678
00679 if(url == context.address_ + ":" + std::to_string(context.port_))
00680 for(auto application: context.applications_)
00681 {
00682 if(!application.status_) continue;
00683
00684 if(application.id_ == id)
00685 {
00686 return application.applicationUID_;
00687 }
00688 }
00689 }
00690 return "";
00691 }
00692
00693 DEFINE_OTS_CONFIGURATION(XDAQContextConfiguration)