api.switchesqinq.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Switches QinQ management
  4. */
  5. class SwitchesQinQ {
  6. /**
  7. * Contains all available QinQ data as switchid=>data
  8. *
  9. * @var array
  10. */
  11. protected $allQinQ = array();
  12. /**
  13. * Contains default table to store switches QinQ mapping data
  14. */
  15. const QINQ_TABLE = 'switches_qinq';
  16. /**
  17. * Creates new SwitchesQinQ instance
  18. *
  19. * @return void
  20. */
  21. public function __construct() {
  22. $this->loadQinQ();
  23. }
  24. /**
  25. * Loads existing QinQ data into protected prop
  26. *
  27. * @return void
  28. */
  29. protected function loadQinQ() {
  30. $query = "SELECT * from `" . self::QINQ_TABLE . "`";
  31. $all = simple_queryall($query);
  32. if (!empty($all)) {
  33. foreach ($all as $io => $each) {
  34. $this->allQinQ[$each['switchid']] = $each;
  35. }
  36. }
  37. }
  38. /**
  39. * Public getter for
  40. *
  41. * @return array
  42. */
  43. public function getAllQinQ() {
  44. return ($this->allQinQ);
  45. }
  46. /**
  47. * Renders QinQ data edit form
  48. *
  49. * @param int $switchId
  50. *
  51. * @return string
  52. */
  53. public function renderEditForm($switchId) {
  54. $result = '';
  55. if (!empty($switchId)) {
  56. @$currentData = $this->allQinQ[$switchId];
  57. $qinqinputs = wf_HiddenInput('qinqswitchid', $switchId);
  58. $qinqinputs .= wf_TextInput('qinqsvlan', __('SVLAN'), @$currentData['svlan'], false, 4, 'digits') . ' ';
  59. $qinqinputs .= wf_TextInput('qinqcvlan', __('CVLAN'), @$currentData['cvlan'], false, 4, 'digits') . ' ';
  60. $qinqinputs .= wf_Submit(__('Apply'));
  61. $result .= wf_Form('', 'POST', $qinqinputs, 'glamour');
  62. }
  63. return ($result);
  64. }
  65. /**
  66. * Checks for validity of svlan+cvlan pair
  67. *
  68. * @param int $svlan
  69. * @param int $cvlan
  70. * @param int $switchId
  71. *
  72. * @return bool
  73. protected function isValid($svlan, $cvlan, $switchId) {
  74. $result = true;
  75. if ((!empty($svlan)) AND ( !empty($cvlan))) {
  76. if (!empty($this->allQinQ)) {
  77. foreach ($this->allQinQ as $io => $each) {
  78. if (($each['cvlan'] == $cvlan) AND ( $each['svlan'] == $svlan)) {
  79. if ($io != $switchId) {
  80. $result = false;
  81. }
  82. }
  83. }
  84. }
  85. } else {
  86. $result = true;
  87. }
  88. return ($result);
  89. }
  90. *
  91. */
  92. /**
  93. * Catches qinq data and saves it if required
  94. *
  95. * @return void/string on error
  96. */
  97. public function saveQinQ() {
  98. $result = '';
  99. if (wf_CheckGet(array('qinqswitchid', 'svlan_id', 'cvlan_num'))) {
  100. $switchId = vf($_GET['qinqswitchid'], 3);
  101. $svlan = vf($_GET['svlan_id'], 3);
  102. $cvlan = vf($_GET['cvlan_num'], 3);
  103. //check is pair unique and not empty?
  104. //if ($this->isValid($svlan, $cvlan, $switchId)) {
  105. if (!isset($this->allQinQ[$switchId])) {
  106. //creating new QinQ record
  107. $query = "INSERT INTO `" . self::QINQ_TABLE . "` (`switchid`,`svlan_id`,`cvlan`) "
  108. . "VALUES ('" . $switchId . "','" . $svlan . "','" . $cvlan . "');";
  109. nr_query($query);
  110. log_register('SWITCH CHANGE [' . $switchId . '] QINQ CREATE SVLAN ID `' . $svlan . '` CVLAN `' . $cvlan . '`');
  111. } else {
  112. //update mapping data if required
  113. $currentData = $this->allQinQ[$switchId];
  114. $where = "WHERE `switchid`='" . $switchId . "'";
  115. if ($currentData['svlan'] != $svlan) {
  116. simple_update_field(self::QINQ_TABLE, 'svlan_id', $svlan, $where);
  117. log_register('SWITCH CHANGE [' . $switchId . '] QINQ SET SVLAN `' . $svlan . '`');
  118. }
  119. if ($currentData['cvlan'] != $cvlan) {
  120. simple_update_field(self::QINQ_TABLE, 'cvlan', $cvlan, $where);
  121. log_register('SWITCH CHANGE [' . $switchId . '] QINQ SET CVLAN `' . $cvlan . '`');
  122. }
  123. }
  124. /*
  125. } else {
  126. $result .= __('SVLAN + CVLAN pair is not valid');
  127. }
  128. *
  129. */
  130. }
  131. return ($result);
  132. }
  133. }