api.ponstels16.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. /**
  3. * OLT Stels FD16XX hardware abstraction layer
  4. */
  5. class PONstels16 extends PONProto {
  6. /**
  7. * Stels FD16XX devices polling
  8. *
  9. * @return void
  10. */
  11. public function collect() {
  12. $oltModelId = $this->oltParameters['MODELID'];
  13. $oltid = $this->oltParameters['ID'];
  14. $oltIp = $this->oltParameters['IP'];
  15. $oltCommunity = $this->oltParameters['COMMUNITY'];
  16. $oltNoFDBQ = $this->oltParameters['NOFDB'];
  17. $oltIPPORT = $oltIp . ':' . self::SNMPPORT;
  18. $deviceType = $this->snmpTemplates[$oltModelId]['define']['DEVICE'];
  19. $ponPrefixAdd = (empty($this->snmpTemplates[$oltModelId]['misc']['INTERFACEADDPONPREFIX'])
  20. ? '' : $this->snmpTemplates[$oltModelId]['misc']['INTERFACEADDPONPREFIX']);
  21. $this->onuSerialCaseMode = (isset($this->snmpTemplates[$oltModelId]['onu']['SERIAL_CASE_MODE'])
  22. ? $this->snmpTemplates[$oltModelId]['onu']['SERIAL_CASE_MODE'] : 0);
  23. $distIndex = array();
  24. $ifaceIndex = array();
  25. $sigIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  26. $this->snmpTemplates[$oltModelId]['signal']['SIGINDEX'],
  27. '',
  28. '.0.0 ', self::SNMPCACHE);
  29. //ONU distance polling for stels16 devices
  30. if (isset($this->snmpTemplates[$oltModelId]['misc'])) {
  31. if (isset($this->snmpTemplates[$oltModelId]['misc']['DISTINDEX'])) {
  32. if (!empty($this->snmpTemplates[$oltModelId]['misc']['DISTINDEX'])) {
  33. $distIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  34. $this->snmpTemplates[$oltModelId]['misc']['DISTINDEX'],
  35. '',
  36. '', self::SNMPCACHE);
  37. $onuIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  38. $this->snmpTemplates[$oltModelId]['misc']['ONUINDEX'],
  39. '',
  40. '.0.0 ', self::SNMPCACHE);
  41. $ifaceIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  42. $this->snmpTemplates[$oltModelId]['misc']['INTERFACEINDEX'],
  43. '',
  44. '"', self::SNMPCACHE);
  45. }
  46. }
  47. if (isset($this->snmpTemplates[$oltModelId]['misc']['DEREGREASON'])) {
  48. if (!empty($this->snmpTemplates[$oltModelId]['misc']['DEREGREASON'])) {
  49. $lastDeregIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  50. $this->snmpTemplates[$oltModelId]['misc']['DEREGREASON'],
  51. '',
  52. '"', self::SNMPCACHE);
  53. }
  54. }
  55. }
  56. //getting MAC index.
  57. $macIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  58. $this->snmpTemplates[$oltModelId]['signal']['MACINDEX'],
  59. '',
  60. '', self::SNMPCACHE);
  61. $macIndexProcessed = $this->macParseStels16($macIndex);
  62. $this->signalParse($oltid, $sigIndex, $macIndex, $this->snmpTemplates[$oltModelId]['signal']);
  63. if (isset($this->snmpTemplates[$oltModelId]['misc'])) {
  64. if (isset($this->snmpTemplates[$oltModelId]['misc']['DISTINDEX'])) {
  65. if (!empty($this->snmpTemplates[$oltModelId]['misc']['DISTINDEX'])) {
  66. // processing distance data
  67. $this->distanceParse($oltid, $distIndex, $macIndex);
  68. //processing interfaces data
  69. //$this->interfaceParsestels16($oltid, $ifaceIndex, $macIndex, $deviceType);
  70. $this->interfaceParsestels16($oltid, $ifaceIndex, $ponPrefixAdd);
  71. }
  72. }
  73. if (isset($this->snmpTemplates[$oltModelId]['misc']['DEREGREASON'])) {
  74. if (!empty($this->snmpTemplates[$oltModelId]['misc']['DEREGREASON'])) {
  75. $this->lastDeregParsestels16($oltid, $lastDeregIndex, $macIndexProcessed);
  76. }
  77. }
  78. }
  79. if (!$oltNoFDBQ) {
  80. $fdbMACIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  81. $this->snmpTemplates[$oltModelId]['misc']['FDBMACINDEX'],
  82. '',
  83. '', self::SNMPCACHE);
  84. $this->fdbParsestels16($fdbMACIndex, $macIndexProcessed);
  85. }
  86. $uniOperStatusIndex = array();
  87. if (isset($this->snmpTemplates[$oltModelId]['misc']['UNIOPERSTATUS'])) {
  88. $uniOperStatusIndex = $this->walkCleared($oltIPPORT, $oltCommunity,
  89. $this->snmpTemplates[$oltModelId]['misc']['UNIOPERSTATUS'],
  90. '',
  91. array($this->snmpTemplates[$oltModelId]['misc']['UNIOPERSTATUSVALUE'], '"'),
  92. self::SNMPCACHE);
  93. $this->uniParseStels16($uniOperStatusIndex, $macIndexProcessed);
  94. }
  95. }
  96. /**
  97. * Processes OLT MAC adresses and returns them in array: LLID=>MAC
  98. *
  99. * @param $macIndex
  100. *
  101. * @return array
  102. */
  103. protected function macParseStels16($macIndex) {
  104. $ONUsMACs = array();
  105. if (!empty($macIndex)) {
  106. //mac index preprocessing
  107. foreach ($macIndex as $io => $eachmac) {
  108. $line = explode('=', $eachmac);
  109. if (empty($line[0]) || empty($line[1])) {
  110. continue;
  111. }
  112. $tmpONUDevIdx = trim($line[0]);
  113. $tmpONUMAC = trim($line[1]);
  114. $tmpONUMAC = str_replace(' ', ':', $tmpONUMAC);
  115. if ($this->onuSerialCaseMode == 1) {
  116. $tmpONUMAC = strtolower($tmpONUMAC);
  117. } elseif ($this->onuSerialCaseMode == 2) {
  118. $tmpONUMAC = strtoupper($tmpONUMAC);
  119. }
  120. //mac is present
  121. if (!empty($tmpONUDevIdx) and ! empty($tmpONUMAC)) {
  122. $ONUsMACs[$tmpONUDevIdx] = $tmpONUMAC;
  123. }
  124. }
  125. }
  126. return ($ONUsMACs);
  127. }
  128. /**
  129. * Parses & stores in cache OLT ONU interfaces
  130. *
  131. * @param int $oltid
  132. * @param array $ifaceIndex
  133. * @param array $macIndex
  134. *
  135. * @return void
  136. */
  137. protected function interfaceParsestels16($oltid, $ifaceIndex, $ponPrefixAdd = '') {
  138. $macIndex = $this->olt->readMacIndex();
  139. $oltid = vf($oltid, 3);
  140. $ifaceTmp = array();
  141. $result = array();
  142. $i = 0;
  143. //iface index preprocessing
  144. if ((!empty($ifaceIndex)) and ( !empty($macIndex))) {
  145. // creating mapping of internal pon ifaces nums to sequential, like this:
  146. //bsPortIndex.1.0.13; Value (Integer): 13 => 1
  147. //bsPortIndex.1.0.14; Value (Integer): 14 => 2
  148. //bsPortIndex.1.0.15; Value (Integer): 15 => 3
  149. //bsPortIndex.1.0.16; Value (Integer): 16 => 4
  150. //bsPortIndex.1.0.17; Value (Integer): 17 => 5
  151. //bsPortIndex.1.0.18; Value (Integer): 18 => 6
  152. //bsPortIndex.1.0.19; Value (Integer): 19 => 7
  153. //bsPortIndex.1.0.20; Value (Integer): 20 => 8
  154. foreach ($ifaceIndex as $io => $eachIface) {
  155. $i++;
  156. $line = explode('=', $eachIface);
  157. if (isset($line[1])) {
  158. $ponIfaceNum = trim($line[1]); // pon interface number
  159. $ifaceTmp[$ponIfaceNum] = $i;
  160. }
  161. }
  162. // using "special" math to get pon port num + LLID from dev index
  163. // formula: dev index DEC to HEX
  164. // 16780033 => 1000B01, where
  165. // 0B => 11 - pon port num
  166. // 01 => 1 - LLID
  167. if (!empty($macIndex) and !empty($ifaceTmp)) {
  168. foreach ($macIndex as $eachMac => $devId) {
  169. $LLID = '';
  170. $hexDevId = dechex($devId);
  171. $portNum = hexdec(substr($hexDevId, -4, 2));
  172. $onuNum = hexdec(substr($hexDevId, -2, 2));
  173. if (!empty($ifaceTmp[$portNum])) {
  174. $portNum = $ifaceTmp[$portNum];
  175. $LLID = $portNum . ":" . $onuNum;
  176. } else {
  177. $LLID = __('On ho');
  178. }
  179. //storing results
  180. $result[$eachMac] = $ponPrefixAdd . $LLID;
  181. }
  182. }
  183. //saving ONUs interfaces
  184. $this->olt->writeInterfaces($result);
  185. }
  186. }
  187. /**
  188. * Parses & stores in cache ONU last dereg reasons
  189. *
  190. * @param int $oltid
  191. * @param array $deregIndex
  192. * @param array $macIndex
  193. *
  194. * @return void
  195. */
  196. protected function lastDeregParsestels16($oltid, $deregIndex, $macIndex) {
  197. $oltid = vf($oltid, 3);
  198. $deregTmp = array();
  199. $macTmp = array();
  200. $result = array();
  201. //dereg index preprocessing
  202. if ((!empty($deregIndex)) and ( !empty($macIndex))) {
  203. foreach ($deregIndex as $io => $eachdereg) {
  204. $line = explode('=', $eachdereg);
  205. //dereg is present
  206. if (isset($line[1])) {
  207. $lastDeregRaw = trim($line[1]); // last dereg reason
  208. $devIndex = trim($line[0]); // device index
  209. $deregTmp[$devIndex] = $lastDeregRaw;
  210. }
  211. }
  212. //mac index preprocessing
  213. /* foreach ($macIndex as $io => $eachmac) {
  214. $line = explode('=', $eachmac);
  215. //mac is present
  216. if (isset($line[1])) {
  217. $macRaw = trim($line[1]); //mac address
  218. $devIndex = trim($line[0]); //device index
  219. $macRaw = str_replace(' ', ':', $macRaw);
  220. $macRaw = strtolower($macRaw);
  221. $macTmp[$devIndex] = $macRaw;
  222. }
  223. }*/
  224. //storing results
  225. if (!empty($macIndex)) {
  226. foreach ($macIndex as $devId => $eachMac) {
  227. $currentInterface = '';
  228. if (!empty($deregTmp)) {
  229. foreach ($deregTmp as $intefaceOffset => $interfaceName) {
  230. // dirty hack for firmware > 1.4.0 - some shitty math used
  231. $recalcIfaceOffset = $intefaceOffset;
  232. if ($recalcIfaceOffset < 100) {
  233. $recalcIfaceOffset = (($recalcIfaceOffset - 10) * 256) + 16779776;
  234. }
  235. if ($devId >= $recalcIfaceOffset) {
  236. $currentInterface = $intefaceOffset;
  237. }
  238. }
  239. $result[$eachMac] = (isset($deregTmp[$currentInterface])) ? $deregTmp[$currentInterface] : __('On ho');
  240. }
  241. }
  242. //saving ONUs dereg reasons
  243. $this->olt->writeDeregs($result);
  244. }
  245. }
  246. }
  247. /**
  248. * Parses & stores in cache OLT ONU interfaces
  249. *
  250. * @param int $oltid
  251. * @param array $intIndex
  252. * @param array $macIndex
  253. *
  254. * @return void
  255. */
  256. protected function interfaceParseStels($oltid, $intIndex, $macIndex) {
  257. $oltid = vf($oltid, 3);
  258. $intTmp = array();
  259. $macTmp = array();
  260. $result = array();
  261. //distance index preprocessing
  262. if ((!empty($intIndex)) and ( !empty($macIndex))) {
  263. foreach ($intIndex as $io => $eachint) {
  264. $line = explode('=', $eachint);
  265. //distance is present
  266. if (isset($line[1])) {
  267. // distance
  268. $devIndex = trim($line[0]); // device index
  269. $devIndex = explode('.', $devIndex);
  270. $portIndex = trim($devIndex[0]);
  271. $interfaceRaw = $devIndex[0] . ':' . $devIndex[1];
  272. // $devIndex = ($devIndex[1] * 256) + 1;
  273. $intTmp[$portIndex . ':' . $devIndex] = $interfaceRaw;
  274. }
  275. }
  276. //mac index preprocessing
  277. foreach ($macIndex as $io => $eachmac) {
  278. $line = explode('=', $eachmac);
  279. //mac is present
  280. if (isset($line[1])) {
  281. $macRaw = trim($line[1]); //mac address
  282. $devIndex = trim($line[0]); //device index
  283. $devIndex = explode('.', $devIndex);
  284. $portIndex = trim($devIndex[0]);
  285. $devIndex = $devIndex[1];
  286. $macRaw = str_replace(' ', ':', $macRaw);
  287. $macRaw = strtolower($macRaw);
  288. $macTmp[$portIndex . ':' . $devIndex] = $macRaw;
  289. }
  290. }
  291. //storing results
  292. if (!empty($macTmp)) {
  293. foreach ($macTmp as $devId => $eachMac) {
  294. if (isset($intTmp[$devId])) {
  295. $interface = $intTmp[$devId];
  296. $result[$eachMac] = $interface;
  297. }
  298. }
  299. //saving ONUs interfaces
  300. $this->olt->writeInterfaces($result);
  301. }
  302. }
  303. }
  304. /**
  305. * Parses & stores in cache OLT ONU interfaces
  306. *
  307. * @param int $oltid
  308. * @param array $deregIndex
  309. * @param array $macIndex
  310. *
  311. * @return void
  312. */
  313. protected function lastDeregParseStels($oltid, $deregIndex, $macIndex) {
  314. $oltid = vf($oltid, 3);
  315. $deregTmp = array();
  316. $onuTmp = array();
  317. $result = array();
  318. //dereg index preprocessing
  319. if ((!empty($deregIndex)) and ( !empty($macIndex))) {
  320. foreach ($deregIndex as $io => $eachdereg) {
  321. $line = explode('=', $eachdereg);
  322. //dereg is present
  323. if (isset($line[1])) {
  324. $deregRaw = trim(trim($line[1]), '"'); // dereg
  325. $devIndex = $line[0];
  326. $devIndex = explode('.', $devIndex);
  327. $portIndex = trim($devIndex[0]);
  328. $devIndex = trim($devIndex[1]);
  329. // $devIndex = (($devIndex * 256) + 1);
  330. $deregTmp[$portIndex . ':' . $devIndex] = $deregRaw;
  331. }
  332. }
  333. //mac index preprocessing
  334. foreach ($macIndex as $io => $eachmac) {
  335. $line = explode('=', $eachmac);
  336. //mac is present
  337. if (isset($line[1])) {
  338. $macRaw = trim($line[1]); //mac address
  339. $devIndex = trim($line[0]);
  340. $devIndex = explode('.', $devIndex);
  341. $portIndex = trim($devIndex[0]);
  342. $devIndex = $devIndex[1];
  343. $macRaw = str_replace(' ', ':', $macRaw);
  344. $macRaw = strtolower($macRaw);
  345. $onuTmp[$portIndex . ':' . $devIndex] = $macRaw;
  346. }
  347. }
  348. //storing results
  349. if (!empty($onuTmp)) {
  350. foreach ($onuTmp as $devId => $eachMac) {
  351. if (isset($deregTmp[$devId])) {
  352. $result[$eachMac] = $deregTmp[$devId];
  353. }
  354. }
  355. //saving ONUs dereg reasons
  356. $this->olt->writeDeregs($result);
  357. }
  358. }
  359. }
  360. /**
  361. * Parses & stores to cache ONUs FDB cache (MACs behind ONU)
  362. *
  363. * @param $fdbMACIndex
  364. *
  365. * @return void
  366. */
  367. protected function fdbParsestels16($fdbMACIndex, $macIndexProcessed) {
  368. $onuMACIndex = $macIndexProcessed;
  369. $i = 0;
  370. $fdbCahce = array();
  371. if (!empty($onuMACIndex) and !empty($fdbMACIndex)) {
  372. // processing $fdbMACIndex array to get a FDB record at once
  373. foreach ($fdbMACIndex as $eachIdx => $eachFDBLine) {
  374. $i++;
  375. if (empty($eachFDBLine) or !ispos($eachFDBLine, '=')) { continue; }
  376. $line = explode('=', $eachFDBLine);
  377. $onuPlasticIdx = trim($line[1]); // ONU Plastic index
  378. // Plastic index is present
  379. if (isset($onuMACIndex[$onuPlasticIdx])) {
  380. $fdbMACVLAN = trim($line[0]); // getting DEC MAC + VLAN portion
  381. $fdbVLAN = substr($fdbMACVLAN, strripos($fdbMACVLAN, '.') + 1); // fdb VLAN
  382. $fdbMAC = convertMACDec2Hex(substr($fdbMACVLAN, 0, strripos($fdbMACVLAN, '.'))); // fdb MAC;
  383. $fdbCahce[$onuMACIndex[$onuPlasticIdx]][$i] = array('mac' => $fdbMAC, 'vlan' => $fdbVLAN);
  384. }
  385. }
  386. }
  387. //saving OLT FDB
  388. $this->olt->writeFdb($fdbCahce);
  389. }
  390. /**
  391. * Performs UNI port oper status preprocessing for index array and stores it into cache
  392. *
  393. * @param $uniOperStatusIndex
  394. * @param $macIndexProcessed
  395. *
  396. * @return void
  397. */
  398. protected function uniParseStels16($uniOperStatusIndex, $macIndexProcessed) {
  399. $uniStats = array();
  400. $result = array();
  401. if (!empty($macIndexProcessed) and !empty($uniOperStatusIndex)) {
  402. //UniOperStats index preprocessing
  403. foreach ($uniOperStatusIndex as $io => $eachRow) {
  404. $line = explode('=', $eachRow);
  405. file_put_contents('exports/pondata/unioperstats/unistats', print_r($line, true));
  406. if (empty($line[0]) || empty($line[1])) {
  407. continue;
  408. }
  409. // dev index + .0. + ether port index
  410. $tmpDevIdxEtherIdx = trim($line[0]);
  411. $tmpDevIdxEtherIdxLen = strlen($tmpDevIdxEtherIdx);
  412. // ehter port index
  413. $tmpEtherIdx = strrchr($tmpDevIdxEtherIdx, '.');
  414. $tmpEtherIdxLen = strlen($tmpEtherIdx);
  415. $tmpEtherIdx = 'eth' . trim($tmpEtherIdx, '.');
  416. file_put_contents('exports/pondata/unioperstats/unistats', $tmpEtherIdx . "\n", 8);
  417. //dev index = $tmpDevIdxEtherIdx - '.0.' - $tmpEtherIdx
  418. $tmpONUDevIdx = substr($tmpDevIdxEtherIdx, 0, $tmpDevIdxEtherIdxLen - $tmpEtherIdxLen - 2);
  419. $tmpUniStatus = trim(trim($line[1]), '"');
  420. file_put_contents('exports/pondata/unioperstats/unistats', $tmpONUDevIdx . "\n", 8);
  421. file_put_contents('exports/pondata/unioperstats/unistats', $tmpUniStatus . "\n", 8);
  422. $uniStats[$tmpONUDevIdx] = array($tmpEtherIdx => $tmpUniStatus);
  423. }
  424. file_put_contents('exports/pondata/unioperstats/unistats', print_r($uniStats, true), 8);
  425. file_put_contents('exports/pondata/unioperstats/unistats', print_r($macIndexProcessed, true), 8);
  426. //storing results
  427. foreach ($macIndexProcessed as $devId => $eachMac) {
  428. if (isset($uniStats[$devId])) {
  429. $result[$eachMac] = $uniStats[$devId];
  430. }
  431. }
  432. //saving UniOperStats
  433. $this->olt->writeUniOperStats($result);
  434. }
  435. }
  436. }