EnvironmentNode.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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) const
  156. {
  157. //
  158. // Check node value
  159. if (m_pLocalValue)
  160. {
  161. m_pLocalValue->validate(status, "");
  162. }
  163. //
  164. // Check any attributes
  165. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  166. {
  167. attrIt->second->validate(status, m_id);
  168. //
  169. // If this value must be unique, make sure it is
  170. if (attrIt->second->getSchemaValue()->isUniqueValue())
  171. {
  172. bool found = false;
  173. std::vector<std::string> allValues;
  174. attrIt->second->getAllValuesForSiblings(allValues);
  175. std::set<std::string> unquieValues;
  176. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  177. {
  178. auto ret = unquieValues.insert(*it);
  179. found = ret.second;
  180. }
  181. if (found)
  182. {
  183. status.addUniqueMsg(statusMsg::error, m_id, attrIt->second->getName(), "", "Attribute value must be unique");
  184. }
  185. }
  186. //
  187. // Does this value need to be from another set of values?
  188. if (attrIt->second->getSchemaValue()->isFromUniqueValueSet())
  189. {
  190. bool found = false;
  191. std::vector<std::string> allValues;
  192. attrIt->second->getSchemaValue()->getAllKeyRefValues(allValues);
  193. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  194. found = *it == attrIt->second->getValue();
  195. if (!found)
  196. {
  197. status.addMsg(statusMsg::error, m_id, attrIt->second->getName(), "", "Attribute value must be from a unique set");
  198. }
  199. }
  200. }
  201. //
  202. // Now check all children
  203. if (includeChildren)
  204. {
  205. for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt)
  206. {
  207. childIt->second->validate(status);
  208. }
  209. }
  210. }
  211. void EnvironmentNode::getAttributeValueForAllSiblings(const std::string &attrName, std::vector<std::string> &result) const
  212. {
  213. std::shared_ptr<EnvironmentNode> pParentNode = m_pParent.lock();
  214. if (pParentNode)
  215. {
  216. std::vector<std::shared_ptr<EnvironmentNode>> nodes;
  217. pParentNode->getChildren(nodes, m_name);
  218. for (auto it = nodes.begin(); it != nodes.end(); ++it)
  219. {
  220. result.push_back((*it)->getAttributeValue(attrName));
  221. }
  222. }
  223. }
  224. const std::shared_ptr<EnvironmentValue> EnvironmentNode::getAttribute(const std::string &name) const
  225. {
  226. std::shared_ptr<EnvironmentValue> pValue;
  227. auto it = m_attributes.find(name);
  228. if (it != m_attributes.end())
  229. {
  230. pValue = it->second;
  231. }
  232. return pValue;
  233. }
  234. void EnvironmentNode::getInsertableItems(std::vector<std::shared_ptr<SchemaItem>> &insertableItems) const
  235. {
  236. std::map<std::string, unsigned> childCounts;
  237. //
  238. // Iterate over the children and for each, create a childCount entry based on the
  239. // child node's configuration type
  240. for (auto childIt = m_children.begin(); childIt != m_children.end(); ++childIt)
  241. {
  242. std::string itemType = childIt->second->getSchemaItem()->getItemType();
  243. auto findIt = childCounts.find(itemType);
  244. if (findIt != childCounts.end())
  245. {
  246. ++findIt->second; // increment the number of instances of this item type.
  247. }
  248. else
  249. {
  250. childCounts.insert({ itemType, 1 });
  251. }
  252. }
  253. //
  254. // Now get the full list of configurable items, then resolve it against the child counts from
  255. // above to build a vector of insertable items
  256. std::vector<std::shared_ptr<SchemaItem>> configChildren;
  257. m_pSchemaItem->getChildren(configChildren);
  258. for (auto cfgIt = configChildren.begin(); cfgIt != configChildren.end(); ++cfgIt)
  259. {
  260. auto findIt = childCounts.find((*cfgIt)->getItemType());
  261. if (findIt != childCounts.end())
  262. {
  263. if (findIt->second < (*cfgIt)->getMaxInstances())
  264. {
  265. insertableItems.push_back(*cfgIt);
  266. }
  267. }
  268. else
  269. {
  270. insertableItems.push_back(*cfgIt);
  271. }
  272. }
  273. }
  274. //
  275. // Called to initialize a newly added node to the environment (not just read from the environment)
  276. void EnvironmentNode::initialize()
  277. {
  278. //
  279. // Add any attributes that are requried
  280. addMissingAttributesFromConfig();
  281. //
  282. // If we are a comonent and there is a buildSet attribute, set the value to the configItem's type
  283. if (!(m_pSchemaItem->getProperty("componentName").empty()) && hasAttribute("buildSet"))
  284. {
  285. Status status;
  286. setAttributeValue("buildSet", m_pSchemaItem->getProperty("category"), status);
  287. }
  288. //
  289. // Initilize each attribute
  290. for (auto attrIt = m_attributes.begin(); attrIt != m_attributes.end(); ++attrIt)
  291. {
  292. attrIt->second->initialize();
  293. }
  294. }