EnvironmentMgr.cpp 15 KB

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