示例#1
0
 public function getPort()
 {
     return Port::findByUrn($this->port_urn);
 }
示例#2
0
 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]);
     }
 }