示例#1
0
 public function save()
 {
     if ($this->validate()) {
         $changes = [];
         $conn = Connection::findOne($this->id);
         if ($this->acceptRelease) {
             if ($conn->getStartDateTime() != DateUtils::fromLocal($this->start)) {
                 $changes['needRelease'] = true;
                 $changes['start'] = DateUtils::localToUTC($this->start);
             }
             if ($conn->bandwidth != $this->bandwidth) {
                 $changes['needRelease'] = true;
                 $changes['bandwidth'] = $this->bandwidth;
             }
         }
         if ($conn->getEndDateTime() != DateUtils::fromLocal($this->end)) {
             $changes['end'] = DateUtils::localToUTC($this->end);
         }
         if (count($changes) > 0) {
             return $conn->buildEvent(ConnectionEvent::TYPE_USER_UPDATE)->setData(json_encode($changes))->setAuthor(Yii::$app->user->getId())->save();
         }
         return false;
     }
     return false;
 }
示例#2
0
 public function save()
 {
     $this->reservation = new Reservation();
     $this->reservation->type = Reservation::TYPE_NORMAL;
     $this->reservation->name = $this->name;
     $this->reservation->date = DateUtils::now();
     $this->reservation->bandwidth = $this->bandwidth;
     $this->reservation->requester_nsa = CircuitsPreference::findOneValue(CircuitsPreference::MEICAN_NSA);
     $this->reservation->provider_nsa = CircuitsPreference::findOneValue(CircuitsPreference::CIRCUITS_DEFAULT_PROVIDER_NSA);
     $this->reservation->request_user_id = Yii::$app->user->getId();
     if ($this->reservation->save()) {
         for ($i = 0; $i < count($this->path['port']); $i++) {
             $path = new ReservationPath();
             $path->reservation_id = $this->reservation->id;
             if ($this->path['mode'][$i] == 'normal') {
                 $path->port_urn = Port::find()->where(['id' => $this->path['port'][$i]])->one()->urn;
             } else {
                 $path->port_urn = str_replace('urn:ogf:network:', '', $this->path['urn'][$i]);
             }
             $path->path_order = $i;
             $path->vlan = $this->path['vlan'][$i];
             if (!$path->save()) {
                 Yii::trace($path->getErrors());
             }
         }
         $this->reservation->createConnections($this->events);
     }
     Yii::trace($this->reservation->getErrors());
     return true;
 }
示例#3
0
 /**
  * Executa a tarefa agendada, criando um objeto do tipo
  * definido e enviando como argumento uma string $obj_data;
  */
 public function execute()
 {
     $this->last_run_at = DateUtils::now();
     $this->save();
     $obj = Yii::createObject($this->obj_class);
     if ($obj && $obj instanceof SchedulableTask) {
         $obj->execute($this->obj_data);
     } else {
         $this->delete();
     }
 }
 static function create($user_id, $msg = "", $date = null)
 {
     $not = new Notification();
     $not->user_id = $user_id;
     //Pode receber uma data por parametro, neste caso, utiliza essa data como a data da criação da notificação
     if ($date) {
         $not->date = $date;
     } else {
         $not->date = DateUtils::now();
     }
     $not->type = Notification::TYPE_TOPOLOGY;
     $not->viewed = 0;
     $not->info = json_encode($msg);
     $not->save();
 }
