api.onubase.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * PON ONU management basic class
  4. */
  5. class OnuBase {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains user's onu data from pononu table
  14. *
  15. * @var array
  16. */
  17. protected $onuData = array();
  18. /**
  19. * Contains OLT data (where user's onu is linked to OLT)
  20. *
  21. * @var array
  22. */
  23. protected $oltData = array();
  24. /**
  25. * Contains OLT snmp template file name
  26. *
  27. * @var array
  28. */
  29. protected $oltSnmptemplate = array();
  30. /**
  31. * Contain's OLT switch ID
  32. *
  33. * @var int
  34. */
  35. protected $oltId = '';
  36. /**
  37. * User's login
  38. *
  39. * @var string
  40. */
  41. protected $login = '';
  42. /**
  43. * Placeholder for snmp class
  44. *
  45. * @var object
  46. */
  47. protected $snmp = '';
  48. /**
  49. * Placeholder for parsed SNMP template contents
  50. *
  51. * @var string
  52. */
  53. protected $snmpTemplateParsed = array();
  54. /**
  55. * Placeholder for any message to return and/or display
  56. *
  57. * @var string
  58. */
  59. public $displayMessage = '';
  60. public function __construct($login = '') {
  61. if (!empty($login)) {
  62. $this->snmp = new SNMPHelper;
  63. $this->loadAlter();
  64. $this->login = $login;
  65. $this->getOnuData($login);
  66. if (empty($this->onuData)) {
  67. $this->getExtUserOnuData($login);
  68. }
  69. if (!empty($this->oltId)) {
  70. $this->getOltData($this->oltId);
  71. }
  72. if (!empty($this->oltData)) {
  73. $this->getOltModelData($this->oltData['modelid']);
  74. }
  75. if (!empty($this->onuData) AND !empty($this->oltData) AND !empty($this->oltSnmptemplate)) {
  76. $this->parseSNMPTemplate();
  77. }
  78. }
  79. }
  80. /**
  81. * load alter.ini config
  82. *
  83. * @return void
  84. */
  85. protected function loadAlter() {
  86. global $ubillingConfig;
  87. $this->altCfg = $ubillingConfig->getAlter();
  88. }
  89. /**
  90. * Get onu data mac and olt ID to which onu is linked
  91. *
  92. * @param string $login
  93. */
  94. protected function getOnuData($login) {
  95. $query = "SELECT * FROM `pononu` WHERE `login` = '$login'";
  96. $data = simple_query($query);
  97. if (!empty($data)) {
  98. $this->oltId = $data['oltid'];
  99. $this->onuData = $data;
  100. }
  101. }
  102. /**
  103. * Get onu data mac and olt ID to which onu is linked for external logins(multiport ONUs)
  104. *
  105. * @param string $login
  106. */
  107. protected function getExtUserOnuData($login) {
  108. $query = "SELECT * FROM `pononuextusers` WHERE `login` = '$login'";
  109. $data = simple_query($query);
  110. if (!empty($data) and !empty($data['onuid'])) {
  111. $onuID = $data['onuid'];
  112. $query = "SELECT * FROM `pononu` WHERE `id` = $onuID";
  113. $data = simple_query($query);
  114. if (!empty($data)) {
  115. $this->oltId = $data['oltid'];
  116. $this->onuData = $data;
  117. }
  118. }
  119. }
  120. /**
  121. * Loads data from table `switches` to $oltData var (filter by OLT switch ID)
  122. *
  123. * @param int $oltID
  124. */
  125. protected function getOltData($oltID) {
  126. $query = "SELECT * FROM `switches` WHERE `id`='$oltID'";
  127. $data = simple_query($query);
  128. if (!empty($data)) {
  129. $this->oltData = $data;
  130. }
  131. }
  132. /**
  133. * Loads data from table `switchmodels` to $oltSnmptemplate (filter by OLT switch model id)
  134. *
  135. * @param int $modelID
  136. */
  137. protected function getOltModelData($modelID) {
  138. $query = "SELECT * FROM `switchmodels` WHERE `id`='$modelID'";
  139. $data = simple_query($query);
  140. if (!empty($data)) {
  141. $this->oltSnmptemplate = $data['snmptemplate'];
  142. }
  143. }
  144. /**
  145. * Format heximal mac address to decimal or show error
  146. *
  147. * @param string $macOnu
  148. *
  149. * @return string
  150. */
  151. protected function macHexToDec($macOnu) {
  152. $string = '';
  153. if (check_mac_format($macOnu)) {
  154. $res = array();
  155. $args = explode(":", $macOnu);
  156. foreach ($args as $each) {
  157. $res[] = hexdec($each);
  158. }
  159. $string = implode(".", $res);
  160. }
  161. return ($string);
  162. }
  163. /**
  164. * Parses SNMP templates and returns it's contents
  165. *
  166. * @return array
  167. */
  168. protected function parseSNMPTemplate($templateName = '') {
  169. $snmpData = array();
  170. $template = (empty($templateName)) ? $this->oltSnmptemplate : $templateName;
  171. if (file_exists(CONFIG_PATH . "/snmptemplates/" . $template)) {
  172. $snmpData = rcms_parse_ini_file(CONFIG_PATH . "/snmptemplates/" . $template, true);
  173. } elseif (file_exists(DATA_PATH . "/documents/mysnmptemplates/" . $template)) {
  174. $snmpData = rcms_parse_ini_file(DATA_PATH . "/documents/mysnmptemplates/" . $template, true);
  175. } else {
  176. log_register('ONUMASTER: ONUBASE SNMP template load ERROR - no template found');
  177. }
  178. $this->snmpTemplateParsed = $snmpData;
  179. return ($snmpData);
  180. }
  181. /**
  182. * Checks for essential SNMP options in template for BDCOMs
  183. *
  184. * @param array $parsedTemplate
  185. *
  186. * @return bool
  187. */
  188. protected function checkBDCOMEssentialOpts($parsedTemplate = array()) {
  189. $checkPassed = true;
  190. $snmpData = (empty($parsedTemplate)) ? $this->snmpTemplateParsed : $parsedTemplate;
  191. if (!empty($snmpData)) {
  192. if (!isset($snmpData['vlan']['VLANMODE'])) {
  193. $checkPassed = false;
  194. }
  195. if (!isset($snmpData['vlan']['SAVE'])) {
  196. $checkPassed = false;
  197. }
  198. if (!isset($snmpData['onu']['DESCRIPTION'])) {
  199. $checkPassed = false;
  200. }
  201. if (!isset($snmpData['onu']['RELOAD'])) {
  202. $checkPassed = false;
  203. }
  204. if (!isset($snmpData['onu']['EPONINDEX'])) {
  205. $checkPassed = false;
  206. }
  207. if (!isset($snmpData['onu']['IFINDEX'])) {
  208. $checkPassed = false;
  209. }
  210. }
  211. return ($checkPassed);
  212. }
  213. /**
  214. * Getter for onuData property
  215. *
  216. * @return array
  217. */
  218. public function getDataONU() {
  219. return ($this->onuData);
  220. }
  221. /**
  222. * Getter for oltData property
  223. *
  224. * @return array
  225. */
  226. public function getDataOLT() {
  227. return ($this->oltData);
  228. }
  229. }