DHCPHostCRUDCommand.php 11 KB

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