SubNet.php 5.9 KB

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