EnvironmentMgr.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #ifndef WIN32
  19. #include <dlfcn.h>
  20. #endif
  21. std::atomic_int EnvironmentMgr::m_key(1);
  22. EnvironmentMgr *getEnvironmentMgrInstance(const EnvironmentType envType)
  23. {
  24. EnvironmentMgr *pEnvMgr = nullptr;
  25. if (envType == XML)
  26. {
  27. pEnvMgr = new XMLEnvironmentMgr();
  28. }
  29. return pEnvMgr;
  30. }
  31. EnvironmentMgr::EnvironmentMgr() :
  32. m_message("Unknown error")
  33. {
  34. m_pSchema = std::make_shared<SchemaItem>("root"); // make the root
  35. }
  36. bool EnvironmentMgr::loadSchema(const std::string &configPath, const std::string &masterConfigFile, const std::map<std::string, std::string> &cfgParms)
  37. {
  38. bool rc = false;
  39. if (createParser())
  40. {
  41. rc = m_pSchemaParser->parse(configPath, masterConfigFile, cfgParms);
  42. if (rc)
  43. {
  44. try {
  45. // unique attribure value sets are global across a schema. Allocate one here and pass it in
  46. // for use in building the necessary references and dependencies across the schema, then pass
  47. // it to the post processing for finalization. Once references and dependencies are built, the
  48. // attribute value sets are no longer needed.
  49. std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> uniqueAttributeValueSets;
  50. m_pSchema->processDefinedUniqueAttributeValueSets(uniqueAttributeValueSets); // This must be done first
  51. m_pSchema->postProcessConfig(uniqueAttributeValueSets);
  52. }
  53. catch (ParseException &pe)
  54. {
  55. m_message = pe.what();
  56. rc = false;
  57. }
  58. }
  59. //
  60. // Load any support libs that are environment specific
  61. auto findIt = cfgParms.find("support_libs");
  62. if (findIt != cfgParms.end())
  63. {
  64. std::vector<std::string> supportLibNames = splitString(findIt->second, ",");
  65. for (auto &libName: supportLibNames)
  66. {
  67. std::string fullName = libName + ".so";
  68. try
  69. {
  70. std::shared_ptr<EnvSupportLib> pLib = std::make_shared<EnvSupportLib>(fullName, this);
  71. m_supportLibs.push_back(pLib);
  72. }
  73. catch (ParseException &pe)
  74. {
  75. m_message = pe.what();
  76. rc = false;
  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::saveEnvironment(const std::string &qualifiedFilename)
  141. {
  142. bool rc = false;
  143. if (m_pRootNode)
  144. {
  145. std::ofstream out;
  146. out.open(qualifiedFilename);
  147. if (out.is_open())
  148. {
  149. rc = save(out);
  150. }
  151. }
  152. else
  153. {
  154. m_message = "No environment loaded";
  155. }
  156. return rc;
  157. }
  158. void EnvironmentMgr::addPath(const std::shared_ptr<EnvironmentNode> pNode)
  159. {
  160. auto retVal = m_nodeIds.insert({pNode->getId(), pNode });
  161. if (!retVal.second)
  162. {
  163. throw (ParseException("Attempted to insert duplicate path name " + pNode->getId() + " for node "));
  164. }
  165. }
  166. std::shared_ptr<EnvironmentNode> EnvironmentMgr::findEnvironmentNodeById(const std::string &nodeId) const
  167. {
  168. std::shared_ptr<EnvironmentNode> pNode;
  169. auto pathIt = m_nodeIds.find(nodeId);
  170. if (pathIt != m_nodeIds.end())
  171. pNode = pathIt->second;
  172. return pNode;
  173. }
  174. std::shared_ptr<EnvironmentNode> EnvironmentMgr::getNewEnvironmentNode(const std::string &parentNodeId, const std::string &inputItemType, Status &status) const
  175. {
  176. std::shared_ptr<EnvironmentNode> pNewEnvNode;
  177. std::shared_ptr<EnvironmentNode> pParentNode = findEnvironmentNodeById(parentNodeId);
  178. if (pParentNode)
  179. {
  180. std::string itemType;
  181. std::vector<NameValue> initAttributeValues;
  182. getInitAttributesFromItemType(inputItemType, itemType, initAttributeValues);
  183. std::shared_ptr<SchemaItem> pNewCfgItem = findInsertableItem(pParentNode, itemType);
  184. if (pNewCfgItem)
  185. {
  186. pNewEnvNode = std::make_shared<EnvironmentNode>(pNewCfgItem, pNewCfgItem->getProperty("name"), pParentNode);
  187. pNewEnvNode->initialize();
  188. pNewEnvNode->setAttributeValues(initAttributeValues, status, false, false);
  189. }
  190. else
  191. {
  192. status.addMsg(statusMsg::error, "Configuration type (" + inputItemType + ") not found");
  193. }
  194. }
  195. return pNewEnvNode;
  196. }
  197. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::string &parentNodeId, const std::string &inputItemType,
  198. std::vector<NameValue> &initAttributes, Status &status)
  199. {
  200. std::shared_ptr<EnvironmentNode> pNewNode;
  201. std::shared_ptr<EnvironmentNode> pParentNode = findEnvironmentNodeById(parentNodeId);
  202. if (pParentNode)
  203. {
  204. std::string itemType;
  205. std::vector<NameValue> initAttributeValues;
  206. getInitAttributesFromItemType(inputItemType, itemType, initAttributeValues);
  207. std::shared_ptr<SchemaItem> pNewCfgItem = findInsertableItem(pParentNode, itemType);
  208. if (pNewCfgItem)
  209. {
  210. pNewNode = addNewEnvironmentNode(pParentNode, pNewCfgItem, initAttributes, status);
  211. if (pNewNode == nullptr)
  212. {
  213. status.addMsg(statusMsg::error, "Unable to create new node for itemType: " + inputItemType);
  214. }
  215. else
  216. {
  217. pNewNode->validate(status, true, false);
  218. }
  219. }
  220. else
  221. {
  222. status.addMsg(statusMsg::error, "Configuration type (" + inputItemType + ") not found");
  223. }
  224. }
  225. else
  226. {
  227. status.addMsg(statusMsg::error, parentNodeId, "", "Unable to find indicated parent node");
  228. }
  229. return pNewNode;
  230. }
  231. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::shared_ptr<SchemaItem> &pCfgItem,
  232. std::vector<NameValue> &initAttributes, Status &status)
  233. {
  234. std::shared_ptr<EnvironmentNode> pNewEnvNode;
  235. //
  236. // Create the new node and add it to the parent
  237. pNewEnvNode = std::make_shared<EnvironmentNode>(pCfgItem, pCfgItem->getProperty("name"), pParentNode);
  238. pNewEnvNode->setId(EnvironmentMgr::getUniqueKey());
  239. addPath(pNewEnvNode);
  240. pNewEnvNode->initialize();
  241. pNewEnvNode->setAttributeValues(initAttributes, status, false, false);
  242. pParentNode->addChild(pNewEnvNode);
  243. //
  244. // Send a create event now that it's been added to the environment
  245. pCfgItem->getSchemaRoot()->processEvent("create", pNewEnvNode);
  246. //
  247. // Call any registered support libs with the event
  248. for (auto &libIt: m_supportLibs)
  249. {
  250. libIt->processEvent("create", m_pSchema, pNewEnvNode, status);
  251. }
  252. insertExtraEnvironmentData(m_pRootNode);
  253. //
  254. // Look through the children and add any that are necessary
  255. std::vector<std::shared_ptr<SchemaItem>> cfgItemChildren;
  256. pCfgItem->getChildren(cfgItemChildren);
  257. std::vector<NameValue> empty;
  258. for (auto &pCfgChild: cfgItemChildren)
  259. {
  260. for (unsigned i = 0; i<pCfgChild->getMinInstances(); ++i)
  261. {
  262. addNewEnvironmentNode(pNewEnvNode, pCfgChild, empty, status);
  263. }
  264. }
  265. return pNewEnvNode;
  266. }
  267. std::shared_ptr<SchemaItem> EnvironmentMgr::findInsertableItem(const std::shared_ptr<EnvironmentNode> &pNode, const std::string &itemType) const
  268. {
  269. std::shared_ptr<SchemaItem> pItem;
  270. std::vector<InsertableItem> insertableItems;
  271. pNode->getInsertableItems(insertableItems);
  272. for (auto &pInsertableIt: insertableItems)
  273. {
  274. if (pInsertableIt.m_pSchemaItem->getItemType() == itemType)
  275. {
  276. pItem = pInsertableIt.m_pSchemaItem;
  277. break; // we found the insertable item we wanted, so time to get out
  278. }
  279. }
  280. return pItem;
  281. }
  282. void EnvironmentMgr::getInitAttributesFromItemType(const std::string &inputItemType, std::string &itemType, std::vector<NameValue> &initAttributes) const
  283. {
  284. //
  285. // In case nothing specifed
  286. itemType = inputItemType;
  287. size_t atPos = itemType.find_first_of('@');
  288. if (atPos != std::string::npos)
  289. {
  290. std::vector<std::string> initAttrs = splitString(inputItemType.substr(atPos + 1), ",");
  291. for (auto &initAttr: initAttrs)
  292. {
  293. std::vector<std::string> kvPair = splitString(initAttr, "=");
  294. if (kvPair.size() == 2)
  295. {
  296. initAttributes.emplace_back(NameValue(kvPair[0], kvPair[1]));
  297. }
  298. else
  299. {
  300. throw (ParseException("Invalid attribute initialization detected: " + initAttr));
  301. }
  302. }
  303. }
  304. }
  305. void EnvironmentMgr::insertExtraEnvironmentData(std::shared_ptr<EnvironmentNode> pParentNode)
  306. {
  307. std::string insertData = pParentNode->getEnvironmentInsertData();
  308. if (!insertData.empty())
  309. {
  310. std::istringstream extraData(insertData);
  311. std::vector<std::shared_ptr<EnvironmentNode>> extraNodes = doLoadEnvironment(extraData, pParentNode->getSchemaItem()); // not root
  312. for (auto &&envNode : extraNodes)
  313. {
  314. assignNodeIds(envNode);
  315. pParentNode->addChild(envNode); // link extra node data to the newly created node
  316. pParentNode->clearEnvironmentInsertData();
  317. }
  318. }
  319. std::vector<std::shared_ptr<EnvironmentNode>> childNodes;
  320. pParentNode->getChildren(childNodes);
  321. for (auto &&child : childNodes)
  322. {
  323. insertExtraEnvironmentData(child);
  324. }
  325. }
  326. bool EnvironmentMgr::removeEnvironmentNode(const std::string &nodeId)
  327. {
  328. bool rc = false;
  329. std::shared_ptr<EnvironmentNode> pNode = findEnvironmentNodeById(nodeId);
  330. std::vector<std::string> deletedNodeIds;
  331. if (pNode)
  332. {
  333. std::shared_ptr<EnvironmentNode> pParentNode = pNode->getParent();
  334. if (pParentNode->removeChild(pNode, deletedNodeIds))
  335. {
  336. for (auto delNodeId: deletedNodeIds)
  337. m_nodeIds.erase(delNodeId);
  338. rc = true;
  339. }
  340. }
  341. return rc;
  342. }
  343. std::string EnvironmentMgr::getUniqueKey()
  344. {
  345. return std::to_string(m_key++);
  346. }
  347. void EnvironmentMgr::validate(Status &status, bool includeHiddenNodes) const
  348. {
  349. if (m_pRootNode)
  350. {
  351. m_pRootNode->validate(status, true, includeHiddenNodes);
  352. }
  353. else
  354. {
  355. status.addMsg(statusMsg::error, "No environment loaded");
  356. }
  357. }
  358. void EnvironmentMgr::assignNodeIds(const std::shared_ptr<EnvironmentNode> &pNode)
  359. {
  360. pNode->setId(getUniqueKey());
  361. addPath(pNode);
  362. std::vector<std::shared_ptr<EnvironmentNode>> children;
  363. pNode->getChildren(children);
  364. for (auto it=children.begin(); it!=children.end(); ++it)
  365. {
  366. assignNodeIds(*it);
  367. }
  368. }
  369. void EnvironmentMgr::fetchNodes(const std::string path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes, const std::shared_ptr<EnvironmentNode> &pStartNode) const
  370. {
  371. const std::shared_ptr<EnvironmentNode> pStart = (pStartNode != nullptr) ? pStartNode : m_pRootNode;
  372. pStart->fetchNodes(path, nodes);
  373. }