Переглянути джерело

Merge branch 'FD3-576' of bitbucket.org:ikflowdat/cablemodem into FD3-576

Luciano Andrade 6 роки тому
батько
коміт
7c29790521

+ 8 - 8
composer.lock

@@ -1,7 +1,7 @@
 {
     "_readme": [
         "This file locks the dependencies of your project to a known state",
-        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
     "content-hash": "cf26a79187488ef3ec3bafb55a6f72f5",
@@ -1643,7 +1643,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/TemplateBundle.git",
-                "reference": "14e7b7c9d0deadf8c779874419cf2f183a02bbf8"
+                "reference": "7e50327475180f03fa24a2fa5ee1dc01dadd2bfa"
             },
             "require": {
                 "ik/base-admin-bundle": "*"
@@ -1661,7 +1661,7 @@
                 "bundle",
                 "template"
             ],
-            "time": "2018-04-24T15:29:11+00:00"
+            "time": "2018-07-04T14:44:27+00:00"
         },
         {
             "name": "ik/webservice-bundle",
@@ -1669,7 +1669,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/Webservice.git",
-                "reference": "4fb907804f45b182cda4c48551adf5240386111a"
+                "reference": "c17a870fa8f24a00b0660b62c20704443c0b032f"
             },
             "require": {
                 "ext-curl": "*",
@@ -1682,7 +1682,7 @@
                 }
             },
             "description": "The Flowdat3 Webservice Rest",
-            "time": "2018-07-03T12:59:44+00:00"
+            "time": "2018-07-31T18:33:39+00:00"
         },
         {
             "name": "ik/workflow-bundle",
@@ -1690,7 +1690,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/WorkflowBundle.git",
-                "reference": "47ca3a59560cd1e7d8ae5e06ddbea62094fc2996"
+                "reference": "184c2d1c00423b71fd1eae098eead7b938de1882"
             },
             "require": {
                 "php-amqplib/rabbitmq-bundle": "^1.12"
@@ -1720,7 +1720,7 @@
                 "bundle",
                 "workflow"
             ],
-            "time": "2018-06-25T18:50:13+00:00"
+            "time": "2018-07-04T14:51:55+00:00"
         },
         {
             "name": "incenteev/composer-parameter-handler",
@@ -6327,7 +6327,7 @@
         "voryx/restgeneratorbundle": 20
     },
     "prefer-stable": false,
-    "prefer-lowest": false,
+    "prefer-lowest": true,
     "platform": {
         "php": ">=5.5.9"
     },

+ 37 - 4
src/CablemodemBundle/Admin/CablemodemAdmin.php

@@ -81,6 +81,11 @@ class CablemodemAdmin extends WorkflowBaseAdmin
                                 'required' => false,
                                 'allow_delete' => true))
                     ->end()
+                    ->with('DHCP')
+                        ->add('fixedIP')
+                        ->add('cpeFixedIP')
+                        ->add('mtaFixedIP')
+                    ->end()
                 ->end()
         ;
     }
@@ -105,6 +110,9 @@ class CablemodemAdmin extends WorkflowBaseAdmin
     }
 
     /**
+     * Agrega en la vista show de Cablemodem un tab DHCP mostrando
+     * Host Cablemodem, CPE y MTA si tiene asociado
+     *
      * @param ShowMapper $showMapper
      */
     protected function addDHCPTab(ShowMapper $showMapper)
@@ -114,14 +122,39 @@ class CablemodemAdmin extends WorkflowBaseAdmin
             ->end()
             ->end();
         }
+        $template = 'CablemodemBundle:CRUD:host_show_field.html.twig';
+        
+        $types = [
+            'Mta',
+            'Cpe',
+        ];
+        
         $showMapper
         ->tab('DHCP')
         ->with('Host')
-        ->add('host', null, array(
-            'template' => 'CablemodemBundle:CRUD:host_show_field.html.twig',
-        ))
-        ->end()
+        ->add('host', null, [
+            'template' => $template,
+        ])
         ->end();
