Pārlūkot izejas kodu

Update to work with state of client when update Onu or Client

Jean Sumara 5 gadi atpakaļ
vecāks
revīzija
c99a89fdd1

+ 8 - 0
app/config/routing.yml

@@ -142,3 +142,11 @@ ftth_tcont_profile_api:
     resource: "@FTTHBundle/Controller/REST/TContProfileRESTController.php"
     type:   rest
     prefix:   /api
+ftth_client_onus_api:
+    resource: "@FTTHBundle/Controller/REST/ClientONURESTController.php"
+    type:   rest
+    prefix:   /api
+ftth_client_onu:
+    resource: "@FTTHBundle/Controller/REST/ClientRESTController.php"
+    type:     annotation
+    prefix: /api

+ 13 - 9
src/FTTHBundle/Admin/ONUAdmin.php

@@ -8,8 +8,7 @@ use Doctrine\ORM\EntityRepository;
 use FTTHBundle\Entity\NAP;
 use FTTHBundle\Entity\ONU;
 use FTTHBundle\Factory\ExceptionFactory;
-use FTTHBundle\Service\ClientService;
-use FTTHBundle\Utils\ONUStateEnum;
+use FTTHBundle\Service\ClientProxyService;
 use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
 use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\ListMapper;
@@ -743,6 +742,7 @@ class ONUAdmin extends WorkflowBaseAdmin
             if(!is_null($status)) $aux = $tr069->setWstatus($id, $status);
         }
 
+        $this->makeProxyUpdateStateOnu($onu);
     }
 
 
