api.ponproto.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Basic PON OLTs devices collectors hardware abstraction layer prototype
  4. */
  5. class PONProto {
  6. /**
  7. * Contains current HAL instance OLT parameters
  8. *
  9. * @var array
  10. */
  11. protected $oltParameters = array();
  12. /**
  13. * Contains available SNMP templates for OLT modelids
  14. *
  15. * @var array
  16. */
  17. protected $snmpTemplates = array();
  18. /**
  19. * Default ONU offline signal level
  20. *
  21. * @var int
  22. */
  23. protected $onuOfflineSignalLevel = '-9000';
  24. /**
  25. * Placeholder for SERIAL_CASE_MODE SNMP template option
  26. * 0 - no case convert
  27. * 1 - lowercase
  28. * 2 - uppercase
  29. *
  30. * As far as MAC and Serial are interchangeable during polling for a historical reason
  31. * - we should keep "1" as a default value.
  32. *
  33. * @var bool
  34. */
  35. protected $onuSerialCaseMode = 1;
  36. /**
  37. * SNMPHelper object instance
  38. *
  39. * @var object
  40. */
  41. protected $snmp = '';
  42. /**
  43. * Contains OLTData
  44. *
  45. * @var object
  46. */
  47. protected $olt = '';
  48. /**
  49. * Contains ONU id and her MAC
  50. *
  51. * @var array as onuId => onuMAC
  52. *
  53. * onuMAC on lower case
  54. */
  55. protected $macIndexProcessed = array();
  56. /**
  57. * Contains ONU device index and her MAC
  58. *
  59. * @var array as onuDevInd => onuMAC
  60. *
  61. * onuMAC on lower case
  62. */
  63. protected $onuDevIndexProcessed = array();
  64. /**
  65. * Replicated paths from primary PONizer class.
  66. * This is here only for legacy of manual data manipulations wit self::
  67. * instead of usage $this->olt abstraction in HAL libs.
  68. */
  69. const SIGCACHE_PATH = OLTAttractor::SIGCACHE_PATH;
  70. const SIGCACHE_EXT = OLTAttractor::SIGCACHE_EXT;
  71. const DISTCACHE_PATH = OLTAttractor::DISTCACHE_PATH;
  72. const DISTCACHE_EXT = OLTAttractor::DISTCACHE_EXT;
  73. const ONUCACHE_PATH = OLTAttractor::ONUCACHE_PATH;
  74. const ONUCACHE_EXT = OLTAttractor::ONUCACHE_EXT;
  75. const INTCACHE_PATH = OLTAttractor::INTCACHE_PATH;
  76. const INTCACHE_EXT = OLTAttractor::INTCACHE_EXT;
  77. const INTDESCRCACHE_EXT = OLTAttractor::INTDESCRCACHE_EXT;
  78. const FDBCACHE_PATH = OLTAttractor::FDBCACHE_PATH;
  79. const FDBCACHE_EXT = OLTAttractor::FDBCACHE_EXT;
  80. const DEREGCACHE_PATH = OLTAttractor::DEREGCACHE_PATH;
  81. const DEREGCACHE_EXT = OLTAttractor::DEREGCACHE_EXT;
  82. const UPTIME_PATH = OLTAttractor::UPTIME_PATH;
  83. const UPTIME_EXT = OLTAttractor::UPTIME_EXT;
  84. const TEMPERATURE_PATH = OLTAttractor::TEMPERATURE_PATH;
  85. const TEMPERATURE_EXT = OLTAttractor::TEMPERATURE_EXT;
  86. const MACDEVIDCACHE_PATH = OLTAttractor::MACDEVIDCACHE_PATH;
  87. const MACDEVIDCACHE_EXT = OLTAttractor::MACDEVIDCACHE_EXT;
  88. const ONUSIG_PATH = OLTAttractor::ONUSIG_PATH;
  89. /**
  90. * Other instance parameters
  91. */
  92. const SNMPCACHE = PONizer::SNMPCACHE;
  93. const SNMPPORT = PONizer::SNMPPORT;
  94. /**
  95. * Creates new PON poller/parser proto
  96. *
  97. * @param array $oltParameters
  98. * @param array $snmpTemplates
  99. */
  100. public function __construct($oltParameters, $snmpTemplates) {
  101. $this->oltParameters = $oltParameters;
  102. $this->snmpTemplates = $snmpTemplates;
  103. $this->initSNMP();
  104. $this->initOltAttractor();
  105. }
  106. /**
  107. * Creates single instance of SNMPHelper object
  108. *
  109. * @return void
  110. */
  111. protected function initSNMP() {
  112. $this->snmp = new SNMPHelper();
  113. }
  114. /**
  115. * Inits current OLT data abstraction layer for further usage
  116. */
  117. protected function initOltAttractor() {
  118. $this->olt = new OLTAttractor($this->oltParameters['ID']);
  119. }
  120. /**
  121. * Sets current instance ONU offline signal level
  122. *
  123. * @param int $level
  124. *
  125. * @return void
  126. */
  127. public function setOfflineSignal($level) {
  128. $this->onuOfflineSignalLevel = $level;
  129. }
  130. /**
  131. * Main data collector method placeholder
  132. *
  133. * @return void
  134. */
  135. public function collect() {
  136. /**
  137. * Ab esse ad posse valet, a posse ad esse non valet consequentia
  138. */
  139. }
  140. /**
  141. * Performs signal preprocessing for sig/mac index arrays and stores it into cache
  142. *
  143. * @param int $oltid
  144. * @param array $sigIndex
  145. * @param array $macIndex
  146. * @param array $snmpTemplate
  147. *
  148. * @return void
  149. */
  150. protected function signalParse($oltid, $sigIndex, $macIndex, $snmpTemplate) {
  151. $oltid = vf($oltid, 3);
  152. $sigTmp = array();
  153. $macTmp = array();
  154. $result = array();
  155. //signal index preprocessing
  156. if ((!empty($sigIndex)) and ( !empty($macIndex))) {
  157. foreach ($sigIndex as $io => $eachsig) {
  158. $line = explode('=', $eachsig);
  159. //signal is present
  160. if (isset($line[1])) {
  161. $signalRaw = trim($line[1]); // signal level
  162. $devIndex = trim($line[0]); // device index
  163. if ($signalRaw == $snmpTemplate['DOWNVALUE']) {
  164. $signalRaw = 'Offline';
  165. } else {
  166. if ($snmpTemplate['OFFSETMODE'] == 'div') {
  167. if ($snmpTemplate['OFFSET']) {
  168. if (is_numeric($signalRaw)) {
  169. $signalRaw = $signalRaw / $snmpTemplate['OFFSET'];
  170. } else {
  171. $signalRaw = 'Fail';
  172. }
  173. }
  174. }
  175. }
  176. $sigTmp[$devIndex] = $signalRaw;
  177. }
  178. }
  179. //mac index preprocessing
  180. foreach ($macIndex as $io => $eachmac) {
  181. $line = explode('=', $eachmac);
  182. //mac is present
  183. if (isset($line[1])) {
  184. $macRaw = trim($line[1]); //mac address
  185. $devIndex = trim($line[0]); //device index
  186. $macRaw = str_replace(' ', ':', $macRaw);
  187. if ($this->onuSerialCaseMode == 1) {
  188. $macRaw = strtolower($macRaw);
  189. } elseif ($this->onuSerialCaseMode == 2) {
  190. $macRaw = strtoupper($macRaw);
  191. }
  192. $macTmp[$devIndex] = $macRaw;
  193. }
  194. }
  195. //storing results
  196. if (!empty($macTmp)) {
  197. foreach ($macTmp as $devId => $eachMac) {
  198. if (isset($sigTmp[$devId])) {
  199. $signal = $sigTmp[$devId];
  200. $result[$eachMac] = $signal;
  201. //signal history preprocessing
  202. if ($signal == 'Offline') {
  203. $signal = $this->onuOfflineSignalLevel; //over 9000 offline signal level :P
  204. }
  205. //saving each ONU signal history
  206. $this->olt->writeSignalHistory($eachMac, $signal);
  207. }
  208. }
  209. //writing signals cache
  210. $this->olt->writeSignals($result);
  211. // saving macindex as MAC => devID
  212. $macTmp = array_flip($macTmp);
  213. $this->olt->writeMacIndex($macTmp);
  214. }
  215. }
  216. }
  217. /**
  218. * Parses & stores in cache OLT ONU distances
  219. *
  220. * @param int $oltid
  221. * @param array $distIndex
  222. * @param array $onuIndex
  223. *
  224. * @return void
  225. */
  226. protected function distanceParse($oltid, $distIndex, $onuIndex) {
  227. $oltid = vf($oltid, 3);
  228. $distTmp = array();
  229. $onuTmp = array();
  230. $result = array();
  231. $curDate = curdatetime();
  232. //distance index preprocessing
  233. if ((!empty($distIndex)) and ( !empty($onuIndex))) {
  234. foreach ($distIndex as $io => $eachdist) {
  235. $line = explode('=', $eachdist);
  236. //distance is present
  237. if (isset($line[1])) {
  238. $distanceRaw = trim($line[1]); // distance
  239. $devIndex = trim($line[0]); // device index
  240. $distTmp[$devIndex] = $distanceRaw;
  241. }
  242. }
  243. //mac index preprocessing
  244. foreach ($onuIndex as $io => $eachmac) {
  245. $line = explode('=', $eachmac);
  246. //mac is present
  247. if (isset($line[1])) {
  248. $macRaw = trim($line[1]); //mac address
  249. $devIndex = trim($line[0]); //device index
  250. $macRaw = str_replace(' ', ':', $macRaw);
  251. if ($this->onuSerialCaseMode == 1) {
  252. $macRaw = strtolower($macRaw);
  253. } elseif ($this->onuSerialCaseMode == 2) {
  254. $macRaw = strtoupper($macRaw);
  255. }
  256. $onuTmp[$devIndex] = $macRaw;
  257. }
  258. }
  259. //storing results
  260. if (!empty($onuTmp)) {
  261. foreach ($onuTmp as $devId => $eachMac) {
  262. if (isset($distTmp[$devId])) {
  263. $distance = $distTmp[$devId];
  264. $result[$eachMac] = $distance;
  265. }
  266. }
  267. //saving distance cache
  268. $this->olt->writeDistances($result);
  269. //saving ONU cache
  270. $this->olt->writeOnuCache($onuTmp);
  271. }
  272. }
  273. }
  274. /**
  275. * Parses BDCom uptime data and saves it into uptime cache
  276. *
  277. * @param int $oltid
  278. * @param string $uptimeRaw
  279. *
  280. * @return void
  281. */
  282. protected function uptimeParse($oltid, $uptimeRaw) {
  283. if (!empty($uptimeRaw)) {
  284. $uptimeRaw = explode(')', $uptimeRaw);
  285. $uptimeRaw = $uptimeRaw[1];
  286. $this->olt->writeUptime($uptimeRaw);
  287. }
  288. }
  289. /**
  290. * Parses BDCom temperature data and saves it into uptime cache
  291. *
  292. * @param int $oltid
  293. * @param string $tempRaw
  294. *
  295. * @return void
  296. */
  297. protected function temperatureParse($oltid, $tempRaw) {
  298. if (!empty($tempRaw)) {
  299. $tempRaw = explode(':', $tempRaw);
  300. $tempRaw = @$tempRaw[1];
  301. $this->olt->writeTemperature($tempRaw);
  302. }
  303. }
  304. /**
  305. * Parses ONU and get her ID and MAC
  306. *
  307. * @param array $macIndex
  308. *
  309. * @return array
  310. */
  311. protected function onuMacProcessing($macIndex) {
  312. if (!empty($macIndex)) {
  313. //mac index preprocessing
  314. foreach ($macIndex as $io => $eachmac) {
  315. $line = explode('=', $eachmac);
  316. //mac is present
  317. if (isset($line[1])) {
  318. $macRaw = trim($line[1]); //mac address
  319. $devIndex = trim($line[0]); //device index
  320. $macRaw = str_replace(' ', ':', $macRaw);
  321. if ($this->onuSerialCaseMode == 1) {
  322. $macRaw = strtolower($macRaw);
  323. } elseif ($this->onuSerialCaseMode == 2) {
  324. $macRaw = strtoupper($macRaw);
  325. }
  326. $this->macIndexProcessed[$devIndex] = $macRaw;
  327. }
  328. }
  329. }
  330. }
  331. /**
  332. * Parses ONU and get her device ID and MAC
  333. *
  334. * @param array $onuIndex
  335. *
  336. * @return array
  337. */
  338. protected function onuDevIndexProcessing($onuIndex) {
  339. if (!empty($onuIndex)) {
  340. // mac index preprocessing
  341. foreach ($onuIndex as $io => $eachmac) {
  342. $line = explode('=', $eachmac);
  343. //mac is present
  344. if (isset($line[1])) {
  345. $macRaw = trim($line[1]); //mac address
  346. $devIndex = trim($line[0]); //device index
  347. $macRaw = str_replace(' ', ':', $macRaw);
  348. $macRaw = strtolower($macRaw);
  349. $this->onuDevIndexProcessed[$devIndex] = $macRaw;
  350. }
  351. }
  352. }
  353. }
  354. /**
  355. * Replaces standard 4-line routine with snmpwalking and removing OID and VALUE portions and returns an array of cleared values
  356. *
  357. * @param string $snmpIPPORT
  358. * @param string $snmpCommunity
  359. * @param string $snmpOID
  360. * @param string $removeOIDPart
  361. * @param string $removeVALUE
  362. * @param bool $snmpCacheON
  363. *
  364. * @return array
  365. */
  366. protected function walkCleared($snmpIPPORT, $snmpCommunity, $snmpOID, $removeOIDPart = '', $removeVALUE = '', $snmpCacheON = false) {
  367. $oidIndex = $this->snmp->walk($snmpIPPORT, $snmpCommunity, $snmpOID, $snmpCacheON);
  368. $oidIndex = trimSNMPOutput($oidIndex, $snmpOID . '.' . $removeOIDPart, $removeVALUE, true);
  369. return ($oidIndex);
  370. }
  371. }