EnvironmentMgr.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. EnvironmentMgr *getEnvironmentMgrInstance(const EnvironmentType envType)
  17. {
  18. EnvironmentMgr *pEnvMgr = nullptr;
  19. if (envType == XML)
  20. {
  21. pEnvMgr = new XMLEnvironmentMgr();
  22. }
  23. return pEnvMgr;
  24. }
  25. EnvironmentMgr::EnvironmentMgr() :
  26. m_key(0)
  27. {
  28. m_pSchema = std::make_shared<SchemaItem>("root"); // make the root
  29. }
  30. bool EnvironmentMgr::loadSchema(const std::string &configPath, const std::string &masterConfigFile, const std::vector<std::string> &cfgParms)
  31. {
  32. bool rc = false;
  33. if (createParser())
  34. {
  35. rc = m_pSchemaParser->parse(configPath, masterConfigFile, cfgParms);
  36. if (rc)
  37. {
  38. // unique attribure value sets are global across a schema. Allocate one here and pass it in
  39. // for use in building the necessary references and dependencies across the schema, then pass
  40. // it to the post processing for finalization. Once referencs and dependencies are built, the
  41. // attribute value sets are no longer needed.
  42. std::map<std::string, std::vector<std::shared_ptr<SchemaValue>>> uniqueAttributeValueSets;
  43. m_pSchema->processDefinedUniqueAttributeValueSets(uniqueAttributeValueSets); // This must be done first
  44. m_pSchema->postProcessConfig(uniqueAttributeValueSets);
  45. }
  46. }
  47. return rc;
  48. }
  49. std::string EnvironmentMgr::getLastSchemaMessage() const
  50. {
  51. if (m_pSchemaParser)
  52. return m_pSchemaParser->getLastMessage();
  53. return "";
  54. }
  55. bool EnvironmentMgr::loadEnvironment(const std::string &qualifiedFilename)
  56. {
  57. bool rc = false;
  58. if (m_pSchema)
  59. {
  60. std::ifstream in;
  61. in.open(qualifiedFilename);
  62. if (in.is_open())
  63. {
  64. rc = doLoadEnvironment(in);
  65. }
  66. else
  67. {
  68. m_message = "Unable to open environment file '" + qualifiedFilename + "'";
  69. }
  70. }
  71. else
  72. {
  73. m_message = "No schema loaded";
  74. }
  75. return rc;
  76. }
  77. bool EnvironmentMgr::saveEnvironment(const std::string &qualifiedFilename)
  78. {
  79. bool rc = false;
  80. if (m_pRootNode)
  81. {
  82. std::ofstream out;
  83. out.open(qualifiedFilename);
  84. if (out.is_open())
  85. {
  86. rc = save(out);
  87. }
  88. }
  89. else
  90. {
  91. m_message = "No environment loaded";
  92. }
  93. return rc;
  94. }
  95. void EnvironmentMgr::addPath(const std::shared_ptr<EnvironmentNode> pNode)
  96. {
  97. auto retVal = m_nodeIds.insert({pNode->getId(), pNode });
  98. if (!retVal.second)
  99. {
  100. throw (ParseException("Attempted to insert duplicate path name " + pNode->getId() + " for node "));
  101. }
  102. }
  103. std::shared_ptr<EnvironmentNode> EnvironmentMgr::getEnvironmentNode(const std::string &nodeId)
  104. {
  105. std::shared_ptr<EnvironmentNode> pNode;
  106. auto pathIt = m_nodeIds.find(nodeId);
  107. if (pathIt != m_nodeIds.end())
  108. pNode = pathIt->second;
  109. return pNode;
  110. }
  111. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::string &parentNodeId, const std::string &configType, Status &status)
  112. {
  113. std::shared_ptr<EnvironmentNode> pNewNode;
  114. std::shared_ptr<EnvironmentNode> pParentNode = getEnvironmentNode(parentNodeId);
  115. if (pParentNode)
  116. {
  117. std::shared_ptr<SchemaItem> pNewCfgItem;
  118. std::vector<std::shared_ptr<SchemaItem>> insertableItems;
  119. pParentNode->getInsertableItems(insertableItems);
  120. for (auto it = insertableItems.begin(); it != insertableItems.end(); ++it)
  121. {
  122. if ((*it)->getItemType() == configType)
  123. {
  124. pNewNode = addNewEnvironmentNode(pParentNode, *it, status);
  125. break;
  126. }
  127. if (pNewNode == nullptr)
  128. {
  129. status.addMsg(statusMsg::error, "Configuration type (" + configType + ") not found");
  130. }
  131. }
  132. }
  133. else
  134. {
  135. status.addMsg(statusMsg::error, parentNodeId, "", "", "Unable to find indicated parent node");
  136. }
  137. return pNewNode;
  138. }
  139. std::shared_ptr<EnvironmentNode> EnvironmentMgr::addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::shared_ptr<SchemaItem> &pNewCfgItem, Status &status)
  140. {
  141. std::shared_ptr<EnvironmentNode> pNewNode;
  142. //
  143. // Create the new node and add it to the parent
  144. pNewNode = std::make_shared<EnvironmentNode>(pNewCfgItem, pNewCfgItem->getProperty("name"), pParentNode);
  145. pNewNode->setId(getUniqueKey());
  146. pParentNode->addChild(pNewNode);
  147. addPath(pNewNode);
  148. pNewNode->initialize();
  149. //
  150. // Look through the children and add any that are necessary
  151. std::vector<std::shared_ptr<SchemaItem>> cfgChildren;
  152. pNewCfgItem->getChildren(cfgChildren);
  153. for (auto childIt = cfgChildren.begin(); childIt != cfgChildren.end(); ++childIt)
  154. {
  155. int numReq = (*childIt)->getMinInstances();
  156. for (int i=0; i<numReq; ++i)
  157. {
  158. addNewEnvironmentNode(pNewNode, *childIt, status);
  159. }
  160. }
  161. return pNewNode;
  162. }
  163. bool EnvironmentMgr::removeEnvironmentNode(const std::string &nodeId, Status &status)
  164. {
  165. bool rc = false;
  166. std::shared_ptr<EnvironmentNode> pNode = getEnvironmentNode(nodeId);
  167. if (pNode)
  168. {
  169. std::shared_ptr<EnvironmentNode> pParentNode = pNode->getParent();
  170. if (pParentNode->removeChild(pNode))
  171. {
  172. m_nodeIds.erase(nodeId);
  173. }
  174. else
  175. {
  176. status.addMsg(statusMsg::error, nodeId, "", "", "Unable to remove the node");
  177. }
  178. }
  179. else
  180. {
  181. status.addMsg(statusMsg::error, nodeId, "", "", "Unable to find indicated node");
  182. }
  183. return rc;
  184. }
  185. std::string EnvironmentMgr::getUniqueKey()
  186. {
  187. return std::to_string(m_key++);
  188. }
  189. void EnvironmentMgr::validate(Status &status) const
  190. {
  191. if (m_pRootNode)
  192. {
  193. m_pRootNode->validate(status, true);
  194. }
  195. else
  196. {
  197. status.addMsg(statusMsg::error, "No environment loaded");
  198. }
  199. }