otsdaq  v2_05_02_indev
ConfigurationManager.h
1 #ifndef _ots_ConfigurationManager_h_
2 #define _ots_ConfigurationManager_h_
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 
8 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
9 #include "otsdaq/ConfigurationInterface/ConfigurationTree.h"
10 #include "otsdaq/TableCore/TableVersion.h"
11 
12 namespace ots
13 {
14 class ProgressBar;
15 
16 #define __GET_CONFIG__(X) getTable<X>(QUOTE(X))
17 
19 {
20  // ConfigurationManagerRW is a "Friend" class of ConfigurationManager so has access to
21  // private members.
22  friend class ConfigurationManagerRW;
23  friend class GatewaySupervisor;
24 
25  public:
26  //==============================================================================
27  // Static members
28  static const std::string READONLY_USER;
29  static const std::string ACTIVE_GROUPS_FILENAME;
30  static const std::string ALIAS_VERSION_PREAMBLE;
31  static const std::string SCRATCH_VERSION_ALIAS;
32 
33  static const std::string XDAQ_CONTEXT_TABLE_NAME;
34  static const std::string XDAQ_APPLICATION_TABLE_NAME;
35  static const std::string XDAQ_APP_PROPERTY_TABLE_NAME;
36  static const std::string GROUP_ALIASES_TABLE_NAME;
37  static const std::string VERSION_ALIASES_TABLE_NAME;
38  static const std::string ARTDAQ_TOP_TABLE_NAME;
39  static const std::string DESKTOP_ICON_TABLE_NAME;
40 
41  static const std::string ACTIVE_GROUP_NAME_CONTEXT;
42  static const std::string ACTIVE_GROUP_NAME_BACKBONE;
43  static const std::string ACTIVE_GROUP_NAME_ITERATE;
44  static const std::string ACTIVE_GROUP_NAME_CONFIGURATION;
45  static const std::string ACTIVE_GROUP_NAME_UNKNOWN;
46 
47  static const std::string LAST_TABLE_GROUP_SAVE_PATH;
48  static const std::string LAST_ACTIVATED_CONFIG_GROUP_FILE;
49  static const std::string LAST_ACTIVATED_CONTEXT_GROUP_FILE;
50  static const std::string LAST_ACTIVATED_BACKBONE_GROUP_FILE;
51  static const std::string LAST_ACTIVATED_ITERATOR_GROUP_FILE;
52 
53  static const uint8_t METADATA_COL_ALIASES;
54  static const uint8_t METADATA_COL_COMMENT;
55  static const uint8_t METADATA_COL_AUTHOR;
56  static const uint8_t METADATA_COL_TIMESTAMP;
57 
58  static const std::set<std::string> contextMemberNames_; // list of context members
59  static const std::set<std::string> backboneMemberNames_; // list of backbone members
60  static const std::set<std::string> iterateMemberNames_; // list of iterate members
61  std::set<std::string> configurationMemberNames_; // list of 'active' configuration members
62 
63  enum class GroupType
64  {
65  CONTEXT_TYPE,
66  BACKBONE_TYPE,
67  ITERATE_TYPE,
68  CONFIGURATION_TYPE
69  };
70 
71  // clang-format off
72 
73  static const std::set<std::string>& getContextMemberNames (void);
74  static const std::set<std::string>& getBackboneMemberNames (void);
75  static const std::set<std::string>& getIterateMemberNames (void);
76  const std::set<std::string>& getConfigurationMemberNames (void);
77 
78  static const std::string& convertGroupTypeToName (const ConfigurationManager::GroupType& groupTypeId);
79  static ConfigurationManager::GroupType getTypeOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
80  static const std::string& getTypeNameOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
81 
82  //==============================================================================
83  // Construct/Destruct
84 
85  ConfigurationManager(bool initForWriteAccess = false,
86  bool initializeFromFhicl = false);
87  virtual ~ConfigurationManager(void);
88 
89 
90 
91  void init (std::string* accumulatedErrors = 0, bool initForWriteAccess = false, std::string* accumulatedWarnings = 0);
92  void destroy (void);
93  void destroyTableGroup (const std::string& theGroup = "", bool onlyDeactivate = false);
94 
95  //==============================================================================
96  // Getters
97 
98  void loadTableGroup (
99  const std::string& configGroupName,
100  TableGroupKey tableGroupKey,
101  bool doActivate = false,
102  std::map<std::string, TableVersion>* groupMembers = 0,
103  ProgressBar* progressBar = 0,
104  std::string* accumulateWarnings = 0,
105  std::string* groupComment = 0,
106  std::string* groupAuthor = 0,
107  std::string* groupCreateTime = 0,
108  bool doNotLoadMember = false,
109  std::string* groupTypeString = 0,
110  std::map<std::string /*name*/, std::string /*alias*/>* groupAliases = 0,
111  bool onlyLoadIfBackboneOrContext = false);
112  void loadMemberMap (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap, std::string* accumulateWarnings = 0);
113  TableGroupKey loadConfigurationBackbone (void);
114 
115  //================
116  // getTable
117  // get configuration * with specific configuration type
118  template<class T>
119  const T* getTable (const std::string& tableName) const { const T* retPtr = dynamic_cast<const T*>(getTableByName(tableName)); if(retPtr == nullptr) { __SS__ << "Illegal cast of '" << tableName << "' to type " << StringMacros::getTypeName<T>() << __E__; __SS_THROW__ } return retPtr;}
120  const TableBase* getTableByName (const std::string& configurationName) const;
121 
122  void dumpActiveConfiguration (const std::string& filePath, const std::string& dumpType);
123  void dumpMacroMakerModeFhicl (void);
124 
125  std::map<std::string /*groupAlias*/,
126  std::pair<std::string /*groupName*/,
127  TableGroupKey>> getActiveGroupAliases (void);
128  // Note: this ConfigurationManager::getVersionAliases is called internally and by
129  // ConfigurationManagerRW::getVersionAliases
130  std::map<std::string /*tableName*/,
131  std::map<std::string /*aliasName*/,
132  TableVersion>> getVersionAliases (void) const;
133  std::pair<std::string /*groupName*/,
134  TableGroupKey> getTableGroupFromAlias (std::string systemAlias, ProgressBar* progressBar = 0);
135  std::map<std::string /*groupType*/,
136  std::pair<std::string /*groupName*/,
137  TableGroupKey>> getActiveTableGroups (void) const;
138  const std::map<std::string /*groupType*/,
139  std::pair<std::string /*groupName*/,
140  TableGroupKey>>& getFailedTableGroups (void) const {return lastFailedGroupLoad_;}
141  const std::string& getActiveGroupName (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
142  TableGroupKey getActiveGroupKey (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
143 
144  ConfigurationTree getNode (const std::string& nodeString, bool doNotThrowOnBrokenUIDLinks = false) const; //"root/parent/parent/"
145  ConfigurationTree getContextNode (const std::string& contextUID, const std::string& applicationUID) const;
146  ConfigurationTree getSupervisorNode (const std::string& contextUID, const std::string& applicationUID) const;
147  ConfigurationTree getSupervisorTableNode (const std::string& contextUID, const std::string& applicationUID) const;
148 
149  std::vector<std::pair<std::string /*childName*/,
150  ConfigurationTree>> getChildren (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
151  std::string getFirstPathToNode (const ConfigurationTree& node, const std::string& startPath = "/") const;
152 
153  std::map<std::string, TableVersion> getActiveVersions (void) const;
154 
155  const std::string& getOwnerContext (void) { return ownerContextUID_; }
156  const std::string& getOwnerApp (void) { return ownerAppUID_; }
157  bool isOwnerFirstAppInContext (void);
158 
159  //==============================================================================
160  // Setters/Modifiers
161  std::shared_ptr<TableGroupKey> makeTheTableGroupKey (TableGroupKey key);
162  void restoreActiveTableGroups (bool throwErrors = false, const std::string& pathToActiveGroupsFile = "", bool onlyLoadIfBackboneOrContext = false, std::string* accumulatedWarnings = 0);
163 
164  void setOwnerContext (const std::string& contextUID) { ownerContextUID_ = contextUID; }
165  void setOwnerApp (const std::string& appUID) { ownerAppUID_ = appUID; }
166  static void saveGroupNameAndKey (const std::pair<std::string /*group name*/, TableGroupKey>& theGroup,const std::string& fileName);
167  static std::pair<
168  std::string /*group name*/,
169  TableGroupKey> loadGroupNameAndKey (const std::string& fileName, std::string& returnedTimeString);
170 
171 
172  private:
173  ConfigurationManager (const std::string& userName); // private constructor called by ConfigurationManagerRW
174 
175  TableBase* getDesktopIconTable (void); //to dynamically affect desktop icons in otherwise readonly environment (e.g. GatewaySupervisor add icon behavior)
176 
177  void initializeFromFhicl (const std::string& fhiclPath);
178  void recursiveInitFromFhiclPSet (const std::string& tableName, const fhicl::ParameterSet& pset, const std::string& recordName = "", const std::string& groupName = "", const std::string& groupLinkIndex = "");
179  void recursiveTreeToFhicl (ConfigurationTree node, std::ostream& out, std::string& tabStr, std::string& commentStr, unsigned int depth = -1);
180 
181 
182  std::string username_; // user of the configuration is READONLY_USER unless using ConfigurationManagerRW
183  ConfigurationInterface* theInterface_;
184  std::shared_ptr<TableGroupKey> theConfigurationTableGroupKey_, theContextTableGroupKey_, theBackboneTableGroupKey_, theIterateTableGroupKey_;
185  std::string theConfigurationTableGroup_, theContextTableGroup_, theBackboneTableGroup_, theIterateTableGroup_;
186 
187  std::map<std::string,
188  std::pair<std::string, TableGroupKey>> lastFailedGroupLoad_;
189 
190  std::map<std::string, TableBase*> nameToTableMap_;
191 
192  TableBase groupMetadataTable_; // special table - version saved each time a group is created
193 
194  std::string ownerContextUID_; // optional, often there is a context that owns this configuration manager
195  std::string ownerAppUID_; // optional, often there is a supervisor that owns this configuration manager
196 
197  // clang-format on
198 };
199 } // namespace ots
200 
201 #endif