示例#5
0
 private static function saveCircuit($gri, $desc, $status, $start, $end, $bandwidth, $path)
 {
     $conn = Connection::findOne(['external_id' => $gri]);
     if ($conn == null) {
         $conn = new Connection();
         $conn->external_id = $gri;
         $conn->name = $desc;
         $conn->type = 'OSCARS';
         $conn->status = 'PROVISIONED';
         $conn->version = 1;
         $conn->dataplane_status = $status == 'ACTIVE' ? 'ACTIVE' : 'INACTIVE';
         $conn->auth_status = 'UNEXECUTED';
         $conn->resources_status = 'PROVISIONED';
         $conn->start = DateUtils::timestampToDB($start);
         $conn->finish = DateUtils::timestampToDB($end);
         $conn->bandwidth = $bandwidth;
         if ($conn->save()) {
             Yii::trace($path);
             for ($i = 0; $i < count($path); $i++) {
                 $point = new ConnectionPath();
                 $point->conn_id = $conn->id;
                 $point->path_order = $i;
                 $urnArray = explode(":", $path[$i]);
                 Yii::trace($urnArray);
                 $point->vlan = explode('=', $urnArray[7])[1];
                 Yii::trace($point->vlan);
                 $point->domain = explode('=', $urnArray[3])[1];
                 Yii::trace($point->domain);
                 $urnArray[7] = '';
                 $urnArray[6] = '';
                 $point->port_urn = substr(implode(':', $urnArray), 0, -2);
                 Yii::trace($point->port_urn);
                 $point->save();
             }
             OscarsService::associateNSICircuits($conn);
         } else {
             Yii::trace($conn->getErrors());
         }
     } else {
         $conn->dataplane_status = $status == 'ACTIVE' ? 'ACTIVE' : 'INACTIVE';
         $conn->save();
     }
 }
示例#6
0
 public function execute($task, $rule)
 {
     $this->task = $task;
     $this->task->started_at = DateUtils::now();
     $this->task->progress = 0;
     $this->task->sync_id = $rule->id;
     $this->task->status = DiscoveryTask::STATUS_INPROGRESS;
     $this->task->save();
     if (!$this->parser) {
         switch ($rule->type) {
             case DiscoveryRule::DESC_TYPE_NSI:
                 $this->parser = new NSIParser();
                 if (!$this->parser->loadFile($rule->url) || !$this->parser->isTD()) {
                     return $this->taskFailed();
                 }
                 $this->parser->parseTopology();
                 //Yii::trace($this->parser->getData());
                 break;
             case DiscoveryRule::DESC_TYPE_NMWG:
                 $this->parser = new NMWGParser();
                 if (!$this->parser->loadFile($rule->url) || !$this->parser->isTD()) {
                     return $this->taskFailed();
                 }
                 $this->parser->parseTopology();
                 Yii::trace($this->parser->getData());
                 break;
         }
     }
     $this->search();
     $this->task->status = DiscoveryTask::STATUS_SUCCESS;
     $this->task->save();
     if ($rule->auto_apply) {
         $this->task->applyChanges();
     }
     //if($this->detectedChanges) TopologyNotification::create(1, $this->task->id);
 }
 public function actionAnswer($id = null, $domain = null)
 {
     if ($id == null || $domain == null) {
         $this->actionAuthorization();
     } else {
         if (!Domain::findOne(['name' => $domain])) {
             $this->actionAuthorization();
         } else {
             Yii::trace("Respondendo a reserva id: " . $id);
             $userId = Yii::$app->user->getId();
             //Confere se usuário pode autorizar
             $conn = Connection::find()->where(['reservation_id' => $id])->one();
             $auth = ConnectionAuth::find()->where(['connection_id' => $conn->id, 'type' => ConnectionAuth::TYPE_GROUP])->one();
             if ($auth) {
                 $onGroup = false;
                 $roles = User::findOne($userId)->getRoles()->all();
                 foreach ($roles as $role) {
                     if (($role->domain == $auth->domain || $role->domain == null) && $role->getGroup()->id == $auth->manager_group_id) {
                         $onGroup = true;
                     }
                 }
                 if (!$onGroup) {
                     return $this->goHome();
                 }
             }
             $auth = ConnectionAuth::find()->where(['connection_id' => $conn->id, 'type' => ConnectionAuth::TYPE_USER])->one();
             if ($auth && $auth->manager_user_id != $userId) {
                 return $this->goHome();
             }
             $reservation = Reservation::findOne(['id' => $id]);
             //Confere se alguma ja expirou
             $connectionsExpired = $conn = Connection::find()->where(['reservation_id' => $reservation->id])->andWhere(['<=', 'start', DateUtils::now()])->all();
             foreach ($connectionsExpired as $connection) {
                 $requests = ConnectionAuth::find()->where(['connection_id' => $connection->id, 'status' => Connection::AUTH_STATUS_PENDING])->all();
                 foreach ($requests as $request) {
                     $request->changeStatusToExpired();
                     $connection->auth_status = Connection::AUTH_STATUS_EXPIRED;
                     $connection->save();
                     ReservationNotification::create($connection->id);
                 }
             }
             $allRequest = null;
             $connections = Connection::find()->where(['reservation_id' => $id])->all();
             foreach ($connections as $conn) {
                 if ($allRequest == null) {
                     $allRequest = ConnectionAuth::find()->where(['connection_id' => $conn->id, 'domain' => $domain]);
                 } else {
                     $allRequest->union(ConnectionAuth::find()->where(['connection_id' => $conn->id, 'domain' => $domain]));
                 }
             }
             $allRequest = $allRequest->all();
             $domainRoles = User::findOne(['id' => $userId])->getRoles()->all();
             $requests = [];
             foreach ($allRequest as $request) {
                 if ($request->manager_user_id == $userId) {
                     $requests[$request->id] = $request;
                 } else {
                     foreach ($domainRoles as $domainRule) {
                         $groupId = $domainRule->getGroup()->id;
                         if ($request->manager_group_id == $groupId) {
                             $requests[$request->id] = $request;
                         }
                     }
                 }
             }
             $events = [];
             foreach ($requests as $request) {
                 $events[] = ['id' => $request->id, 'title' => "\n" . $request->getConnection()->one()->getReservation()->one()->bandwidth . " Mbps", 'start' => Yii::$app->formatter->asDatetime($request->getConnection()->one()->start, "php:Y-m-d H:i:s"), 'end' => Yii::$app->formatter->asDatetime($request->getConnection()->one()->finish, "php:Y-m-d H:i:s")];
             }
             $info = new AuthorizationDetailed($reservation, Connection::find()->where(['reservation_id' => $id])->one()->id, $domain);
             if (sizeof($requests) <= 0) {
                 return $this->redirect('index');
             } else {
                 return $this->render('detailed', array('domain' => $domain, 'info' => $info, 'requests' => $requests, 'events' => $events, 'language' => Yii::$app->language));
             }
         }
     }
 }
