Bladeren bron

Created command to read a log and create ONUs in FD. The log depend of
the OLT mark and model, for now, the command was tested and work with Fiberhome-AN5516_01.

Maximiliano Schvindt 6 jaren geleden
bovenliggende
commit
cde25539fe
1 gewijzigde bestanden met toevoegingen van 164 en 0 verwijderingen
  1. 164 0
      src/FTTHBundle/Command/AutodiscoveryONUCommand.php

+ 164 - 0
src/FTTHBundle/Command/AutodiscoveryONUCommand.php

@@ -0,0 +1,164 @@
+<?php
+
+namespace FTTHBundle\Command;
+
+use FTTHBundle\Entity\OLT;
+use FTTHBundle\Entity\NAP;
+use FTTHBundle\Entity\ONU;
+use FTTHBundle\Entity\ONUModel;
+use FTTHBundle\Entity\ONUProfile;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * Process OLT log of Discovery action and create ONUs and load it a basic configuration
+ */
+class AutodiscoveryONUCommand extends ContainerAwareCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('autodiscovery:onu')
+            ->setDescription('Process OLT log of Discovery action and create ONUs and load it a basic configuration')
+            ->setHelp('We need OLT id and a log file. This command works with Discovery Action')
+            ->addArgument('oltid', InputArgument::REQUIRED, 'Pon serial number')
+            ->addArgument('file', InputArgument::REQUIRED, 'File log')
+            ->addArgument('status', InputArgument::OPTIONAL, 'Status 0 (OK), 1 (ERROR) other value (PENDING)')
+            ->addOption('only-show', null, InputOption::VALUE_OPTIONAL, 'Only show the log with ONUS. Default: false', false);
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $date = date("Y-m-d H:i:s");
+
+        try {
+            $em = $this->getContainer()->get('doctrine')->getManager();
+            $oltid = $input->getArgument('oltid');
+            $file = $input->getArgument('file');
+            $status = $input->getArgument('status');
+
+            $optionValue = $input->getOption('only-show');
+            $onlyShow = ($optionValue !== false);
+            
+            $output->writeln("CMD autodiscovery:onu to OLT id: {$oltid} - {$date}");
+
+            $olt = $em->getRepository(OLT::class)->findOneById($oltid);
+
+            if(is_null($olt)) {$output->writeln("OLT id: {$oltid} doesn't exist - exit");exit(1);}
+            if(!file_exists($file)) {$output->writeln("File '{$file}' doesn't exist - exit");exit(1);}
+
+            if(is_null($olt->getClientId()) || is_null($olt->getDiscoveryProfile())) {
+                $output->writeln("OLT doesn't have clientId or profile to assign in the new onus - exit");exit(1);
+            }
+
+            $profile = $olt->getDiscoveryProfile();
+            $client = $olt->getClientId();
+            
+            $onus = $this->fiberHome_AN5516_01($file, $onlyShow);
+            
+            if($onus) {
+                
+                foreach($onus as $onu) {
+                    $slot = (int) $onu['slot'];
+                    $link = (int) $onu['port']; //link,port,pon
+                    $index = (int) $onu['index'];
+                    
+                    $model = $onu['model'];
+                    $psn = $onu['psn'];
+                    
+                    $output->writeln("ONU IN {$slot}/{$link}");
+                    $output->writeln("In log '{$onu['reg']}'");
+                    
+                    $nap = $em->getRepository(NAP::class)->findOneBy(array('olt' => $olt, 'slot' => $slot, 'link' => $link));
+                    if(is_null($nap)) {$output->writeln("NAP with slot: {$slot} and link: {$link} in OLT id: {$oltid} doesn't exist - continue");continue;}
+                    
+                    $modelName = "FiberHome-{$model}";
+                    $onuModel = $em->getRepository(ONUModel::class)->findOneByName($modelName);
+                    if(is_null($onuModel)) {$output->writeln("ONU MODEL ({$modelName}) doesn't exist - continue");continue;}
+                    
+                    
+                    $onu = $em->getRepository(ONU::class)->findOneByPonSerialNumber($psn);
+                    if($onu) {$output->writeln("ONU ({$psn}) exist - continue");continue;}
+                    
+                    $onu = $em->getRepository(ONU::class)->findOneBy(array('olt' => $olt, 'nap' => $nap, 'position' => $index));
+                    if($onu) {$output->writeln("ONU({$onu->getPonSerialNumber()}) in OLT({$olt->getName()}) > NAP({$nap->getName()} {$slot}/{$link}) > Position ($index) exist - continue");continue;}
+
+                    $output->writeln("OK with PSN - OLT - NAP - POSITION - PROFILE, so we create the ONU");
+
+                    $newOnu = new ONU();
+                    $newOnu->setPonSerialNumber($psn);
+                    $newOnu->setOlt($olt);
+                    $newOnu->setNap($nap);
+                    $newOnu->setModel($onuModel);
+                    $newOnu->setProfile($profile);
+                    $newOnu->setClientId($client);
+
+                    $em->persist($newOnu);
+                    $em->flush();
+
+                }
+
+            }
+            
+        } catch (\Throwable $e) {
+            echo("\nERROR CODE: " . $e->getCode() . "\nERROR MESSAGE: " . $e->getMessage() . "\n");
+            echo("\nTRACE: \n" . $e->getTraceAsString() . "\n\n");
+        }
+    }
+
+    private function fiberHome_AN5516_01($file, $onlyShow) {
+
+        $slot = $port = null;
+        $onus = array();
+        $showFile = "";
+
+        if ($file = fopen($file, "r")) {
+
+            while(!feof($file)) {
+                $line = fgets($file);
+                $showFile .= PHP_EOL.$line;
+                $output = array();
+                
+                if(preg_match('/SLOT=(\d+) PON=(\d+) ,ITEM=(\d+)/', $line, $output)) {
+                    if(isset($output[3]) && ($output[3] > 0))  {
+                        $slot = $output[1];
+                        $port = $output[2];
+                        continue;
+                    }
+                } 
+                
+                if(preg_match('/(\S+)\s\s(\S+)\s*(\S+)\s*,/', $line, $output)) {
+                    if(isset($output[1]) && isset($output[2]) && isset($output[3]) && !is_null($slot) && !is_null($port)) {
+                        $onu = array();
+                        $onu['reg'] = $output[0];
+                        $onu['slot'] = $slot;
+                        $onu['port'] = $port;
+                        $onu['index'] = $output[1];
+                        $onu['model'] = $output[2];
+                        $onu['psn'] = $output[3];
+
+                        $onus[] = $onu;
+                    }
+                }
+
+            }
+
+            fclose($file);
+        }
+
+        if($onlyShow) {
+            print_r($showFile);
+            print_r(PHP_EOL);
+            print_r($onus);
+            $onus = array();
+        }
+
+
+        return $onus;
+    }
+
+
+}