|
@@ -0,0 +1,135 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace FD3\Command;
|
|
|
+
|
|
|
+use Symfony\Component\Console\Command\Command;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Input\InputArgument;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
+use Symfony\Component\BrowserKit\Cookie;
|
|
|
+use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
|
|
|
+use GuzzleHttp\Client as GuzzClient;
|
|
|
+use GuzzleHttp\Exception\ServerException as RequestException;
|
|
|
+use GuzzleHttp\Psr7;
|
|
|
+use GuzzleHttp\Cookie\CookieJar;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class ImportONUCommand extends Command
|
|
|
+{
|
|
|
+
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this
|
|
|
+ ->setName('import:ftth:onu')
|
|
|
+ ->setDescription('Import ONU entities from json file')
|
|
|
+ ->setHelp('ONU.json shuld be a valid json file, probably looking like the following example:
|
|
|
+[
|
|
|
+ {"clientId":1,"ponSerialNumber":"1","transitionState":"success","tenancyId":3},
|
|
|
+ {"clientId":1,"ponSerialNumber":"1","transitionState":"success","tenancyId":3}
|
|
|
+]
|
|
|
+
|
|
|
+')
|
|
|
+ ->addArgument('base_url', InputArgument::REQUIRED, 'The base url of the FD3 Client.')
|
|
|
+ ->addArgument('data_file.json', InputArgument::OPTIONAL, 'The data source from where to get data.', "ONU.json")
|
|
|
+ ->addOption('uri', null, InputOption::VALUE_REQUIRED, 'uri after the base_url for the complete URL', '/api/onus.json')
|
|
|
+ ->addOption('user', null, InputOption::VALUE_REQUIRED, 'The FD3 Username.', 'admin')
|
|
|
+ ->addOption('pass', null, InputOption::VALUE_REQUIRED, 'The FD3 Password.', 'adminpass')
|
|
|
+ ->addOption('client_id', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client ID.', '1_3323sq6urn8kwccg8s4ok848ggwwgkw4c08wsc4cwkc08osocc')
|
|
|
+ ->addOption('client_secret', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client Secret.', '5w7gx6ptdoo4g8cwwo88o8gowosgco84sso08ssow0osg88g8k')
|
|
|
+ ;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
+ {
|
|
|
+ $base_url = $input->getArgument("base_url");
|
|
|
+ $uri = $input->getOption("uri");
|
|
|
+ $username = $input->getOption('user');
|
|
|
+ $password = $input->getOption('pass');
|
|
|
+
|
|
|
+ $file = $input->getArgument("data_file.json");
|
|
|
+
|
|
|
+ $client_id = $input->getOption('client_id');
|
|
|
+ $client_secret = $input->getOption('client_secret');
|
|
|
+
|
|
|
+ $token = $this->getAccessToken($username, $password, $client_id, $client_secret);
|
|
|
+
|
|
|
+ $url = $base_url . $uri;
|
|
|
+
|
|
|
+ $parser = new \JsonCollectionParser\Parser();
|
|
|
+ $client = new GuzzClient();
|
|
|
+ $parser->parse($file, function($data) use ($client, $url, $token) {
|
|
|
+ try {
|
|
|
+
|
|
|
+
|
|
|
+// $cookie = $this->logIn($user, $token['access_token'], $token['refresh_token'], $token['expires_in']);
|
|
|
+// $cookieJar = CookieJar::fromArray([
|
|
|
+// $cookie->getName() => $cookie->getValue()
|
|
|
+// ], 'ftth.fd3.flowdat.com');
|
|
|
+//
|
|
|
+
|
|
|
+// $res = $client->request("POST", 'base.fd3.flowdat.com/oauth/v2/auth/login_check', [
|
|
|
+// 'auth' => [$user, $pass],
|
|
|
+// 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
|
|
|
+// 'body' => http_build_query([
|
|
|
+// '_username'=>$user,
|
|
|
+// '_password'=>$pass,
|
|
|
+// '_csrf_token'=>'XqptzqyIjxEvmrp8kPb6-1AuTNmxYwH5qfhVR6QNiKg'
|
|
|
+// ])
|
|
|
+// ]);
|
|
|
+//
|
|
|
+ $res = $client->request("POST", $url, [
|
|
|
+// 'cookies' => $cookieJar,
|
|
|
+ 'headers' => ['Authorization' => "Bearer {$token['access_token']}"],
|
|
|
+ 'body' => json_encode($data)
|
|
|
+ ]);
|
|
|
+ echo $res->getBody();
|
|
|
+ } catch (RequestException $e) {
|
|
|
+ echo Psr7\str($e->getRequest());
|
|
|
+ if ($e->hasResponse()) {
|
|
|
+ echo Psr7\str($e->getResponse());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getAccessToken($username, $password, $client_id, $client_secret, $url = 'http://base.fd3.flowdat.com/oauth/v2/token')
|
|
|
+ {
|
|
|
+ $body = ['grant_type' => 'password',
|
|
|
+ 'client_id' => $client_id,
|
|
|
+ 'client_secret' => $client_secret,
|
|
|
+ 'username' => $username,
|
|
|
+ 'password' => $password];
|
|
|
+
|
|
|
+ $client = new GuzzClient();
|
|
|
+ $res = $client->request("POST", $url, [
|
|
|
+ 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
|
|
|
+ 'body' => http_build_query($body),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return json_decode($res->getBody(), true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function logIn($username, $access_token, $refresh_token, $expires_in)
|
|
|
+ {
|
|
|
+ $session = new Session();
|
|
|
+ $session->setName('ftth_session');
|
|
|
+ $session->start();
|
|
|
+
|
|
|
+ // the firewall context (defaults to the firewall name)
|
|
|
+ $firewall = 'secured_area';
|
|
|
+
|
|
|
+ $token = new OAuthToken($access_token, array('ROLE_USER'));
|
|
|
+ $token->setAccessToken($access_token);
|
|
|
+ $token->setRefreshToken($refresh_token);
|
|
|
+ $token->setExpiresIn($expires_in);
|
|
|
+ $session->set('_security_'.$firewall, serialize($token));
|
|
|
+ $session->save();
|
|
|
+
|
|
|
+ return new Cookie($session->getName(), $session->getId());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|