Selaa lähdekoodia

FD3-576 refactory dhcp:host:crud para crear Host CPE y MTA

Espinoza Guillermo 6 vuotta sitten
vanhempi
commit
e952bb9da7
1 muutettua tiedostoa jossa 144 lisäystä ja 53 poistoa
  1. 144 53
      src/CablemodemBundle/Command/DHCPHostCRUDCommand.php

+ 144 - 53
src/CablemodemBundle/Command/DHCPHostCRUDCommand.php

@@ -51,68 +51,147 @@ EOT
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $this->input = $input;
-        $mac = $input->getArgument('mac');
-        $delete = $input->getOption('delete');
-        $credentials = [
+        $this->mac = $input->getArgument('mac');
+        $this->webservice = $this->getContainer()->get('webservice');
+        $this->credentials = [
             'username' => $input->getOption('api-username'),
             'password' => $input->getOption('api-password'),
         ];
 
         $em = $this->getContainer()->get('doctrine.orm.entity_manager');
-        $cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($mac);
-        if ($cablemodem) {
-            $webservice = $this->getContainer()->get('webservice');
-            // consulto si hay un DHCP Host para la mac y traigo el HostType
-            $host = null;
-            $url = $webservice->buildUrl($input->getOption('url-get'), [
-                'mac' => $mac,
-            ]);
-            $hostJSON = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $credentials);
-            if ($hostJSON != '') {
-                $host = current(json_decode($hostJSON, true));
+        if ($this->cablemodem = $em->getRepository('CablemodemBundle:Cablemodem')->findOneByMac($this->mac)) {
+            // Crea el Host
+            $output->writeln($this->createHost());
+            
+            // Crea o elimina si existe el Host CPE
+            $cpeFixedIP = $this->cablemodem->getCpeFixedIP();
+            if ($cpeFixedIP) {
+                $output->writeln($this->createHost('CPE'));
+            } else {
+                $output->writeln($this->deleteHost('CPE'));
             }
-
-            $hostType = null;
-            if (isset($host['hostType'])) {
-                $hostType = $host['hostType']['id'];
+            
+            // Crea o elimina si existe el Host MTA
+            $mtaFixedIP = $this->cablemodem->getMtaFixedIP();
+            if ($mtaFixedIP) {
+                $output->writeln($this->createHost('MTA'));
             } else {
-                $url = $webservice->buildUrl($this->getUrlParameterHostType(), [
-                    'name' => 'Cablemodem',
-                ]);
-                $hostTypeJSON = $webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $credentials);
-                if ($hostTypeJSON != '' && !is_null($hostTypeJSON)) {
-                    $hostType = current(json_decode($hostTypeJSON, true))['id'];
-                }
+                $output->writeln($this->deleteHost('MTA'));
             }
-
-            $method = HttpRequestInterface::METHOD_POST;
-            if ($delete == true) {
-                $method = HttpRequestInterface::METHOD_DELETE;
-            } elseif ($host) {
-                $method = HttpRequestInterface::METHOD_PUT;
+            
+        } else {
+            $output->writeln("<error>Cablemodem mac {$this->mac} not found</error>");
+        }
+    }
+    
+    /**
+     * @param string $type HostType name Cablemodem | MTA | CPE
+     *
+     * @return string
+     */
+    private function createHost($type = 'Cablemodem')
+    {
+        $ws = $this->webservice;
+        $urlGET = $this->input->getOption('url-get');
+        
+        // Consulto en DHCP por Host
+        $host = null;
+        $url = $ws->buildUrl($urlGET, [
+            'mac' => $this->mac,
+        ]);
+        if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
+            $host = current(json_decode($hostJSON, true));
+        }
+        
+        // Consulto por Host con HostType MTA o CPE relacionado
+        if ($host && ($type == 'MTA' || $type == 'CPE')) {
+            $hostId = $host['id'];
+            $url = $ws->buildUrl($urlGET, [
+                'host' => $hostId,
+                'hostType' => $this->getIdHostType($type),
+            ]);
+            if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
+                $host = current(json_decode($hostJSON, true));
             }
+        }
+        
+        // Creo, edito o elimino dependiendo los parámetros
+        $method = HttpRequestInterface::METHOD_POST;
+        if ($this->input->getOption('delete') == true) {
+            $method = HttpRequestInterface::METHOD_DELETE;
+        } elseif ($host) {
+            $method = HttpRequestInterface::METHOD_PUT;
+        }
+        
+        $data = [
+            'mac' => $this->mac,
+            'hostType' => isset($host['hostType']) ? $host['hostType']['id'] : $this->getIdHostType($type),
+            'state' => 'active',
+            'fixedIP' => $this->cablemodem->getFixedIP(),
+        ];
+        if ($type == 'MTA' || $type == 'CPE') {
+            unset($data['mac']);
+            $data['host'] = $hostId;
+            $data['fixedIP'] = $type == 'MTA' ? $this->cablemodem->getMtaFixedIP() : $this->cablemodem->getCpeFixedIP();
+        }
+        
+        $dhcpOptions = $this->cablemodem->getDHCPOptions();
+        $data = array_merge($data, $dhcpOptions);
 
-            $data = [
-                'mac' => $mac,
-                'hostType' => $hostType,
-                'state' => 'active',
-                'fixedIP' => $cablemodem->getFixedIP(),
-            ];
-
-            $dhcpOptions = $cablemodem->getDHCPOptions();
-            $data = array_merge($data, $dhcpOptions);
-
-            $result = $webservice->makeGetRequest($this->getUrlParameter($method, $host), $method, $data, $credentials);
-
-            $output->writeln($result);
-        } else {
-            $output->writeln("<error>Cablemodem mac {$mac} not found</error>");
+        return $ws->makeGetRequest($this->getUrlParameter($method, $host), $method, $data, $this->credentials);
+    }
+    
+    /**
+     * @param string $type  HostType name Cablemodem | MTA | CPE
+     *
+     * @return string
+     */
+    private function deleteHost($type = 'Cablemodem')
+    {
+        $ws = $this->webservice;
+        $urlGET = $this->input->getOption('url-get');
+        
+        // Consulto en DHCP por Host
+        $host = null;
+        $url = $ws->buildUrl($urlGET, [
+            'mac' => $this->mac,
+        ]);
+        if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
+            $host = current(json_decode($hostJSON, true));
+        }
+        
+        // Consulto por Host con HostType MTA o CPE relacionado
+        $deleteHost = null;
+        if ($host && ($type == 'MTA' || $type == 'CPE')) {
+            $hostId = $host['id'];
+            $url = $ws->buildUrl($urlGET, [
+                'host' => $hostId,
+                'hostType' => $this->getIdHostType($type),
+            ]);
+            if ($hostJSON = $ws->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials)) {
+                $deleteHost = current(json_decode($hostJSON, true));
+            }
         }
