api.vlanchange.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. /**
  3. * Apply vlan on device
  4. */
  5. class VlanChange {
  6. CONST MODULE_ONU_APPLY_AJAXONULIST = '?module=vlanmanagement_onu_apply&ajaxOnuList=true';
  7. CONST MODULE_ONU_APPLY_VLAN = '?module=vlanmanagement_onu_apply&apply_vlan=true';
  8. /**
  9. * OLT ID
  10. *
  11. * @var int
  12. */
  13. protected $oltId = -1;
  14. /**
  15. * User's login
  16. *
  17. * @var string
  18. */
  19. protected $username = '';
  20. /**
  21. * OLT IP
  22. *
  23. * @var string
  24. */
  25. protected $oltIp = '';
  26. /**
  27. * OLT read snmp community
  28. *
  29. * @var string
  30. */
  31. protected $oltCommunityRead = '';
  32. /**
  33. * OLT write snmp community
  34. *
  35. * @var string
  36. */
  37. protected $oltCommunityWrite = '';
  38. /**
  39. *
  40. *
  41. * @var string
  42. */
  43. protected $oltInterface = '';
  44. /**
  45. * OLT's SNMP template file name
  46. *
  47. * @var string
  48. */
  49. protected $snmpTemplateFile = '';
  50. /**
  51. * OLT's SNMP template content
  52. *
  53. * @var array
  54. */
  55. protected $snmpTemplate = array();
  56. /**
  57. * Guest vlans list
  58. *
  59. * @var array
  60. */
  61. protected $guestVlans = array();
  62. /**
  63. * Contains all guest ONUs for certain OLT
  64. *
  65. * @var array
  66. */
  67. protected $guestOnus = array();
  68. /**
  69. * ONU MAC
  70. *
  71. * @var string
  72. */
  73. protected $onuMac = '';
  74. /**
  75. * ONU Serial
  76. *
  77. * @var string
  78. */
  79. protected $onuSerial = '';
  80. /**
  81. * Contains all realnames
  82. *
  83. * @var array
  84. */
  85. protected $allRealnames = array();
  86. /**
  87. * Vlan mode
  88. *
  89. * @var string
  90. */
  91. protected $mode = 'none';
  92. /**
  93. * Contains all possible cvlans for olt
  94. *
  95. * @var array
  96. */
  97. protected $oltCvlans = array();
  98. /**
  99. * Contains all used cvlans
  100. *
  101. * @var array
  102. */
  103. protected $usedCvlans = array();
  104. /**
  105. * Contains all universal qinq assignments
  106. *
  107. * @var array
  108. */
  109. protected $universalAssign = array();
  110. /**
  111. * CVLAN placeholder
  112. *
  113. * @var int
  114. */
  115. protected $cvlan = -1;
  116. /**
  117. * SVLAN placeholder
  118. *
  119. * @var int
  120. */
  121. protected $svlan = -1;
  122. /**
  123. * SVLAN id placeholder
  124. *
  125. * @var int
  126. */
  127. protected $svlanid = -1;
  128. /**
  129. * Contains all switchmodels
  130. *
  131. * @var array
  132. */
  133. protected $allModelsData = array();
  134. /**
  135. * Containts all pononu data with serial key
  136. *
  137. * @var array
  138. */
  139. protected $allSerial = array();
  140. /**
  141. * Containts all pononu data with mac key
  142. *
  143. * @var array
  144. */
  145. protected $allMac = array();
  146. /**
  147. * Placeholder for snmp helper object
  148. *
  149. * @var object
  150. */
  151. protected $snmp = '';
  152. /**
  153. * Placeholder for nyan_orm instance for switches table
  154. *
  155. * @var object
  156. */
  157. protected $switchesDB = '';
  158. /**
  159. * Placeholder for nyan_orm instance for switchmodels table
  160. *
  161. * @var object
  162. */
  163. protected $switchModelsDB = '';
  164. /**
  165. *
  166. *
  167. * @var object
  168. */
  169. protected $oltqinqDB = '';
  170. /**
  171. *
  172. *
  173. * @var object
  174. */
  175. protected $cvlanDB = '';
  176. /**
  177. *
  178. *
  179. * @var object
  180. */
  181. protected $svlanDB = '';
  182. /**
  183. * Placeholder for nyan_orm object for pononu table
  184. *
  185. * @var object
  186. */
  187. protected $ponDB = '';
  188. /**
  189. *
  190. *
  191. * @var object
  192. */
  193. protected $realNameDB = '';
  194. /**
  195. * General ubiiling config content
  196. *
  197. * @var array
  198. */
  199. protected $altCfg = '';
  200. public function __construct($oltid = -1, $username = '') {
  201. $this->oltId = $oltid;
  202. $this->snmp = new SNMPHelper();
  203. $this->loadAlter();
  204. $this->switchesDB = new NyanORM('switches');
  205. $this->switchModelsDB = new NyanORM('switchmodels');
  206. $this->oltqinqDB = new NyanORM('olt_qinq');
  207. $this->cvlanDB = new NyanORM('qinq_bindings');
  208. $this->svlanDB = new NyanORM('qinq_svlan');
  209. $this->ponDB = new NyanORM('pononu');
  210. $this->realNameDB = new NyanORM('realname');
  211. $this->usernameGuess($username);
  212. if ($this->oltId) {
  213. $this->allModelsData = $this->switchModelsDB->getAll('id');
  214. $this->setSnmpTemplateFile();
  215. $this->loadSnmpTemplate();
  216. $this->loadAllOnu();
  217. $this->loadAllRealnames();
  218. $this->setVlanMode();
  219. }
  220. }
  221. /**
  222. * Dirty hacks to set up environment with $_GET param or by onu
  223. *
  224. * @param string $username
  225. *
  226. * @return void
  227. */
  228. protected function usernameGuess($username) {
  229. if (!empty($username)) {
  230. $this->username = $username;
  231. } else {
  232. if (ubRouting::checkGet('username')) {
  233. $this->username = ubRouting::get('username', 'mres');
  234. } else {
  235. if (ubRouting::checkGet('onuid')) {
  236. $this->getUsernameByOnu();
  237. }
  238. }
  239. }
  240. }
  241. /**
  242. * Load all ONU
  243. *
  244. * @return void
  245. */
  246. protected function loadAllOnu() {
  247. $this->allMac = $this->ponDB->getAll('mac');
  248. $this->allSerial = $this->ponDB->getAll('serial');
  249. }
  250. /**
  251. * Check if onu already exist and linked to some user
  252. *
  253. * @param string $onuid
  254. * @param bool $check
  255. * @return string
  256. */
  257. protected function getUsernameByOnu($onuid = '', $check = false) {
  258. if (empty($onuid)) {
  259. $onuid = ubRouting::get('onuid', 'mres');
  260. }
  261. if (!empty($this->allMac)) {
  262. if (isset($this->allMac[$onuid])) {
  263. if (!empty($this->allMac[$onuid]['login'])) {
  264. if (!$check) {
  265. $this->username = $this->allMac[$onuid]['login'];
  266. } else {
  267. return ($this->allMac[$onuid]['login']);
  268. }
  269. }
  270. }
  271. } else {
  272. if (!empty($data)) {
  273. if (isset($this->allSerial[$onuid])) {
  274. if (!empty($this->allMac[$onuid]['login'])) {
  275. if (!$check) {
  276. $this->username = $this->allSerial[$onuid]['login'];
  277. } else {
  278. return ($this->allSerial[$onuid]['login']);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. return('');
  285. }
  286. /**
  287. * Poll selected OLT is there is ONU with guest vlan
  288. *
  289. * @return void
  290. */
  291. public function checkOltGuestVlan() {
  292. $this->setGuestVlans();
  293. $this->checkUserOnu();
  294. $this->pollOlt();
  295. }
  296. /**
  297. * Loads system alter.ini config for further usage
  298. *
  299. * @global object $ubillingConfig
  300. *
  301. * @return void
  302. */
  303. protected function loadAlter() {
  304. global $ubillingConfig;
  305. $this->altCfg = $ubillingConfig->getAlter();
  306. }
  307. /**
  308. * Loads all realnames
  309. *
  310. * @return void
  311. */
  312. protected function loadAllRealnames() {
  313. $this->allRealnames = $this->realNameDB->getAll('login');
  314. }
  315. /**
  316. * Returns html string to load JS file.
  317. *
  318. * @return string
  319. */
  320. protected function loadVlanmanagementJs() {
  321. $result = '<script src = "./modules/jsc/vlanmanagement.js" type = "text/javascript"></script>';
  322. return ($result);
  323. }
  324. /**
  325. * Find and set snmp template file name
  326. *
  327. * @return void
  328. */
  329. protected function setSnmpTemplateFile() {
  330. $this->switchesDB->where('id', '=', $this->oltId);
  331. $switchData = $this->switchesDB->getAll('id');
  332. $modelId = $switchData[$this->oltId]['modelid'];
  333. $this->oltIp = $switchData[$this->oltId]['ip'];
  334. $this->oltCommunityRead = $switchData[$this->oltId]['snmp'];
  335. if (empty($switchData[$this->oltId]['snmpwrite'])) {
  336. $this->oltCommunityWrite = $switchData[$this->oltId]['snmp'];
  337. } else {
  338. $this->oltCommunityWrite = $switchData[$this->oltId]['snmpwrite'];
  339. }
  340. $this->switchModelsDB->where('id', '=', $modelId);
  341. $modelData = $this->switchModelsDB->getAll('id');
  342. $this->snmpTemplateFile = $modelData[$modelId]['snmptemplate'];
  343. if (empty($switchData[$this->oltId]['snmp'])) {
  344. throw new Exception('EMPTY OLT SNMP COMMUNITY');
  345. }
  346. }
  347. /**
  348. * Find and set available guest vlans from config
  349. *
  350. * @return void
  351. */
  352. protected function setGuestVlans() {
  353. if (isset($this->altCfg['VLAN_MANAGEMENT_ONU_GUEST_VLANS'])) {
  354. if (!empty($this->altCfg['VLAN_MANAGEMENT_ONU_GUEST_VLANS'])) {
  355. if (strpos($this->altCfg['VLAN_MANAGEMENT_ONU_GUEST_VLANS'], ',') !== false) {
  356. $vlans = explode(",", trim($this->altCfg['VLAN_MANAGEMENT_ONU_GUEST_VLANS']));
  357. foreach ($vlans as $each) {
  358. array_push($this->guestVlans, trim($each));
  359. }
  360. } else {
  361. array_push($this->guestVlans, trim($this->altCfg['VLAN_MANAGEMENT_ONU_GUEST_VLANS']));
  362. }
  363. }
  364. }
  365. }
  366. /**
  367. * Check if user has assigned ONU with proper OLT link
  368. *
  369. * @return void
  370. *
  371. * @throws Exception
  372. */
  373. protected function checkUserOnu() {
  374. $this->ponDB->where('login', '=', $this->username);
  375. $this->ponDB->where('oltid', '=', $this->oltId);
  376. $onuData = $this->ponDB->getAll('login');
  377. if (!empty($onuData)) {
  378. $this->onuMac = $onuData[$this->username]['mac'];
  379. $this->onuSerial = $onuData[$this->username]['serial'];
  380. }
  381. $this->ponDB->where('login', '=', $this->username);
  382. $onuDataAlt = $this->ponDB->getAll('login');
  383. if (!empty($onuDataAlt)) {
  384. //throw new Exception('OLT MISMATCH');
  385. } else {
  386. //throw new Exception('NO ONU WAS FOUND');
  387. }
  388. }
  389. /**
  390. * Load content of snmp template file if found
  391. *
  392. * @return void
  393. *
  394. * @throws Exception
  395. */
  396. protected function loadSnmpTemplate() {
  397. $templateFile = 'config/snmptemplates/' . $this->snmpTemplateFile;
  398. $privateTemplateFile = DATA_PATH . 'documents/mysnmptemplates/' . $this->snmpTemplateFile;
  399. if (file_exists($templateFile)) {
  400. $this->snmpTemplate = rcms_parse_ini_file($templateFile, true);
  401. if (file_exists($privateTemplateFile)) {
  402. $this->snmpTemplate = rcms_parse_ini_file($privateTemplateFile, true);
  403. }
  404. }
  405. if (empty($this->snmpTemplate)) {
  406. throw new Exception('NO SNMP TEMPLATE FILE WAS FOUND');
  407. }
  408. }
  409. /**
  410. * load all data from table switches related to olt
  411. *
  412. * @return array
  413. */
  414. protected function loadOltDetails() {
  415. $this->switchesDB->where('id', '=', $this->oltId);
  416. $data = $this->switchesDB->getAll('id');
  417. return ($data);
  418. }
  419. /**
  420. * Load all available assigns
  421. *
  422. * @return void
  423. */
  424. protected function loadCvlans() {
  425. $this->loadUniversalAssign();
  426. $this->loadOltCvlans();
  427. $this->loadUsedCvlans();
  428. }
  429. /**
  430. * Load olt pool
  431. *
  432. * @return void
  433. */
  434. protected function loadOltCvlans() {
  435. $this->oltqinqDB->where('swid', '=', $this->oltId);
  436. $this->oltqinqDB->where('port', '=', $this->oltInterface);
  437. $this->oltCvlans = $this->oltqinqDB->getAll('swid');
  438. }
  439. /**
  440. * Load all assignments from universal qinq by svlan
  441. *
  442. * @return void
  443. */
  444. protected function loadUsedCvlans() {
  445. if (!empty($this->oltCvlans)) {
  446. if (isset($this->oltCvlans[$this->oltId])) {
  447. $this->cvlanDB->where('svlan_id', '=', $this->oltCvlans[$this->oltId]['svlan_id']);
  448. $this->usedCvlans = $this->cvlanDB->getAll('cvlan');
  449. }
  450. }
  451. }
  452. /**
  453. * load assignment from universal qinq if exist for user
  454. *
  455. * @return void
  456. */
  457. protected function loadUniversalAssign() {
  458. $this->universalAssign = $this->cvlanDB->getAll('login');
  459. }
  460. protected function clearInterface($interface = '') {
  461. if (empty($interface)) {
  462. $interface = ubRouting::get('interface_olt', 'mres');
  463. }
  464. if (strpos($interface, '/') !== false) {
  465. $interfaceExplode = explode("/", $interface);
  466. $this->oltInterface = $interfaceExplode[1];
  467. } else {
  468. $this->oltInterface = $interface;
  469. }
  470. }
  471. /**
  472. * Compare available and used arrays to get free one
  473. *
  474. * @return void
  475. */
  476. protected function setCvlanFromPool() {
  477. $freeCvlans = array();
  478. $usedRange = array();
  479. if (!empty($this->oltCvlans)) {
  480. if (isset($this->oltCvlans[$this->oltId])) {
  481. $oltRange = range($this->oltCvlans[$this->oltId]['cvlan'], $this->oltCvlans[$this->oltId]['cvlan'] + 63);
  482. foreach ($this->usedCvlans as $eachCvlan => $io) {
  483. $usedRange[$eachCvlan] = $eachCvlan;
  484. }
  485. $freeCvlans = array_diff($oltRange, $usedRange);
  486. }
  487. }
  488. reset($freeCvlans);
  489. $firstCvlan = current($freeCvlans);
  490. $this->cvlan = $firstCvlan;
  491. }
  492. /**
  493. * Find svlan number by id
  494. *
  495. * @return void
  496. */
  497. protected function setSvlanFromPool() {
  498. $this->svlanDB->where('id', '=', $this->oltCvlans[$this->oltId]['svlan_id']);
  499. $data = $this->svlanDB->getAll('id');
  500. $this->svlan = $data[$this->oltCvlans[$this->oltId]['svlan_id']]['svlan'];
  501. $this->svlanid = $this->oltCvlans[$this->oltId]['svlan_id'];
  502. }
  503. /**
  504. * Set svlan number from universal assignment
  505. *
  506. * @return void
  507. */
  508. protected function setSvlanFromUniversal() {
  509. $this->svlanDB->where('id', '=', $this->universalAssign[$this->username]['svlan_id']);
  510. $data = $this->svlanDB->getAll('id');
  511. $this->svlan = $data[$this->universalAssign[$this->username]['svlan_id']]['svlan'];
  512. $this->svlanid = $this->universalAssign[$this->username]['svlan_id'];
  513. }
  514. /**
  515. * Get cvlan from universal qinq or from pool
  516. *
  517. * @return void
  518. */
  519. protected function setFreeCvlan() {
  520. if (empty($this->universalAssign)) {
  521. $this->setCvlanFromPool();
  522. } else {
  523. if (!isset($this->universalAssign[$this->username])) {
  524. $this->setCvlanFromPool();
  525. } else {
  526. $this->cvlan = $this->universalAssign[$this->username]['cvlan'];
  527. }
  528. }
  529. }
  530. /**
  531. * Set svlan from universal qinq or from pool
  532. *
  533. * @return void
  534. */
  535. protected function setSvlan() {
  536. if ($this->cvlan) {
  537. if (!empty($this->universalAssign)) {
  538. $this->setSvlanFromPool();
  539. } else {
  540. if (!isset($this->universalAssign[$this->username])) {
  541. $this->setSvlanFromPool();
  542. } else {
  543. $this->setSvlanFromUniversal();
  544. }
  545. }
  546. }
  547. }
  548. /**
  549. * Set svlan and cvlan
  550. *
  551. * @return void
  552. */
  553. protected function getVlanPair() {
  554. $this->loadCvlans();
  555. $this->setFreeCvlan();
  556. $this->setSvlan();
  557. }
  558. /**
  559. * Set vlan mode if was found in snmp template
  560. *
  561. * @return void
  562. *
  563. * @throws Exception
  564. */
  565. protected function setVlanMode() {
  566. if (isset($this->snmpTemplate['vlan'])) {
  567. if (isset($this->snmpTemplate['vlan']['VLANMODE'])) {
  568. switch ($this->snmpTemplate['vlan']['VLANMODE']) {
  569. case 'BDCOM_B':
  570. $this->mode = 'bdcom_b';
  571. break;
  572. default:
  573. $this->mode = 'none';
  574. break;
  575. }
  576. } else {
  577. throw new Exception('no vlanmode set in snmp template file');
  578. }
  579. } else {
  580. throw new Exception('no vlan section in snmp template file');
  581. }
  582. }
  583. /**
  584. * List all unknown onu from olt
  585. *
  586. * @return void
  587. */
  588. protected function pollOlt() {
  589. switch ($this->mode) {
  590. case 'bdcom_b':
  591. $this->pollOltBdcom();
  592. break;
  593. default:
  594. exit();
  595. }
  596. }
  597. /**
  598. * Get ONUs with guest vlan configured
  599. *
  600. * @return void
  601. */
  602. protected function pollOltBdcom() {
  603. $allOnuIndex = array();
  604. $interfaceIndex = array();
  605. $interfaceOltIndex = array();
  606. //$allOnuPvid = array();
  607. $allOnuIndexRaw = $this->snmp->walk($this->oltIp, $this->oltCommunityRead, $this->snmpTemplate['vlan']['IFINDEX'], false);
  608. $allOnuIndexRaw = trim($allOnuIndexRaw);
  609. if (!empty($allOnuIndexRaw)) {
  610. $allOnuIndexRows = explodeRows($allOnuIndexRaw);
  611. foreach ($allOnuIndexRows as $eachRow) {
  612. $eachRow = trim($eachRow);
  613. $eachRowExplode = explode("=", $eachRow);
  614. $oid = $eachRowExplode[0];
  615. $oid = trim(str_replace($this->snmpTemplate['vlan']['IFINDEX'] . '.', '', $oid));
  616. $mac = convertMACDec2Hex($oid);
  617. $value = $eachRowExplode[1];
  618. $value = trim(str_replace("INTEGER:", '', $value));
  619. $allOnuIndex[$value] = $mac;
  620. }
  621. unset($value);
  622. unset($oid);
  623. unset($eachRow);
  624. unset($eachRowExplode);
  625. $interfaceIndexRaw = $this->snmp->walk($this->oltIp, $this->oltCommunityRead, $this->snmpTemplate['misc']['INTERFACEINDEX'], false);
  626. $interfaceIndexRaw = trim($interfaceIndexRaw);
  627. if (!empty($interfaceIndexRaw)) {
  628. $interfaceIndexRows = explodeRows($interfaceIndexRaw);
  629. foreach ($interfaceIndexRows as $eachRow) {
  630. $eachRow = trim($eachRow);
  631. $eachRowExplode = explode("=", $eachRow);
  632. $oidRaw = $eachRowExplode[0];
  633. $oid = trim(str_replace($this->snmpTemplate['misc']['INTERFACEINDEX'] . ".", '', $oidRaw));
  634. $valueRaw = $eachRowExplode[1];
  635. $value = trim(str_replace("STRING:", '', $valueRaw));
  636. $interfaceIndex[$oid] = $value;
  637. $valueExplode = explode(":", $value);
  638. $interfaceOltIndex[$oid] = $valueExplode[0];
  639. }
  640. }
  641. unset($value);
  642. unset($oid);
  643. unset($eachRow);
  644. unset($eachRowExplode);
  645. $allOnuPvidRaw = $this->snmp->walk($this->oltIp, $this->oltCommunityRead, $this->snmpTemplate['vlan']['PVID'], false);
  646. $allOnuPvidRaw = trim($allOnuPvidRaw);
  647. if (!empty($allOnuPvidRaw)) {
  648. $allOnuPvidRows = explodeRows($allOnuPvidRaw);
  649. foreach ($allOnuPvidRows as $eachRow) {
  650. $eachRow = trim($eachRow);
  651. $eachRowExplode = explode("=", $eachRow);
  652. $oidRaw = $eachRowExplode[0];
  653. $oidRaw = trim(str_replace($this->snmpTemplate['vlan']['PVID'] . ".", '', $oidRaw));
  654. $oidRawExplode = explode(".", $oidRaw);
  655. $onuId = trim($oidRawExplode[0]);
  656. $portNumber = trim($oidRawExplode[1]);
  657. $value = $eachRowExplode[1];
  658. $value = trim(str_replace("INTEGER:", '', $value));
  659. foreach ($this->guestVlans as $eachGuest) {
  660. if ($eachGuest == $value) {
  661. if (isset($allOnuIndex[$onuId])) {
  662. //$allOnuPvid[$onuId][$portNumber] = $value;
  663. $this->guestOnus[$allOnuIndex[$onuId]] = array('id' => $allOnuIndex[$onuId], 'port' => $portNumber, 'vlan' => $value, 'interface' => $interfaceIndex[$onuId], 'interface_olt' => $interfaceOltIndex[$onuId], 'type' => 'epon', 'snmp_index' => $onuId);
  664. }
  665. }
  666. }
  667. }
  668. }
  669. }
  670. }
  671. /**
  672. * Ajax loader for onu list
  673. *
  674. * @return string
  675. */
  676. public function onuListShow() {
  677. $result = '';
  678. $add = '&oltid=' . ubRouting::get('oltid', 'mres');
  679. if (ubRouting::checkGet('username')) {
  680. $add .= "&username=" . $this->username;
  681. }
  682. $columns = array('Interface', 'MAC/Serial', 'Port', 'VLAN', 'Real Name', 'Actions');
  683. $opts = '"order": [[ 0, "asc" ]]';
  684. $result .= wf_JqDtLoader($columns, self::MODULE_ONU_APPLY_AJAXONULIST . $add, false, __('Request'), 100, $opts);
  685. $result .= $this->loadVlanmanagementJs();
  686. return($result);
  687. }
  688. /**
  689. * Generate ajax list of OLTs.
  690. *
  691. * @return string
  692. */
  693. public function onuListAjaxRender() {
  694. $json = new wf_JqDtHelper();
  695. $this->checkOltGuestVlan();
  696. $currentUsername = $this->username;
  697. if (!empty($this->guestOnus)) {
  698. foreach ($this->guestOnus as $each) {
  699. if (ubRouting::checkGet('username')) {
  700. $this->$currentUsername = ubRouting::get('username', 'mres');
  701. $this->username = ubRouting::get('username', 'mres');
  702. }
  703. $altUsername = $this->getUsernameByOnu($each['id'], true);
  704. if (!$currentUsername) {
  705. $this->getUsernameByOnu($each['id']);
  706. $currentUsername = $altUsername;
  707. }
  708. if ($currentUsername) {
  709. $controls = wf_modalAuto(wf_img_sized('skins/add_icon.png', '', '16', '16'), __('Assign VLAN'), $this->changeVlanForm($each['id'], $each['port'], $each['vlan'], $each['type'], $each['interface'], $each['interface_olt'], $each['snmp_index']));
  710. } else {
  711. $controls = wf_modalAuto(wf_img_sized('skins/add_icon.png', '', '16', '16'), __('Assign VLAN'), $this->userNameForm($each['id'], $each['port'], $each['vlan'], $each['type'], $each['interface'], $each['interface_olt'], $each['snmp_index']));
  712. }
  713. $data[] = trim($each['interface']);
  714. $data[] = trim($each['id']);
  715. $data[] = trim($each['port']);
  716. $data[] = trim($each['vlan']);
  717. $data[] = wf_link('?module=userprofile&username=', trim(@$this->allRealnames[$altUsername]['realname']));
  718. $data[] = $controls;
  719. $json->addRow($data);
  720. $currentUsername = '';
  721. unset($data);
  722. }
  723. }
  724. $json->getJson();
  725. }
  726. protected function onuModelsSelector() {
  727. $models = array();
  728. if (!empty($this->allModelsData)) {
  729. foreach ($this->allModelsData as $io => $each) {
  730. if (@$this->altCfg['ONUMODELS_FILTER']) {
  731. if (ispos($each['modelname'], 'ONU')) {
  732. $models[$each['id']] = $each['modelname'];
  733. }
  734. } else {
  735. $models[$each['id']] = $each['modelname'];
  736. }
  737. }
  738. }
  739. return($models);
  740. }
  741. /**
  742. * Returns ONU creation form
  743. *
  744. * @return string
  745. */
  746. protected function userNameForm($onuId, $port, $vlan, $type, $interface, $interface_olt, $snmp_index) {
  747. $result = '';
  748. $inputs = wf_TextInput('username', __('Login'), '', true, 20, '', '', 'usernameInput---' . ubRouting::get('oltid', 'mres') . '---' . $onuId . '---' . $port);
  749. $inputs .= wf_Submit(__('Validate'));
  750. $result .= wf_Form('', 'POST', $inputs, 'glamour', '', 'usernameForm---' . ubRouting::get('oltid', 'mres') . '---' . $onuId . '---' . $port, '', 'onsubmit="return validateVlanUsernameForm(' . ubRouting::get('oltid', 'mres') . ",'" . $onuId . "', " . $port . ',' . $vlan . ",'" . $type . "','" . $interface . "','" . $interface_olt . "'," . $snmp_index . ')"');
  751. return ($result);
  752. }
  753. /**
  754. * Show form to change cvlan on onu
  755. *
  756. * @param string $onuId
  757. * @param int $port
  758. * @param int $vlan
  759. * @param string $type
  760. * @param string $interface
  761. * @param string $interface_olt
  762. *
  763. * @return string
  764. */
  765. public function changeVlanForm($onuId, $port, $vlan, $type, $interface, $interface_olt, $snmp_index) {
  766. $result = '';
  767. $oltDetails = $this->loadOltDetails();
  768. $this->clearInterface($interface_olt);
  769. $this->getVlanPair();
  770. $models = $this->onuModelsSelector();
  771. $inputs = wf_HiddenInput('change_cvlan', true);
  772. $inputs .= wf_HiddenInput('interface_olt', $interface_olt);
  773. $inputs .= wf_HiddenInput('interface', $interface);
  774. $inputs .= wf_HiddenInput('onuid', $onuId);
  775. $inputs .= wf_HiddenInput('port', $port);
  776. $inputs .= wf_HiddenInput('cvlan', $this->cvlan);
  777. $inputs .= wf_HiddenInput('svlan', $this->svlan);
  778. $inputs .= wf_HiddenInput('svlanid', $this->svlanid);
  779. $inputs .= wf_HiddenInput('snmp_index', $snmp_index);
  780. $inputs .= wf_HiddenInput('type', $type);
  781. $cells = wf_TableCell(__('Login'));
  782. $cells .= wf_TableCell(wf_TextInput('username', '', $this->username, false, 20));
  783. $rows = wf_TableRow($cells);
  784. $checkedUsername = $this->getUsernameByOnu($onuId, true);
  785. if (!empty($checkedUsername)) {
  786. if ($this->username != $checkedUsername) {
  787. $rows .= 'this onu is assigned to another user: ' . wf_Link('?module=userprofile&username=' . $checkedUsername, $checkedUsername);
  788. }
  789. }
  790. $cells = wf_TableCell(__('OLT'));
  791. $cells .= wf_TableCell($oltDetails[$this->oltId]['ip'] . ' ' . $oltDetails[$this->oltId]['location']);
  792. $rows .= wf_TableRow($cells);
  793. $cells = wf_TableCell(__('Model'));
  794. $cells .= wf_tableCell(wf_Selector('onumodelid', $models, '', ''));
  795. $rows .= wf_TableRow($cells);
  796. if ($this->cvlan) {
  797. if (!empty($this->universalAssign)) {
  798. if (isset($this->universalAssign[$this->username])) {
  799. $cells = wf_TableCell('Found UniversalQINQ assignment');
  800. } else {
  801. $cells = wf_TableCell('New vlan pair assigned from pool');
  802. }
  803. } else {
  804. $cells = wf_TableCell('New vlan pair assigned from pool');
  805. }
  806. $rows .= wf_TableRow($cells);
  807. $cells = wf_TableCell('SVLAN');
  808. $cells .= wf_TableCell($this->svlan);
  809. $rows .= wf_TableRow($cells);
  810. $cells = wf_TableCell('CVLAN');
  811. $cells .= wf_TableCell($this->cvlan);
  812. $rows .= wf_TableRow($cells);
  813. } else {
  814. $rows .= 'There is no vlan pool for OLT or UniversalQINQ assignment for user' . wf_delimiter();
  815. }
  816. $cells = wf_TableCell('OLT ' . __('Interface'));
  817. $cells .= wf_TableCell($interface_olt);
  818. $rows .= wf_TableRow($cells);
  819. $cells = wf_TableCell('ONU ' . __('Interface'));
  820. $cells .= wf_TableCell($interface);
  821. $rows .= wf_TableRow($cells);
  822. $cells = wf_TableCell('ONU MAC/SN');
  823. $cells .= wf_TableCell($onuId);
  824. $rows .= wf_TableRow($cells);
  825. $cells = wf_TableCell(__('Port'));
  826. $cells .= wf_TableCell($port);
  827. $rows .= wf_TableRow($cells);
  828. $cells = wf_TableCell('Guest vlan');
  829. $cells .= wf_TableCell($vlan);
  830. $rows .= wf_TableRow($cells);
  831. if ($this->cvlan) {
  832. $cells = wf_TableCell(wf_Submit(__('Apply')));
  833. $rows .= wf_TableRow($cells);
  834. }
  835. $inputs .= wf_TableBody($rows, '100%', 0, 'sortable');
  836. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  837. return($result);
  838. }
  839. /**
  840. * Apply cvlan on onu
  841. *
  842. * @return void
  843. */
  844. public function changeVlan() {
  845. switch ($this->mode) {
  846. case 'bdcom_b':
  847. $this->changeOltBdcom();
  848. break;
  849. default:
  850. exit();
  851. }
  852. $this->routineAdd();
  853. }
  854. protected function changeOltBdcom() {
  855. $this->loadCvlans();
  856. $pvidData['oid'] = $this->snmpTemplate['vlan']['PVID'] . '.' . ubRouting::post('snmp_index', 'vf') . '.' . ubRouting::post('port', 'vf');
  857. $pvidData['type'] = 'i';
  858. $pvidData['value'] = ubRouting::post('cvlan', 'vf');
  859. $saveData['oid'] = $this->snmpTemplate['vlan']['SAVE'];
  860. $saveData['type'] = 'i';
  861. $saveData['value'] = '1';
  862. $setData[] = $pvidData;
  863. $setData[] = $saveData;
  864. @$this->snmp->set($this->oltIp, $this->oltCommunityWrite, $setData);
  865. rcms_redirect(VlanManagement::MODULE_ONU_APPLY . '&oltid=' . ubRouting::get('oltid', 'mres'));
  866. }
  867. protected function routineAdd() {
  868. $this->ponizerRoutineAdd();
  869. $this->universalRoutineAdd();
  870. }
  871. protected function ponizerRoutineAdd() {
  872. $type = ubRouting::post('type', 'mres');
  873. $onuid = ubRouting::post('onuid', 'mres');
  874. $oltid = ubRouting::post('oltid', 'mres');
  875. $login = ubRouting::post('username', 'mres');
  876. $onumodelid = ubRouting::post('onumodelid', 'mres');
  877. if (!isset($this->onuMac[$onuid])) {
  878. $this->ponizerAdd($type, $oltid, $onumodelid, $onuid, $login);
  879. } else {
  880. if ($this->onuMac[$onuid]['oltid'] !== $oltid) {
  881. $this->ponizerFixOlt($type, $oltid, $onumodelid, $onuid, $login);
  882. }
  883. }
  884. }
  885. protected function ponizerAdd($type, $oltid, $onumodelid, $onuid, $login) {
  886. $pon = new PONizer();
  887. switch ($type) {
  888. case 'epon':
  889. $pon->onuCreate($onumodelid, $oltid, '', $onuid, '', $login);
  890. }
  891. }
  892. protected function ponizerFixOlt($type, $oltid, $onumodelid, $onuid, $login) {
  893. $pon = new PONizer();
  894. switch ($type) {
  895. case 'epon':
  896. $pon->onuCreate($onumodelid, $oltid, '', $onuid, '', $login);
  897. }
  898. }
  899. protected function universalRoutineAdd() {
  900. $login = trim(ubRouting::post('username', 'mres'));
  901. $svlanid = trim(ubRouting::post('svlanid', 'int'));
  902. $svlan = trim(ubRouting::post('svlan', 'int'));
  903. $cvlan = trim(ubRouting::post('cvlan', 'int'));
  904. if (!isset($this->universalAssign[$login])) {
  905. $universal = new UniversalQINQ();
  906. $this->cvlanDB->data('login', $login);
  907. $this->cvlanDB->data('svlan_id', $svlanid);
  908. $this->cvlanDB->data('cvlan', $cvlan);
  909. $this->cvlanDB->create();
  910. $universal->logAdd($login, $svlan, $cvlan);
  911. }
  912. }
  913. }