public function actionCreate() { if (!self::can('domainTopology/create')) { if (!self::can("domainTopology/read")) { return $this->goHome(); } else { Yii::$app->getSession()->addFlash('warning', Yii::t('topology', 'You are not allowed to add devices')); return $this->redirect(array('index')); } } $device = new Device(); if ($device->load($_POST)) { if ($device->save()) { Yii::$app->getSession()->addFlash("success", Yii::t('topology', 'Device {name} added successfully', ['name' => $device->name])); return $this->redirect(array('index')); } else { foreach ($device->getErrors() as $attribute => $error) { Yii::$app->getSession()->addFlash("error", $error[0]); } $device->clearErrors(); } } return $this->render('create', ['device' => $device, 'domains' => self::whichDomainsCan('domainTopology/create')]); }
public function apply() { $data = json_decode($this->data); switch ($this->item_type) { case self::ITEM_TYPE_DOMAIN: $dom = new Domain(); $dom->name = $this->domain; $dom->color = ColorUtils::generate(); $dom->default_policy = Domain::ACCEPT_ALL; if ($dom->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } break; case self::ITEM_TYPE_PROVIDER: switch ($this->type) { case self::TYPE_CREATE: $dom = Domain::findOneByName($this->domain); if ($dom) { $prov = new Provider(); $prov->nsa = $data->nsa; $prov->name = $data->name; $prov->type = $data->type; $prov->latitude = $data->lat; $prov->longitude = $data->lng; $prov->domain_id = $dom->id; if ($prov->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Domain does not exist"; } break; case self::TYPE_UPDATE: $dom = Domain::findOneByName($this->domain); if ($dom) { $prov = Provider::findOne($this->item_id); if ($prov) { $prov->name = $data->name; $prov->type = $data->type; $prov->latitude = $data->lat; $prov->longitude = $data->lng; $prov->domain_id = $dom->id; if ($prov->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Provider does not exist"; } } else { $this->error = "Domain does not exist"; } break; case self::TYPE_DELETE: $this->error = 'Invalid action'; break; } break; case self::ITEM_TYPE_PEERING: switch ($this->type) { case self::TYPE_CREATE: $prov = Provider::findOneByNsa($data->srcNsaId); if ($prov) { $dstProv = Provider::findOneByNsa($data->dstNsaId); if ($dstProv) { $peering = new Peering(); $peering->src_id = $prov->id; $peering->dst_id = $dstProv->id; if ($peering->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Destination not found"; } } else { $this->error = "Source not found"; } break; case self::TYPE_UPDATE: $port = Port::findOneByUrn($data->urn); if ($port) { $dstPort = Port::findOneByUrn($data->dst_urn); if ($dstPort) { $port->setAlias($dstPort); if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Destination not found"; } } else { $this->error = "Source not found"; } break; case self::TYPE_DELETE: $port = Port::findOne($this->item_id); if ($port) { $port->alias_id = null; if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Source not found"; } } break; case self::ITEM_TYPE_SERVICE: switch ($this->type) { case self::TYPE_CREATE: $prov = Provider::findOneByNsa($data->provNsa); if ($prov) { $service = new Service(); $service->provider_id = $prov->id; $service->type = $data->type; $service->url = $data->url; if ($service->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Provider not found"; } break; case self::TYPE_UPDATE: $this->error = "Invalid action"; break; case self::TYPE_DELETE: $service = Service::findOne($this->item_id); if ($service) { if ($service->delete()) { $this->setApplied(); return $this->save(); } else { $this->error = "Error deleting"; } } else { $this->error = "Service not found"; } break; } break; case self::ITEM_TYPE_NETWORK: switch ($this->type) { case self::TYPE_CREATE: $dom = Domain::findOneByName($this->domain); if ($dom) { $net = new Network(); $net->latitude = $data->lat; $net->longitude = $data->lng; $net->name = $data->name; $net->urn = $data->urn; $net->domain_id = $dom->id; if ($net->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Domain not found"; } break; case self::TYPE_UPDATE: $net = Network::findOne($this->item_id); if ($net) { $net->latitude = $data->lat; $net->longitude = $data->lng; $net->name = $data->name; if ($net->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Network not found"; } break; case self::TYPE_DELETE: $this->error = "In development"; break; } break; case self::ITEM_TYPE_DEVICE: switch ($this->type) { case self::TYPE_CREATE: //NECESSARIO pq o parser nao analisa esse caso VERIFICAR /* $dev = Device::findOneByDomainAndNode($this->domain, $data->node); if ($dev) { $dev->latitude = $data->lat; $dev->longitude = $data->lng; $dev->address = $data->address; } else {*/ //} $dom = Domain::findOneByName($this->domain); if ($dom) { $dev = new Device(); $dev->domain_id = $dom->id; $dev->latitude = $data->lat; $dev->longitude = $data->lng; $dev->node = $data->node; $dev->name = $dev->node; $dev->address = $data->address; if ($dev->save()) { $this->setApplied(); return $this->save(); } else { Yii::trace($dev->getErrors()); $this->error = "Unknown"; } } else { $this->error = 'Domain not found'; } break; case self::TYPE_UPDATE: $dev = Device::findOne($this->item_id); if ($dev) { $dev->latitude = $data->lat; $dev->longitude = $data->lng; $dev->address = $data->address; if ($dev->save()) { $this->setApplied(); return $this->save(); } else { Yii::trace($dev->getErrors()); $this->error = "Unknown"; } } else { $this->error = 'Device not found'; } break; case self::TYPE_DELETE: $dev = Device::findOne($this->item_id); if ($dev) { if ($dev->delete()) { $this->setApplied(); return $this->save(); } else { $this->error = 'Fatal error. Contact the administrator.'; } } else { $this->error = 'Device not found'; } } break; case self::ITEM_TYPE_BIPORT: switch ($this->type) { case self::TYPE_CREATE: $dev = Device::findOneByDomainAndNode($this->domain, $data->node); if ($dev) { $port = new Port(); $port->type = $data->type; $port->directionality = Port::DIR_BI; $port->urn = $data->urn; $port->name = $data->name; $port->max_capacity = $data->cap_max; $port->min_capacity = $data->cap_min; $port->granularity = $data->granu; $port->vlan_range = $data->vlan; $port->device_id = $dev->id; if ($data->netUrn) { $net = Network::findByUrn($data->netUrn)->one(); if ($net) { $port->network_id = $net->id; } } if ($port->save()) { $this->setApplied(); return $this->save(); } else { Yii::trace($port->getErrors()); $this->error = "Unknown"; } } break; case self::TYPE_UPDATE: $port = Port::findOne($this->item_id); if ($port) { $port->name = $data->name; $port->max_capacity = $data->cap_max; $port->min_capacity = $data->cap_min; $port->granularity = $data->granu; $port->vlan_range = $data->vlan; if ($port->save()) { $this->setApplied(); return $this->save(); } else { Yii::trace($port->getErrors()); $this->error = "Unknown"; } } else { $this->error = "Port not found"; } break; case self::TYPE_DELETE: $port = Port::findOne($this->item_id); if ($port) { if ($port->delete()) { $this->setApplied(); return $this->save(); } else { $this->error = 'Error deleting'; } } else { $this->error = 'Port not found'; } } break; case self::ITEM_TYPE_UNIPORT: switch ($this->type) { case self::TYPE_CREATE: $dev = Device::findOneByDomainAndNode($this->domain, $data->node); $biPortUrn = Port::findOne(['urn' => $data->biPortUrn]); if ($dev && $biPortUrn) { $port = new Port(); $port->type = Port::TYPE_NSI; if ($data->dir == "IN") { $port->directionality = Port::DIR_UNI_IN; } else { $port->directionality = Port::DIR_UNI_OUT; } $port->urn = $data->urn; $port->name = $data->name; $port->max_capacity = $data->cap_max; $port->min_capacity = $data->cap_min; $port->granularity = $data->granu; $port->biport_id = $biPortUrn->id; $port->vlan_range = $data->vlan; $port->device_id = $dev->id; if ($data->netUrn) { $net = Network::findByUrn($data->netUrn)->one(); if ($net) { $port->network_id = $net->id; } } if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } break; case self::TYPE_UPDATE: $port = Port::findOne($this->item_id); if ($port) { $port->name = isset($data->name) ? $data->name : $port->name; $port->max_capacity = isset($data->cap_max) ? $data->cap_max : null; $port->min_capacity = isset($data->cap_min) ? $data->cap_min : null; $port->granularity = isset($data->granu) ? $data->granu : null; $port->vlan_range = isset($data->vlan) ? $data->vlan : null; if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Port not found"; } break; case self::TYPE_DELETE: $port = Port::findOne($this->item_id); if ($port) { if ($port->delete()) { $this->setApplied(); return $this->save(); } else { $this->error = 'Error deleting port'; } } else { $this->error = 'Port not found'; } } break; case self::ITEM_TYPE_LINK: switch ($this->type) { case self::TYPE_CREATE: $port = Port::findOneByUrn($data->urn); if ($port) { $dstPort = Port::findOneByUrn($data->dst_urn); if ($dstPort) { $port->setAlias($dstPort); if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = 'Destination port not found'; } } else { $this->error = "Source port not found"; } break; case self::TYPE_UPDATE: $port = Port::findOneByUrn($data->urn); if ($port) { $dstPort = Port::findOneByUrn($data->dst_urn); if ($dstPort) { $port->setAlias($dstPort); if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = 'Destination port not found'; } } else { $this->error = "Source port not found"; } break; case self::TYPE_DELETE: $port = Port::findOne($this->item_id); if ($port) { $port->alias_id = null; if ($port->save()) { $this->setApplied(); return $this->save(); } else { $this->error = "Unknown"; } } else { $this->error = "Source port not found"; } } break; default: break; } $this->status = self::STATUS_FAILED; $this->save(); return false; }