+        
+        if (!$deleteHost) {
+            return null;
+        } 
+        
+        $method = HttpRequestInterface::METHOD_DELETE;
+
+        $data = [
+            'hostType' => isset($deleteHost['hostType']) ? $deleteHost['hostType']['id'] : $this->getIdHostType($type),
+            'host' => $hostId,
+        ];
+
+        return $ws->makeGetRequest($this->getUrlParameter($method, $deleteHost), $method, $data, $this->credentials);
     }
 
     /**
-     * @param string $method
-     * @param array $host
+     * Retorna una url de DHCP seteada como parámetro según el método http (GET|PUT|DELETE)
+     *
+     * @param string $method GET | PUT | DELETE
+     * @param array $host Host
      *
      * @return string
      */
@@ -135,18 +214,30 @@ EOT
 
         return $container->hasParameter($parameter) ? str_replace('{id}', $id, $container->getParameter($parameter)) : $url;
     }
-
+    
     /**
-     * @return string
+     * @param string $name HostType name Cablemodem | MTA | CPE
+     *
+     * @return int
      */
-    private function getUrlParameterHostType()
+    private function getIdHostType($name = 'Cablemodem')
     {
+        $hostType = null;
         $container = $this->getContainer();
         $parameter = 'dhcp_host_type_get_url';
-
-        return $container->hasParameter($parameter) ?
+        $url = $container->hasParameter($parameter) ?
             $container->getParameter($parameter) :
             $this->input->getOption('url-get-hosttype');
+        
+        $url = $this->webservice->buildUrl($url, [
+            'name' => $name,
+        ]);
+        $hostTypeJSON = $this->webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [], $this->credentials);
+        if ($hostTypeJSON != '' && !is_null($hostTypeJSON)) {
+            $hostType = current(json_decode($hostTypeJSON, true))['id'];
+        }
+        
+        return $hostType;
     }
 
 }