|
@@ -0,0 +1,120 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace CablemodemBundle\Command;
|
|
|
+
|
|
|
+use mysqli;
|
|
|
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
+use Symfony\Component\Console\Helper\ProgressBar;
|
|
|
+use Symfony\Component\Console\Question\ConfirmationQuestion;
|
|
|
+use Doctrine\DBAL\DriverManager;
|
|
|
+use Symfony\Component\Yaml\Yaml;
|
|
|
+
|
|
|
+
|
|
|
+class AssignedIPImportDataCommand extends ContainerAwareCommand
|
|
|
+{
|
|
|
+
|
|
|
+ protected $em;
|
|
|
+
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this
|
|
|
+ ->setName('assignedip:import:data')
|
|
|
+ ->setDescription('Imports AssignedIP data from Flowdat 2')
|
|
|
+ ->setHelp('Imports AssignedIP data from Flowdat 2.')
|
|
|
+ ->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Origin Database name. Default: flowdat2')
|
|
|
+ ->addOption('user', null, InputOption::VALUE_OPTIONAL, 'User name for origin database. Default: root')
|
|
|
+ ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for origin database. Default: "empty_value"')
|
|
|
+ ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of origin database. Default: localhost')
|
|
|
+ ;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
+ {
|
|
|
+
|
|
|
+ $container = $this->getContainer();
|
|
|
+ $this->em = $container->get('doctrine.orm.entity_manager');
|
|
|
+
|
|
|
+ // Get data
|
|
|
+ $host = $input->getOption('host');
|
|
|
+ $dbname = $input->getOption('dbname');
|
|
|
+ $user = $input->getOption('user');
|
|
|
+ $password = $input->getOption('password');
|
|
|
+
|
|
|
+ // Set defaults
|
|
|
+ if (!$dbname) { $dbname = 'flowdat2'; }
|
|
|
+ if (!$user) { $user = 'root'; }
|
|
|
+ if (!$password) { $password = ''; }
|
|
|
+ if (!$host) { $host = 'localhost'; }
|
|
|
+
|
|
|
+ // Presentation
|
|
|
+ $io = new SymfonyStyle($input, $output);
|
|
|
+ $io->title('Flowdat3 AssignedIP Data Importer');
|
|
|
+
|
|
|
+ // Ask for confirmation
|
|
|
+ if (!$input->getOption('no-interaction')) {
|
|
|
+ $helper = $this->getHelper('question');
|
|
|
+ $question = new ConfirmationQuestion("This will IMPORT DATA FROM a Flowdat2 database. The CABLEMODEM TABLE will be ALTERED by OVERWRITING existing data, leading to potential DATA LOSS. Are you really sure you want to proceed? ", false);
|
|
|
+ if (!$helper->ask($input, $output, $question)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $io->newLine();
|
|
|
+
|
|
|
+ $conn = new mysqli($host, $user, $password, $dbname);
|
|
|
+ $sql = "SELECT * FROM AssignedIP a JOIN HostType h ON a.host_type_id = h.id;";
|
|
|
+ $rows = $conn->query($sql);
|
|
|
+
|
|
|
+ // Progress bar
|
|
|
+ $progressBar = new ProgressBar($output, $rows->num_rows);
|
|
|
+ $errors = [];
|
|
|
+
|
|
|
+ while ($row = $rows->fetch_assoc()) {
|
|
|
+
|
|
|
+ if ($row['cablemodem_id'] != null) {
|
|
|
+
|
|
|
+ $object = $this->em->getRepository('CablemodemBundle:Cablemodem')->findOneById($row['cablemodem_id']);
|
|
|
+
|
|
|
+ if (!is_null($object)) {
|
|
|
+
|
|
|
+ if (($row['name'] == 'Cablemodem' || $row['shortname'] == 'cm') &&
|
|
|
+ ($object->getFixedIP() == '' || is_null($object->getFixedIP()))) {
|
|
|
+ $object->setFixedIP($row['ip']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (($row['name'] == 'Consumer Provided Endpoint' || $row['shortname'] == 'cpe') &&
|
|
|
+ ($object->getCpeFixedIP() == '' || is_null($object->getCpeFixedIP()))) {
|
|
|
+ $object->setCpeFixedIP($row['ip']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (($row['name'] == 'Embedded Multimedia Terminal Adapter' || $row['shortname'] == 'mta') &&
|
|
|
+ ($object->getMtaFixedIP() == '' || is_null($object->getMtaFixedIP()))) {
|
|
|
+ $object->setMtaFixedIP($row['ip']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($container->get('validator')->validate($object)->count() == 0) {
|
|
|
+ $this->em->persist($object);
|
|
|
+ $this->em->flush();
|
|
|
+ } else {
|
|
|
+ $errors[] = "<error>[{$row['id']}] Cablemodem with ID {$row['cablemodem_id']}. Element has errors.</error>";
|
|
|
+ }
|
|
|
+
|
|
|
+ // Detach the object from the entity manager
|
|
|
+ $this->em->detach($object);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem with ID {$row['cablemodem_id']}. Element has been skipped.</error>";
|
|
|
+ }
|
|
|
+
|
|
|
+ $progressBar->advance();
|
|
|
+ }
|
|
|
+
|
|
|
+ $output->writeln(['']);
|
|
|
+ $output->writeln($errors);
|
|
|
+ $output->writeln(['', 'Done!']);
|
|
|
+ }
|
|
|
+}
|