SubNet.php 6.6 KB

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