示例#8
0
 public function save()
 {
     $this->reservation = new Reservation();
     $this->reservation->type = Reservation::TYPE_TEST;
     $this->reservation->name = Test::AT_PREFIX;
     $this->reservation->date = DateUtils::now();
     $this->reservation->start = DateUtils::now();
     $this->reservation->finish = DateUtils::now();
     $this->reservation->bandwidth = 10;
     $this->reservation->requester_nsa = CircuitsPreference::findOneValue(CircuitsPreference::MEICAN_NSA);
     $this->reservation->provider_nsa = CircuitsPreference::findOneValue(CircuitsPreference::CIRCUITS_DEFAULT_PROVIDER_NSA);
     $this->reservation->request_user_id = Yii::$app->user->getId();
     //Confere se usuário tem permissão para testar na origem OU no destino
     $source = Port::findOne(['id' => $this->src_port]);
     $destination = Port::findOne(['id' => $this->dst_port]);
     $permission = false;
     if ($source) {
         $source = $source->getDevice()->one();
         if ($source) {
             $domain = $source->getDomain()->one();
             if ($domain && RbacController::can('test/create', $domain->name)) {
                 $permission = true;
             }
         }
     }
     if ($destination) {
         $destination = $destination->getDevice()->one();
         if ($destination) {
             $domain = $destination->getDomain()->one();
             if ($domain && RbacController::can('test/create', $domain->name)) {
                 $permission = true;
             }
         }
     }
     if (!$permission) {
         Yii::$app->getSession()->addFlash("warning", Yii::t("circuits", "You are not allowed to create a automated test involving these selected domains"));
         return false;
     }
     if ($this->reservation->save()) {
         $this->reservation->name .= $this->reservation->id;
         $this->reservation->save();
         $path = new ReservationPath();
         $path->reservation_id = $this->reservation->id;
         $path->port_urn = Port::findOne($this->src_port)->urn;
         $path->path_order = 0;
         $path->vlan = $this->src_vlan;
         if (!$path->save()) {
             Yii::trace($path->getErrors());
             return false;
         }
         $path = new ReservationPath();
         $path->reservation_id = $this->reservation->id;
         $path->port_urn = Port::findOne($this->dst_port)->urn;
         $path->path_order = 1;
         $path->vlan = $this->dst_vlan;
         if (!$path->save()) {
             Yii::trace($path->getErrors());
             return false;
         }
         $task = new ScheduledTask();
         $task->freq = $this->cron_value;
         $task->obj_class = 'meican\\tester\\models\\Test';
         $task->obj_data = $this->reservation->id;
         if (!$task->save()) {
             Yii::trace($task->getErrors());
             return false;
         }
     } else {
         Yii::trace($this->reservation->getErrors());
         return false;
     }
     return true;
 }
 public function searchByDomains($params)
 {
     $this->load($params);
     $userId = Yii::$app->user->getId();
     $now = DateUtils::now();
     $authorizations = [];
     //Armazena os pedidos
     $reservationsVisited = [];
     //Armazena as reservas ja incluidas nos pedidos e o dominio ao qual o pedido foi feito.
     //Pega todas requisições feitas para o usuário
     if ($this->domain) {
         $userRequests = ConnectionAuth::find()->where(['domain' => $this->domain, 'manager_user_id' => $userId, 'status' => Connection::AUTH_STATUS_PENDING])->all();
     } else {
         $userRequests = ConnectionAuth::find()->where(['manager_user_id' => $userId, 'status' => Connection::AUTH_STATUS_PENDING])->all();
     }
     foreach ($userRequests as $request) {
         //Limpa mantendo apenas 1 por reserva
         $uniq = true;
         $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['<=', 'start', DateUtils::now()])->one();
         if (isset($conn)) {
             $request->changeStatusToExpired();
             $conn->auth_status = Connection::AUTH_STATUS_EXPIRED;
             $conn->save();
             ReservationNotification::create($conn->id);
         } else {
             $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['>', 'start', DateUtils::now()])->one();
             foreach ($reservationsVisited as $res) {
                 if ($conn->reservation_id == $res[0] && $request->domain == $res[1]) {
                     $uniq = false;
                 }
             }
             if ($uniq) {
                 $aux = [];
                 $aux[0] = $conn->reservation_id;
                 $aux[1] = $request->domain;
                 $reservationsVisited[] = $aux;
                 $source = $conn->getFirstPath()->one();
                 $destination = $conn->getLastPath()->one();
                 if (!$this->src_domain || $this->src_domain == $source->domain) {
                     if (!$this->dst_domain || $this->dst_domain == $destination->domain) {
                         $form = new AuthorizationForm();
                         $form->setValues(Reservation::findOne(['id' => $conn->reservation_id]), $request->domain, $source->domain, $destination->domain);
                         $authorizations[] = $form;
                     }
                 }
             }
         }
     }
     //Pega todos os papeis do usuário
     $domainRoles = User::findOne(['id' => $userId])->getRoles()->all();
     foreach ($domainRoles as $role) {
         //Passa por todos papeis
         if ($this->domain) {
             $groupRequests = ConnectionAuth::find()->where(['domain' => $this->domain, 'manager_group_id' => $role->getGroup()->id, 'status' => Connection::AUTH_STATUS_PENDING])->all();
         } else {
             $groupRequests = ConnectionAuth::find()->where(['manager_group_id' => $role->getGroup()->id, 'status' => Connection::AUTH_STATUS_PENDING])->all();
         }
         foreach ($groupRequests as $request) {
             //Passa por todas requisições para testar se o dominio corresponde
             $domain = Domain::findOne(['name' => $request->domain]);
             if ($domain) {
                 if ($role->domain == NULL || $role->domain == $domain->name) {
                     $uniq = true;
                     $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['<=', 'start', DateUtils::now()])->one();
                     if (isset($conn)) {
                         $request->changeStatusToExpired();
                         $conn->auth_status = Connection::AUTH_STATUS_EXPIRED;
                         $conn->save();
                         ReservationNotification::create($conn->id);
                     } else {
                         $conn = Connection::find()->where(['id' => $request->connection_id])->andWhere(['>', 'start', DateUtils::now()])->one();
                         foreach ($reservationsVisited as $res) {
                             if ($conn->reservation_id == $res[0] && $domain->name == $res[1]) {
                                 $uniq = false;
                             }
                         }
                         if ($uniq) {
                             $aux = [];
                             $aux[0] = $conn->reservation_id;
                             $aux[1] = $request->domain;
                             $reservationsVisited[] = $aux;
                             $source = $conn->getFirstPath()->one();
                             $destination = $conn->getLastPath()->one();
                             if (!$this->src_domain || $this->src_domain == $source->domain) {
                                 if (!$this->dst_domain || $this->dst_domain == $destination->domain) {
                                     $form = new AuthorizationForm();
                                     $form->setValues(Reservation::findOne(['id' => $conn->reservation_id]), $request->domain, $source->domain, $destination->domain);
                                     $authorizations[] = $form;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $dataProvider = new ArrayDataProvider(['allModels' => $authorizations, 'sort' => false, 'pagination' => ['pageSize' => 15]]);
     return $dataProvider;
 }
示例#10
0
 public function searchTerminatedByDomains($params, $allowed_domains)
 {
     $validDomains = [];
     $this->load($params);
     $userId = Yii::$app->user->getId();
     $domains_name = [];
     foreach ($allowed_domains as $domain) {
         $domains_name[] = $domain->name;
     }
     $connPaths = [];
     if ($this->src_domain && $this->dst_domain) {
         //if(in_array($this->src_domain, $domains_name) && in_array($this->dst_domain, $domains_name)){
         $dstPaths = ConnectionPath::findBySql("\n                    SELECT cp1.conn_id\n                    FROM (\n                        SELECT conn_id, MAX(`path_order`) AS last_path\n                        FROM `meican_connection_path`\n                        GROUP BY `conn_id`\n                        ) cp2\n                    JOIN    `meican_connection_path` cp1\n                    ON      cp1.conn_id = cp2.conn_id AND cp1.domain = '" . $this->dst_domain . "' AND cp1.path_order = cp2.last_path")->asArray()->all();
         $allowedConns = [];
         foreach ($dstPaths as $dstPath) {
             $allowedConns[] = $dstPath['conn_id'];
         }
         //filtra por conn aceitas pela query anterior
         $connPaths = ConnectionPath::find()->where(['in', 'domain', [$this->src_domain]])->andWhere(['path_order' => 0])->andWhere(['in', 'conn_id', $allowedConns])->select(["conn_id"])->distinct(true)->asArray()->all();
         //}
     } elseif ($this->src_domain) {
         //if(in_array($this->src_domain, $domains_name)){
         $connPaths = ConnectionPath::find()->where(['in', 'domain', [$this->src_domain]])->andWhere(['path_order' => 0])->select(["conn_id"])->distinct(true)->asArray()->all();
         //}
     } elseif ($this->dst_domain) {
         //if(in_array($this->dst_domain, $domains_name)){
         $connPaths = ConnectionPath::findBySql("\n                    SELECT cp1.conn_id\n                    FROM (\n                        SELECT conn_id, MAX(`path_order`) AS last_path\n                        FROM `meican_connection_path`\n                        GROUP BY `conn_id`\n                        ) cp2\n                    JOIN    `meican_connection_path` cp1\n                    ON      cp1.conn_id = cp2.conn_id AND cp1.domain = '" . $this->dst_domain . "' AND cp1.path_order = cp2.last_path")->asArray()->all();
         //}
     } else {
         foreach ($allowed_domains as $domain) {
             $validDomains[] = $domain->name;
         }
         $connPaths = ConnectionPath::find()->where(['in', 'domain', $validDomains])->select(["conn_id"])->distinct(true)->asArray()->all();
     }
     $validConnIds = [];
     foreach ($connPaths as $connPath) {
         $validConnIds[] = $connPath['conn_id'];
     }
     $validConns = Connection::find()->where(['in', 'id', $validConnIds])->select('reservation_id')->distinct(true)->asArray()->all();
     $validIds = [];
     foreach ($validConns as $conn) {
         $validIds[] = $conn['reservation_id'];
     }
     $invalidConnections = Connection::find()->where(['>=', 'finish', DateUtils::now()])->andWhere(['status' => ["PENDING", "CREATED", "CONFIRMED", "SUBMITTED", "PROVISIONED"]])->select(["reservation_id"])->distinct(true)->asArray()->all();
     $invalidIds = [];
     foreach ($invalidConnections as $conn) {
         $invalidIds[] = $conn['reservation_id'];
     }
     if (!$this->src_domain && !$this->dst_domain) {
         $reservations = Reservation::find()->where(['in', 'id', $validIds])->orWhere(['request_user_id' => Yii::$app->user->getId()])->andWhere(['not in', 'id', $invalidIds])->andWhere(['type' => self::TYPE_NORMAL])->orderBy(['date' => SORT_DESC])->asArray()->all();
     } else {
         $reservations = Reservation::find()->where(['not in', 'id', $invalidIds])->andWhere(['in', 'id', $validIds])->andWhere(['type' => self::TYPE_NORMAL])->orderBy(['date' => SORT_DESC])->asArray()->all();
     }
     $validResIds = [];
     foreach ($reservations as $res) {
         if ($res['request_user_id'] == $userId) {
             $validResIds[] = $res['id'];
         } else {
             if (!$this->request_user) {
                 $conns = Connection::find()->where(['reservation_id' => $res['id']])->select(["id"])->asArray()->all();
                 if (!empty($conns)) {
                     $conn_ids = [];
                     foreach ($conns as $conn) {
                         $conn_ids[] = $conn['id'];
                     }
                     $paths = ConnectionPath::find()->where(['in', 'domain', $domains_name])->andWhere(['in', 'conn_id', $conn_ids])->select(["conn_id"])->distinct(true)->asArray()->all();
                     if (!empty($paths)) {
                         $validResIds[] = $res['id'];
                     }
                 }
             }
         }
     }
     $dataProvider = new ActiveDataProvider(['query' => Reservation::find()->where(['in', 'id', $validResIds])->orderBy(['date' => SORT_DESC]), 'sort' => false, 'pagination' => ['pageSize' => 10]]);
     return $dataProvider;
 }
示例#11
0
 public function updateContinue()
 {
     $event = $this->conn->getUpdateEventInProgress();
     $changes = json_decode($event->data);
     $path = [];
     foreach ($this->conn->getPaths()->all() as $point) {
         $path[] = $point->getFullPortUrn() . "?vlan=" . $point->vlan;
     }
     Yii::trace($changes);
     $this->soapClient->requestReserve($this->conn->external_id, $this->conn->version + 1, isset($changes->bandwidth) ? $changes->bandwidth : $this->conn->bandwidth, isset($changes->start) ? DateUtils::fromDB($changes->start) : DateUtils::fromDB($this->conn->start), isset($changes->end) ? DateUtils::fromDB($changes->end) : DateUtils::fromDB($this->conn->finish), $path, $this->conn->getReservation()->asArray()->select(['name'])->one()['name']);
     $this->conn->buildEvent(ConnectionEvent::TYPE_NSI_RESERVE, $this->soapClient->__getLastRequest())->save();
     $this->conn->buildEvent(ConnectionEvent::TYPE_NSI_RESERVE_RESPONSE, $this->soapClient->__getLastResponse())->save();
 }
示例#12
0
 /**
  * CREATE TOPOLOGY NOTIFICAION
  * @param string $msg (A tag no caso)
  * @param string $date
  * Cria notificação de mudança na topologia. VERSÃO BETA
  */
 public static function createTopologyNotification($msg, $date = null)
 {
     $users = User::find()->all();
     foreach ($users as $user) {
         $not = Notification::findOne(['user_id' => $user->id, 'type' => self::TYPE_TOPOLOGY, 'info' => $msg]);
         if ($not) {
             //Pode receber uma data por parametro, neste caso, utiliza essa data como a data da criação da notificação
             if ($date) {
                 $not->date = $date;
             } else {
                 $not->date = DateUtils::now();
             }
             $not->viewed = 0;
             $not->save();
         } else {
             $not = new Notification();
             $not->user_id = $user->id;
             //Pode receber uma data por parametro, neste caso, utiliza essa data como a data da criação da notificação
             if ($date) {
                 $not->date = $date;
             } else {
                 $not->date = DateUtils::now();
             }
             $not->type = self::TYPE_TOPOLOGY;
             $not->viewed = 0;
             $not->info = $msg;
             $not->save();
         }
     }
 }
示例#13
0
 public function getEndDateTime()
 {
     return DateUtils::fromDB($this->finish);
 }
示例#14
0
 private function setApplied()
 {
     $this->applied_at = DateUtils::now();
     $this->status = self::STATUS_APPLIED;
     $this->error = null;
 }