|
@@ -12,8 +12,12 @@ use Symfony\Component\HttpFoundation\Request;
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
use WebserviceBundle\Controller\RESTController;
|
|
|
use FOS\RestBundle\Controller\Annotations as Rest;
|
|
|
+use Symfony\Bundle\FrameworkBundle\Console\Application;
|
|
|
+use Symfony\Component\Console\Input\ArgvInput;
|
|
|
+use Symfony\Component\Console\Output\BufferedOutput;
|
|
|
|
|
|
use FOS\RestBundle\Controller\Annotations\Patch;
|
|
|
+use FOS\RestBundle\Controller\Annotations\Get;
|
|
|
|
|
|
/**
|
|
|
* ONU controller.
|
|
@@ -80,36 +84,122 @@ class ONURESTController extends RESTController
|
|
|
* @View(statusCode=201, serializerEnableMaxDepthChecks=true)
|
|
|
*
|
|
|
* @param Request $request
|
|
|
- * @param $entity
|
|
|
+ * @param $id
|
|
|
* @param $transition
|
|
|
* @param $workflow
|
|
|
*
|
|
|
* @return Response
|
|
|
*/
|
|
|
- public function applyAction(Request $request, ONU $entity, String $workflow, String $transition)
|
|
|
+ public function applyAction(Request $request, Int $id, String $workflow, String $transition)
|
|
|
{
|
|
|
+ $tenancyService = $this->getTenancyService();
|
|
|
+
|
|
|
try {
|
|
|
+
|
|
|
+ $em = $this->container->get("doctrine.orm.entity_manager");
|
|
|
+ $tenancyService->disableFilter();
|
|
|
+ $entity = $em->getRepository('FTTHBundle:ONU')->find($id);
|
|
|
|
|
|
$wr = $this->container->get("workflow.registry");
|
|
|
$wf = $wr->get($entity, $workflow);
|
|
|
|
|
|
$newState = $wf->apply($entity, $transition);
|
|
|
|
|
|
-
|
|
|
- $em = $this->container->get("doctrine.orm.entity_manager");
|
|
|
$validator = $this->container->get('validator');
|
|
|
$errors = $validator->validate($entity);
|
|
|
if (count($errors) > 0) {
|
|
|
- $errorsString = (string) $errors;
|
|
|
+ $errorsString = (string) $errors;
|
|
|
+ $tenancyService->enableFilter();
|
|
|
return FOSView::create($errorsString, Codes::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
}else{
|
|
|
$em->persist($entity);
|
|
|
- $em->flush($entity);
|
|
|
-
|
|
|
+ $em->flush($entity);
|
|
|
+ $tenancyService->enableFilter();
|
|
|
return $entity;
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * GET Route annotation.
|
|
|
+ * @Get("/client/{id}/apply/{transition}")
|
|
|
+ * @View(statusCode=201, serializerEnableMaxDepthChecks=true)
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @param $id
|
|
|
+ * @param $transition
|
|
|
+ *
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ public function clientAction(Request $request, Int $id, String $transition)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $tenancyService = $this->getTenancyService();
|
|
|
+ $em = $this->container->get("doctrine.orm.entity_manager");
|
|
|
+
|
|
|
+ $tenancyService->disableFilter();
|
|
|
+ $query = $em->createQuery('SELECT o FROM FTTHBundle:ONU o WHERE o.currentState != :transition AND o.clientId = :client')->setParameter('transition', $transition)->setParameter('client', $id);
|
|
|
+ $onus = $query->getResult();
|
|
|
+ $tenancyService->enableFilter();
|
|
|
+
|
|
|
+ $cmd_args = array();
|
|
|
+ $cmd_args['entity'] = '--entity:FTTHBundle\\Entity\\ONU';
|
|
|
+ $cmd_args['workflow'] = '--workflow:onu_workflow';
|
|
|
+ $cmd_args['transition'] = "--transition:{$transition}";
|
|
|
+
|
|
|
+ $serialNumbers = array();
|
|
|
+ if($onus) {
|
|
|
+
|
|
|
+ foreach($onus as $onu) {
|
|
|
+ $onuId = $onu->getId();
|
|
|
+ $cmd_args['id'] = "--id:{$onuId}";
|
|
|
+ $serialNumbers[$onuId] = $onu->getPonSerialNumber();
|
|
|
+
|
|
|
+ $this->runCommand('workflow:apply', $cmd_args);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return array('client' => $id, 'transition' => $transition, "count" => count($onus), 'onus' => $serialNumbers);
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Corro el comando para crear el device por AMQP
|
|
|
+ * @global kernel $kernel
|
|
|
+ *
|
|
|
+ * @param string $name
|
|
|
+ * @param array $args
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public 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();
|
|
|
+ }
|
|
|
}
|