Beispiel #1
0
 public function searchByDomains($params, $domains)
 {
     $validDomains = [];
     $this->load($params);
     if ($this->domain_name) {
         $domain = Domain::findOne(['name' => $this->domain_name]);
         $networks = Network::find()->where(['domain_id' => $domain->id]);
     } else {
         foreach ($domains as $domain) {
             $validDomains[] = $domain->id;
         }
         $networks = Network::find()->where(['in', 'domain_id', $validDomains]);
     }
     $dataProvider = new ActiveDataProvider(['query' => $networks, 'sort' => false, 'pagination' => ['pageSize' => 15]]);
     return $dataProvider;
 }
Beispiel #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getNetwork()
 {
     return $this->hasOne(Network::className(), ['id' => 'network_id']);
 }
 private function importDevices($devicesArray, $domainName, $invalidDevices, $netUrn = null)
 {
     $validDevices = [];
     //VERIFICA PORTAS
     $validBiPorts = [];
     if ($this->parser instanceof NMWGParser) {
         $type = Port::TYPE_NMWG;
         //$invalidBiPorts = Port::find()->where(['type'=>$type]);
         $invalidBiPorts = false;
     } else {
         $type = Port::TYPE_NSI;
         $net = Network::findByUrn($netUrn)->one();
         if ($net) {
             $invalidBiPorts = Port::find()->where(['type' => $type, 'directionality' => Port::DIR_BI, 'network_id' => $net->id]);
         } else {
             $invalidBiPorts = false;
         }
     }
     foreach ($devicesArray as $deviceNode => $deviceData) {
         $device = Device::findOneByDomainAndNode($domainName, $deviceNode);
         if (!$device) {
             $change = $this->buildChange();
             $change->type = Change::TYPE_CREATE;
             $change->domain = $domainName;
             $change->item_type = Change::ITEM_TYPE_DEVICE;
             $change->data = json_encode(['node' => $deviceNode, 'lat' => isset($deviceData["lat"]) ? $deviceData['lat'] : null, 'lng' => isset($deviceData["lng"]) ? $deviceData['lng'] : null, 'address' => isset($deviceData["address"]) ? $deviceData['address'] : null]);
             $change->save();
         } else {
             $validDevices[] = $device->id;
             if (isset($deviceData['lat']) && isset($deviceData['lng']) && (intval($device->latitude) != intval($deviceData['lat']) || intval($device->longitude) != intval($deviceData['lng']))) {
                 $change = $this->buildChange();
                 $change->type = Change::TYPE_UPDATE;
                 $change->domain = $domainName;
                 $change->item_type = Change::ITEM_TYPE_DEVICE;
                 $change->item_id = $device->id;
                 $change->data = json_encode(['node' => $deviceNode, 'lat' => $deviceData['lat'], 'lng' => $deviceData['lng'], 'address' => $deviceData['address']]);
                 $change->save();
             }
         }
         foreach ($deviceData["biports"] as $urnString => $portData) {
             $port = Port::findByUrn($urnString)->one();
             if (!$port) {
                 $change = $this->buildChange();
                 $change->type = Change::TYPE_CREATE;
                 $change->domain = $domainName;
                 $change->item_type = Change::ITEM_TYPE_BIPORT;
                 $change->data = json_encode(['netUrn' => $netUrn, 'node' => $deviceNode, 'urn' => $urnString, 'type' => $type, 'name' => $portData["port"], 'cap_max' => isset($portData["capMax"]) ? $portData["capMax"] : null, 'cap_min' => isset($portData["capMin"]) ? $portData["capMin"] : null, 'granu' => isset($portData["granu"]) ? $portData["granu"] : null, 'vlan' => isset($portData["vlan"]) ? $portData["vlan"] : null]);
                 $change->save();
             } else {
                 $validBiPorts[] = $port->id;
             }
             if ($this->parser instanceof NMWGParser) {
                 //PERFSONAR
                 $srcPort = Port::findByUrn($urnString)->one();
                 if (isset($portData['aliasUrn'])) {
                     $dstPort = Port::findByUrn($portData['aliasUrn'])->one();
                     if (!$dstPort || !$srcPort || $srcPort->alias_id != $dstPort->id) {
                         $change = $this->buildChange();
                         $change->domain = $domainName;
                         $change->type = $srcPort ? $srcPort->alias_id ? Change::TYPE_UPDATE : Change::TYPE_CREATE : Change::TYPE_CREATE;
                         $change->item_type = Change::ITEM_TYPE_LINK;
                         $change->data = json_encode(['node' => $deviceNode, 'port' => $portData["port"], 'urn' => $urnString, 'dst_urn' => $portData['aliasUrn']]);
                         $change->save();
                     }
                 } elseif ($srcPort && $srcPort->alias_id) {
                     $change = $this->buildChange();
                     $change->type = Change::TYPE_DELETE;
                     $change->domain = $domainName;
                     $change->item_type = Change::ITEM_TYPE_LINK;
                     $change->item_id = $srcPort->id;
                     $change->data = json_encode(['' => '']);
                     $change->save();
                 }
             } else {
                 foreach ($portData["uniports"] as $uniPortUrn => $uniPortData) {
                     $uniport = Port::findByUrn($uniPortUrn)->one();
                     if (!$uniport) {
                         $change = $this->buildChange();
                         $change->type = Change::TYPE_CREATE;
                         $change->domain = $domainName;
                         $change->item_type = Change::ITEM_TYPE_UNIPORT;
                         $change->data = json_encode(['netUrn' => $netUrn, 'node' => $deviceNode, 'type' => Port::TYPE_NSI, 'dir' => $uniPortData['type'], 'urn' => $uniPortUrn, 'biPortUrn' => $urnString, 'biPort' => $portData["port"], 'name' => $uniPortData["port"], 'cap_max' => isset($uniPortData["capMax"]) ? $uniPortData["capMax"] : null, 'cap_min' => isset($uniPortData["capMin"]) ? $uniPortData["capMin"] : null, 'granu' => isset($uniPortData["granu"]) ? $uniPortData["granu"] : null, 'vlan' => isset($uniPortData["vlan"]) ? $uniPortData["vlan"] : null]);
                         $change->save();
                     } else {
                         //update port
                         if (isset($uniPortData["vlan"]) && $uniport->vlan_range != $uniPortData['vlan']) {
                             $change = $this->buildChange();
                             $change->type = Change::TYPE_UPDATE;
                             $change->domain = $domainName;
                             $change->item_type = Change::ITEM_TYPE_UNIPORT;
                             $change->item_id = $uniport->id;
                             $change->data = json_encode(['vlan' => $uniPortData["vlan"]]);
                             $change->save();
                         }
                     }
                     if (isset($uniPortData['aliasUrn'])) {
                         $dstPort = Port::findByUrn($uniPortData['aliasUrn'])->one();
                         if (!$dstPort || !$uniport || $uniport->alias_id != $dstPort->id) {
                             $change = $this->buildChange();
                             $change->type = $uniport ? $uniport->alias_id ? Change::TYPE_UPDATE : Change::TYPE_CREATE : Change::TYPE_CREATE;
                             $change->domain = $domainName;
                             $change->item_type = Change::ITEM_TYPE_LINK;
                             $change->data = json_encode(['node' => $deviceNode, 'port' => $uniPortData["port"], 'urn' => $uniPortUrn, 'dst_urn' => $uniPortData['aliasUrn']]);
                             $change->save();
                         }
                     } elseif ($uniport && $uniport->alias_id) {
                         $change = $this->buildChange();
                         $change->type = Change::TYPE_DELETE;
                         $change->domain = $domainName;
                         $change->item_type = Change::ITEM_TYPE_LINK;
                         $change->item_id = $uniport->id;
                         $change->data = json_encode(['' => '']);
                         $change->save();
                     }
                 }
             }
         }
     }
     if ($invalidBiPorts) {
         $invalidBiPorts = $invalidBiPorts->andWhere(['not in', 'id', $validBiPorts])->all();
         foreach ($invalidBiPorts as $port) {
             $change = $this->buildChange();
             $change->type = Change::TYPE_DELETE;
             $change->domain = $domainName;
             $change->item_type = Change::ITEM_TYPE_BIPORT;
             $change->item_id = $port->id;
             $change->data = json_encode(["node" => $port]);
             $change->save();
         }
     }
     if ($invalidDevices) {
         $invalidDevices->andWhere(['not in', 'id', $validDevices]);
     }
 }
 public function actionGetByNetwork($id)
 {
     $net = Network::findOne($id);
     $data = $net->getDomain()->toArray(['id', 'name']);
     $temp = Json::encode($data);
     Yii::trace($temp);
     return $temp;
 }
 public function actionGet($id)
 {
     $data = Network::findOne($id);
     $temp = Json::encode($data);
     Yii::trace($temp);
     return $temp;
 }
Beispiel #6
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getNetworks()
 {
     return $this->hasMany(Network::className(), ['domain_id' => 'id']);
 }
Beispiel #7
0
 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;
 }