EnvironmentMgr.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "EnvironmentMgr.hpp"
  14. #include "Exceptions.hpp"
  15. #include "XMLEnvironmentMgr.hpp"
  16. #include "InsertableItem.hpp"
  17. #include "Utils.hpp"
  18. #include "jfile.hpp"
  19. #ifndef WIN32
  20. #include <dlfcn.h>
  21. #endif
  22. std::atomic_int EnvironmentMgr::m_key(1);
  23. EnvironmentMgr *getEnvironmentMgrInstance(const EnvironmentType envType)
  24. {
  25. EnvironmentMgr *pEnvMgr = nullptr;
  26. if (envType == XML)
  27. {
  28. pEnvMgr = new XMLEnvironmentMgr();
  29. }
  30. return pEnvMgr;
  31. }
  32. EnvironmentMgr::EnvironmentMgr() :
  33. m_message("Unknown error")
  34. {
  35. m_pSchema = std::make_shared<SchemaItem>("root"); // make the root
  36. }
  37. bool EnvironmentMgr::loadSchema(const std::string &configPath, const std::string &masterConfigFile, const std::map<std::string, std::string> &cfgParms)
  38. {
  39. bool rc = false;
  40. if (createParser())
  41. {
  42. rc = m_pSchemaParser->parse(configPath, masterConfigFile, cfgParms);
  43. if (rc)
  44. {
  45. try {
  46. // unique attribure value sets are global across a schema. Allocate one here and pass it in
  47. // for use in building the necessary references and dependencies across the schema, then pass
  48. // it to the post processing for finalization. Once references and dependencies are built, the
  49. // attribute value sets are no longer needed.
  50. std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> uniqueAttributeValueSets;
  51. m_pSchema->processDefinedUniqueAttributeValueSets(uniqueAttributeValueSets); // This must be done first
  52. m_pSchema->postProcessConfig(uniqueAttributeValueSets);
  53. }
  54. catch (ParseException &pe)
  55. {
  56. m_message = pe.what();
  57. rc = false;
  58. }
  59. }
  60. //
  61. // Load support libs based on the schema type which is read from the itemType property of the schema root node.
  62. std::string envType = m_pSchema->getItemType();
  63. std::string libPath = hpccBuildInfo.libDir;
  64. std::string libMask = "libcfg" + envType + "_*";
  65. Owned<IFile> pDir = createIFile(libPath.c_str());
  66. if (pDir->exists())
  67. {
  68. Owned<IDirectoryIterator> it = pDir->directoryFiles(libMask.c_str(), false, false);
  69. ForEach(*it)
  70. {
  71. StringBuffer fname;
  72. std::string filename = it->getName(fname).str();
  73. std::shared_ptr<EnvSupportLib> pLib = std::make_shared<EnvSupportLib>(filename, this);
  74. if (pLib->isValid())
  75. {
  76. m_supportLibs.push_back(pLib);
  77. }
  78. }
  79. }
  80. }
  81. return rc;
  82. }
  83. std::string EnvironmentMgr::getLastSchemaMessage() const
  84. {
  85. std::string msg;
  86. if (m_pSchemaParser)
  87. msg = m_pSchemaParser->getLastMessage();
  88. if (msg.empty())
  89. msg = m_message;
  90. return msg;
  91. }
  92. bool EnvironmentMgr::loadEnvironment(const std::string &qualifiedFilename)
  93. {
  94. bool rc = false;
  95. if (m_pSchema)
  96. {
  97. std::ifstream in;
  98. in.open(qualifiedFilename);
  99. if (in.is_open())
  100. {
  101. try
  102. {
  103. std::vector<std::shared_ptr<EnvironmentNode>> rootNodes = doLoadEnvironment(in, m_pSchema); // root
  104. if (rootNodes.size() == 1)
  105. {
  106. m_pRootNode = rootNodes[0];
  107. assignNodeIds(m_pRootNode);
  108. rc = true;
  109. }
  110. else
  111. {
  112. m_message = "There was an unknown error loading the environment";
  113. }
  114. }
  115. catch (ParseException &pe)
  116. {
  117. m_message = pe.what();
  118. }
  119. }
  120. else
  121. {
  122. m_message = "Unable to open environment file '" + qualifiedFilename + "'";
  123. }
  124. }
  125. else
  126. {
  127. m_message = "No schema loaded";
  128. }
  129. return rc;
  130. }
  131. std::string EnvironmentMgr::getRootNodeId() const
  132. {
  133. std::string nodeId;
  134. if (m_pRootNode != nullptr)
  135. {
  136. nodeId = m_pRootNode->getId();
  137. }
  138. return nodeId;
  139. }
  140. bool EnvironmentMgr::serialize(std::ostream &out, const std::shared_ptr<EnvironmentNode> &pStartNode)
  141. {
  142. return false;
  143. }
  144. bool EnvironmentMgr::saveEnvironment(const std::string &qualifiedFilename)
  145. {
  146. bool rc = false;
  147. if (m_pRootNode)
  148. {
  149. std::ofstream out;
  150. out.open(qualifiedFilename);
  151. if (out.is_open())
  152. {
  153. rc = save(out);
  154. }
  155. }
  156. else
  157. {
  158. m_message = "No environment loaded";
  159. }
  160. return rc;
  161. }
  162. void EnvironmentMgr::addPath(const std::shared_ptr<EnvironmentNode> pNode)
  163. {
  164. auto retVal = m_nodeIds.insert({pNode->getId(), pNode });
  165. if (!retVal.second)
  166. {
  167. throw (ParseException("Attempted to insert duplicate path name " + pNode->getId() + " for node "));
  168. }
  169. }
  170. std::shared_ptr<EnvironmentNode> EnvironmentMgr::findEnvironmentNodeById(const std::string &nodeId) const
  171. {
  172. std::shared_ptr<EnvironmentNode> pNode;
  173. auto pathIt = m_nodeIds.find(nodeId);
  174. if (pathIt != m_nodeIds.end())
  175. pNode = pathIt->second;
  176. return pNode;
  177. }
  178. std::shared_ptr<EnvironmentNode> EnvironmentMgr::getNewEnvironmentNode(const std::string &parentNodeId, const std::string &inputItemType, Status &status) const
  179. {
  180. std::shared_ptr<EnvironmentNode> pNewEnvNode;
  181. std::shared_ptr<EnvironmentNode> pParentNode = findEnvironmentNodeById(parentNodeId);
  182. if (pParentNode)
  183. {
  184. std::string itemType;
  185. std::vector<NameValue> initAttributeValues;
  186. getPredefinedAttributeValues(inputItemType, itemType, initAttributeValues);
  187. std::shared_ptr<SchemaItem> pNewCfgItem = findInsertableItem(pParentNode, itemType);
  188. if (pNewCfgItem)
  189. {
  190. pNewEnvNode = std::make_shared<EnvironmentNode>(pNewCfgItem, pNewCfgItem->getProperty("name"), pParentNode);
  191. pNewEnvNode->initialize();
  192. pNewEnvNode->setAttributeValues(initAttributeValues, status, false, false);
  193. }
  194. else
  195. {
  196. status.addMsg(statusMsg::error, "Configuration type (" + inputItemType + ") not found");
  197. }
  198. }
  199. return pNewEnvNode;
  200. }
  201. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::string &parentNodeId, const std::string &inputItemType,
  202. std::vector<NameValue> &initAttributes, Status &status, bool allowInvalid, bool forceCreate)
  203. {
  204. std::shared_ptr<EnvironmentNode> pNewNode;
  205. std::shared_ptr<EnvironmentNode> pParentNode = findEnvironmentNodeById(parentNodeId);
  206. if (pParentNode)
  207. {
  208. std::string itemType;
  209. std::vector<NameValue> attributeValues;
  210. getPredefinedAttributeValues(inputItemType, itemType, attributeValues);
  211. std::shared_ptr<SchemaItem> pNewCfgItem = findInsertableItem(pParentNode, itemType);
  212. if (pNewCfgItem)
  213. {
  214. // concatenate input attribute values with any predefined values.
  215. attributeValues.insert( attributeValues.end(), initAttributes.begin(), initAttributes.end() );
  216. pNewNode = addNewEnvironmentNode(pParentNode, pNewCfgItem, attributeValues, status, allowInvalid, forceCreate);
  217. if (pNewNode == nullptr)
  218. {
  219. status.addMsg(statusMsg::error, "Unable to create new node for itemType: " + inputItemType);
  220. }
  221. else
  222. {
  223. m_pSchema->validate(status, true, false);
  224. }
  225. }
  226. else
  227. {
  228. status.addMsg(statusMsg::error, "Configuration type (" + inputItemType + ") not found");
  229. }
  230. }
  231. else
  232. {
  233. status.addMsg(statusMsg::error, parentNodeId, "", "Unable to find indicated parent node");
  234. }
  235. return pNewNode;
  236. }
  237. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::shared_ptr<SchemaItem> &pCfgItem,
  238. std::vector<NameValue> &initAttributes, Status &status, bool allowInvalid, bool forceCreate)
  239. {
  240. std::shared_ptr<EnvironmentNode> pNewEnvNode;
  241. //
  242. // Create the new node and add it to the parent
  243. pNewEnvNode = std::make_shared<EnvironmentNode>(pCfgItem, pCfgItem->getProperty("name"), pParentNode);
  244. pNewEnvNode->setId(EnvironmentMgr::getUniqueKey());
  245. addPath(pNewEnvNode);
  246. pNewEnvNode->initialize();
  247. pNewEnvNode->setAttributeValues(initAttributes, status, allowInvalid, forceCreate);
  248. pParentNode->addChild(pNewEnvNode);
  249. //
  250. // Send a create event now that it's been added to the environment
  251. pCfgItem->getSchemaRoot()->processEvent("create", pNewEnvNode);
  252. //
  253. // Call any registered support libs with the event
  254. for (auto &libIt: m_supportLibs)
  255. {
  256. libIt->processEvent("create", m_pSchema, pNewEnvNode, status);
  257. }
  258. insertExtraEnvironmentData(m_pRootNode);
  259. //
  260. // Look through the children and add any that are necessary
  261. std::vector<std::shared_ptr<SchemaItem>> cfgItemChildren;
  262. pCfgItem->getChildren(cfgItemChildren);
  263. std::vector<NameValue> empty;
  264. for (auto &pCfgChild: cfgItemChildren)
  265. {
  266. for (unsigned i = 0; i<pCfgChild->getMinInstances(); ++i)
  267. {
  268. addNewEnvironmentNode(pNewEnvNode, pCfgChild, empty, status);
  269. }
  270. }
  271. return pNewEnvNode;
  272. }
  273. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::string &nodeData,
  274. Status &status, const std::string &itemType)
  275. {
  276. std::shared_ptr<EnvironmentNode> pNewEnvNode;
  277. std::istringstream newNodeData(nodeData);
  278. std::vector<std::shared_ptr<EnvironmentNode>> newNodes = doLoadEnvironment(newNodeData, pParentNode->getSchemaItem(), itemType); // not root
  279. if (!newNodes.empty())
  280. {
  281. pNewEnvNode = newNodes[0];
  282. pParentNode->addChild(pNewEnvNode);
  283. assignNodeIds(pNewEnvNode);
  284. validate(status);
  285. }
  286. else
  287. {
  288. status.addMsg(statusMsg::error, "Error pasting new node data.");
  289. }
  290. return pNewEnvNode;
  291. }
  292. std::shared_ptr<SchemaItem> EnvironmentMgr::findInsertableItem(const std::shared_ptr<EnvironmentNode> &pNode, const std::string &itemType) const
  293. {
  294. // todo -should there be an environment strict mode that forces schema compliance?
  295. std::shared_ptr<SchemaItem> pSchemaItem = std::make_shared<SchemaItem>(itemType, "default"); // default item if none found
  296. std::vector<InsertableItem> insertableItems;
  297. pNode->getInsertableItems(insertableItems);
  298. for (auto &pInsertableIt: insertableItems)
  299. {
  300. if (pInsertableIt.m_pSchemaItem->getItemType() == itemType)
  301. {
  302. pSchemaItem = pInsertableIt.m_pSchemaItem;
  303. break; // we found the insertable item we wanted, so time to get out
  304. }
  305. }
  306. return pSchemaItem;
  307. }
  308. void EnvironmentMgr::getPredefinedAttributeValues(const std::string &inputItemType, std::string &itemType,
  309. std::vector<NameValue> &initAttributes) const
  310. {
  311. //
  312. // In case nothing specifed
  313. itemType = inputItemType;
  314. size_t atPos = itemType.find_first_of('@');
  315. if (atPos != std::string::npos)
  316. {
  317. std::vector<std::string> initAttrs = splitString(inputItemType.substr(atPos + 1), ",");
  318. for (auto &initAttr: initAttrs)
  319. {
  320. std::vector<std::string> kvPair = splitString(initAttr, "=");
  321. if (kvPair.size() == 2)
  322. {
  323. initAttributes.emplace_back(NameValue(kvPair[0], kvPair[1]));
  324. }
  325. else
  326. {
  327. throw (ParseException("Invalid attribute initialization detected: " + initAttr));
  328. }
  329. }
  330. }
  331. }
  332. void EnvironmentMgr::insertExtraEnvironmentData(std::shared_ptr<EnvironmentNode> pParentNode)
  333. {
  334. std::string insertData = pParentNode->getEnvironmentInsertData();
  335. if (!insertData.empty())
  336. {
  337. std::istringstream extraData(insertData);
  338. std::vector<std::shared_ptr<EnvironmentNode>> extraNodes = doLoadEnvironment(extraData, pParentNode->getSchemaItem()); // not root
  339. for (auto &&envNode : extraNodes)
  340. {
  341. assignNodeIds(envNode);
  342. pParentNode->addChild(envNode); // link extra node data to the newly created node
  343. pParentNode->clearEnvironmentInsertData();
  344. }
  345. }
  346. std::vector<std::shared_ptr<EnvironmentNode>> childNodes;
  347. pParentNode->getChildren(childNodes);
  348. for (auto &&child : childNodes)
  349. {
  350. insertExtraEnvironmentData(child);
  351. }
  352. }
  353. bool EnvironmentMgr::removeEnvironmentNode(const std::string &nodeId)
  354. {
  355. bool rc = false;
  356. std::shared_ptr<EnvironmentNode> pNode = findEnvironmentNodeById(nodeId);
  357. std::vector<std::string> deletedNodeIds;
  358. if (pNode)
  359. {
  360. std::shared_ptr<EnvironmentNode> pParentNode = pNode->getParent();
  361. if (pParentNode->removeChild(pNode, deletedNodeIds))
  362. {
  363. for (auto delNodeId: deletedNodeIds)
  364. m_nodeIds.erase(delNodeId);
  365. rc = true;
  366. }
  367. }
  368. return rc;
  369. }
  370. std::string EnvironmentMgr::getUniqueKey()
  371. {
  372. return std::to_string(m_key++);
  373. }
  374. void EnvironmentMgr::validate(Status &status, bool includeHiddenNodes) const
  375. {
  376. if (m_pRootNode)
  377. {
  378. m_pSchema->validate(status, true, includeHiddenNodes);
  379. //
  380. // Now call all support libs for additional validation.
  381. //
  382. // Call any registered support libs with the event
  383. for (auto &libIt: m_supportLibs)
  384. {
  385. libIt->validate(m_pSchema, m_pRootNode, status);
  386. }
  387. }
  388. else
  389. {
  390. status.addMsg(statusMsg::error, "No environment loaded");
  391. }
  392. }
  393. void EnvironmentMgr::assignNodeIds(const std::shared_ptr<EnvironmentNode> &pNode)
  394. {
  395. pNode->setId(getUniqueKey());
  396. addPath(pNode);
  397. std::vector<std::shared_ptr<EnvironmentNode>> children;
  398. pNode->getChildren(children);
  399. for (auto it=children.begin(); it!=children.end(); ++it)
  400. {
  401. assignNodeIds(*it);
  402. }
  403. }
  404. void EnvironmentMgr::fetchNodes(const std::string path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes, const std::shared_ptr<EnvironmentNode> &pStartNode) const
  405. {
  406. const std::shared_ptr<EnvironmentNode> pStart = (pStartNode != nullptr) ? pStartNode : m_pRootNode;
  407. pStart->fetchNodes(path, nodes);
  408. }