DHCPHostCRUDCommand.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace CablemodemBundle\Command;
  3. use Buzz\Message\RequestInterface as HttpRequestInterface;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. class DHCPHostCRUDCommand extends ContainerAwareCommand
  10. {
  11. /**
  12. * @see Command
  13. */
  14. protected function configure()
  15. {
  16. $params = [
  17. 'CMD_USERNAME' => getenv("CMD_USERNAME") ? getenv("CMD_USERNAME") : 'admin',
  18. 'CMD_PASSWORD' => getenv("CMD_PASSWORD") ? getenv("CMD_PASSWORD") : 'adminpass'
  19. ];
  20. $dhcpUrl = getenv("HOST_DHCP");
  21. $this
  22. ->setName('dhcp:host:crud')
  23. ->setDescription('DHCP Host CRUD')
  24. ->setDefinition(array(
  25. new InputArgument('mac', InputArgument::REQUIRED, "Cablemodem mac address"),
  26. new InputOption('delete', 'd'),
  27. new InputOption('url-get', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts', "https://{$dhcpUrl}/api/hosts.json"),
  28. new InputOption('url-post', null, InputOption::VALUE_OPTIONAL, 'API URL POST hosts', "https://{$dhcpUrl}/api/hosts.json"),
  29. new InputOption('url-put', null, InputOption::VALUE_OPTIONAL, 'API URL PUT hosts', "https://{$dhcpUrl}/api/hosts/{id}.json"),
  30. new InputOption('url-delete', null, InputOption::VALUE_OPTIONAL, 'API URL DELETE hosts', "https://{$dhcpUrl}/api/hosts/{id}.json"),
  31. new InputOption('url-get-hosttype', null, InputOption::VALUE_OPTIONAL, 'API URL GET hosts types', "https://{$dhcpUrl}/api/hosttypes.json"),
  32. new InputOption('api-username', null, InputOption::VALUE_OPTIONAL, 'API username', $params["CMD_USERNAME"]),
  33. new InputOption('api-password', null, InputOption::VALUE_OPTIONAL, 'API password', $params["CMD_PASSWORD"]),
  34. ))
  35. ->setHelp(<<<EOT
  36. The <info>dhcp:host:crud</info> command create or delete a DHCP Host from Cablemodem HostType and mac parameter
  37. EOT
  38. );
  39. }
  40. /**
  41. * @param InputInterface $input
  42. * @param OutputInterface $output
  43. */
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. $this->input = $input;
  47. $this->mac = $input->getArgument('mac');
  48. $this->webservice = $this->getContainer()->get('webservice');
  49. $this->credentials = [
  50. 'username' => $input->getOption('api-username'),
  51. 'password' => $input->getOption('api-password'),
  52. ];
  53. $em = $this->getContainer()->get('doctrine.orm.entity_manager');
  54. if ($this->cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($this->mac)) {
  55. // Crea el Host
  56. $output->writeln($this->createHost());
  57. // Crea o elimina si existe el Host CPE
  58. $cpeFixedIP = $this->cablemodem->getCpeFixedIP();
  59. if ($cpeFixedIP) {
  60. $output->writeln($this->createHost('CPE'));
  61. } else {
  62. $output->writeln($this->deleteHost('CPE'));
  63. }
  64. // Crea o elimina si existe el Host MTA
  65. $mtaFixedIP = $this->cablemodem->getMtaFixedIP();
  66. if ($mtaFixedIP) {
  67. $output->writeln($this->createHost('MTA'));
  68. } else {
  69. $output->writeln($this->deleteHost('MTA'));
  70. }
  71. } else {
  72. $output->writeln("<error>Cablemodem mac {$this->mac} not found</error>");
  73. }
  74. }
  75. /**
  76. * @param string $type HostType name Cablemodem | MTA | CPE
  77. *
  78. * @return string
  79. */
  80. private function createHost($type = 'Cablemodem')
  81. {
  82. $ws = $this->webservice;
  83. $urlGET = $this->input->getOption('url-get');
  84. // Consulto en DHCP por Host
  85. $host = null;
  86. $url = $ws->buildUrl($urlGET, [
  87. 'mac' => $this->mac,
  88. ]);
  89. if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
  90. $host = current(json_decode($hostJSON, true));
  91. }
  92. // Consulto por Host con HostType MTA o CPE relacionado
  93. if ($host && ($type == 'MTA' || $type == 'CPE')) {
  94. $hostId = $host['id'];
  95. $url = $ws->buildUrl($urlGET, [
  96. 'host' => $hostId,
  97. 'hostType' => $this->getIdHostType($type),
  98. ]);
  99. if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
  100. $host = current(json_decode($hostJSON, true));
  101. }
  102. }
  103. // Creo, edito o elimino dependiendo los parámetros
  104. $method = HttpRequestInterface::METHOD_POST;
  105. if ($this->input->getOption('delete') == true) {
  106. $method = HttpRequestInterface::METHOD_DELETE;
  107. } elseif ($host) {
  108. $method = HttpRequestInterface::METHOD_PUT;
  109. }
  110. $data = [
  111. 'mac' => $this->mac,
  112. 'hostType' => isset($host['hostType']) ? $host['hostType']['id'] : $this->getIdHostType($type),
  113. 'state' => 'active',
  114. 'fixedIP' => $this->cablemodem->getFixedIP(),
  115. ];
  116. if ($type == 'MTA' || $type == 'CPE') {
  117. unset($data['mac']);
  118. $data['host'] = $hostId;
  119. $data['fixedIP'] = $type == 'MTA' ? $this->cablemodem->getMtaFixedIP() : $this->cablemodem->getCpeFixedIP();
  120. }
  121. $dhcpOptions = $this->cablemodem->getDHCPOptions();
  122. $data = array_merge($data, $dhcpOptions);
  123. return $ws->makeGetRequest($this->getUrlParameter($method, $host), $method, $data, $this->credentials);
  124. }
  125. /**
  126. * @param string $type HostType name Cablemodem | MTA | CPE
  127. *
  128. * @return string
  129. */
  130. private function deleteHost($type = 'Cablemodem')
  131. {
  132. $ws = $this->webservice;
  133. $urlGET = $this->input->getOption('url-get');
  134. // Consulto en DHCP por Host
  135. $host = null;
  136. $url = $ws->buildUrl($urlGET, [
  137. 'mac' => $this->mac,
  138. ]);
  139. if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
  140. $host = current(json_decode($hostJSON, true));
  141. }
  142. // Consulto por Host con HostType MTA o CPE relacionado
  143. $deleteHost = null;
  144. if ($host && ($type == 'MTA' || $type == 'CPE')) {
  145. $hostId = $host['id'];
  146. $url = $ws->buildUrl($urlGET, [
  147. 'host' => $hostId,
  148. 'hostType' => $this->getIdHostType($type),
  149. ]);
  150. if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
  151. $deleteHost = current(json_decode($hostJSON, true));
  152. }
  153. }
  154. if (!$deleteHost) {
  155. return null;
  156. }
  157. $method = HttpRequestInterface::METHOD_DELETE;
  158. $data = [
  159. 'hostType' => isset($deleteHost['hostType']) ? $deleteHost['hostType']['id'] : $this->getIdHostType($type),
  160. 'host' => $hostId,
  161. ];
  162. return $ws->makeGetRequest($this->getUrlParameter($method, $deleteHost), $method, $data, $this->credentials);
  163. }
  164. /**
  165. * Retorna una url de DHCP seteada como parámetro según el método http (GET|PUT|DELETE)
  166. *
  167. * @param string $method GET | PUT | DELETE
  168. * @param array $host Host
  169. *
  170. * @return string
  171. */
  172. private function getUrlParameter($method = HttpRequestInterface::METHOD_POST, $host = [])
  173. {
  174. $container = $this->getContainer();
  175. $parameter = 'dhcp_host_post_url';
  176. $url = $this->input->getOption('url-post');
  177. $id = '';
  178. if (isset($host['id'])) {
  179. $id = $host['id'];
  180. }
  181. if ($method == HttpRequestInterface::METHOD_PUT && $id) {
  182. $parameter = 'dhcp_host_put_url';
  183. $url = $this->input->getOption('url-put');
  184. } elseif ($method == HttpRequestInterface::METHOD_DELETE && $id) {
  185. $parameter = 'dhcp_host_del_url';
  186. $url = $this->input->getOption('url-delete');
  187. }
  188. return $container->hasParameter($parameter) ? str_replace('{id}', $id, $container->getParameter($parameter)) : $url;
  189. }
  190. /**
  191. * @param string $name HostType name Cablemodem | MTA | CPE
  192. *
  193. * @return int
  194. */
  195. private function getIdHostType($name = 'Cablemodem')
  196. {
  197. $hostType = null;
  198. $container = $this->getContainer();
  199. $parameter = 'dhcp_host_type_get_url';
  200. $url = $container->hasParameter($parameter) ?
  201. $container->getParameter($parameter) :
  202. $this->input->getOption('url-get-hosttype');
  203. $url = $this->webservice->buildUrl($url, [
  204. 'shortname' => $name,
  205. ]);
  206. $hostTypeJSON = $this->webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials);
  207. if ($hostTypeJSON != '' && !is_null($hostTypeJSON)) {
  208. $hostType = current(json_decode($hostTypeJSON, true))['id'];
  209. }
  210. return $hostType;
  211. }
  212. }