123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace IPv4Bundle\Entity;
- use Base\AdminBundle\Traits\TenancyIdTrait;
- use Base\AdminBundle\Traits\TenancyIdTraitInterface;
- use Doctrine\ORM\Mapping as ORM;
- use Doctrine\Common\Collections\ArrayCollection;
- use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Component\Validator\Context\ExecutionContextInterface;
- use HostBundle\Traits\DHCPOptionTrait;
- use HostBundle\Entity\HostType;
- use JMS\Serializer\Annotation as JMS;
- use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
- use WorkflowBundle\Entity\Traits\WorkflowTrait;
- /**
- * @ORM\Entity(repositoryClass="IPv4Bundle\Repository\SubNetRepository")
- *
- * @UniqueEntity("address")
- *
- * @Assert\Callback("validateAddress")
- *
- * @ORM\HasLifecycleCallbacks
- */
- class SubNet implements TenancyIdTraitInterface, WorkflowInterface
- {
- use TenancyIdTrait;
- use DHCPOptionTrait;
- use WorkflowTrait;
- /**
- * @ORM\Column(name="id", type="bigint", nullable=false)
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="IDENTITY")
- */
- protected $id;
- /**
- * @var string $address
- *
- * @ORM\Column(type="string", length=100, unique=true)
- *
- * @Assert\Regex(pattern="/^[ A-Za-z0-9_\-ñÑ:.\/]+$/")
- * @Assert\NotBlank
- */
- protected $address;
- /**
- * @var HostType $allowedHostType
- *
- * @ORM\ManyToOne(targetEntity="HostBundle\Entity\HostType")
- * @ORM\JoinColumn(onDelete="CASCADE")
- *
- * @Assert\NotNull
- */
- protected $allowedHostType;
- /**
- * @var string $options
- *
- * @ORM\Column(type="text", nullable=true)
- */
- protected $options;
- /**
- * @var NetGroup $netGroup
- *
- * @ORM\ManyToOne(targetEntity="NetGroup", inversedBy="subNets")
- * @ORM\JoinColumn(onDelete="CASCADE")
- *
- * @Assert\NotNull
- */
- protected $netGroup;
- /**
- * @var Pool $ipPool
- *
- * @ORM\OneToMany(targetEntity="Pool", mappedBy="subNet", cascade="persist")
- */
- protected $ipPool;
- /**
- * @ORM\ManyToOne(targetEntity="\WorkflowBundle\Entity\Workflow", fetch="EXTRA_LAZY")
- * @ORM\JoinColumn(name="workflow_id", referencedColumnName="id", onDelete="SET NULL")
- *
- * @JMS\MaxDepth(1)
- */
- protected $workflow;
- /**
- * @ORM\Column(type="string", nullable=true)
- */
- protected $currentState = null;
- /**
- * @ORM\Column(type="array")
- *
- * @Assert\All({
- * @Assert\NotBlank
- * })
- * @Assert\Count(
- * min = 1,
- * minMessage = "You must specify at least one status"
- * )
- */
- protected $status = [ \HostBundle\Utils\HostStatus::STATE_ACTIVE ];
- public function __construct()
- {
- $this->ipPool = new ArrayCollection;
- }
- /**
- * @return string
- */
- public function __toString()
- {
- return strval($this->address);
- }
- /**
- * @return bigint
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @param string $address
- *
- * @return SubNet
- */
- public function setAddress($address)
- {
- $this->address = $address;
- return $this;
- }
- /**
- * @return string
- */
- public function getAddress()
- {
- return $this->address;
- }
- /**
- * @param text $options
- *
- * @return SubNet
- */
- public function setOptions($options)
- {
- $this->options = $options;
- return $this;
- }
- /**
- * @return string
- */
- public function getOptions()
- {
- return $this->options;
- }
- /**
- * @param HostType $allowedHostType
- *
- * @return SubNet
- */
- public function setAllowedHostType(HostType $allowedHostType)
- {
- $this->allowedHostType = $allowedHostType;
- return $this;
- }
- /**
- * @return HostType
- */
- public function getAllowedHostType()
- {
- return $this->allowedHostType;
- }
- /**
- * @param NetGroup $netGroup
- *
- * @return SubNet
- */
- public function setNetGroup(NetGroup $netGroup)
- {
- $this->netGroup = $netGroup;
- return $this;
- }
- /**
- * @return NetGroup
- */
- public function getNetGroup()
- {
- return $this->netGroup;
- }
- /**
- * @param Pool $ipPool
- *
- * @return SubNet
- */
- public function addIpPool(Pool $ipPool)
- {
- $ipPool->setSubNet($this);
- $this->ipPool[] = $ipPool;
- return $this;
- }
- /**
- * @param Pool $ipPool
- *
- * @return SubNet
- */
- public function removeIpPool(Pool $ipPool)
- {
- $ipPool->setSubNet(null);
- $this->ipPool->removeElement($ipPool);
- return $this;
- }
- /**
- * @return Doctrine\Common\Collections\Collection
- */
- public function getIpPool()
- {
- return $this->ipPool;
- }
- /**
- * @param Doctrine\Common\Collections\Collection $ipPool
- *
- * @return SubNet
- */
- public function setIpPool($ipPool)
- {
- $this->ipPool = $ipPool;
- return $this;
- }
- /**
- * @return string
- */
- public function getSubnetName()
- {
- if ($this->getAllowedHostType() && $this->getNetGroup()) {
- return "{$this->address} ({$this->getAllowedHostType()->getShortName()}) - {$this->getNetGroup()->getName()}";
- } else {
- return $this->address;
- }
- }
- /**
- * @param array $status
- *
- * @return SubNet
- */
- public function setStatus($status)
- {
- $statuses = array_values(\HostBundle\Utils\HostStatus::getConstants());
- $this->status = [];
- foreach ($statuses as $key => $s) {
- if (in_array($s, $status)) {
- $k = array_search($s, $status);
- $this->status[$key] = $status[$k];
- }
- }
- return $this;
- }
- /**
- * @return array
- */
- public function getStatus()
- {
- return $this->status;
- }
- /**
- * @return string
- */
- public function getStatusString()
- {
- return implode(',', array_values($this->status));
- }
- /**
- * @param ExecutionContextInterface $context
- */
- public function validateAddress(ExecutionContextInterface $context)
- {
- $address_pieces = explode('/', $this->address);
- if (!(count($address_pieces) == 2 && filter_var($address_pieces[0], FILTER_VALIDATE_IP) &&
- (filter_var($address_pieces[1], FILTER_VALIDATE_IP) || (is_numeric($address_pieces[1]) &&
- 0 <= $address_pieces[1] && $address_pieces[1] <= 32)))) {
- $context->addViolation('subnet.address.error');
- }
- }
- }
|