EnvironmentNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. void EnvironmentNode::addChild(std::shared_ptr<EnvironmentNode> pNode)
  15. {
  16. m_children.insert(std::make_pair(pNode->getName(), pNode));
  17. }
  18. bool EnvironmentNode::removeChild(const std::shared_ptr<EnvironmentNode> pNode)
  19. {
  20. bool removed = false;
  21. for (auto it=m_children.begin(); it!= m_children.end() && !removed; ++it)
  22. {
  23. if (pNode == it->second)
  24. {
  25. m_children.erase(it);
  26. removed = true;
  27. }
  28. }
  29. return removed;
  30. }
  31. bool EnvironmentNode::addAttribute(const std::string &name, std::shared_ptr<EnvironmentValue> pValue)
  32. {
  33. auto retValue = m_attributes.insert(std::make_pair(name, pValue));
  34. return retValue.second;
  35. }
  36. void EnvironmentNode::getChildren(std::vector<std::shared_ptr<EnvironmentNode>> &childNodes, const std::string &name) const
  37. {
  38. if (name.empty())
  39. {
  40. for (auto nodeIt = m_children.begin(); nodeIt != m_children.end(); ++nodeIt)
  41. {
  42. childNodes.push_back(nodeIt->second);
  43. }
  44. }
  45. else
  46. {
  47. auto rangeIt = m_children.equal_range(name);
  48. for (auto it = rangeIt.first; it != rangeIt.second; ++it)
  49. {
  50. childNodes.push_back(it->second);
  51. }
  52. }
  53. }
  54. std::shared_ptr<EnvironmentNode> EnvironmentNode::getParent() const
  55. {
  56. std::shared_ptr<EnvironmentNode> pParent;
  57. if (!m_pParent.expired())
  58. {
  59. pParent = m_pParent.lock();
  60. }
  61. return pParent;
  62. }
  63. void EnvironmentNode::getAttributes(std::vector<std::shared_ptr<EnvironmentValue>> &attrs) const
  64. {
  65. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  66. {
  67. attrs.push_back(attrIt->second);
  68. }
  69. }
  70. void EnvironmentNode::addMissingAttributesFromConfig()
  71. {
  72. std::vector<std::shared_ptr<SchemaValue>> configuredAttributes;
  73. m_pSchemaItem->getAttributes(configuredAttributes);
  74. //
  75. // go through all the configured attrubutes and for each that is not present in our list, add it
  76. for (auto it = configuredAttributes.begin(); it != configuredAttributes.end(); ++it)
  77. {
  78. auto attrIt = m_attributes.find((*it)->getName());
  79. if (attrIt == m_attributes.end())
  80. {
  81. std::shared_ptr<SchemaValue> pCfgValue = *it;
  82. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, pCfgValue->getName());
  83. pCfgValue->addEnvironmentValue(pEnvValue);
  84. addAttribute(pCfgValue->getName(), pEnvValue);
  85. }
  86. }
  87. }
  88. void EnvironmentNode::setAttributeValues(const std::vector<NameValue> &values, Status &status, bool allowInvalid, bool forceCreate)
  89. {
  90. for (auto it = values.begin(); it != values.end(); ++it)
  91. {
  92. setAttributeValue((*it).name, (*it).value, status, allowInvalid, forceCreate);
  93. }
  94. }
  95. void EnvironmentNode::setAttributeValue(const std::string &attrName, const std::string &value, Status &status, bool allowInvalid, bool forceCreate)
  96. {
  97. std::shared_ptr<EnvironmentValue> pEnvValue;
  98. auto it = m_attributes.find(attrName);
  99. if (it != m_attributes.end())
  100. {
  101. pEnvValue = it->second;
  102. }
  103. //
  104. // Not found on this node. See if the configuration defines the attribute. If so, set the value and move on.
  105. // If not and the forceCreate flag is set, create it.
  106. else if (forceCreate)
  107. {
  108. std::shared_ptr<SchemaValue> pCfgValue = m_pSchemaItem->getAttribute(attrName);
  109. pEnvValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, attrName);
  110. addAttribute(attrName, pEnvValue);
  111. if (!pCfgValue->isDefined())
  112. {
  113. status.addMsg(statusMsg::warning, getId(), attrName, "Undefined attribute did not exist in configuration, was created");
  114. }
  115. }
  116. if (pEnvValue)
  117. {
  118. pEnvValue->setValue(value, &status, allowInvalid);
  119. }
  120. else
  121. {
  122. status.addMsg(statusMsg::error, getId(), attrName, "The attribute does not exist and was not created");
  123. }
  124. }
  125. std::string EnvironmentNode::getAttributeValue(const std::string &name) const
  126. {
  127. std::string value;
  128. std::shared_ptr<EnvironmentValue> pAttribute = getAttribute(name);
  129. if (pAttribute)
  130. value = pAttribute->getValue();
  131. return value;
  132. }
  133. bool EnvironmentNode::setLocalValue(const std::string &newValue, Status &status, bool force)
  134. {
  135. bool rc = false;
  136. //
  137. // If no environment value is present, create one first
  138. if (!m_pLocalValue)
  139. {
  140. std::shared_ptr<SchemaValue> pCfgValue = m_pSchemaItem->getItemSchemaValue();
  141. m_pLocalValue = std::make_shared<EnvironmentValue>(shared_from_this(), pCfgValue, ""); // node's value has no name
  142. }
  143. rc = m_pLocalValue->setValue(newValue, &status, force);
  144. return rc;
  145. }
  146. std::string EnvironmentNode::getLocalValue() const
  147. {
  148. std::string value;
  149. if (m_pLocalValue)
  150. {
  151. value = m_pLocalValue->getValue();
  152. }
  153. return value;
  154. }
  155. void EnvironmentNode::validate(Status &status, bool includeChildren, bool includeHiddenNodes) const
  156. {
  157. if (!m_pSchemaItem->isHidden() || includeHiddenNodes)
  158. {
  159. //
  160. // Check node value
  161. if (m_pLocalValue)
  162. {
  163. m_pLocalValue->validate(status, m_id);
  164. }
  165. //
  166. // Check any attributes
  167. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  168. {
  169. attrIt->second->validate(status, m_id);
  170. //
  171. // If this value must be unique, make sure it is
  172. if (attrIt->second->getSchemaValue()->isUniqueValue())
  173. {
  174. bool found = false;
  175. std::vector<std::string> allValues;
  176. attrIt->second->getAllValuesForSiblings(allValues);
  177. std::set<std::string> unquieValues;
  178. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  179. {
  180. auto ret = unquieValues.insert(*it);
  181. found = ret.second;
  182. }
  183. if (found)
  184. {
  185. status.addUniqueMsg(statusMsg::error, m_id, attrIt->second->getName(), "Attribute value must be unique");
  186. }
  187. }
  188. //
  189. // Does this value need to be from another set of values?
  190. if (attrIt->second->getSchemaValue()->isFromUniqueValueSet())
  191. {
  192. bool found = false;
  193. std::vector<std::string> allValues;
  194. attrIt->second->getSchemaValue()->getAllKeyRefValues(allValues);
  195. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  196. found = *it == attrIt->second->getValue();
  197. if (!found)
  198. {
  199. status.addMsg(statusMsg::error, m_id, attrIt->second->getName(), "Attribute value must be from a unique set");
  200. }
  201. }
  202. }
  203. //
  204. // Now check all children
  205. if (includeChildren)
  206. {
  207. for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt)
  208. {
  209. childIt->second->validate(status, includeChildren);
  210. }
  211. }
  212. }
  213. }
  214. void EnvironmentNode::getAttributeValueForAllSiblings(const std::string &attrName, std::vector<std::string> &result) const
  215. {
  216. std::shared_ptr<EnvironmentNode> pParentNode = m_pParent.lock();
  217. if (pParentNode)
  218. {
  219. std::vector<std::shared_ptr<EnvironmentNode>> nodes;
  220. pParentNode->getChildren(nodes, m_name);
  221. for (auto it = nodes.begin(); it != nodes.end(); ++it)
  222. {
  223. result.push_back((*it)->getAttributeValue(attrName));
  224. }
  225. }
  226. }
  227. const std::shared_ptr<EnvironmentValue> EnvironmentNode::getAttribute(const std::string &name) const
  228. {
  229. std::shared_ptr<EnvironmentValue> pValue;
  230. auto it = m_attributes.find(name);
  231. if (it != m_attributes.end())
  232. {
  233. pValue = it->second;
  234. }
  235. return pValue;
  236. }
  237. void EnvironmentNode::getInsertableItems(std::vector<std::shared_ptr<SchemaItem>> &insertableItems) const
  238. {
  239. std::map<std::string, unsigned> childCounts;
  240. //
  241. // Iterate over the children and for each, create a childCount entry based on the
  242. // child node's configuration type
  243. for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt)
  244. {
  245. std::string itemType = childIt->second->getSchemaItem()->getItemType();
  246. auto findIt = childCounts.find(itemType);
  247. if (findIt != childCounts.end())
  248. {
  249. ++findIt->second; // increment the number of instances of this item type.
  250. }
  251. else
  252. {
  253. childCounts.insert({ itemType, 1 });
  254. }
  255. }
  256. //
  257. // Now get the full list of configurable items, then resolve it against the child counts from
  258. // above to build a vector of insertable items
  259. std::vector<std::shared_ptr<SchemaItem>> configChildren;
  260. m_pSchemaItem->getChildren(configChildren);
  261. for (auto cfgIt = configChildren.begin(); cfgIt != configChildren.end(); ++cfgIt)
  262. {
  263. auto findIt = childCounts.find((*cfgIt)->getItemType());
  264. if (findIt != childCounts.end())
  265. {
  266. if (findIt->second < (*cfgIt)->getMaxInstances())
  267. {
  268. insertableItems.push_back(*cfgIt);
  269. }
  270. }
  271. else
  272. {
  273. insertableItems.push_back(*cfgIt);
  274. }
  275. }
  276. }
  277. //
  278. // Called to initialize a newly added node to the environment (not just read from the environment)
  279. void EnvironmentNode::initialize()
  280. {
  281. //
  282. // Add any attributes that are requried
  283. addMissingAttributesFromConfig();
  284. //
  285. // If we are a comonent and there is a buildSet attribute, set the value to the configItem's type
  286. if (!(m_pSchemaItem->getProperty("componentName").empty()) && hasAttribute("buildSet"))
  287. {
  288. Status status;
  289. setAttributeValue("buildSet", m_pSchemaItem->getProperty("category"), status);
  290. }
  291. //
  292. // Initilize each attribute
  293. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  294. {
  295. attrIt->second->initialize();
  296. }
  297. }