EnvironmentMgr.cpp 13 KB

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