EnvironmentMgr.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. std::atomic_int EnvironmentMgr::m_key(1);
  18. EnvironmentMgr *getEnvironmentMgrInstance(const EnvironmentType envType)
  19. {
  20. EnvironmentMgr *pEnvMgr = nullptr;
  21. if (envType == XML)
  22. {
  23. pEnvMgr = new XMLEnvironmentMgr();
  24. }
  25. return pEnvMgr;
  26. }
  27. EnvironmentMgr::EnvironmentMgr()
  28. {
  29. m_pSchema = std::make_shared<SchemaItem>("root"); // make the root
  30. }
  31. bool EnvironmentMgr::loadSchema(const std::string &configPath, const std::string &masterConfigFile, const std::vector<std::string> &cfgParms)
  32. {
  33. bool rc = false;
  34. if (createParser())
  35. {
  36. rc = m_pSchemaParser->parse(configPath, masterConfigFile, cfgParms);
  37. if (rc)
  38. {
  39. // unique attribure value sets are global across a schema. Allocate one here and pass it in
  40. // for use in building the necessary references and dependencies across the schema, then pass
  41. // it to the post processing for finalization. Once referencs and dependencies are built, the
  42. // attribute value sets are no longer needed.
  43. std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> uniqueAttributeValueSets;
  44. m_pSchema->processDefinedUniqueAttributeValueSets(uniqueAttributeValueSets); // This must be done first
  45. m_pSchema->postProcessConfig(uniqueAttributeValueSets);
  46. }
  47. }
  48. return rc;
  49. }
  50. std::string EnvironmentMgr::getLastSchemaMessage() const
  51. {
  52. if (m_pSchemaParser)
  53. return m_pSchemaParser->getLastMessage();
  54. return "";
  55. }
  56. bool EnvironmentMgr::loadEnvironment(const std::string &qualifiedFilename)
  57. {
  58. bool rc = false;
  59. if (m_pSchema)
  60. {
  61. std::ifstream in;
  62. in.open(qualifiedFilename);
  63. if (in.is_open())
  64. {
  65. try
  66. {
  67. std::vector<std::shared_ptr<EnvironmentNode>> rootNodes = doLoadEnvironment(in, m_pSchema); // root
  68. if (rootNodes.size() == 1)
  69. {
  70. m_pRootNode = rootNodes[0];
  71. assignNodeIds(m_pRootNode);
  72. rc = true;
  73. }
  74. else
  75. {
  76. m_message = "There was an unknown error loading the environment";
  77. }
  78. }
  79. catch (ParseException &pe)
  80. {
  81. m_message = pe.what();
  82. }
  83. }
  84. else
  85. {
  86. m_message = "Unable to open environment file '" + qualifiedFilename + "'";
  87. }
  88. }
  89. else
  90. {
  91. m_message = "No schema loaded";
  92. }
  93. return rc;
  94. }
  95. std::string EnvironmentMgr::getRootNodeId() const
  96. {
  97. std::string nodeId;
  98. if (m_pRootNode != nullptr)
  99. {
  100. nodeId = m_pRootNode->getId();
  101. }
  102. return nodeId;
  103. }
  104. bool EnvironmentMgr::saveEnvironment(const std::string &qualifiedFilename)
  105. {
  106. bool rc = false;
  107. if (m_pRootNode)
  108. {
  109. std::ofstream out;
  110. out.open(qualifiedFilename);
  111. if (out.is_open())
  112. {
  113. rc = save(out);
  114. }
  115. }
  116. else
  117. {
  118. m_message = "No environment loaded";
  119. }
  120. return rc;
  121. }
  122. void EnvironmentMgr::addPath(const std::shared_ptr<EnvironmentNode> pNode)
  123. {
  124. auto retVal = m_nodeIds.insert({pNode->getId(), pNode });
  125. if (!retVal.second)
  126. {
  127. throw (ParseException("Attempted to insert duplicate path name " + pNode->getId() + " for node "));
  128. }
  129. }
  130. std::shared_ptr<EnvironmentNode> EnvironmentMgr::getEnvironmentNode(const std::string &nodeId)
  131. {
  132. std::shared_ptr<EnvironmentNode> pNode;
  133. auto pathIt = m_nodeIds.find(nodeId);
  134. if (pathIt != m_nodeIds.end())
  135. pNode = pathIt->second;
  136. return pNode;
  137. }
  138. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::string &parentNodeId, const std::string &configType, Status &status)
  139. {
  140. std::shared_ptr<EnvironmentNode> pNewNode;
  141. std::shared_ptr<EnvironmentNode> pParentNode = getEnvironmentNode(parentNodeId);
  142. if (pParentNode)
  143. {
  144. std::shared_ptr<SchemaItem> pNewCfgItem;
  145. std::vector<InsertableItem> insertableItems;
  146. std::string itemType = configType;
  147. std::pair<std::string, std::string> initAttributeValue;
  148. size_t atPos = itemType.find_first_of('@');
  149. if (atPos != std::string::npos)
  150. {
  151. std::string attrNameValue = itemType.substr(atPos + 1);
  152. itemType.erase(atPos, std::string::npos);
  153. size_t equalPos = attrNameValue.find_first_of('=');
  154. if (equalPos != std::string::npos)
  155. {
  156. initAttributeValue.first = attrNameValue.substr(0, equalPos);
  157. initAttributeValue.second = attrNameValue.substr(equalPos + 1);
  158. }
  159. }
  160. pParentNode->getInsertableItems(insertableItems);
  161. for (auto it = insertableItems.begin(); it != insertableItems.end(); ++it)
  162. {
  163. if ((*it).m_pSchemaItem->getItemType() == itemType)
  164. {
  165. pNewNode = addNewEnvironmentNode(pParentNode, (*it).m_pSchemaItem, status, initAttributeValue);
  166. break;
  167. }
  168. }
  169. if (pNewNode == nullptr)
  170. {
  171. status.addMsg(statusMsg::error, "Configuration type (" + configType + ") not found");
  172. }
  173. }
  174. else
  175. {
  176. status.addMsg(statusMsg::error, parentNodeId, "", "Unable to find indicated parent node");
  177. }
  178. pNewNode->validate(status, true, false);
  179. return pNewNode;
  180. }
  181. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::shared_ptr<SchemaItem> &pCfgItem, Status &status,
  182. const std::pair<std::string, std::string> &initAttribute)
  183. {
  184. std::shared_ptr<EnvironmentNode> pNewEnvNode;
  185. //
  186. // Create the new node and add it to the parent
  187. pNewEnvNode = std::make_shared<EnvironmentNode>(pCfgItem, pCfgItem->getProperty("name"), pParentNode);
  188. pNewEnvNode->setId(EnvironmentMgr::getUniqueKey());
  189. addPath(pNewEnvNode);
  190. pNewEnvNode->initialize();
  191. if (!initAttribute.first.empty())
  192. {
  193. std::shared_ptr<EnvironmentValue> pAttr = pNewEnvNode->getAttribute(initAttribute.first);
  194. if (pAttr)
  195. {
  196. pAttr->setValue(initAttribute.second, nullptr);
  197. }
  198. }
  199. pParentNode->addChild(pNewEnvNode);
  200. //
  201. // Send a create event now that it's been added to the environment
  202. pCfgItem->getSchemaRoot()->processEvent("create", pNewEnvNode);
  203. insertExtraEnvironmentData(m_pRootNode);
  204. //
  205. // Look through the children and add any that are necessary
  206. std::vector<std::shared_ptr<SchemaItem>> cfgChildren;
  207. pCfgItem->getChildren(cfgChildren);
  208. for (auto childIt = cfgChildren.begin(); childIt != cfgChildren.end(); ++childIt)
  209. {
  210. int numReq = (*childIt)->getMinInstances();
  211. for (int i = 0; i<numReq; ++i)
  212. {
  213. std::pair<std::string, std::string> empty;
  214. addNewEnvironmentNode(pNewEnvNode, *childIt, status, empty);
  215. }
  216. }
  217. return pNewEnvNode;
  218. }
  219. void EnvironmentMgr::insertExtraEnvironmentData(std::shared_ptr<EnvironmentNode> pParentNode)
  220. {
  221. std::string insertData = pParentNode->getEnvironmentInsertData();
  222. if (!insertData.empty())
  223. {
  224. std::istringstream extraData(insertData);
  225. std::vector<std::shared_ptr<EnvironmentNode>> extraNodes = doLoadEnvironment(extraData, pParentNode->getSchemaItem()); // not root
  226. for (auto &&envNode : extraNodes)
  227. {
  228. assignNodeIds(envNode);
  229. pParentNode->addChild(envNode); // link extra node data to the newly created node
  230. pParentNode->clearEnvironmentInsertData();
  231. }
  232. }
  233. std::vector<std::shared_ptr<EnvironmentNode>> childNodes;
  234. pParentNode->getChildren(childNodes);
  235. for (auto &&child : childNodes)
  236. {
  237. insertExtraEnvironmentData(child);
  238. }
  239. }
  240. bool EnvironmentMgr::removeEnvironmentNode(const std::string &nodeId)
  241. {
  242. bool rc = false;
  243. std::shared_ptr<EnvironmentNode> pNode = getEnvironmentNode(nodeId);
  244. if (pNode)
  245. {
  246. std::shared_ptr<EnvironmentNode> pParentNode = pNode->getParent();
  247. if (pParentNode->removeChild(pNode))
  248. {
  249. m_nodeIds.erase(nodeId);
  250. rc = true;
  251. }
  252. }
  253. return rc;
  254. }
  255. std::string EnvironmentMgr::getUniqueKey()
  256. {
  257. return std::to_string(m_key++);
  258. }
  259. void EnvironmentMgr::validate(Status &status, bool includeHiddenNodes) const
  260. {
  261. if (m_pRootNode)
  262. {
  263. m_pRootNode->validate(status, true, includeHiddenNodes);
  264. }
  265. else
  266. {
  267. status.addMsg(statusMsg::error, "No environment loaded");
  268. }
  269. }
  270. void EnvironmentMgr::assignNodeIds(const std::shared_ptr<EnvironmentNode> &pNode)
  271. {
  272. pNode->setId(getUniqueKey());
  273. addPath(pNode);
  274. std::vector<std::shared_ptr<EnvironmentNode>> children;
  275. pNode->getChildren(children);
  276. for (auto it=children.begin(); it!=children.end(); ++it)
  277. {
  278. assignNodeIds(*it);
  279. }
  280. }
  281. void EnvironmentMgr::fetchNodes(const std::string path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes, const std::shared_ptr<EnvironmentNode> &pStartNode) const
  282. {
  283. const std::shared_ptr<EnvironmentNode> pStart = (pStartNode != nullptr) ? pStartNode : m_pRootNode;
  284. pStart->fetchNodes(path, nodes);
  285. }