@@ -753,17 +753,21 @@ class ONUAdmin extends WorkflowBaseAdmin
     public function prePersist($onu)
     {
         $onu->setOlt($onu->getNap()->getOlt());
+        $this->makeProxyUpdateStateOnu($onu);
+    }
+
+    private function makeProxyUpdateStateOnu($onu){
         $webService = $this->get("webservice");
-        if($webService instanceof Webservice){
-            try{
-                $clientService = new ClientService($webService, $this->getConfigurationPool()->getContainer());
-                $clientService->setStateOnu($onu);
-            }catch (Exception $ex){
-                ExceptionFactory::make($ex->getMessage());
-            }
+
+        try{
+            $clientProxyService = new ClientProxyService($webService, $this->getConfigurationPool()->getContainer());
+            $clientProxyService->setStateOnu($onu);
+        } catch (Exception $ex){
+            ExceptionFactory::make($ex->getMessage());
         }
     }
 
+
     /**
      * @param Form $form
      * @param Template $template

+ 87 - 0
src/FTTHBundle/Controller/REST/ClientONURESTController.php

@@ -0,0 +1,87 @@
+<?php
+
+
+namespace FTTHBundle\Controller\REST;
+
+use FOS\RestBundle\Controller\Annotations\RouteResource;
+use FOS\RestBundle\Request\ParamFetcherInterface;
+use FOS\RestBundle\Util\Codes;
+use FOS\RestBundle\View\View as FOSView;
+use FTTHBundle\Factory\ExceptionFactory;
+use FTTHBundle\Form\ONUType;
+use WebserviceBundle\Controller\RESTController;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * ONUClient controller.
+ * @RouteResource("ONUClient")
+ */
+class ClientONURESTController extends RESTController
+{
+    /**
+     * @return string Retorna el nombre de la Entity de trabajo.
+     */
+    public function getRepository()
+    {
+        return 'FTTHBundle:ONU';
+    }
+
+    /**
+     * @return string Retorna el tipo de la clase.
+     */
+    public function getFormEntityType()
+    {
+        return get_class(new ONUType());
+    }
+
+    public function cgetAction(ParamFetcherInterface $paramFetcher)
+    {
+        return FOSView::create("Not implemented.", Codes::HTTP_INTERNAL_SERVER_ERROR);
+    }
+
+    public function postAction(Request $request)
+    {
+        return FOSView::create("Not implemented.", Codes::HTTP_INTERNAL_SERVER_ERROR);
+    }
+
+    public function putAction(Request $request, $entity = null)
+    {
+        return FOSView::create("Not implemented.", Codes::HTTP_INTERNAL_SERVER_ERROR);
+    }
+
+    public function patchAction(Request $request, $entity)
+    {
+        return FOSView::create("Not implemented.", Codes::HTTP_INTERNAL_SERVER_ERROR);
+    }
+
+    public function deleteAction(Request $request, $entity)
+    {
+        try{
+            $tenancyService = $this->getTenancyService();
+            $tenancyService->disableFilter();
+
+            $em = $this->container->get("doctrine.orm.entity_manager");
+
+            $criteria = new \Doctrine\Common\Collections\Criteria();
+            $criteria->where($criteria->expr()->contains("clientId", $entity));
+
+            $repository = $em->getRepository($this->getRepository());
+            $entities = $repository->matching($criteria);
+            foreach ($entities as $onu){
+                $em->remove($onu);
+                $em->flush();
+            }
+
+            $tenancyService->enableFilter();
+        } catch (\Exception $ex){
+            ExceptionFactory::make($ex->getMessage());
+        }
+    }
+
+    public function getAction($entity)
+    {
+        return FOSView::create("Not implemented.", Codes::HTTP_INTERNAL_SERVER_ERROR);
+    }
+
+
+}

+ 28 - 0
src/FTTHBundle/Controller/REST/ClientRESTController.php

@@ -0,0 +1,28 @@
+<?php
+
+
+namespace FTTHBundle\Controller\REST;
+
+use FTTHBundle\Utils\JsonResponseUtils;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
+use FOS\RestBundle\Controller\Annotations\RouteResource;
+
+class ClientRESTController extends Controller
+{
+    /**
+     * @param Request $request
+     * @param int $id
+     * @param string $transition
+     *
+     * @return JsonResponse
+     *
+     * @Route("/clients/{id}/apply-transition/{transition}", name="client_onu_apply_transition", methods={"GET"})
+     */
+    public function clientsApplyTransitionAction(int $id, string $transition, Request $request)
+    {
+        $clientService = $this->get('client_service');
+        return JsonResponseUtils::make($clientService->applyTransition($id, $transition));
+    }
+}

+ 20 - 0
src/FTTHBundle/Domain/OnuTransition/ITransition.php

@@ -0,0 +1,20 @@
+<?php
+
+
+namespace FTTHBundle\Domain\OnuTransition;
+
+
+use FTTHBundle\Entity\ONU;
+
+interface ITransition
+{
+    public function setOnu(ONU $onu);
+
+    public function getFrom();
+
+    public function getTo();
+
+    public function type();
+
+    public function getTransitions();
+}

+ 68 - 0
src/FTTHBundle/Domain/OnuTransition/OnuActive.php

@@ -0,0 +1,68 @@
+<?php
+
+
+namespace FTTHBundle\Domain\OnuTransition;
+
+use FTTHBundle\Entity\ONU;
+use FTTHBundle\Utils\ONUStateEnum;
+
+class OnuActive implements ITransition
+{
+    private $onu;
+
+    private $from;
+
+    private $to;
+
+    public function type()
+    {
+        return 'active';
+    }
+
+    public function getFrom()
+    {
+        return $this->from;
+    }
+
+    public function getTo()
+    {
+        return $this->to;
+    }
+
+    /**
+     * @param mixed $from
+     */
+    private function setFrom($from)
+    {
+        $this->from = $from;
+        if($this->from == ONUStateEnum::DELETED){
+            $this->setTo(TransitionOnu::DELETED_ACTIVE);
+        }else if($this->from == ONUStateEnum::DISABLE){
+            $this->setTo(TransitionOnu::DISABLE_ACTIVE);
+        }
+    }
+
+    /**
+     * @param mixed $to
+     */
+    private function setTo($to)
+    {
+        $this->to = $to;
+    }
+
+    /**
+     * @param ONU $onu
+     */
+    public function setOnu(ONU $onu)
+    {
+        $this->onu = $onu;
+        $this->setFrom($onu->getCurrentState());
+    }
+
+    public function getTransitions()
+    {
+        // TODO: Implement getTransitions() method.
+    }
+
+
+}

+ 69 - 0
src/FTTHBundle/Domain/OnuTransition/OnuDeleted.php

@@ -0,0 +1,69 @@
+<?php
+
+
+namespace FTTHBundle\Domain\OnuTransition;
+
+
+use FTTHBundle\Entity\ONU;
+use FTTHBundle\Utils\ONUStateEnum;
+
+class OnuDeleted implements ITransition
+{
+    private $onu;
+
+    private $from;
+
+    private $to;
+
+    public function type()
+    {
+        return 'active';
+    }
+
+    public function getFrom()
+    {
+        return $this->from;
+    }
+
+    public function getTo()
+    {
+        return $this->to;
+    }
+
+    /**
+     * @param mixed $from
+     */
+    private function setFrom($from)
+    {
+        $this->from = $from;
+        if($this->from == ONUStateEnum::ACTIVE){
+            $this->setTo(TransitionOnu::ACTIVE_DELETED);
+        }else if($this->from == ONUStateEnum::DISABLE){
+            $this->setTo(TransitionOnu::DISABLE_DELETED);
+        }
+    }
+
+    /**
+     * @param mixed $to
+     */
+    private function setTo($to)
+    {
+        $this->to = $to;
+    }
+
+    /**
+     * @param ONU $onu
+     */
+    public function setOnu(ONU $onu)
+    {
+        $this->onu = $onu;
+        $this->setFrom($onu->getCurrentState());
+    }
+
+    public function getTransitions()
+    {
+        // TODO: Implement getTransitions() method.
+    }
+
+
+}

+ 78 - 0
src/FTTHBundle/Domain/OnuTransition/OnuDisable.php

@@ -0,0 +1,78 @@
+<?php
+
+
+namespace FTTHBundle\Domain\OnuTransition;
+
+
+use FTTHBundle\Entity\ONU;
+use FTTHBundle\Utils\ONUStateEnum;
+
+class OnuDisable implements ITransition
+{
+    private $onu;
+
+    private $from;
+
+    private $to;
+
+    private $transitions;
+
+    public function type()
+    {
+        return 'active';
+    }
+
+    public function getFrom()
+    {
+        return $this->from;
+    }
+
+    public function getTo()
+    {
+        return $this->to;
+    }
+
+    /**
+     * @param mixed $from
+     */
+    private function setFrom($from)
+    {
+        $this->from = $from;
+        if($this->from == ONUStateEnum::ACTIVE){
+            $this->setTo(TransitionOnu::ACTIVE_DISABLE);
+        }else if($this->from == ONUStateEnum::DISABLE){
+            $this->setTo(TransitionOnu::DISABLE_ACTIVE);
+        }else if($this->from == ONUStateEnum::DELETED){
+            $transitions = [TransitionOnu::DELETED_ACTIVE, TransitionOnu::ACTIVE_DISABLE];
+            $this->setTransitions($transitions);
+        }
+    }
+
+    /**
+     * @param mixed $to
+     */
+    private function setTo($to)
+    {
+        $this->to = $to;
+    }
+
+    /**
+     * @param ONU $onu
+     */
+    public function setOnu(ONU $onu)
+    {
+        $this->onu = $onu;
+        $this->setFrom($onu->getCurrentState());
+    }
+
+    public function getTransitions()
+    {
+        return $this->transitions;
+    }
+
+    public function setTransitions($transitions)
+    {
+        $this->transitions = $transitions;
+    }
+
+}

+ 34 - 0
src/FTTHBundle/Domain/OnuTransition/OnuTransitionFactory.php

@@ -0,0 +1,34 @@
+<?php
+
+
+namespace FTTHBundle\Domain\OnuTransition;
+
+use FTTHBundle\Entity\ONU;
+
+class OnuTransitionFactory
+{
+    private $factory;
+
+    /**
+     * OnuTransitionFactory constructor.
+     * @param $factory
+     * @throws \Exception
+     */
+    public function __construct($factory)
+    {
+        $kindClass = "FTTHBundle\Domain\OnuTransition\Onu{$factory}";
+
+        if(!class_exists($kindClass)) {
+            throw new \Exception("Class Onu{$factory} not found");
+        }
+
+        $this->factory = new $kindClass;
+    }
+
+    public function build(ONU $onu){
+        if($this->factory instanceof ITransition){
+            $this->factory->setOnu($onu);
+        }
+        return $this->factory;
+    }
+}

+ 14 - 0
src/FTTHBundle/Domain/OnuTransition/TransitionOnu.php

@@ -0,0 +1,14 @@
+<?php
+
+
+namespace FTTHBundle\Domain\OnuTransition;
+
+
+class TransitionOnu
+{
+    const DELETED_ACTIVE = 'deleted_active';
+    const DISABLE_ACTIVE = 'disable_active';
+    const ACTIVE_DELETED = 'active_deleted';
+    const DISABLE_DELETED = 'disable_deleted';
+    const ACTIVE_DISABLE = 'active_disable';
+}

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

@@ -141,3 +141,7 @@ services:
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: validator.constraint_validator }
+
+    client_service:
+        class: FTTHBundle\Service\ClientService
+        arguments: ["@doctrine.orm.entity_manager", "@base_tenancy.tenancy_service", "@service_container"]

