SubNet.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace IPv4Bundle\Entity;
  3. use Base\AdminBundle\Traits\TenancyIdTrait;
  4. use Base\AdminBundle\Traits\TenancyIdTraitInterface;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  10. use IPv4Bundle\Traits\DHCPOptionTrait;
  11. use JMS\Serializer\Annotation as JMS;
  12. use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
  13. use WorkflowBundle\Entity\Traits\WorkflowTrait;
  14. /**
  15. * @ORM\Entity
  16. *
  17. * @UniqueEntity("address")
  18. *
  19. * @Assert\Callback("validateAddress")
  20. *
  21. * @ORM\HasLifecycleCallbacks
  22. */
  23. class SubNet implements TenancyIdTraitInterface, WorkflowInterface
  24. {
  25. use TenancyIdTrait;
  26. use DHCPOptionTrait;
  27. use WorkflowTrait;
  28. /**
  29. * @ORM\Column(name="id", type="bigint", nullable=false)
  30. * @ORM\Id
  31. * @ORM\GeneratedValue(strategy="IDENTITY")
  32. */
  33. protected $id;
  34. /**
  35. * @var string $address
  36. *
  37. * @ORM\Column(type="string", length=100, unique=true)
  38. *
  39. * @Assert\Regex(pattern="/^[ A-Za-z0-9_\-ñÑ:.\/]+$/")
  40. */
  41. protected $address;
  42. /**
  43. * @var HostType $allowedHostType
  44. *
  45. * @ORM\ManyToOne(targetEntity="HostType", inversedBy="subNets")
  46. * @ORM\JoinColumn(onDelete="CASCADE")
  47. */
  48. protected $allowedHostType;
  49. /**
  50. * @var string $options
  51. *
  52. * @ORM\Column(type="text", nullable=true)
  53. */
  54. protected $options;
  55. /**
  56. * @var NetGroup $netGroup
  57. *
  58. * @ORM\ManyToOne(targetEntity="NetGroup", inversedBy="subNets")
  59. * @ORM\JoinColumn(onDelete="CASCADE")
  60. */
  61. protected $netGroup;
  62. /**
  63. * @var Pool $ipPool
  64. *
  65. * @ORM\OneToMany(targetEntity="Pool", mappedBy="subNet", cascade="persist")
  66. */
  67. protected $ipPool;
  68. /**
  69. * @ORM\ManyToOne(targetEntity="\WorkflowBundle\Entity\Workflow", fetch="EXTRA_LAZY")
  70. * @ORM\JoinColumn(name="workflow_id", referencedColumnName="id", onDelete="SET NULL")
  71. * @JMS\MaxDepth(1)
  72. */
  73. protected $workflow;
  74. /**
  75. * @ORM\Column(type="string", nullable=true)
  76. */
  77. protected $currentState = null;
  78. /**
  79. * @ORM\Column(type="string", nullable=true)
  80. */
  81. protected $status = null;
  82. public function __construct()
  83. {
  84. $this->ipPool = new ArrayCollection;
  85. }
  86. /**
  87. * @return string
  88. */
  89. public function __toString()
  90. {
  91. return strval($this->address);
  92. }
  93. /**
  94. * @return bigint
  95. */
  96. public function getId()
  97. {
  98. return $this->id;
  99. }
  100. /**
  101. * @param string $address
  102. *
  103. * @return SubNet
  104. */
  105. public function setAddress($address)
  106. {
  107. $this->address = $address;
  108. return $this;
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function getAddress()
  114. {
  115. return $this->address;
  116. }
  117. /**
  118. * @param text $options
  119. *
  120. * @return SubNet
  121. */
  122. public function setOptions($options)
  123. {
  124. $this->options = $options;
  125. return $this;
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getOptions()
  131. {
  132. return $this->options;
  133. }
  134. /**
  135. * @param HostType $allowedHostType
  136. *
  137. * @return SubNet
  138. */
  139. public function setAllowedHostType(HostType $allowedHostType)
  140. {
  141. $this->allowedHostType = $allowedHostType;
  142. return $this;
  143. }
  144. /**
  145. * @return HostType
  146. */
  147. public function getAllowedHostType()
  148. {
  149. return $this->allowedHostType;
  150. }
  151. /**
  152. * @param NetGroup $netGroup
  153. *
  154. * @return SubNet
  155. */
  156. public function setNetGroup(NetGroup $netGroup = null)
  157. {
  158. $this->netGroup = $netGroup;
  159. return $this;
  160. }
  161. /**
  162. * @return NetGroup
  163. */
  164. public function getNetGroup()
  165. {
  166. return $this->netGroup;
  167. }
  168. /**
  169. * @param Pool $ipPool
  170. *
  171. * @return SubNet
  172. */
  173. public function addIpPool(Pool $ipPool)
  174. {
  175. $ipPool->setSubNet($this);
  176. $this->ipPool[] = $ipPool;
  177. return $this;
  178. }
  179. /**
  180. * @param Pool $ipPool
  181. *
  182. * @return SubNet
  183. */
  184. public function removeIpPool(Pool $ipPool)
  185. {
  186. $ipPool->setSubNet(null);
  187. $this->ipPool->removeElement($ipPool);
  188. return $this;
  189. }
  190. /**
  191. * @return Doctrine\Common\Collections\Collection
  192. */
  193. public function getIpPool()
  194. {
  195. return $this->ipPool;
  196. }
  197. /**
  198. * @param Doctrine\Common\Collections\Collection $ipPool
  199. *
  200. * @return SubNet
  201. */
  202. public function setIpPool($ipPool)
  203. {
  204. $this->ipPool = $ipPool;
  205. return $this;
  206. }
  207. /**
  208. * @return string
  209. */
  210. public function getSubnetName()
  211. {
  212. if ($this->getAllowedHostType() && $this->getNetGroup()) {
  213. return "{$this->address} ({$this->getAllowedHostType()->getShortName()}) - {$this->getNetGroup()->getName()}";
  214. } else {
  215. return $this->address;
  216. }
  217. }
  218. /**
  219. * @param string $status
  220. *
  221. * @return SubNet
  222. */
  223. public function setStatus($status = null)
  224. {
  225. $this->status = $status;
  226. return $this;
  227. }
  228. /**
  229. * @return string
  230. */
  231. public function getStatus()
  232. {
  233. return $this->status;
  234. }
  235. /**
  236. * @param ExecutionContextInterface $context
  237. */
  238. public function validateAddress(ExecutionContextInterface $context)
  239. {
  240. $address_pieces = explode('/', $this->address);
  241. if (!(count($address_pieces) == 2 && filter_var($address_pieces[0], FILTER_VALIDATE_IP) &&
  242. (filter_var($address_pieces[1], FILTER_VALIDATE_IP) || (is_numeric($address_pieces[1]) &&
  243. 0 <= $address_pieces[1] && $address_pieces[1] <= 32)))) {
  244. $context->addViolation('subnet.address.error');
  245. }
  246. }
  247. }