SWTopology.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 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 "SWTopology.hpp"
  14. //#include "deployutils.hpp"
  15. namespace ech
  16. {
  17. SWTopology::SWTopology(EnvHelper * envHelper):SWComponentBase("topology", envHelper)
  18. {
  19. }
  20. void SWTopology::create(IPropertyTree *params)
  21. {
  22. //to do
  23. }
  24. unsigned SWTopology::add(IPropertyTree *params)
  25. {
  26. IPropertyTree * envTree = m_envHelper->getEnvTree();
  27. const char* key = params->queryProp("@key");
  28. StringBuffer xpath;
  29. xpath.clear().appendf(XML_TAG_SOFTWARE "/Topology[@name=\"%s\"]", key);
  30. IPropertyTree * compTree = envTree->queryPropTree(xpath.str());
  31. synchronized block(mutex);
  32. if (!compTree)
  33. {
  34. compTree = createPTree("Topology");
  35. compTree->addProp("@name", key);
  36. envTree->addPropTree("Topology", compTree);
  37. }
  38. const char* selector = params->queryProp("@selector");
  39. IPropertyTree * pAttrs = params->queryPropTree("Attributes");
  40. if (!selector)
  41. {
  42. if (pAttrs)
  43. updateNode(compTree, pAttrs);
  44. checkTopology(compTree);
  45. return 0;
  46. }
  47. if (stricmp(selector, "Cluster"))
  48. throw MakeStringException(CfgEnvErrorCode::InvalidParams,
  49. "Miss \"Cluster\" in adding topology.");
  50. const char* selectorKey = params->queryProp("@selector-key");
  51. if (!pAttrs)
  52. throw MakeStringException(CfgEnvErrorCode::InvalidParams,
  53. "Miss cluster name in creating a new cluster.");
  54. xpath.clear().append("Cluster[@name=");
  55. if (selectorKey && (*selectorKey))
  56. xpath.appendf("\"%s\"]", selectorKey);
  57. else if (pAttrs)
  58. {
  59. const char *clusterName = getAttributeFromParams(pAttrs, "name", NULL);
  60. if (clusterName)
  61. xpath.appendf("\"%s\"]", clusterName);
  62. else
  63. throw MakeStringException(CfgEnvErrorCode::InvalidParams,
  64. "Miss cluster name query/create topology cluster");
  65. }
  66. else
  67. throw MakeStringException(CfgEnvErrorCode::InvalidParams,
  68. "Miss cluster attributes to query/create topology cluster");
  69. IPropertyTree * cluster = compTree->queryPropTree(xpath.str());
  70. if (!cluster)
  71. {
  72. cluster = createPTree("Cluster");
  73. updateNode(cluster, pAttrs);
  74. checkTopologyCluster(cluster);
  75. compTree->addPropTree("Cluster", cluster);
  76. }
  77. IPropertyTree * children = params->queryPropTree("Children");
  78. if (children)
  79. createChildrenNodes(cluster, children);
  80. return 0;
  81. }
  82. void SWTopology::checkTopologyCluster(IPropertyTree *cluster)
  83. {
  84. if (!cluster->hasProp("@alias"))
  85. cluster->addProp("@alias", "");
  86. if (!cluster->hasProp("@prefix"))
  87. cluster->addProp("@prefix", cluster->queryProp("@name"));
  88. }
  89. void SWTopology::checkTopology(IPropertyTree *topology)
  90. {
  91. if (!topology->hasProp("@build"))
  92. topology->addProp("@build", m_buildName.str());
  93. if (!topology->hasProp("@buildSet"))
  94. topology->addProp("@buildSet", m_name.str());
  95. }
  96. void SWTopology::processNameChanged(const char* process, const char* newName, const char* oldName)
  97. {
  98. const char* tagName = m_envHelper->getXMLTagName(process);
  99. if (!tagName || !(*tagName))
  100. throw MakeStringException(CfgEnvErrorCode::InvalidParams, "Can't find XML tag name for %s", process);
  101. StringBuffer xpath;
  102. xpath.clear().appendf("/Software/Topology/Cluster/%s[@process=\"%s\"", tagName, oldName);
  103. IPropertyTree * envTree = m_envHelper->getEnvTree();
  104. Owned<IPropertyTreeIterator> processIter = envTree->getElements(xpath.str());
  105. synchronized block(mutex);
  106. ForEach (*processIter)
  107. {
  108. IPropertyTree * process = &processIter->query();
  109. process->setProp("process", newName);
  110. }
  111. }
  112. }