+ 77 - 0
src/FTTHBundle/Service/ClientProxyService.php

@@ -0,0 +1,77 @@
+<?php
+
+
+namespace FTTHBundle\Service;
+
+
+use Exception;
+use FTTHBundle\Entity\ONU;
+use FTTHBundle\Factory\ExceptionFactory;
+use FTTHBundle\Utils\ClientStateEnum;
+use FTTHBundle\Utils\ONUStateEnum;
+use WebserviceBundle\Services\Webservice;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+class ClientProxyService
+{
+    /**
+     * @var Webservice
+     */
+    private $webservice;
+
+    /**
+     * @var ContainerInterface
+     */
+    private $serviceContainer;
+
+    /**
+     * @param Webservice $webservice
+     * @param ContainerInterface $serviceContainer
+     */
+    public function __construct(Webservice $webservice, ContainerInterface $serviceContainer)
+    {
+        $this->webservice = $webservice;
+        $this->serviceContainer = $serviceContainer;
+    }
+
+    /**
+     * @param $id int
+     * @return Client || Exception
+     * @throws Exception
+     */
+    public function getById($id){
+        try{
+            $this->checkContainerParameter();
+            $filters = array("qb-ids" => $id, 'qb-criteria' => true);
+            return $this->webservice->getData($this->serviceContainer->getParameter('client'), $filters)[0];
+        }catch (Exception $ex){
+            ExceptionFactory::make($ex->getMessage());
+        }
+    }
+
+    /**
+     * @param ONU $onu
+     * @throws Exception
+     */
+    public function setStateOnu($onu){
+        try{
+            $clientEnums = new ClientStateEnum();
+            $client = $this->getById($onu->getClientId());
+            if(!is_null($client) && $clientEnums->containsStatesDisabled($client['currentState'])){
+                ExceptionFactory::make("Not is possible use client with current state 'cancelled' or 'deleted'");
+            }
+            $onu->setCurrentState($client['currentState'] == ClientStateEnum::CURRENT_STATE_SUSPEND ? ONUStateEnum::DISABLE : ONUStateEnum::ACTIVE);
+        }catch (Exception $ex){
+            ExceptionFactory::make($ex->getMessage());
+        }
+    }
+
+    /**
+     * @throws Exception
+     */
+    private function checkContainerParameter(){
+        if(!$this->serviceContainer->hasParameter('client')){
+            ExceptionFactory::make("Don't exists parameters 'client' to search the webservice");
+        }
+    }
+}

