EnvironmentNode.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "EnvironmentNode.hpp"
  14. #include "Exceptions.hpp"
  15. #include "Utils.hpp"
  16. #include "ConfigPath.hpp"
  17. void EnvironmentNode::addChild(std::shared_ptr<EnvironmentNode> pNode)
  18. {
  19. m_children.insert(std::make_pair(pNode->getName(), pNode));
  20. }
  21. bool EnvironmentNode::removeChild(const std::shared_ptr<EnvironmentNode> pNode)
  22. {
  23. bool removed = false;
  24. for (auto it=m_children.begin(); it!= m_children.end() && !removed; ++it)
  25. {
  26. if (pNode == it->second)
  27. {
  28. m_children.erase(it);
  29. removed = true;
  30. }
  31. }
  32. return removed;
  33. }
  34. bool EnvironmentNode::addAttribute(const std::string &name, std::shared_ptr<EnvironmentValue> pValue)
  35. {
  36. auto retValue = m_attributes.insert(std::make_pair(name, pValue));
  37. return retValue.second;
  38. }
  39. void EnvironmentNode::getChildren(std::vector<std::shared_ptr<EnvironmentNode>> &childNodes, const std::string &name) const
  40. {
  41. if (name.empty())
  42. {
  43. for (auto nodeIt = m_children.begin(); nodeIt != m_children.end(); ++nodeIt)
  44. {
  45. childNodes.push_back(nodeIt->second);
  46. }
  47. }
  48. else
  49. {
  50. auto rangeIt = m_children.equal_range(name);
  51. for (auto it = rangeIt.first; it != rangeIt.second; ++it)
  52. {
  53. childNodes.push_back(it->second);
  54. }
  55. }
  56. }
  57. std::shared_ptr<EnvironmentNode> EnvironmentNode::getParent() const
  58. {
  59. std::shared_ptr<EnvironmentNode> pParent;
  60. if (!m_pParent.expired())
  61. {
  62. pParent = m_pParent.lock();
  63. }
  64. return pParent;
  65. }
  66. void EnvironmentNode::getAttributes(std::vector<std::shared_ptr<EnvironmentValue>> &attrs) const
  67. {
  68. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  69. {
  70. attrs.push_back(attrIt->second);
  71. }
  72. }
  73. void EnvironmentNode::addMissingAttributesFromConfig()
  74. {
  75. std::vector<std::shared_ptr<SchemaValue>> configuredAttributes;
  76. m_pSchemaItem->getAttributes(configuredAttributes);
  77. //
  78. // go through all the configured attrubutes and for each that is not present in our list, add it
  79. for (auto it = configuredAttributes.begin(); it != configuredAttributes.end(); ++it)
  80. {
  81. auto attrIt = m_attributes.find((*it)->getName());
  82. if (attrIt == m_attributes.end())
  83. {
  84. std::shared_ptr<SchemaValue> pCfgValue = *it;
  85. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, pCfgValue->getName());
  86. pCfgValue->addEnvironmentValue(pEnvValue);
  87. addAttribute(pCfgValue->getName(), pEnvValue);
  88. }
  89. }
  90. }
  91. void EnvironmentNode::setAttributeValues(const std::vector<NameValue> &values, Status &status, bool allowInvalid, bool forceCreate)
  92. {
  93. for (auto it = values.begin(); it != values.end(); ++it)
  94. {
  95. setAttributeValue((*it).name, (*it).value, status, allowInvalid, forceCreate);
  96. }
  97. }
  98. void EnvironmentNode::setAttributeValue(const std::string &attrName, const std::string &value, Status &status, bool allowInvalid, bool forceCreate)
  99. {
  100. std::shared_ptr<EnvironmentValue> pEnvValue;
  101. auto it = m_attributes.find(attrName);
  102. if (it != m_attributes.end())
  103. {
  104. pEnvValue = it->second;
  105. }
  106. //
  107. // Not found on this node. See if the configuration defines the attribute. If so, set the value and move on.
  108. // If not and the forceCreate flag is set, create it.
  109. else if (forceCreate)
  110. {
  111. std::shared_ptr<SchemaValue> pCfgValue = m_pSchemaItem->getAttribute(attrName);
  112. pEnvValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, attrName);
  113. addAttribute(attrName, pEnvValue);
  114. if (!pCfgValue->isDefined())
  115. {
  116. status.addMsg(statusMsg::warning, getId(), attrName, "Undefined attribute did not exist in configuration, was created");
  117. }
  118. }
  119. if (pEnvValue)
  120. {
  121. pEnvValue->setValue(value, &status, allowInvalid);
  122. }
  123. else
  124. {
  125. status.addMsg(statusMsg::error, getId(), attrName, "The attribute does not exist and was not created");
  126. }
  127. }
  128. std::string EnvironmentNode::getAttributeValue(const std::string &name) const
  129. {
  130. std::string value;
  131. std::shared_ptr<EnvironmentValue> pAttribute = getAttribute(name);
  132. if (pAttribute)
  133. value = pAttribute->getValue();
  134. return value;
  135. }
  136. bool EnvironmentNode::setLocalValue(const std::string &newValue, Status &status, bool force)
  137. {
  138. bool rc = false;
  139. //
  140. // If no environment value is present, create one first
  141. if (!m_pLocalValue)
  142. {
  143. std::shared_ptr<SchemaValue> pCfgValue = m_pSchemaItem->getItemSchemaValue();
  144. m_pLocalValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, ""); // node's value has no name
  145. }
  146. rc = m_pLocalValue->setValue(newValue, &status, force);
  147. return rc;
  148. }
  149. std::string EnvironmentNode::getLocalValue() const
  150. {
  151. std::string value;
  152. if (m_pLocalValue)
  153. {
  154. value = m_pLocalValue->getValue();
  155. }
  156. return value;
  157. }
  158. void EnvironmentNode::validate(Status &status, bool includeChildren, bool includeHiddenNodes) const
  159. {
  160. if (!m_pSchemaItem->isHidden() || includeHiddenNodes)
  161. {
  162. //
  163. // Check node value
  164. if (m_pLocalValue)
  165. {
  166. m_pLocalValue->validate(status, m_id);
  167. }
  168. //
  169. // Check any attributes
  170. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  171. {
  172. attrIt->second->validate(status, m_id);
  173. //
  174. // If this value must be unique, make sure it is
  175. if (attrIt->second->getSchemaValue()->isUniqueValue())
  176. {
  177. bool found = false;
  178. std::vector<std::string> allValues;
  179. attrIt->second->getAllValuesForSiblings(allValues);
  180. std::set<std::string> unquieValues;
  181. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  182. {
  183. auto ret = unquieValues.insert(*it);
  184. found = !ret.second;
  185. }
  186. if (found)
  187. {
  188. status.addUniqueMsg(statusMsg::error, m_id, attrIt->second->getName(), "Attribute value must be unique");
  189. }
  190. }
  191. //
  192. // Does this value need to be from another set of values?
  193. if (attrIt->second->getSchemaValue()->isFromUniqueValueSet())
  194. {
  195. bool found = false;
  196. std::vector<std::string> allValues;
  197. attrIt->second->getSchemaValue()->getAllKeyRefValues(allValues);
  198. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  199. found = *it == attrIt->second->getValue();
  200. if (!found)
  201. {
  202. status.addMsg(statusMsg::error, m_id, attrIt->second->getName(), "Attribute value must be from a unique set");
  203. }
  204. }
  205. }
  206. //
  207. // Now check all children
  208. if (includeChildren)
  209. {
  210. for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt)
  211. {
  212. childIt->second->validate(status, includeChildren, includeHiddenNodes);
  213. }
  214. }
  215. }
  216. }
  217. void EnvironmentNode::getAttributeValueForAllSiblings(const std::string &attrName, std::vector<std::string> &result) const
  218. {
  219. std::shared_ptr<EnvironmentNode> pParentNode = m_pParent.lock();
  220. if (pParentNode)
  221. {
  222. std::vector<std::shared_ptr<EnvironmentNode>> nodes;
  223. pParentNode->getChildren(nodes, m_name);
  224. for (auto it = nodes.begin(); it != nodes.end(); ++it)
  225. {
  226. result.push_back((*it)->getAttributeValue(attrName));
  227. }
  228. }
  229. }
  230. const std::shared_ptr<EnvironmentValue> EnvironmentNode::getAttribute(const std::string &name) const
  231. {
  232. std::shared_ptr<EnvironmentValue> pValue;
  233. auto it = m_attributes.find(name);
  234. if (it != m_attributes.end())
  235. {
  236. pValue = it->second;
  237. }
  238. return pValue;
  239. }
  240. void EnvironmentNode::getInsertableItems(std::vector<InsertableItem> &insertableItems) const
  241. {
  242. std::map<std::string, unsigned> childCounts;
  243. //
  244. // Iterate over the children and for each, create a childCount entry based on the
  245. // child node's configuration type
  246. for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt)
  247. {
  248. std::string itemType = childIt->second->getSchemaItem()->getItemType();
  249. auto findIt = childCounts.find(itemType);
  250. if (findIt != childCounts.end())
  251. {
  252. ++findIt->second; // increment the number of instances of this item type.
  253. }
  254. else
  255. {
  256. childCounts.insert({ itemType, 1 });
  257. }
  258. }
  259. //
  260. // Now get the full list of configurable items, then resolve it against the child counts from
  261. // above to build a vector of insertable items
  262. std::vector<std::shared_ptr<SchemaItem>> configChildren;
  263. m_pSchemaItem->getChildren(configChildren);
  264. for (auto cfgIt = configChildren.begin(); cfgIt != configChildren.end(); ++cfgIt)
  265. {
  266. auto findIt = childCounts.find((*cfgIt)->getItemType());
  267. if (findIt != childCounts.end())
  268. {
  269. if (findIt->second < (*cfgIt)->getMaxInstances())
  270. {
  271. insertableItems.push_back(InsertableItem(shared_from_this(), *cfgIt));
  272. }
  273. }
  274. else
  275. {
  276. insertableItems.push_back(InsertableItem(shared_from_this(), *cfgIt));
  277. }
  278. }
  279. }
  280. //
  281. // Called to initialize a newly added node to the environment (not just read from the environment)
  282. void EnvironmentNode::initialize()
  283. {
  284. //
  285. // Add missing attributes
  286. addMissingAttributesFromConfig();
  287. //
  288. // If we are a component and there is a buildSet attribute, set the value to the configItem's type
  289. if (!(m_pSchemaItem->getProperty("componentName").empty()) && hasAttribute("buildSet"))
  290. {
  291. Status status;
  292. setAttributeValue("buildSet", m_pSchemaItem->getProperty("componentName"), status);
  293. }
  294. //
  295. // Initilize each attribute
  296. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  297. {
  298. attrIt->second->initialize();
  299. }
  300. }
  301. void EnvironmentNode::fetchNodes(const std::string &path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes) const
  302. {
  303. ConfigPath configPath(path);
  304. doFetchNodes(configPath, nodes);
  305. }
  306. void EnvironmentNode::doFetchNodes(ConfigPath &configPath, std::vector<std::shared_ptr<EnvironmentNode>> &nodes) const
  307. {
  308. std::shared_ptr<ConfigPathItem> pPathItem = configPath.getNextPathItem();
  309. if (pPathItem)
  310. {
  311. if (pPathItem->isRoot())
  312. {
  313. std::shared_ptr<const EnvironmentNode> pRoot = getRoot();
  314. if (pRoot->getName() == pPathItem->getElementName())
  315. {
  316. pRoot->doFetchNodes(configPath, nodes);
  317. }
  318. else
  319. {
  320. throw new ParseException("Invalid root element name ('" + pPathItem->getElementName() + "') specified in path");
  321. }
  322. }
  323. else if (pPathItem->isParentPathtItem())
  324. {
  325. getParent()->doFetchNodes(configPath, nodes);
  326. }
  327. else if (pPathItem->isCurrentPathItem())
  328. {
  329. doFetchNodes(configPath, nodes);
  330. }
  331. else
  332. {
  333. //
  334. // Get children nodes matching path element name (use this node if no element name)
  335. std::vector<std::shared_ptr<EnvironmentNode>> childNodes;
  336. if (pPathItem->getElementName().empty())
  337. {
  338. childNodes.push_back(std::const_pointer_cast<EnvironmentNode>(shared_from_this()));
  339. }
  340. else
  341. {
  342. getChildren(childNodes, pPathItem->getElementName());
  343. }
  344. //
  345. // If there is an attribute and/or attribute values, search the child nodes from above
  346. if (!pPathItem->getAttributeName().empty())
  347. {
  348. std::string attrName = pPathItem->getAttributeName();
  349. auto childNodeIt = childNodes.begin();
  350. while (childNodeIt != childNodes.end())
  351. {
  352. //
  353. // If an attribute, then search attributes for those with the specified name, otherwise
  354. // get the schema item for this node and see if it has a property with the specified name.
  355. // In each case, if there is a value, check it too
  356. if (!pPathItem->isSchemaItem())
  357. {
  358. std::shared_ptr<EnvironmentValue> pAttribute = (*childNodeIt)->getAttribute(attrName);
  359. if (pAttribute)
  360. {
  361. if (pPathItem->isValuePresentInValueList(pAttribute->getValue(), true))
  362. {
  363. ++childNodeIt;
  364. }
  365. else
  366. {
  367. childNodeIt = childNodes.erase(childNodeIt);
  368. }
  369. }
  370. else
  371. {
  372. childNodeIt = childNodes.erase(childNodeIt);
  373. }
  374. }
  375. else
  376. {
  377. std::shared_ptr<SchemaItem> pSchemaItem = (*childNodeIt)->getSchemaItem();
  378. std::string propertyValue = (attrName == "itemType") ? pSchemaItem->getItemType() : pSchemaItem->getProperty(attrName);
  379. if (pPathItem->isValuePresentInValueList(propertyValue, true))
  380. {
  381. ++childNodeIt;
  382. }
  383. else
  384. {
  385. childNodeIt = childNodes.erase(childNodeIt);
  386. }
  387. }
  388. }
  389. }
  390. if (configPath.isPathRemaining())
  391. {
  392. //
  393. // For all the matching nodes at this element, call each to continue the search
  394. for (auto childNodeIt = childNodes.begin(); childNodeIt != childNodes.end(); ++childNodeIt)
  395. {
  396. (*childNodeIt)->doFetchNodes(configPath, nodes);
  397. }
  398. }
  399. else
  400. {
  401. nodes.insert(nodes.end(), childNodes.begin(), childNodes.end());
  402. }
  403. }
  404. }
  405. }
  406. std::shared_ptr<const EnvironmentNode> EnvironmentNode::getRoot() const
  407. {
  408. if (!m_pParent.expired())
  409. {
  410. return m_pParent.lock()->getRoot();
  411. }
  412. std::shared_ptr <const EnvironmentNode> ptr = shared_from_this();
  413. return ptr;
  414. }