ConfiguratorAPI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2015 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 "ConfiguratorAPI.hpp"
  14. #include "ConfiguratorMain.hpp"
  15. #include "BuildSet.hpp"
  16. #include "ConfigSchemaHelper.hpp"
  17. #include "ConfiguratorMain.hpp"
  18. #include "jstring.hpp"
  19. #include "SchemaMapManager.hpp"
  20. #include "SchemaEnumeration.hpp"
  21. #include "SchemaCommon.hpp"
  22. #include "ConfiguratorMain.hpp"
  23. #include "EnvironmentModel.hpp"
  24. #include "ConfigNotifications.hpp"
  25. #include <iostream>
  26. #include "jlog.hpp"
  27. static int nAllocatedTables = 0;
  28. static char *modelName[MAX_ARRAY_X];
  29. const char* getTableDataModelName(int index)
  30. {
  31. if (index < nAllocatedTables)
  32. return modelName[index];
  33. else
  34. {
  35. modelName[index] = new char[MAX_ARRAY_Y];
  36. sprintf(modelName[index],"tableDataModel%d", index);
  37. nAllocatedTables++;
  38. return modelName[index];
  39. }
  40. }
  41. void deleteTableModels()
  42. {
  43. while (nAllocatedTables > 0)
  44. {
  45. delete[] modelName[nAllocatedTables];
  46. nAllocatedTables--;
  47. }
  48. }
  49. namespace CONFIGURATOR_API
  50. {
  51. using namespace CONFIGURATOR;
  52. static CConfigSchemaHelper *s_pConfigSchemaHelper = NULL;
  53. void reload(const char *pFile)
  54. {
  55. assert(pFile != NULL && *pFile != 0);
  56. delete s_pConfigSchemaHelper;
  57. s_pConfigSchemaHelper = NULL;
  58. s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
  59. s_pConfigSchemaHelper->populateSchema();
  60. CConfigSchemaHelper::getInstance()->loadEnvFromConfig(pFile);
  61. }
  62. int getNumberOfAvailableComponents()
  63. {
  64. assert(s_pConfigSchemaHelper != NULL);
  65. return CBuildSetManager::getInstance()->getBuildSetComponentCount();
  66. }
  67. int getNumberOfAvailableServices()
  68. {
  69. assert(s_pConfigSchemaHelper != NULL);
  70. return CBuildSetManager::getInstance()->getBuildSetServiceCount();
  71. }
  72. #ifdef CONFIGURATOR_LIB
  73. int initialize()
  74. {
  75. assert(s_pConfigSchemaHelper == NULL);
  76. static bool bOnce = true;
  77. if (bOnce == true)
  78. {
  79. bOnce = false;
  80. InitModuleObjects();
  81. }
  82. s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
  83. s_pConfigSchemaHelper->populateSchema();
  84. return 1;
  85. }
  86. #else // CONFIGURATOR_LIB
  87. int initialize(int argc, char *argv[])
  88. {
  89. assert(s_pConfigSchemaHelper == NULL);
  90. InitModuleObjects();
  91. s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
  92. return 0;
  93. }
  94. #endif // CONFIGURATOR_LIB
  95. int getValue(const char *pXPath, char *pValue)
  96. {
  97. // By Default, return xPath as value.
  98. if (pXPath == NULL || *pXPath == 0)
  99. return 0;
  100. strcpy(pValue, pXPath[0] == '#' ? &(pXPath[1]) : pXPath); // href for frontend
  101. CAttribute *pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(pXPath);
  102. if(pAttribute == NULL)
  103. ::std::cout << "xPath: " << pXPath << "| value: " << pXPath << ::std::endl;
  104. else if (pAttribute->isInstanceValueValid() == true)
  105. {
  106. strcpy(pValue, pAttribute->getInstanceValue());
  107. ::std::cout << "xPath: " << pXPath << "| value: " << pValue << ::std::endl;
  108. }
  109. return 1;
  110. }
  111. bool setValue(const char *pXPath, const char *pValue)
  112. {
  113. assert(pXPath != NULL && pXPath[0] != 0);
  114. assert(pValue != NULL);
  115. StringBuffer strXPath(pXPath);
  116. strXPath.replace('_','/');
  117. CAttribute *pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(strXPath.str());
  118. if (pAttribute == NULL)
  119. return false;
  120. assert(pAttribute != NULL);
  121. pAttribute->setEnvValueFromXML(pValue);
  122. /*if (strstr(pValue, "/") == NULL)
  123. {
  124. strXPath.replace('_','/');
  125. }*/
  126. CConfigSchemaHelper::getInstance()->setEnvTreeProp(strXPath.str(), pValue);
  127. return true;
  128. }
  129. int getIndex(const char *pXPath)
  130. {
  131. CRestriction *pRestriction = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getRestrictionFromXPath(pXPath);
  132. assert(pRestriction != NULL);
  133. assert(pRestriction->getEnumerationArray() != NULL);
  134. return pRestriction->getEnumerationArray()->getEnvValueNodeIndex();
  135. }
  136. void setIndex(const char *pXPath, int newIndex)
  137. {
  138. assert(newIndex >= 0);
  139. CRestriction *pRestriction = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getRestrictionFromXPath(pXPath);
  140. assert(pRestriction != NULL);
  141. assert(pRestriction->getEnumerationArray() != NULL);
  142. pRestriction->getEnumerationArray()->setEnvValueNodeIndex(newIndex);
  143. CConfigSchemaHelper::getInstance()->setEnvTreeProp(pXPath, pRestriction->getEnumerationArray()->item(newIndex).getValue());
  144. }
  145. const char* getTableValue(const char *pXPath, int nRow)
  146. {
  147. assert(pXPath != NULL && *pXPath != 0);
  148. CAttribute *pAttribute = NULL;
  149. CElement *pElement = NULL;
  150. if (CConfigSchemaHelper::isXPathTailAttribute(pXPath) == true)
  151. pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(pXPath);
  152. if (pAttribute == NULL)
  153. {
  154. pElement = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getElementFromXPath(pXPath);
  155. assert(pElement != NULL);
  156. return pAttribute->getInstanceValue();
  157. }
  158. else
  159. {
  160. assert(pAttribute != NULL);
  161. if (nRow == 1)
  162. return pAttribute->getInstanceValue();
  163. else
  164. {
  165. StringBuffer strXPath(pXPath);
  166. const StringBuffer strXPathOriginal(pXPath);
  167. int offset = strXPathOriginal.length() - (CConfigSchemaHelper::stripXPathIndex(strXPath) + 1) ;
  168. CConfigSchemaHelper::stripXPathIndex(strXPath);
  169. strXPath.appendf("[%d]", nRow);
  170. strXPath.append(strXPathOriginal, offset, strXPathOriginal.length() - offset);
  171. //confirm the next 2 lines are sufficient
  172. //CConfigSchemaHelper::stripXPathIndex(strXPath);
  173. //strXPath.appendf("[%d]", nRow);
  174. pAttribute = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getAttributeFromXPath(strXPath.str());
  175. if (STRICTNESS_LEVEL >= DEFAULT_STRICTNESS)
  176. assert(pAttribute != NULL);
  177. if (pAttribute == NULL)
  178. return NULL;
  179. return pAttribute->getInstanceValue();
  180. }
  181. }
  182. }
  183. void setTableValue(const char *pXPath, int index, const char *pValue)
  184. {
  185. UNIMPLEMENTED;
  186. }
  187. int getNumberOfUniqueColumns()
  188. {
  189. return CConfigSchemaHelper::getInstance()->getEnvironmentXPathSize();
  190. }
  191. const char* getColumnName(int idx)
  192. {
  193. if (idx < CConfigSchemaHelper::getInstance()->getEnvironmentXPathSize())
  194. return CConfigSchemaHelper::getInstance()->getEnvironmentXPaths(idx);
  195. else
  196. return NULL;
  197. }
  198. int getNumberOfRows(const char* pXPath)
  199. {
  200. assert(pXPath != NULL && *pXPath != 0);
  201. PROGLOG("Get number of rows for %s = %d", pXPath, CConfigSchemaHelper::getInstance()->getElementArraySize(pXPath));
  202. return CConfigSchemaHelper::getInstance()->getElementArraySize(pXPath);
  203. }
  204. int getNumberOfTables()
  205. {
  206. return CConfigSchemaHelper::getInstance()->getNumberOfTables();
  207. }
  208. const char* getServiceName(int idx, char *pName)
  209. {
  210. if (pName != NULL)
  211. strcpy (pName, CBuildSetManager::getInstance()->getBuildSetServiceName(idx));
  212. return CBuildSetManager::getInstance()->getBuildSetServiceName(idx);
  213. }
  214. const char* getComponentName(int idx, char *pName)
  215. {
  216. if (pName != NULL)
  217. strcpy (pName, CBuildSetManager::getInstance()->getBuildSetComponentTypeName(idx));
  218. return CBuildSetManager::getInstance()->getBuildSetComponentTypeName(idx);
  219. }
  220. int openConfigurationFile(const char* pFile)
  221. {
  222. /*s_pConfigSchemaHelper = NULL;
  223. s_pConfigSchemaHelper = CConfigSchemaHelper::getInstance();
  224. StringArray arrXSDS;
  225. arrXSDS.append("dali.xsd");
  226. CBuildSetManager::getInstance()->setBuildSetArray(arrXSDS);
  227. s_pConfigSchemaHelper->populateSchema();*/
  228. CConfigSchemaHelper::getNewInstance()->loadEnvFromConfig(pFile);
  229. return 1;
  230. }
  231. int getNumberOfComponentsInConfiguration(void *pData)
  232. {
  233. if (pData == NULL)
  234. {
  235. return CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getNumberOfComponents();
  236. }
  237. else
  238. {
  239. CElement *pElement = static_cast<CElement*>(pData);
  240. assert(pElement->getNodeType() == XSD_ELEMENT);
  241. CElementArray *pElementArray = static_cast<CElementArray*>(pElement->getParentNode());
  242. assert(pElementArray->getNodeType() == XSD_ELEMENT_ARRAY);
  243. return pElementArray->length();
  244. }
  245. }
  246. void* getComponentInConfiguration(int idx)
  247. {
  248. assert(idx < CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getNumberOfComponents());
  249. return CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx);
  250. }
  251. void* getComponentInstance(int idx, void *pData)
  252. {
  253. assert(pData != NULL);
  254. assert(((static_cast<CElement*>(pData))->getNodeType()) == XSD_ELEMENT);
  255. CElement *pElement = static_cast<CElement*>(pData);
  256. CElementArray *pElementArray = static_cast<CElementArray*>(pElement->getParentNode());
  257. if (pElementArray->length() >= idx)
  258. idx = 0;
  259. return &(pElementArray->item(idx));
  260. }
  261. const char* getComponentNameInConfiguration(int idx, void *pData)
  262. {
  263. if (pData == NULL)
  264. return CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx)->getName();
  265. assert(!"Invalid component index");
  266. return NULL;
  267. }
  268. const void* getPointerToComponentInConfiguration(int idx, void *pData, int compIdx)
  269. {
  270. if (pData == NULL)
  271. {
  272. const CElement *pElement = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx);
  273. assert(pElement != NULL);
  274. const CXSDNodeBase *pNodeBase = pElement->getConstParentNode();
  275. const CElementArray *pElementArray = dynamic_cast<const CElementArray*>(pNodeBase);
  276. assert(pElementArray != NULL);
  277. return pElementArray;
  278. }
  279. else
  280. {
  281. assert( compIdx >= 0);
  282. CElementArray *pElementArray = static_cast<CElementArray*>(pData);
  283. assert(pElementArray->getNodeType() == XSD_ELEMENT_ARRAY);
  284. const CXSDNodeBase *pNodeBase = &(pElementArray->item(compIdx+idx));
  285. return(dynamic_cast<const CElement*>(pNodeBase));
  286. }
  287. }
  288. const void* getPointerToComponentTypeInConfiguration(void *pData)
  289. {
  290. assert (pData != NULL);
  291. CElement *pElement = static_cast<CElement*>(pData);
  292. assert (pElement->getNodeType() == XSD_ELEMENT);
  293. CElementArray *pElementArray = static_cast<CElementArray*>(pElement->getParentNode());
  294. return &(pElementArray->item(0));
  295. }
  296. int getIndexOfParent(void *pData)
  297. {
  298. assert (pData != NULL);
  299. assert((static_cast<CElement*>(pData))->getNodeType() == XSD_ELEMENT);
  300. int nIndexOfParent = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getIndexOfElement(static_cast<CElement*>(pData));
  301. assert(nIndexOfParent >= 0);
  302. return nIndexOfParent;
  303. }
  304. const void* getPointerToComponents()
  305. {
  306. assert(CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(0)->getConstParentNode()->getNodeType() == XSD_ELEMENT_ARRAY);
  307. return (CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(0)->getConstParentNode());
  308. }
  309. int getNumberOfChildren(void *pData)
  310. {
  311. int nRetVal = 0;
  312. if (pData == NULL)
  313. {
  314. assert(!"Should not be null"); // why ever null?
  315. return 0;
  316. }
  317. if (pData == (void*)(CEnvironmentModel::getInstance()))
  318. nRetVal = (static_cast<CEnvironmentModel*>(pData))->getNumberOfRootNodes();
  319. else // must be of type CEnvironmentModelNode*
  320. {
  321. CEnvironmentModelNode *pNode = static_cast<CEnvironmentModelNode*>(pData);
  322. nRetVal = pNode->getNumberOfChildren();
  323. }
  324. return nRetVal;
  325. }
  326. const char* getData(void *pData)
  327. {
  328. if (pData == NULL)
  329. return NULL;
  330. return CEnvironmentModel::getInstance()->getData(static_cast<CEnvironmentModelNode*>(pData));
  331. }
  332. const char* getName(void *pData)
  333. {
  334. if (pData == NULL)
  335. return NULL;
  336. return CEnvironmentModel::getInstance()->getInstanceName(static_cast<CEnvironmentModelNode*>(pData));
  337. }
  338. const char* getFileName(void *pData)
  339. {
  340. if (pData == NULL)
  341. return NULL;
  342. return CEnvironmentModel::getInstance()->getXSDFileName(static_cast<CEnvironmentModelNode*>(pData));
  343. }
  344. void* getParent(void *pData)
  345. {
  346. if (pData == NULL)
  347. return NULL;
  348. if (pData == (void*)(CEnvironmentModel::getInstance()->getRoot()))
  349. return (void*)(CEnvironmentModel::getInstance());
  350. else
  351. return (void*)(CEnvironmentModel::getInstance()->getParent(static_cast<CEnvironmentModelNode*>(pData)));
  352. }
  353. void* getChild(void *pData, int idx)
  354. {
  355. if (pData == NULL || pData == CEnvironmentModel::getInstance())
  356. {
  357. if (idx == 0)
  358. return (void*)(CEnvironmentModel::getInstance()->getRoot(0));
  359. return NULL;
  360. }
  361. else
  362. return (void*)(CEnvironmentModel::getInstance()->getChild(static_cast<CEnvironmentModelNode*>(pData), idx));
  363. }
  364. int getIndexFromParent(void *pData)
  365. {
  366. CEnvironmentModelNode *pNode = static_cast<CEnvironmentModelNode*>(pData);
  367. if (pNode->getParent() == NULL)
  368. return 0; // Must be 'Environment' node
  369. const CEnvironmentModelNode *pGrandParent = pNode->getParent();
  370. int nChildren = pGrandParent->getNumberOfChildren();
  371. for (int idx = 0; idx < nChildren; idx++)
  372. {
  373. if (pNode == pGrandParent->getChild(idx))
  374. return idx;
  375. }
  376. assert(!"Should not reach here");
  377. return 0;
  378. }
  379. void* getRootNode(int idx)
  380. {
  381. return (void*)(CEnvironmentModel::getInstance()->getRoot(idx));
  382. }
  383. void* getModel()
  384. {
  385. return (void*)(CEnvironmentModel::getInstance());
  386. }
  387. void getJSON(void *pData, char **pOutput, int nIdx)
  388. {
  389. CConfigSchemaHelper::getInstance()->printJSON(CONFIGURATOR_API::getFileName(pData), pOutput, nIdx, true);
  390. }
  391. void getNavigatorJSON(char **pOutput)
  392. {
  393. CConfigSchemaHelper::getInstance()->printNavigatorJSON(pOutput, true);
  394. }
  395. void getJSONByComponentName(const char *pComponentName, char **pOutput, int nIdx)
  396. {
  397. CConfigSchemaHelper::getInstance()->printJSON(pComponentName, pOutput, nIdx, true);
  398. }
  399. void getJSONByComponentKey(const char *pKey, char **pOutput)
  400. {
  401. CConfigSchemaHelper::getInstance()->printJSONByKey(pKey, pOutput, true);
  402. }
  403. void getDocBookByIndex(int idx, char **pOutput)
  404. {
  405. const char *pFileName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  406. CConfigSchemaHelper::getInstance()->printDocumentation(pFileName, pOutput);
  407. }
  408. bool saveConfigurationFile()
  409. {
  410. return CConfigSchemaHelper::getInstance()->saveConfigurationFile();
  411. }
  412. bool saveConfigurationFileAs(const char *pFilePath)
  413. {
  414. if (pFilePath == NULL || *pFilePath == 0)
  415. return false;
  416. return CConfigSchemaHelper::getInstance()->saveConfigurationFileAs(pFilePath);
  417. }
  418. int getNumberOfNotificationTypes()
  419. {
  420. return CNotificationManager::getInstance()->getNumberOfNotificationTypes();
  421. }
  422. const char* getNotificationTypeName(int type)
  423. {
  424. return CNotificationManager::getInstance()->getNotificationTypeName(type);
  425. }
  426. int getNumberOfNotifications(int type)
  427. {
  428. enum ENotificationType eType = static_cast<ENotificationType>(type);
  429. return CNotificationManager::getInstance()->getNumberOfNotifications(eType);
  430. }
  431. const char* getNotification(int type, int idx)
  432. {
  433. const char *pRet = NULL;
  434. enum ENotificationType eType = static_cast<ENotificationType>(type);
  435. return CNotificationManager::getInstance()->getNotification(eType, idx);
  436. }
  437. } // CONFIGURATOR_API namespace