+ 94 - 43
src/FTTHBundle/Service/ClientService.php

@@ -3,75 +3,126 @@
 
 namespace FTTHBundle\Service;
 
-use Exception;
-use FTTHBundle\Entity\ONU;
+use Base\AdminBundle\Controller\TenancyService;
+use Doctrine\ORM\EntityManager;
+use FTTHBundle\Domain\OnuTransition\OnuTransitionFactory;
 use FTTHBundle\Factory\ExceptionFactory;
-use FTTHBundle\Utils\ClientStateEnum;
 use FTTHBundle\Utils\ONUStateEnum;
-use WebserviceBundle\Services\Webservice;
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\Console\Input\ArgvInput;
+use Symfony\Component\Console\Output\BufferedOutput;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 class ClientService
 {
     /**
-     * @var Webservice
+     * @var EntityManager
      */
-    protected $webservice;
+    private $entityManager;
+
+    /**
+     * @var TenancyService
+     */
+    private $tenancyService;
+
 
     /**
      * @var ContainerInterface
      */
-    protected $serviceContainer;
+    private $container;
 
     /**
-     * @param Webservice $webservice
-     * @param ContainerInterface $serviceContainer
+     * @param EntityManager $entityManager
+     * @param TenancyService $tenancyService
+     * @param ContainerInterface $container
      */
-    public function __construct(Webservice $webservice, ContainerInterface $serviceContainer)
+    public function __construct(EntityManager $entityManager, TenancyService $tenancyService, ContainerInterface $container)
     {
-        $this->webservice = $webservice;
-        $this->serviceContainer = $serviceContainer;
+        $this->entityManager = $entityManager;
+        $this->tenancyService = $tenancyService;
+        $this->container = $container;
     }
 
-    /**
-     * @param $id int
-     * @return Client || Exception
-     * @throws Exception
-     */
-    public function getById($id){
+    public function applyTransition($clientId, $transition){
         try{
-            $this->checkContainerParameter();
-            $filters = array("qb-ids" => $id, 'qb-criteria' => true);
-            return $this->webservice->getData($this->serviceContainer->getParameter('client'), $filters)[0];
-        }catch (Exception $ex){
-            ExceptionFactory::make($ex->getMessage());
-        }
-    }
+            if(!in_array($transition, ONUStateEnum::getStatesTypes())){
+                throw new \Exception("Don't apply this transition");
+            }
 
+            $query = $this->entityManager->createQuery("
+                SELECT o FROM FTTHBundle:ONU o
+                WHERE o.clientId = :client
+            ")->setParameters([
+                'client' => $clientId
+            ]);
 
-    /**
-     * @param ONU $onu
-     * @throws Exception
-     */
-    public function setStateOnu(ONU $onu){
-        try{
-            $clientEnums = new ClientStateEnum();
-            $client = $this->getById($onu->getClientId());
-            if(!is_null($client) && $clientEnums->containsStatesDisabled($client['currentState'])){
-                ExceptionFactory::make("Not is possible use client with current state 'cancelled' or 'deleted'");
+            $serialNumbers = array();
+
+            $onus = $query->getResult();
+            if(!is_null($onus)){
+                $factory = (new OnuTransitionFactory($transition));
+
+                foreach ($onus as $onu){
+                    if($onu->getCurrentState() !== strtolower($transition)){
+                        $onuTransition = $factory->build($onu);
+                        if(!is_null($onuTransition->getTransitions())){
+                            foreach ($onuTransition->getTransitions() as $t){
+                                $this->execute($onu->getId(), $t);
+                            }
+                            $serialNumbers[$onu->getId()] = $onu->getPonSerialNumber();
+                        } else if($onuTransition->getTo() !== null && is_null($onuTransition->getTransitions())){
+                            $this->execute($onu->getId(), $onuTransition->getTo());
+                            $serialNumbers[$onu->getId()] = $onu->getPonSerialNumber();
+                        }
+                    }
+                }
             }
-            $onu->setCurrentState(ONUStateEnum::ACTIVE);
-        }catch (Exception $ex){
+
+            return array(
+                'transition' => $transition,
+                'count' => count($onus),
+                'onus' => $serialNumbers
+            );
+        } catch (\Exception $ex){
             ExceptionFactory::make($ex->getMessage());
         }
+
     }
 
-    /**
-     * @throws Exception
-     */
-    private function checkContainerParameter(){
-        if(!$this->serviceContainer->hasParameter('client')){
-            ExceptionFactory::make("Don't exists parameters 'client' to search the webservice");
+    private function execute($onuId, $transition){
+        $cmd_args = [
+            'entity' => '--entity:FTTHBundle\\Entity\\ONU',
+            'workflow' => '--workflow:onu_workflow',
+            'transition' => "--transition:{$transition}",
+            'id' => "--id:{$onuId}"
+        ];
+
+        $this->runCommand('workflow:apply', $cmd_args);
+    }
+
+    private function runCommand($name, $cmd_args = array())
+    {
+
+        $kernel = $this->container->get('kernel');
+
+        $application = new Application($kernel);
+        $application->setAutoExit(false);
+
+        $args = [
+            '',
+            'amqp:remote',
+            $name,
+            '--route=' . getenv("AMQP_KEY"),
+        ];
+        foreach ($cmd_args as $cmd_arg) {
+            $args[] = "--args={$cmd_arg}";
         }
+        $input = new ArgvInput($args);
+
+        $output = new BufferedOutput();
+
+        $application->run($input, $output);
+
+        return $output->fetch();
     }
 }

+ 18 - 0
src/FTTHBundle/Utils/JsonResponseUtils.php

@@ -0,0 +1,18 @@
+<?php
+
+
+namespace FTTHBundle\Utils;
+
+use Symfony\Component\HttpFoundation\JsonResponse;
+
+class JsonResponseUtils
+{
+
+    /**
+     * @param $json
+     * @return JsonResponse
+     */
+    public static function make($json){
+        return new JsonResponse($json);
+    }
+}

+ 21 - 0
src/FTTHBundle/Utils/ONUStateEnum.php

@@ -8,4 +8,25 @@ class ONUStateEnum
 {
     const ACTIVE = "active";
     const DISABLE = "disable";
+    const DELETED = "deleted";
+
+    const ACTIVE_TYPE = "Active";
+    const DISABLE_TYPE = "Disable";
+    const DELETED_TYPE = "Deleted";
+
+    public static function getStates(){
+        return [
+            self::ACTIVE,
+            self::DISABLE,
+            self::DELETED
+        ];
+    }
+
+    public static function getStatesTypes(){
+        return [
+            self::ACTIVE_TYPE,
+            self::DISABLE_TYPE,
+            self::DELETED_TYPE
+        ];
+    }
 }