api.onudescribe.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * PON ONU describe management class
  4. */
  5. class OnuDescribe extends OnuBase {
  6. /**
  7. * Flag to determine that operation did actually happened and went successfully
  8. *
  9. * @var bool
  10. */
  11. public $operationSuccessful = false;
  12. /**
  13. * Returns ONU description string
  14. *
  15. * @return bool|mixed|string
  16. *
  17. * @throws Exception
  18. */
  19. public function getOnuDescription() {
  20. $result = false;
  21. $onuFound = true;
  22. if (empty($this->snmpTemplateParsed)) {
  23. $this->displayMessage = __('SNMP template is not found or not exists');
  24. return ($result);
  25. }
  26. if (empty($this->onuData)) {
  27. $this->displayMessage = __('ONU data is empty');
  28. return ($result);
  29. }
  30. $macOnu = $this->onuData['mac'];
  31. $snmpData = $this->snmpTemplateParsed;
  32. if (isset($snmpData['onu']['CONTROLMODE'])) {
  33. $snmpControlMode = $snmpData['onu']['CONTROLMODE'];
  34. if ($snmpControlMode == 'STELSFD11' or $snmpControlMode == 'STELSFD12') {
  35. $this->displayMessage = __('Function is not supported by this OLT') . ': ' . $snmpControlMode;
  36. return ($result);
  37. }
  38. if ($snmpControlMode == 'VSOL_1600D') {
  39. $macIndexOID = $snmpData['signal']['MACINDEX'];
  40. $macValType = $snmpData['signal']['MACVALUE'];
  41. $descrGetIdx = $snmpData['onu']['DESCRIPTIONGET'];
  42. $descrValue = $snmpData['onu']['DESCRVALUE'];
  43. $macIndexFull = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $macIndexOID);
  44. if (!empty($macIndexFull)) {
  45. $macIndexFull = str_ireplace(array($macIndexOID, $macValType, '"'), ' ', $macIndexFull);
  46. $macIndexFull = explodeRows($macIndexFull);
  47. foreach ($macIndexFull as $eachRow) {
  48. $indexMAC = explode(' = ', $eachRow);
  49. if (!empty($indexMAC[1])) {
  50. $tmpCleanMAC = strtolower(trim($indexMAC[1]));
  51. if ($macOnu == $tmpCleanMAC) {
  52. $onuFound = true;
  53. $tmpPONONUIdx = trim($indexMAC[0]);
  54. $result = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $descrGetIdx . $tmpPONONUIdx, false);
  55. if (!empty($result) or $result === '') {
  56. $this->operationSuccessful = true;
  57. $result = str_replace(array($descrGetIdx . $tmpPONONUIdx, $descrValue, '=', '"', ' '), '', $result);
  58. return ($result);
  59. }
  60. }
  61. } else {
  62. $onuFound = false;
  63. }
  64. }
  65. } else {
  66. $onuFound = false;
  67. }
  68. }
  69. } elseif ($this->checkBDCOMEssentialOpts()) {
  70. $eponInt = '';
  71. $decMacOnu = $this->macHexToDec($macOnu);
  72. if (empty($decMacOnu)) {
  73. $this->displayMessage = __('Wrong MAC format (should be XX:XX:XX:XX:XX:XX)');
  74. return ($result);
  75. }
  76. if ($snmpData['vlan']['VLANMODE'] == 'BDCOM_B') {
  77. $ifIndexOid = $snmpData['onu']['IFINDEX'] . '.' . $decMacOnu;
  78. $ifIndexFull = snmp2_get($this->oltData['ip'], $this->oltData['snmp'], $ifIndexOid);
  79. $ifIndex = trim(str_replace(array($ifIndexOid, 'INTEGER:', '= '), '', $ifIndexFull));
  80. if (!empty($ifIndex)) {
  81. $eponIntBare = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $snmpData['onu']['EPONINDEX'] . '.' . $ifIndex);
  82. $eponInt = trim(str_replace(array($snmpData['onu']['EPONINDEX'] . '.' . $ifIndex, ' = INTEGER: '), '', $eponIntBare));
  83. }
  84. if (!empty($eponInt)) {
  85. $descriptionOid = $snmpData['onu']['DESCRIPTION'] . '.' . $eponInt . '.' . $decMacOnu;
  86. $checkResult = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $descriptionOid, FALSE);
  87. $result = trim(str_replace(array($descriptionOid, ' = STRING: '), '', $checkResult));
  88. if (!empty($result) or $result === '') {
  89. $this->operationSuccessful = true;
  90. return ($result);
  91. }
  92. } else {
  93. $onuFound = false;
  94. }
  95. }
  96. if ($snmpData['vlan']['VLANMODE'] == 'BDCOM_C') {
  97. $allOnuOid = $snmpData['signal']['MACINDEX'];
  98. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  99. $allOnu = @snmp2_real_walk($this->oltData['ip'], $this->oltData['snmp'], $allOnuOid);
  100. $searchArray = array();
  101. if (!empty($allOnu)) {
  102. foreach ($allOnu as $eachIndex => $eachOnu) {
  103. $eachIndex = trim(str_replace($allOnuOid . '.', '', $eachIndex));
  104. $eachOnu = strtolower(trim(str_replace($snmpData['signal']['MACVALUE'], '', $eachOnu)));
  105. $eachOnuMacArray = explode(" ", $eachOnu);
  106. $eachOnuMac = implode(":", $eachOnuMacArray);
  107. $searchArray[$eachOnuMac] = $eachIndex;
  108. }
  109. if (!empty($searchArray) and isset($searchArray[$macOnu])) {
  110. $ifIndex = $searchArray[$macOnu];
  111. $eponIntBare = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $snmpData['onu']['EPONINDEX'] . '.' . $ifIndex);
  112. $eponInt = trim(str_replace(array($snmpData['onu']['EPONINDEX'] . '.' . $ifIndex, ' = INTEGER: '), '', $eponIntBare));
  113. }
  114. if (!empty($eponInt)) {
  115. $descriptionOid = $snmpData['onu']['DESCRIPTION'] . '.' . $eponInt . '.' . $decMacOnu;
  116. $checkResult = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $descriptionOid, FALSE);
  117. $result = trim(str_replace(array($descriptionOid, ' = STRING: '), '', $checkResult));
  118. if (!empty($result) or $result === '') {
  119. $this->operationSuccessful = true;
  120. return ($result);
  121. }
  122. } else {
  123. $onuFound = false;
  124. }
  125. }
  126. }
  127. } else {
  128. $this->displayMessage = __('Essential SNMP options are missing in template');
  129. return ($result);
  130. }
  131. if (!$this->operationSuccessful) {
  132. if ($onuFound) {
  133. $this->displayMessage = __('Operation unsuccessful');
  134. } else {
  135. $this->displayMessage = __('ONU not found');
  136. }
  137. }
  138. return ($result);
  139. }
  140. /**
  141. * Sets ONU description on OLT
  142. *
  143. * @param $description
  144. *
  145. * @return bool|mixed|string
  146. *
  147. * @throws Exception
  148. */
  149. public function describeOnu($description) {
  150. $result = false;
  151. $onuFound = true;
  152. $description = trim($description, " \t\n\r\0\x0B\x31\x22");
  153. $descrIsEmptyStr = ($description === '');
  154. if (empty($this->snmpTemplateParsed)) {
  155. $this->displayMessage = __('SNMP template is not found or not exists');
  156. return ($result);
  157. }
  158. if (empty($this->onuData)) {
  159. $this->displayMessage = __('ONU data is empty');
  160. return ($result);
  161. }
  162. $macOnu = $this->onuData['mac'];
  163. $snmpData = $this->snmpTemplateParsed;
  164. if (isset($snmpData['onu']['CONTROLMODE'])) {
  165. $snmpControlMode = $snmpData['onu']['CONTROLMODE'];
  166. if ($snmpControlMode == 'STELSFD11' or $snmpControlMode == 'STELSFD12') {
  167. $this->displayMessage = __('Function is not supported by this OLT') . ': ' . $snmpControlMode;
  168. return ($result);
  169. }
  170. if ($snmpControlMode == 'VSOL_1600D') {
  171. $description = ($descrIsEmptyStr) ? '""' : $description;
  172. $macIndexOID = $snmpData['signal']['MACINDEX'];
  173. $macValType = $snmpData['signal']['MACVALUE'];
  174. $descrPONIdx = $snmpData['onu']['DESCRPONINDEX'];
  175. $descrONUIdx = $snmpData['onu']['DESCRONUINDEX'];
  176. $descrStrIdx = $snmpData['onu']['DESCRSTRING'];
  177. $descrCommit = $snmpData['onu']['DESCRCOMMIT'];
  178. $descrGetIdx = $snmpData['onu']['DESCRIPTIONGET'];
  179. $descrValue = $snmpData['onu']['DESCRVALUE'];
  180. $macIndexFull = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $macIndexOID);
  181. if (!empty($macIndexFull)) {
  182. $macIndexFull = str_ireplace(array($macIndexOID, $macValType, '"'), ' ', $macIndexFull);
  183. $macIndexFull = explodeRows($macIndexFull);
  184. $describeData = array();
  185. foreach ($macIndexFull as $eachRow) {
  186. $indexMAC = explode(' = ', $eachRow);
  187. if (!empty($indexMAC[1])) {
  188. $tmpCleanMAC = strtolower(trim($indexMAC[1]));
  189. if ($macOnu == $tmpCleanMAC) {
  190. $onuFound = true;
  191. $tmpPONONUIdx = trim($indexMAC[0]);
  192. $tmpIdx = trim(substr($indexMAC[0], 1), '.');
  193. $ponIfaceIndex = substr($tmpIdx, 0, strpos($tmpIdx, '.', 1));
  194. $onuIndex = substr($tmpIdx, strpos($tmpIdx, '.', 1) + 1);
  195. $describeData[] = array('oid' => $descrPONIdx, 'type' => 'i', 'value' => $ponIfaceIndex);
  196. $describeData[] = array('oid' => $descrONUIdx, 'type' => 'i', 'value' => $onuIndex);
  197. $describeData[] = array('oid' => $descrStrIdx, 'type' => 's', 'value' => $description);
  198. $describeData[] = array('oid' => $descrCommit, 'type' => 'i', 'value' => '1');
  199. $this->snmp->set($this->oltData['ip'], $this->oltData['snmpwrite'], $describeData);
  200. //very shitty hack
  201. //sleep(4);
  202. $result = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $descrGetIdx . $tmpPONONUIdx, false);
  203. if (!empty($result) or ($result === '' and $descrIsEmptyStr)) {
  204. $this->operationSuccessful = true;
  205. $result = str_replace(array($descrGetIdx . $tmpPONONUIdx, $descrValue, '=', '"', ' '), '', $result);
  206. return ($result);
  207. }
  208. } else {
  209. $onuFound = false;
  210. }
  211. }
  212. }
  213. } else {
  214. $onuFound = false;
  215. }
  216. }
  217. } elseif ($this->checkBDCOMEssentialOpts()) {
  218. $eponInt = '';
  219. $decMacOnu = $this->macHexToDec($macOnu);
  220. if (empty($decMacOnu)) {
  221. $this->displayMessage = __('Wrong MAC format (should be XX:XX:XX:XX:XX:XX)');
  222. return ($result);
  223. }
  224. if ($snmpData['vlan']['VLANMODE'] == 'BDCOM_B') {
  225. $ifIndexOid = $snmpData['onu']['IFINDEX'] . '.' . $decMacOnu;
  226. $ifIndexFull = snmp2_get($this->oltData['ip'], $this->oltData['snmp'], $ifIndexOid);
  227. $ifIndex = trim(str_replace(array($ifIndexOid, 'INTEGER:', '= '), '', $ifIndexFull));
  228. if (!empty($ifIndex)) {
  229. $eponIntBare = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $snmpData['onu']['EPONINDEX'] . '.' . $ifIndex);
  230. $eponInt = trim(str_replace(array($snmpData['onu']['EPONINDEX'] . '.' . $ifIndex, ' = INTEGER: '), '', $eponIntBare));
  231. }
  232. if (!empty($eponInt)) {
  233. $describeData[] = array(
  234. 'oid' => $snmpData['onu']['DESCRIPTION'] . '.' . $eponInt . '.' . $decMacOnu,
  235. 'type' => 's',
  236. 'value' => '"' . addcslashes($description, '_') . '"',
  237. );
  238. $describeData[] = array(
  239. 'oid' => $snmpData['vlan']['SAVE'],
  240. 'type' => 'i',
  241. 'value' => '1'
  242. );
  243. $result = $this->snmp->set($this->oltData['ip'], $this->oltData['snmpwrite'], $describeData);
  244. $result.= $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $snmpData['onu']['DESCRIPTION'] . '.' . $eponInt . '.' . $decMacOnu, FALSE);
  245. if (!empty($result) or ($result === '' and $descrIsEmptyStr)) {
  246. $this->operationSuccessful = true;
  247. return ($result);
  248. }
  249. } else {
  250. $onuFound = false;
  251. }
  252. }
  253. if ($snmpData['vlan']['VLANMODE'] == 'BDCOM_C') {
  254. $allOnuOid = $snmpData['signal']['MACINDEX'];
  255. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  256. $allOnu = @snmp2_real_walk($this->oltData['ip'], $this->oltData['snmp'], $allOnuOid);
  257. $searchArray = array();
  258. if (!empty($allOnu)) {
  259. foreach ($allOnu as $eachIndex => $eachOnu) {
  260. $eachIndex = trim(str_replace($allOnuOid . '.', '', $eachIndex));
  261. $eachOnu = strtolower(trim(str_replace($snmpData['signal']['MACVALUE'], '', $eachOnu)));
  262. $eachOnuMacArray = explode(" ", $eachOnu);
  263. $eachOnuMac = implode(":", $eachOnuMacArray);
  264. $searchArray[$eachOnuMac] = $eachIndex;
  265. }
  266. if (!empty($searchArray) and isset($searchArray[$macOnu])) {
  267. $ifIndex = $searchArray[$macOnu];
  268. $eponIntBare = $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $snmpData['onu']['EPONINDEX'] . '.' . $ifIndex);
  269. $eponInt = trim(str_replace(array($snmpData['onu']['EPONINDEX'] . '.' . $ifIndex, ' = INTEGER: '), '', $eponIntBare));
  270. }
  271. if (!empty($eponInt)) {
  272. $describeData[] = array(
  273. 'oid' => $snmpData['onu']['DESCRIPTION'] . '.' . $eponInt . '.' . $decMacOnu,
  274. 'type' => 's',
  275. 'value' => '"' . addcslashes($description, '_') . '"',
  276. );
  277. $describeData[] = array(
  278. 'oid' => $snmpData['vlan']['SAVE'],
  279. 'type' => 'i',
  280. 'value' => '1'
  281. );
  282. $result = $this->snmp->set($this->oltData['ip'], $this->oltData['snmpwrite'], $describeData);
  283. $result.= $this->snmp->walk($this->oltData['ip'], $this->oltData['snmp'], $snmpData['onu']['DESCRIPTION'] . '.' . $eponInt . '.' . $decMacOnu, FALSE);
  284. if (!empty($result) or ($result === '' and $descrIsEmptyStr)) {
  285. $this->operationSuccessful = true;
  286. return ($result);
  287. }
  288. } else {
  289. $onuFound = false;
  290. }
  291. }
  292. }
  293. } else {
  294. $this->displayMessage = __('Essential SNMP options are missing in template');
  295. return ($result);
  296. }
  297. if (!$this->operationSuccessful) {
  298. if ($onuFound) {
  299. $this->displayMessage = __('Operation unsuccessful');
  300. } else {
  301. $this->displayMessage = __('ONU not found');
  302. }
  303. }
  304. return ($result);
  305. }
  306. /**
  307. * Returns ONU description controls
  308. *
  309. * @param $login
  310. *
  311. * @return string
  312. *
  313. * @throws Exception
  314. */
  315. public function describeForm($login) {
  316. $onuDescription = trim($this->getOnuDescription(), " \t\n\r\0\x0B\x31\x22");
  317. $onuDescription = (empty($onuDescription)) ? $login : $onuDescription;
  318. $descriptionInputId = wf_InputId();
  319. $inputs = wf_delimiter();
  320. $inputs .= wf_tag('input', false, '', 'type="text" name="onuDescription" value="' . $onuDescription . '" id="' . $descriptionInputId . '" size="60" style="margin-left: 10px;"');
  321. $inputs .= wf_tag('label', false, '', 'for ="' . $descriptionInputId . '"') . __('Description') . wf_tag('label', true);
  322. $inputs .= wf_delimiter();
  323. $inputs .= wf_SubmitClassed('true', 'vlanButton', 'DescribeOnu', __('Change onu description'));
  324. $form = wf_Form("", 'POST', $inputs);
  325. return ($form);
  326. }
  327. }