KeaConfigService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace KeaBundle\Services;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityRepository;
  5. use DHCPBundle\Entity\DHCP;
  6. use KeaBundle\Interfaces\KeaConfigInterface;
  7. use WebserviceBundle\Services\Webservice;
  8. class KeaConfigService
  9. {
  10. /**
  11. * @var EntityRepository
  12. */
  13. private $dhcpRepository;
  14. /**
  15. * @var EntityRepository
  16. */
  17. private $subnetRepository;
  18. /**
  19. * @var EntityRepository
  20. */
  21. private $hostRepository;
  22. /**
  23. * @var Webservice
  24. */
  25. private $webService;
  26. /**
  27. * @param Webservice $ws
  28. * @param EntityManager $em
  29. */
  30. public function __construct(Webservice $ws, EntityManager $em)
  31. {
  32. $this->dhcpRepository = $em->getRepository('DHCPBundle:DHCP');
  33. $this->subnetRepository = $em->getRepository('IPv4Bundle:SubNet');
  34. $this->hostRepository = $em->getRepository('HostBundle:Host');
  35. $this->webService = $ws;
  36. }
  37. /**
  38. * @param int $id
  39. * @param string $className
  40. * @param string $library
  41. *
  42. * @return string
  43. */
  44. public function getConfig($id, $className = 'BaseKea', $library = '/kea-cm-hook/kea-hook-flowdat.so')
  45. {
  46. $config = '';
  47. $fullClass = 'KeaBundle\\Services\\' . $className;
  48. if (class_exists($fullClass)) {
  49. $keaConfig = new $fullClass;
  50. if ($keaConfig instanceof KeaConfigInterface) {
  51. $data = [
  52. 'dhcp' => $id ? $this->dhcpRepository->find($id) : null,
  53. 'hosts' => $this->hostRepository->findAll(),
  54. 'subnets' => $this->subnetRepository->findAll(),
  55. 'library' => $library,
  56. ];
  57. $config = $keaConfig->getConfig($data);
  58. }
  59. }
  60. return $config;
  61. }
  62. /**
  63. * @param DHCP $dhcp
  64. * @param string $service
  65. *
  66. * @return string
  67. */
  68. public function getRemoteConfig(DHCP $dhcp, $service = "dhcp4")
  69. {
  70. $host = $dhcp->getHost();
  71. if (is_null($host)) $host = "kea:8080";
  72. $_params = array(
  73. "command" => "config-get",
  74. "service" => array($service),
  75. );
  76. $params = json_encode($_params);
  77. $config = $this->curlPost($host, $params);
  78. return $config;
  79. }
  80. /**
  81. * @param DHCP $dhcp
  82. * @param string $service
  83. *
  84. * @return string
  85. */
  86. public function reloadConfig(DHCP $dhcp, $service = "dhcp4")
  87. {
  88. $host = $dhcp->getHost();
  89. if(is_null($host)) $host = "kea:8080";
  90. $_params = array(
  91. "command" => "config-reload",
  92. "service" => array($service),
  93. );
  94. $params = json_encode($_params);
  95. $config = $this->curlPost($host, $params);
  96. return $config;
  97. }
  98. /**
  99. * @param DHCP $dhcp
  100. * @param string $service
  101. *
  102. * @return string
  103. */
  104. public function reloadHook(DHCP $dhcp, $service = "dhcp4")
  105. {
  106. $host = $dhcp->getHost();
  107. if(is_null($host)) $host = "kea:8080";
  108. $_params = array(
  109. "command" => "libreload",
  110. "service" => array($service),
  111. );
  112. $params = json_encode($_params);
  113. $config = $this->curlPost($host, $params);
  114. return $config;
  115. }
  116. /**
  117. * @param DHCP $dhcp
  118. * @param json params
  119. *
  120. * @return string
  121. */
  122. public function setConfig(DHCP $dhcp, $params = null)
  123. {
  124. $host = $dhcp->getHost();
  125. if(is_null($host)) $host = "kea:8080";
  126. $_params = json_decode($params, true);
  127. $json = json_encode($_params);
  128. if(isset($_params[0]) && isset($_params[0]['arguments'])) {
  129. $_params = array(
  130. "command" => "config-set",
  131. "service" => array("dhcp4"),
  132. "arguments" => $_params[0]['arguments']);
  133. } else {
  134. return json_encode(array("error" => "Config Error. Check template DHCP"));
  135. }
  136. $config = $this->curlPost($host, json_encode($_params));
  137. return $config;
  138. }
  139. /**
  140. * @param string $host
  141. * @param array $params
  142. *
  143. * @return string
  144. */
  145. private function curlPost($host, $params)
  146. {
  147. $ch = curl_init();
  148. curl_setopt($ch, CURLOPT_URL, "http://{$host}");
  149. curl_setopt($ch, CURLOPT_POST, true);
  150. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  151. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($params)));
  152. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  153. try {
  154. $return = curl_exec($ch);
  155. } catch (\Exception $ex) {
  156. return json_encode(array("error" => "Kea Connection Error."));
  157. }
  158. return $return;
  159. }
  160. }