OLT.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. namespace FTTHBundle\Entity;
  3. use Base\AdminBundle\Interfaces\PreRemoveInterface;
  4. use Base\AdminBundle\Traits\TenancyIdTrait;
  5. use Base\AdminBundle\Traits\TenancyIdTraitInterface;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use ExtraDataBundle\Entity\Traits\ExtraDataTrait;
  8. use JMS\Serializer\Annotation as JMS;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Symfony\Component\Workflow\Exception\ExceptionInterface;
  12. use DeviceBundle\Validator\Constraints as ValidatorAssert;
  13. use DeviceBundle\Interfaces\DeviceInterface;
  14. use MapBundle\Entity\Interfaces\LocationInterface;
  15. use MapBundle\Entity\Traits\LocationTrait;
  16. use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
  17. use WorkflowBundle\Entity\Traits\WorkflowTrait;
  18. use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity as SoftDeleteable;
  19. use Gedmo\Mapping\Annotation as Gedmo;
  20. use Base\AdminBundle\Interfaces\SoftDeleteInterface;
  21. /**
  22. * @ORM\Entity
  23. * @UniqueEntity("ip")
  24. * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=true)
  25. *
  26. * @ValidatorAssert\Device
  27. */
  28. class OLT implements DeviceInterface, TenancyIdTraitInterface, LocationInterface, WorkflowInterface, PreRemoveInterface, SoftDeleteInterface
  29. {
  30. use ExtraDataTrait;
  31. use TenancyIdTrait;
  32. use LocationTrait;
  33. use WorkflowTrait;
  34. use SoftDeleteable;
  35. /**
  36. * @var bigint $id
  37. *
  38. * @ORM\Column(type="bigint", nullable=false)
  39. * @ORM\Id
  40. * @ORM\GeneratedValue(strategy="IDENTITY")
  41. */
  42. private $id;
  43. /**
  44. * @var string $name
  45. *
  46. * @ORM\Column(type="string", length=255)
  47. * @Assert\NotNull
  48. */
  49. protected $name;
  50. /**
  51. * @var string $ip
  52. *
  53. * @ORM\Column(type="string", length=50, nullable=true, unique=true)
  54. */
  55. protected $ip;
  56. /**
  57. * @var string $snmpCommunity
  58. *
  59. * @ORM\Column(type="string", length=255, nullable=true, unique=false)
  60. */
  61. protected $snmpCommunity;
  62. /**
  63. * @var string $sshUser
  64. *
  65. * @ORM\Column(type="string", length=255, nullable=true, unique=false)
  66. */
  67. protected $sshUser;
  68. /**
  69. * @var string $sshPass
  70. *
  71. * @ORM\Column(type="string", length=255, nullable=true, unique=false)
  72. */
  73. protected $sshPass;
  74. /**
  75. * @var string $libraryVersion
  76. *
  77. * @ORM\Column(type="string", length=255, nullable=true, unique=false)
  78. */
  79. protected $libraryVersion;
  80. /**
  81. * @ORM\ManyToOne(targetEntity="OLTModel", inversedBy="olts", fetch="EXTRA_LAZY")
  82. * @ORM\JoinColumn(name="model_id", referencedColumnName="id", onDelete="SET NULL")
  83. *
  84. * @JMS\MaxDepth(1)
  85. */
  86. protected $model;
  87. /**
  88. * @ORM\OneToMany(targetEntity="ONU", mappedBy="olt", fetch="EXTRA_LAZY", cascade={"remove"})
  89. *
  90. * @JMS\MaxDepth(2)
  91. */
  92. protected $onus;
  93. /**
  94. * @ORM\OneToMany(targetEntity="NAP", mappedBy="olt", fetch="EXTRA_LAZY", cascade={"remove"})
  95. *
  96. * @JMS\MaxDepth(2)
  97. */
  98. protected $naps;
  99. /**
  100. * @ORM\Column(type="string", nullable=true)
  101. */
  102. protected $currentState = null;
  103. /**
  104. * @ORM\Column(type="string", nullable=false, options={"default": "success"})
  105. */
  106. protected $transitionState = 'success';
  107. /**
  108. * @ORM\ManyToOne(targetEntity="\WorkflowBundle\Entity\Workflow", fetch="EXTRA_LAZY")
  109. * @ORM\JoinColumn(name="workflow_id", referencedColumnName="id", onDelete="SET NULL")
  110. *
  111. * @JMS\MaxDepth(1)
  112. */
  113. protected $workflow;
  114. /**
  115. * @ORM\Column(type="boolean", nullable=true, columnDefinition="BOOLEAN DEFAULT TRUE")
  116. */
  117. protected $executeSnmp = true;
  118. /**
  119. * @ORM\Column(type="integer", options={"unsigned":true, "default":10})
  120. */
  121. protected $timeScan = 10;
  122. /**
  123. * @ORM\Column(type="integer", options={"unsigned":true, "default":5})
  124. */
  125. protected $timeOltOctets = 5;
  126. /**
  127. * @ORM\Column(type="integer", options={"unsigned":true, "default":5})
  128. */
  129. protected $timePonStats = 5;
  130. /**
  131. * @ORM\Column(type="integer", options={"unsigned":true, "default":5})
  132. */
  133. protected $timeOnuStats = 5;
  134. /**
  135. * @ORM\Column(type="string", nullable=true)
  136. */
  137. protected $enable;
  138. /**
  139. * @ORM\Column(type="boolean", options={"default":true})
  140. */
  141. protected $backups;
  142. /**
  143. * @return string
  144. */
  145. public function __toString()
  146. {
  147. return (string)$this->name;
  148. }
  149. /**
  150. * @return int
  151. */
  152. public function getId()
  153. {
  154. return $this->id;
  155. }
  156. /**
  157. * @return string
  158. */
  159. public function getName()
  160. {
  161. return $this->name;
  162. }
  163. /**
  164. * @return string
  165. */
  166. public function getIp()
  167. {
  168. return $this->ip;
  169. }
  170. /**
  171. * @return string
  172. */
  173. public function getSnmpCommunity()
  174. {
  175. return $this->snmpCommunity;
  176. }
  177. /**
  178. * @return string
  179. */
  180. public function getSshUser()
  181. {
  182. return $this->sshUser;
  183. }
  184. /**
  185. * @return string
  186. */
  187. public function getSshPass()
  188. {
  189. return $this->sshPass;
  190. }
  191. /**
  192. * @param string $name
  193. * @return $this
  194. */
  195. public function setName($name)
  196. {
  197. $this->name = $name;
  198. return $this;
  199. }
  200. /**
  201. * @param string $ip
  202. * @return $this
  203. */
  204. public function setIp($ip)
  205. {
  206. $this->ip = $ip;
  207. return $this;
  208. }
  209. /**
  210. * @param string $snmpCommunity
  211. * @return $this
  212. */
  213. public function setSnmpCommunity($snmpCommunity)
  214. {
  215. $this->snmpCommunity = $snmpCommunity;
  216. return $this;
  217. }
  218. /**
  219. * @param string $sshUser
  220. * @return $this
  221. */
  222. public function setSshUser($sshUser)
  223. {
  224. $this->sshUser = $sshUser;
  225. return $this;
  226. }
  227. /**
  228. * @param string $sshPass
  229. * @return $this
  230. */
  231. public function setSshPass($sshPass)
  232. {
  233. $this->sshPass = $sshPass;
  234. return $this;
  235. }
  236. /**
  237. * @param ONU $onu
  238. * @return OLT
  239. */
  240. public function addOnu(ONU $onu)
  241. {
  242. $this->onus[] = $onu;
  243. return $this;
  244. }
  245. /**
  246. * @param ONU $onu
  247. * @return OLT
  248. */
  249. public function removeOnu(ONU $onu)
  250. {
  251. $this->onus->removeElement($onu);
  252. return $this;
  253. }
  254. /**
  255. * @return Doctrine\Common\Collections\Collection
  256. */
  257. public function getOnus()
  258. {
  259. return $this->onus;
  260. }
  261. /**
  262. * @param NAP $nap
  263. *
  264. * @return OLT
  265. */
  266. public function addNap(NAP $nap)
  267. {
  268. $this->naps[] = $nap;
  269. return $this;
  270. }
  271. /**
  272. * @param NAP $nap
  273. *
  274. * @return OLT
  275. */
  276. public function removeNap(NAP $nap)
  277. {
  278. $this->naps->removeElement($nap);
  279. return $this;
  280. }
  281. /**
  282. * @return Doctrine\Common\Collections\Collection
  283. */
  284. public function getNaps()
  285. {
  286. return $this->naps;
  287. }
  288. /**
  289. * @return OLTModel
  290. */
  291. public function getModel()
  292. {
  293. return $this->model;
  294. }
  295. /**
  296. * @param OLTModel $model
  297. * @return $this
  298. */
  299. public function setModel($model)
  300. {
  301. $this->model = $model;
  302. return $this;
  303. }
  304. /* Va a quedar deprecate */
  305. public function getWorkflowObject()
  306. {
  307. if ($this->model) {
  308. $model = $this->model;
  309. if ($model->getWorkflow()) {
  310. return $model->getWorkflow();
  311. }
  312. }
  313. return $this->workflow;
  314. }
  315. /**
  316. * @return array
  317. */
  318. public function getDeviceData()
  319. {
  320. $deviceData = array();
  321. $deviceData['deviceType'] = get_class($this);
  322. $deviceData['deviceId'] = $this->id;
  323. $deviceData['ip'] = $this->ip;
  324. $deviceData['tenancy'] = $this->tenancyId;
  325. $deviceExtraData = array('snmpCommunity' => $this->snmpCommunity, 'sshUser' => $this->sshUser, 'sshPass' => $this->sshPass,
  326. 'libraryVersion' => $this->libraryVersion, 'name' => $this->name);
  327. if ($this->model) {
  328. $model = $this->getModel();
  329. $deviceExtraData['modelId'] = $model->getId();
  330. $deviceExtraData['mark'] = $model->getMark();
  331. $deviceExtraData['library'] = $model->getLibrary();
  332. } else {
  333. $deviceExtraData['modelId'] = null;
  334. $deviceExtraData['mark'] = null;
  335. $deviceExtraData['library'] = null;
  336. }
  337. $deviceExtraData['executeSnmp'] = $this->executeSnmp;
  338. $deviceExtraData['timeScan'] = $this->timeScan;
  339. $deviceExtraData['timeOnuStats'] = $this->timeOnuStats;
  340. $deviceExtraData['timePonStats'] = $this->timePonStats;
  341. $deviceExtraData['timeOltOctets'] = $this->timeOltOctets;
  342. $deviceData['extraData'] = json_encode($deviceExtraData);
  343. return $deviceData;
  344. }
  345. /**
  346. * Set executeSnmp
  347. *
  348. * @param boolean $e
  349. * @return OLT
  350. */
  351. public function setExecuteSnmp($e)
  352. {
  353. $this->executeSnmp = $e;
  354. return $this;
  355. }
  356. /**
  357. * Get executeSnmp
  358. *
  359. * @return boolean
  360. */
  361. public function getExecuteSnmp()
  362. {
  363. return $this->executeSnmp;
  364. }
  365. /**
  366. * Set timeScan
  367. *
  368. * @param integer $timeScan
  369. */
  370. public function setTimeScan($timeScan)
  371. {
  372. $this->timeScan = $timeScan;
  373. }
  374. /**
  375. * Get timeScan
  376. *
  377. * @return integer
  378. */
  379. public function getTimeScan()
  380. {
  381. return $this->timeScan;
  382. }
  383. /**
  384. * Set timeOltOctets
  385. *
  386. * @param integer $timeOltOctets
  387. */
  388. public function setTimeOltOctets($timeOltOctets)
  389. {
  390. $this->timeOltOctets = $timeOltOctets;
  391. }
  392. /**
  393. * Get timeOltOctets
  394. *
  395. * @return integer
  396. */
  397. public function getTimeOltOctets()
  398. {
  399. return $this->timeOltOctets;
  400. }
  401. /**
  402. * Set timeOnuStats
  403. *
  404. * @param integer $timeOnuStats
  405. */
  406. public function setTimeOnuStats($timeOnuStats)
  407. {
  408. $this->timeOnuStats = $timeOnuStats;
  409. }
  410. /**
  411. * Get timeOnuStats
  412. *
  413. * @return integer
  414. */
  415. public function getTimeOnuStats()
  416. {
  417. return $this->timeOnuStats;
  418. }
  419. /**
  420. * Set timePonStats
  421. *
  422. * @param integer $timePonStats
  423. */
  424. public function setTimePonStats($timePonStats)
  425. {
  426. $this->timePonStats = $timePonStats;
  427. }
  428. /**
  429. * Get timePonStats
  430. *
  431. * @return integer
  432. */
  433. public function getTimePonStats()
  434. {
  435. return $this->timePonStats;
  436. }
  437. /**
  438. * @return string
  439. */
  440. public function getEnable()
  441. {
  442. return $this->enable;
  443. }
  444. /**
  445. * @param string $enable
  446. *
  447. * @return $this
  448. */
  449. public function setEnable($enable)
  450. {
  451. $this->enable = $enable;
  452. return $this;
  453. }
  454. /**
  455. * @return int
  456. */
  457. public function getLibraryVersion()
  458. {
  459. return $this->libraryVersion;
  460. }
  461. /**
  462. * @return array
  463. */
  464. public function getSoftDeleteCriteria()
  465. {
  466. return array('ip' => $this->ip);
  467. }
  468. /**
  469. * @return array
  470. */
  471. public function getEntitiesForRemove()
  472. {
  473. $entities = [];
  474. if ($this->onus->count() != 0) {
  475. $entities['onus'] = $this->onus;
  476. }
  477. if ($this->naps->count() != 0) {
  478. $entities['naps'] = $this->naps;
  479. }
  480. return $entities;
  481. }
  482. /**
  483. * @return mixed
  484. */
  485. public function getBackups()
  486. {
  487. return $this->backups;
  488. }
  489. /**
  490. * @param mixed $backups
  491. */
  492. public function setBackups($backups)
  493. {
  494. $this->backups = $backups;
  495. }
  496. /**
  497. * Obtiene los archivos de backups generados.
  498. * @param mixed $directory Contiene el directorio a leer.
  499. * @return array Retorna un array con los archivos del directorio.
  500. */
  501. public function obtainFiles($directory)
  502. {
  503. $resp = array();
  504. try {
  505. if ($this->getId()) {
  506. $dir = $directory . '/olt/' . $this->getId() . '/';
  507. $files = scandir($dir);
  508. rsort($files);
  509. $nc = 0;
  510. foreach ($files as $file) {
  511. if ($file != '.' && $file != '..') {
  512. if ($nc < 10) {
  513. $resp[$file] = $dir . $file;
  514. } else {
  515. break;
  516. }
  517. $nc++;
  518. }
  519. }
  520. }
  521. } catch (\Throwable $ignore) {
  522. }
  523. return $resp;
  524. }
  525. }