PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/circuits/models/reservation_info.php

https://github.com/jplante815/meican
PHP | 382 lines | 274 code | 64 blank | 44 comment | 55 complexity | 7f9bac57d6bf3c245f3e711dbd13359e MD5 | raw file
  1. <?php
  2. include_once 'libs/Model/resource_model.php';
  3. include_once 'apps/circuits/models/gri_info.php';
  4. include_once 'apps/circuits/models/flow_info.php';
  5. include_once 'apps/circuits/models/timer_info.php';
  6. include_once 'apps/circuits/models/oscars_reservation.php';
  7. include_once 'apps/circuits/models/OSCARSVersionTester.php';
  8. include_once 'apps/bpm/models/request_info.php';
  9. class reservation_info extends Resource_Model {
  10. var $displayField = "res_name";
  11. public function reservation_info() {
  12. $this->setTableName("reservation_info");
  13. // Add all table attributes
  14. $this->addAttribute("res_id", "INTEGER", true, false, false);
  15. $this->addAttribute("res_name", "VARCHAR");
  16. $this->addAttribute("bandwidth", "INTEGER");
  17. $this->addAttribute("flw_id", "INTEGER");
  18. $this->addAttribute("tmr_id", "INTEGER");
  19. $this->addAttribute("usr_id", "INTEGER");
  20. $this->addAttribute("creation_time", "VARCHAR");
  21. }
  22. public function getStatus() {
  23. if (!$this->res_id)
  24. return FALSE;
  25. $gri = new gri_info();
  26. $gri->res_id = $this->res_id;
  27. $gris = $gri->fetch(FALSE);
  28. //Log::write('debug', "get status:\n" . print_r($gris,true));
  29. $req = new request_info();
  30. $req->resource_id = $this->res_id;
  31. $req->resource_type = 'reservation_info';
  32. $req->answerable = 'no';
  33. $request = $req->fetch(false);
  34. $status_obj = new stdClass();
  35. if ($gris) {
  36. if ($request && $request[0]->response != 'accept') {
  37. // show request status
  38. if ($request[0]->response == 'reject') {
  39. // reservation request was denied
  40. $status_obj->status = gri_info::translateStatus('REJECTED');
  41. $status_obj->original_status = 'REJECTED';
  42. } else {
  43. // reservation request is pending
  44. $status = ($request[0]->status) ? $request[0]->status : "UNKNOWN";
  45. $status_obj->status = gri_info::translateStatus($status);
  46. $status_obj->original_status = "REQ_PENDING";
  47. }
  48. } else {
  49. // show GRI status
  50. $now = time();
  51. $gri_to_show = $gris[0];
  52. foreach ($gris as $g) {
  53. // executa lógica para determinar qual status mostrar na página
  54. $date_tmp = new DateTime($g->start);
  55. $start = $date_tmp->getTimestamp();
  56. $date_tmp = new DateTime($g->finish);
  57. $finish = $date_tmp->getTimestamp();
  58. $date_tmp = new DateTime($gri_to_show->start);
  59. $to_show_start = $date_tmp->getTimestamp();
  60. $date_tmp = new DateTime($gri_to_show->finish);
  61. $to_show_finish = $date_tmp->getTimestamp();
  62. // se tempo atual for antes do GRI, calcula a diferença, senão deixa FALSE
  63. $start_diff = ($now < $start) ? $start - $now : FALSE;
  64. $start_show_diff = ($now < $to_show_start) ? $to_show_start - $now : FALSE;
  65. // se tempo atual for depois do GRI, calcula a diferença, senão deixa FALSE
  66. $finish_diff = ($now > $finish) ? $now - $finish : FALSE;
  67. $finish_show_diff = ($now > $to_show_finish) ? $now - $to_show_finish : FALSE;
  68. if ($now >= $start && $now <= $finish) {
  69. // período do GRI é o tempo atual: encerra iteração, pois sempre será um só
  70. $gri_to_show = $g;
  71. break;
  72. } elseif (($start_diff !== FALSE) && ($start_show_diff !== FALSE) && ($start_diff < $start_show_diff)) {
  73. // tempo está antes e GRI é o próximo
  74. $gri_to_show = $g;
  75. } elseif (($finish_diff !== FALSE) && ($finish_show_diff !== FALSE) && ($finish_diff < $finish_show_diff)) {
  76. // tempo já passou e GRI foi o mais recente
  77. $gri_to_show = $g;
  78. } elseif (($start_diff !== FALSE) && ($finish_show_diff !== FALSE)) {
  79. // tempo está entre o tempo dos GRIs, mostra o próximo
  80. $gri_to_show = $g;
  81. }
  82. }
  83. $status_obj->status = gri_info::translateStatus($gri_to_show->status);
  84. $status_obj->original_status = $gri_to_show->status;
  85. }
  86. } else {
  87. $status = "NO_GRI";
  88. $status_obj->status = gri_info::translateStatus($status);
  89. $status_obj->original_status = $status;
  90. }
  91. return $status_obj;
  92. }
  93. /**
  94. *
  95. * @param Array $resIdArray An array containing reservation IDs to filter by status
  96. * @return Array An array containing all reservation objects that user has permission to view
  97. */
  98. public function getReservationsToShow($resIdArray=array()) {
  99. /**
  100. * reservations that user has permission to read, e.g., enginner can read reservations from his domain
  101. */
  102. $res_info = new reservation_info();
  103. if ($resIdArray)
  104. $res_info->res_id = $resIdArray;
  105. $topologyReservations = $res_info->fetch();
  106. /**
  107. * initialize the array containing the reservations to show
  108. */
  109. $resevartionsToShow = $topologyReservations;
  110. /**
  111. * reservations that the user has requested
  112. */
  113. $ures_info = new reservation_info();
  114. $ures_info->usr_id = AuthSystem::getUserId();
  115. if ($userReservations = $ures_info->fetch(FALSE)) {
  116. if (empty($topologyReservations)) {
  117. // user has only self-requested reservations
  118. $resevartionsToShow = $userReservations;
  119. } else {
  120. // user has both type of reservations, merge them
  121. $topResIdArray = Common::arrayExtractAttr($topologyReservations, "res_id");
  122. foreach ($userReservations as $res) {
  123. if (array_search($res->res_id, $topResIdArray) === FALSE) {
  124. array_push($resevartionsToShow, $res);
  125. }
  126. }
  127. }
  128. }
  129. return $resevartionsToShow;
  130. }
  131. public function getReservationDetails() {
  132. $res = $this->fetch(FALSE);
  133. if (!$res) {
  134. debug('reservation not found');
  135. return FALSE;
  136. }
  137. $flow = new flow_info();
  138. $flow->flw_id = $res[0]->flw_id;
  139. $flow = $flow->fetch(FALSE);
  140. $flow_info = $flow[0]->getFlowDetails();
  141. $timer = new timer_info();
  142. $timer->tmr_id = $res[0]->tmr_id;
  143. $tmp = $timer->fetch();
  144. $timer_info = $tmp[0];
  145. $user = new user_info();
  146. $user->usr_id = $res[0]->usr_id;
  147. $tmp = $user->fetch();
  148. $user_info = $tmp[0];
  149. $return = array(
  150. 'res_id' => $this->res_id,
  151. 'res_name' => $res[0]->res_name,
  152. 'dom_src' => $flow_info->source->idc_url,
  153. 'urn_src' => $flow_info->source->urn,
  154. 'dom_dst' => $flow_info->dest->idc_url,
  155. 'urn_dst' => $flow_info->dest->urn,
  156. 'bandwidth' => $res[0]->bandwidth,
  157. 'path' => $flow_info->path,
  158. 'timer_begin' => $timer_info->begin_timestamp,
  159. 'timer_end' => $timer_info->end_timestamp,
  160. 'timer_rec' => $timer_info->summary,
  161. 'usr_login' => $user_info->usr_login
  162. );
  163. $object = (object) $return;
  164. return $object;
  165. }
  166. public function sendForAuthorization() {
  167. //cria nova request
  168. $newReq = new request_info();
  169. $newReq->setDom('dom_src', Configure::read('domIp'));
  170. $newReq->req_id = $newReq->getNextId('req_id');
  171. //para buscar o dom_dst_ip
  172. $flow = new flow_info();
  173. $flow->flw_id = $this->flw_id;
  174. $res_flow = $flow->getFlowDetails2();
  175. $newReq->dom_dst = $res_flow->dst_dom_id;
  176. $newReq->usr_src = AuthSystem::getUserId();
  177. //acho q nao vai precisar possuir res_id na tabela de requests
  178. //$newReq->res_id = $this->res_id;
  179. $newReq->resource_type = 'reservation_info';
  180. $newReq->resource_id = $this->res_id;
  181. $newReq->answerable = 'no';
  182. debug('dentro do sendauth');
  183. $endpoint = Configure::read('odeWSDLToRequest');
  184. //$endpoint = "http://".odeip."/ode/deployment/bundles/v1_workflow_pietro/processes/v1_workflow_pietro/processes.ode/diagrama-v1-workflow_pietro.wsdl";
  185. if ($client = new SoapClient($endpoint, array('cache_wsdl' => 0))) {
  186. $requestSOAP = array(
  187. 'req_id' => $newReq->req_id,
  188. 'dom_src_ip' => $res_flow->src_dom_ip,
  189. 'dom_dst_ip' => $res_flow->dst_dom_ip,
  190. 'usr_src' => $newReq->usr_src);
  191. debug('ira enviar para autorizaçao...', $requestSOAP);
  192. if ($result = $client->Start_v1_workflow_felipe($requestSOAP)) {
  193. $newReq->status = 'SENT FOR AUTHORIZATION';
  194. //insere a requisicao no banco local
  195. if ($req = $newReq->insert())
  196. //atualiza tabela da reserva com o id da requisicao
  197. return TRUE;
  198. else {
  199. debug('failed to insert request at local database');
  200. return FALSE;
  201. }
  202. } else {
  203. debug('cant call soap operation at ',$endpoint);
  204. return FALSE;
  205. }
  206. } else {
  207. debug('cant call soap client at ',$endpoint);
  208. return FALSE;
  209. }
  210. }
  211. public function getPath() {
  212. $flow_info = new flow_info();
  213. if ($this->flw_id)
  214. $flow_info->flw_id = $this->flw_id;
  215. else {
  216. if (!$this->res_id)
  217. return FALSE;
  218. $flow_info->flw_id = $this->get('flw_id',false);
  219. }
  220. $path = null;
  221. if (isset($flow_info->flw_id))
  222. $path = $flow_info->get('path', false);
  223. $pathArray = array();
  224. if ($path) {
  225. //Log::write("circuits", "Path found in local database...");
  226. $pathArray = explode(';', $path);
  227. } else {
  228. if (!$this->res_id) {
  229. if ($this->flw_id) {
  230. $res_tmp = new reservation_info();
  231. $res_tmp->flw_id = $this->flw_id;
  232. $this->res_id = $res_tmp->get('res_id',false);
  233. } else
  234. return FALSE;
  235. }
  236. Log::write("circuits", "Path not found, trying OSCARS...");
  237. $gri_info = new gri_info();
  238. $gri_info->res_id = $this->res_id;
  239. $gri = $gri_info->fetch(FALSE);
  240. $domain_info = new domain_info();
  241. $domain_info->dom_id = $gri[0]->dom_id;
  242. $domain = $domain_info->fetch(FALSE);
  243. //$oscars = new OSCARSReservation(); // OLD DESIGN
  244. $versTest = new OSCARSVersionTester($domain_info->getDomVersion()); // Added by Jeremy
  245. $oscars = $versTest->checkVersion(); // NEW DESIGN -- Added by Jeremy
  246. $oscars->setGri($gri[0]->gri_descr);
  247. $oscars->setOscarsUrl($domain[0]->idc_url);
  248. $response = FALSE;
  249. $cont = 0;
  250. Log::write("circuits", "Getting GRI path:\n" . print_r(array("OSCARS URL" => $domain[0]->idc_url, "GRI" => $gri[0]->gri_descr), TRUE));
  251. while (!$response && $cont < 15) {
  252. if ($oscars->queryReservation()) {
  253. $status = $oscars->getStatus();
  254. if (($status == "PENDING") || ($status == "ACTIVE") || ($status == "FINISHED") || ($status == "FAILED") || ($status == "CANCELLED")){
  255. if ($pathArray = explode(";", $oscars->getPath())) {
  256. $pathArray = array_filter($pathArray, 'strlen');
  257. Log::write("circuits", "Get path sucessful. Complete path:\n" . print_r($pathArray, TRUE));
  258. $response = TRUE;
  259. }
  260. } elseif (($status == "INCREATE") || ($status == "ACCEPTED")) {
  261. sleep(2);
  262. $cont++;
  263. continue;
  264. //} elseif (($status == "FAILED") || ($status == "CANCELLED")) {
  265. // } elseif ($status == "CANCELLED") {
  266. // Log::write("error", "Couldn't get path. Reservation FAILED or CANCELLED");
  267. // $pathArray = FALSE;
  268. // $response = TRUE;
  269. // }
  270. }
  271. } else {
  272. Log::write("error", "Couldn't get path. Query reservation failed");
  273. $pathArray = null;
  274. $response = true;
  275. }
  276. }
  277. $pathString = implode(';', $pathArray);
  278. $flow_info->updateTo(array('path' => $pathString), false);
  279. }
  280. return $pathArray;
  281. }
  282. static public function getAvailableBandwidth($res_id, $gri_id = null) {
  283. $available_bands = array();
  284. if ($res_id) {
  285. $gri_info = new gri_info();
  286. $gri_info->res_id = $res_id;
  287. if ($gri_id)
  288. $gri_info->gri_id = $gri_id;
  289. if ($gris = $gri_info->fetch(false)) {
  290. foreach ($gris as $gri) {
  291. $capacity = 1000;
  292. $linkUtilization = 0;
  293. $conflicted = gri_info::getConflictedGris($gri->start, $gri->finish, $res_id);
  294. foreach ($conflicted as $cg) {
  295. $linkUtilization += $cg->bandwidth;
  296. }
  297. $available_bands[] = $capacity - $linkUtilization;
  298. // Log::write("debug", "Start: " . print_r($gri->start, true));
  299. // Log::write("debug", "Finish: " . print_r($gri->finish, true));
  300. // Log::write("debug", "Available Band: " . print_r($capacity, true) . " - " . print_r($linkUtilization, true));
  301. }
  302. }
  303. }
  304. return $available_bands;
  305. }
  306. }
  307. ?>