+        
+        foreach ($types as $type) {
+            $method = "get{$type}FixedIp";
+            if ($this->getSubject()->$method()) {
+                $showMapper
+                    ->with($type)
+                    ->add($type, null, [
+                        'template' => $template,
+                        'data' => [
+                            'hostType' => [
+                                $type => $this->get('cablemodem.host_service')->getIdHostType(strtoupper($type)),
+                                ],
+                            ],
+                    ])
+                    ->end();
+            }
+        }
+        
+        $showMapper->end();
     }
 
 }

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

@@ -51,67 +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',
-            ];
-
-            $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
      */
@@ -134,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;
     }
 
 }

+ 87 - 5
src/CablemodemBundle/Entity/Cablemodem.php

@@ -119,7 +119,7 @@ class Cablemodem implements DeviceInterface, TenancyIdTraitInterface, LocationIn
      * @ORM\ManyToOne(targetEntity="\WorkflowBundle\Entity\Workflow", fetch="EXTRA_LAZY")
      * @ORM\JoinColumn(name="workflow_id", referencedColumnName="id", onDelete="SET NULL")
      *
-     * @JMS\MaxDepth(1)
+     * @JMS\Exclude
      */
     protected $workflow;
 
@@ -153,10 +153,25 @@ class Cablemodem implements DeviceInterface, TenancyIdTraitInterface, LocationIn
      * )
      */
     protected $voip = null;
-        
+    
     /**
-     * @ORM\Column(type="text", nullable=true)
-     */
+    * @ORM\Column(type="boolean", nullable=true)
+    */
+    protected $fixedIP = false;
+    
+    /**
+    * @ORM\Column(type="boolean", nullable=true)
+    */
+    protected $cpeFixedIP = false;
+    
+    /**
+    * @ORM\Column(type="boolean", nullable=true)
+    */
+    protected $mtaFixedIP = false;
+    
+    /**
+    * @ORM\Column(type="text", nullable=true)
+    */
     protected $comments;
 
 
@@ -421,13 +436,20 @@ class Cablemodem implements DeviceInterface, TenancyIdTraitInterface, LocationIn
     {
         if(is_null($voip)) {
             $this->voip = $voip;
+            
             return $this;
         }
 
         $this->voip = array_values($voip);
+        
         return $this;
     }
-
+    
+    /**
+     * @param string $index
+     *
+     * @return mixed
+     */
     public function getVoipData($index)
     {
         if(is_null($this->voip)) return null;
@@ -435,6 +457,66 @@ class Cablemodem implements DeviceInterface, TenancyIdTraitInterface, LocationIn
         if(isset($this->voip[$index])) return $this->voip[$index];
     }
     
+    /**
+     * @return boolean
+     */
+    public function getFixedIP()
+    {
+        return $this->fixedIP;
+    }
+    
+    /**
+     * @param boolean $fixedIP
+     *
+     * @return Cablemodem
+     */
+    public function setFixedIP($fixedIP)
+    {
+        $this->fixedIP = $fixedIP;
+        
+        return $this;
+    }
+    
+    /**
+     * @return boolean
+     */
+    public function getCpeFixedIP()
+    {
+        return $this->cpeFixedIP;
+    }
+    
+    /**
+     * @param boolean $cpeFixedIP
+     *
+     * @return Cablemodem
+     */
+    public function setCpeFixedIP($cpeFixedIP)
+    {
+        $this->cpeFixedIP = $cpeFixedIP;
+        
+        return $this;
+    }
+    
+    /**
+     * @return boolean
+     */
+    public function getMtaFixedIP()
+    {
+        return $this->mtaFixedIP;
+    }
+    
+    /**
+     * @param boolean $mtaFixedIP
+     *
+     * @return Cablemodem
+     */
+    public function setMtaFixedIP($mtaFixedIP)
+    {
+        $this->mtaFixedIP = $mtaFixedIP;
+        
+        return $this;
+    }
+    
     /**
      * @return string
      */

+ 4 - 0
src/CablemodemBundle/Resources/config/services.yml

@@ -53,3 +53,7 @@ services:
         calls:
             - [setTranslationDomain, [CablemodemBundle]]
         public: true
+        
+    cablemodem.host_service:
+        class: CablemodemBundle\Services\HostService
+        arguments: [ "%dhcp_host_type_get_url%", "@webservice" ]

+ 3 - 0
src/CablemodemBundle/Resources/translations/CablemodemBundle.es.yml

@@ -72,6 +72,9 @@ form:
     label_docs_version: Versión DOCSIS
     label_mta_enabled: MTA Enabled
     label_voip: Voip
+    label_fixed_i_p: IP fija
+    label_cpe_fixed_i_p: IP fija CPE
+    label_mta_fixed_i_p:  IP fija MTA
     label_comments: Comentarios
     label_time_cm_stats: Estadísticas y SLA de CMs
     

+ 15 - 1
src/CablemodemBundle/Resources/views/CRUD/host_show_field.html.twig

@@ -1,16 +1,29 @@
 {% set host = get_json('dhcp_host_post_url', { mac: object.mac }) %}
 
+{% if host and object.mtaFixedIP and field_description.options.data is defined and 'Mta' in field_description.options.data.hostType|keys %}
+{% set host = get_json('dhcp_host_post_url', { host: host.id, hostType: field_description.options.data.hostType.Mta }) %}
+{% endif %}
+
+{% if host and object.cpeFixedIP and field_description.options.data is defined and 'Cpe' in field_description.options.data.hostType|keys %}
+{% set host = get_json('dhcp_host_post_url', { host: host.id, hostType: field_description.options.data.hostType.Cpe }) %}
+{% endif %}
+
 {% block field %}
+
 {% if host %}
 <td>
 <table class="table">
     <tbody>
+        {% if host.mac %}
         <tr class="sonata-ba-view-container">
             <th>{{ 'Mac'|trans({}, 'CablemodemBundle') }}</th><td>{{ host.mac }}</td>
         </tr>
+        {% endif %}
+        {% if host.options %}
         <tr class="sonata-ba-view-container">
             <th>{{ 'Options'|trans({}, 'CablemodemBundle') }}</th><td>{{ host.options }}</td>
         </tr>
+        {% endif %}
         <tr class="sonata-ba-view-container">
             <th>{{ 'HostType'|trans({}, 'CablemodemBundle') }}</th><td>{{ host.hostType.name }}</td>
         </tr>
@@ -21,4 +34,5 @@
 </table>
 </td>
 {% endif %}
-{% endblock %}
+
+{% endblock %}

+ 52 - 0
src/CablemodemBundle/Services/HostService.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace CablemodemBundle\Services;
+
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+use WebserviceBundle\Services\Webservice;
+
+class HostService
+{
+    
+    /**
+     * @var Webservice
+     */
+    private $webservice;
+    
+    /**
+     * @var string
+     */
+    private $dhcpHostTypeGetUrl;
+    
+    
+    /**
+     * @param string $dhcpHostTypeGetUrl
+     * @param Webservice $webservice
+     */
+    public function __construct($dhcpHostTypeGetUrl, Webservice $webservice)
+    {
+        $this->dhcpHostTypeGetUrl = $dhcpHostTypeGetUrl;
+        $this->webservice = $webservice;
+    }
+    
+    /**
+     * @param string $name HostType name Cablemodem | MTA | CPE
+     * @param string $url url API DHCP a consultar
+     *
+     * @return int
+     */
+    public function getIdHostType($name = 'Cablemodem', $url = null)
+    {
+        $url = $this->webservice->buildUrl($url ?: $this->dhcpHostTypeGetUrl, [
+            'name' => $name,
+        ]);
+        
+        $hostType = null;
+        if ($hostTypeJSON = $this->webservice->makeGetRequest($url, HttpRequestInterface::METHOD_GET, [])) {
+            $hostType = current(json_decode($hostTypeJSON, true))['id'];
+        }
+        
+        return $hostType;
+    }
+    
+}