change container display
[racktables-contribs] / LinkManagement / plugins / linkmgmt.php
<
CommitLineData
9276dc97 1<?php
3badc888 2// TODO linkchain cytoscape create libs?
3// linkchain all objects graph cytoscape takes ages
4// avoid create own linkchain for every port on global map
5// highlight port cytoscape maps
9276dc97 6/*
c7a681fc 7 * Link Management for RT >= 0.20.9
9276dc97 8 *
fdc74cf0 9 * Features:
10 * - create links between ports
11 * - create backend links between ports
12 * - visually display links / chains
13 * e.g.
14 * (Object)>[port] -- front --> [port]<(Object) == back == > (Object)>[port] -- front --> [port]<(Object)
15 * - Link object backend ports by name (e.g. handy for patch panels)
16ec374c 16 * - change/create CableID
17 * - change/create Port Reservation Comment
fdc74cf0 18 * - multiple backend links for supported port types (e.g. AC-in, DC)
19 * - GraphViz Maps (Objects, Ports and Links) (needs GraphViz_Image 1.3.0)
20 * - object,port or link highligthing (just click on it)
21 * - context menu to link and unlink ports
9276dc97 22 *
fdc74cf0 23 * Usage:
24 * 1. select "Link Management" tab
25 * 2. you should see link chains of all linked ports
26 * 3. to display all ports hit "Show All Ports" in the left upper corner
27 * 4. to link all ports with the same name of two different objects use "Link Object Ports by Name"
28 * a. select the other object you want to backend link to
29 * b. "show back ports" gives you the list of possible backend links
30 * !! Important port names have to be the same on both objects !!
31 * e.g. (Current Object):Portname -?-> Portname:(Selected Object)
32 * c. select all backend link to create (Ctrl + a for all)
33 * d. Enter backend CableID for all selected links
34 * e. "Link back" create the backend links
35 * 5. If you have an backend link within the same Object the link isn't shown until
36 * "Expand Backend Links on same Object" is hit
37 * 6. "Object Map" displays Graphviz Map of current object
38 * 7. To get a Graphviz Map of a single port click the port name on the left
9276dc97
ME
39 *
40 *
3badc888 41 * Changes:
42 * switch to new linkchain class
43 * - nicer looking graphviz maps
44 * - better multilink support
45 *
46 * add cytoscape.js maps
47 * - path higlighting
48 * - zooming
49 *
fdc74cf0 50 * Requirements:
51 * PHP 5 (http://php.net/)
52 * GraphViz_Image 1.3.0 or newer (http://pear.php.net/package/Image_GraphViz)
53 * GraphViz (http://www.graphviz.org/)
9276dc97 54 *
3badc888 55 * to user cytoscape js map the following is required:
383230c5 56 * create plugins/linkmgmt directory and place the following 7 files in it
3badc888 57 *
58 * Cytoscape.js (http://js.cytoscape.org/)
3badc888 59 * https://cytoscape.github.io/cytoscape.js/api/cytoscape.js-latest/cytoscape.min.js
60 * dagre.js
3badc888 61 * https://raw.githubusercontent.com/cpettitt/dagre/master/dist/dagre.min.js
62 * https://raw.githubusercontent.com/cytoscape/cytoscape.js-dagre/master/cytoscape-dagre.js
63 * qtip
3badc888 64 * https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/jquery.qtip.min.css
65 *
3badc888 66 * https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.1/jquery.qtip.min.js
67 * https://raw.githubusercontent.com/cytoscape/cytoscape.js-qtip/master/cytoscape-qtip.js
68 *
69 * jquery >= 1.10.0 (qtip requirement)
3badc888 70 * https://code.jquery.com/jquery-1.11.3.min.js
71 *
9276dc97
ME
72 * INSTALL:
73 *
fdc74cf0 74 * 1. create LinkBackend Table in your RackTables database
75 *
76 * Multilink table
9276dc97
ME
77
78CREATE TABLE `LinkBackend` (
79 `porta` int(10) unsigned NOT NULL DEFAULT '0',
80 `portb` int(10) unsigned NOT NULL DEFAULT '0',
81 `cable` char(64) DEFAULT NULL,
82 PRIMARY KEY (`porta`,`portb`),
cb15aecc 83 KEY `LinkBackend_FK_a` (`porta`),
fdc74cf0 84 KEY `LinkBackend_FK_b` (`portb`),
85 CONSTRAINT `LinkBackend_FK_a` FOREIGN KEY (`porta`) REFERENCES `Port` (`id`) ON DELETE CASCADE,
86 CONSTRAINT `LinkBackend_FK_b` FOREIGN KEY (`portb`) REFERENCES `Port` (`id`) ON DELETE CASCADE
1fb0f48b 87) ENGINE=InnoDB DEFAULT CHARSET=utf8 collate=utf8_unicode_ci;
9276dc97 88
16ec374c 89 * 2. copy linkmgmt.php to plugins directory
fdc74cf0 90 *
91 * Ready to go!
92 *
93 *
94 * UPDATE TABLE:
95 *
96 * Update from non-multilink table
97 * ALTER TABLE
98
99ALTER TABLE LinkBackend ADD KEY `LinkBackend_FK_b` (`portb`);
100ALTER TABLE LinkBackend DROP INDEX porta;
101ALTER TABLE LinkBackend DROP INDEX portb;
102
b695efd7 103 * UPDATE to RT 0.20.7
104
105ALTER TABLE LinkBackend CONVERT to CHARACTER SET utf8 COLLATE utf8_unicode_ci;
106
fdc74cf0 107 *
9276dc97
ME
108 *
109 * TESTED on FreeBSD 9.0, nginx/1.0.11, php 5.3.9
fdc74cf0 110 * GraphViz_Image 1.3.0
9276dc97 111 *
16ec374c 112 * (c)2012-2016 Maik Ehinger <m.ehinger@ltur.de>
9276dc97
ME
113 */
114
fdc74cf0 115/**
116 * The newest version of this plugin can be found at:
9276dc97 117 *
3badc888 118 * https://github.com/github138/myRT-contribs/tree/develop-0.20.x
9276dc97
ME
119 *
120 */
121
122/*************************
123 * TODO
124 *
125 * - code cleanups
126 * - bug fixing
127 *
fdc74cf0 128 * - put selected object/port top left of graph
129 * - multlink count for Graphviz maps empty or full dot
130 *
131 * - csv list
9276dc97
ME
132 *
133 */
134
fdc74cf0 135/* DEBUG */
136//error_reporting(E_ALL);
9276dc97
ME
137
138$tab['object']['linkmgmt'] = 'Link Management';
139$tabhandler['object']['linkmgmt'] = 'linkmgmt_tabhandler';
140//$trigger['object']['linkmgmt'] = 'linkmgmt_tabtrigger';
141
142$ophandler['object']['linkmgmt']['update'] = 'linkmgmt_opupdate';
143$ophandler['object']['linkmgmt']['unlinkPort'] = 'linkmgmt_opunlinkPort';
144$ophandler['object']['linkmgmt']['PortLinkDialog'] = 'linkmgmt_opPortLinkDialog';
145$ophandler['object']['linkmgmt']['Help'] = 'linkmgmt_opHelp';
146
fdc74cf0 147$ophandler['object']['linkmgmt']['map'] = 'linkmgmt_opmap';
148$ajaxhandler['lm_mapinfo'] = 'linkmgmt_ajax_mapinfo';
16ec374c 149$ajaxhandler['lm-upd-reservation-cable'] = 'linkmgmt_updateCableIdAJAX';
fdc74cf0 150
3badc888 151$ophandler['object']['linkmgmt']['cytoscapemap'] = 'linkmgmt_cytoscapemap';
152
fdc74cf0 153/* ------------------------------------------------- */
154
1e6c47e6 155define('LM_MULTILINK',TRUE);
fdc74cf0 156
157/* -------------------------------------------------- */
158
159$lm_multilink_port_types = array(
160 16, /* AC-in */
161 //1322, /* AC-out */
162 1399, /* DC */
163 );
9276dc97
ME
164
165/* -------------------------------------------------- */
166
167$lm_cache = array(
3254ec33 168 'allowcomment' => TRUE, /* RackCode {$op_set_reserve_comment} */
169 'allowlink' => TRUE, /* RackCode {$op_set_link} */
170 'allowbacklink' => TRUE, /* RackCode {$op_set_backlink} */
9276dc97
ME
171 'rackinfo' => array(),
172 );
173
174/* -------------------------------------------------- */
175
176//function linkmgmt_tabtrigger() {
177// return 'std';
178//} /* linkmgmt_tabtrigger */
179
3badc888 180/* -------------------linkchain stuff------------------------------- */
181class linkchain_cache
182{
183 public $cache = array();
9276dc97 184
3badc888 185 function getobject($object_id , &$rack = null)
186 {
187 if(!$object_id)
188 return null;
9276dc97 189
3badc888 190 if(!isset($this->cache['o'.$object_id]))
191 {
192 $object = spotEntity('object', $object_id);
193 $object['IPV4OBJ'] = considerConfiguredConstraint ($object, 'IPV4OBJ_LISTSRC');
9276dc97 194
3badc888 195 /* get more object info */
9276dc97 196
3badc888 197 if($object['IPV4OBJ'])
198 {
199 // ip addresses
200 //amplifyCell($object); /* get ports, ipv4, ipv6, nat4 and files */
201 $object['ipv4'] = getObjectIPv4Allocations ($object_id);
202 $object['portip'] = array();
203 foreach($object['ipv4'] as $ipv4)
204 {
205 $object['portip'][$ipv4['osif']] = $ipv4['addrinfo']['ip'];
206 }
207 }
9276dc97 208
3badc888 209 if($object['container_id'])
210 {
211 $container = $this->getobject($object['container_id']);
212 }
fdc74cf0 213
3badc888 214 $this->cache['o'.$object_id] = $object;
215 }
216 else
217 $object = $this->cache['o'.$object_id];
fdc74cf0 218
3badc888 219 $rack = $this->getrack($object['rack_id']);
fdc74cf0 220
3badc888 221 return $object;
222 }
fdc74cf0 223
3badc888 224 function getrack($rack_id)
225 {
226 // rack
227 if($rack_id)
228 {
229 if(!isset($this->cache['r'.$rack_id]))
230 {
231 $rack = spotEntity('rack', $rack_id);
232 $this->cache['r'.$rack_id] = $rack;
233 }
234 else
235 $rack = $this->cache['r'.$rack_id];
236 }
237 else
238 $rack = null;
fdc74cf0 239
3badc888 240 return $rack;
fdc74cf0 241
3badc888 242 }
243}
fdc74cf0 244
3badc888 245$lc_cache = new linkchain_cache();
fdc74cf0 246
3badc888 247/* recursive class */
248class lm_linkchain implements Iterator {
fdc74cf0 249
3badc888 250 const B2B_LINK_BGCOLOR = '#d8d8d8';
251 const CURRENT_PORT_BGCOLOR = '#ffff99';
252 const CURRENT_OBJECT_BGCOLOR = '#ff0000';
253 const HL_PORT_BGCOLOR = '#A0FFA0';
254 const ALTERNATE_ROW_BGCOLOR = '#f0f0f0';
255
256 public $first = null;
257 public $last = null;
258 public $init = null;
259 public $linked = null;
260 public $linkcount = 0;
261
262 public $loop = false;
263 private $lastipobjport = null;
264
265 public $ports = array();
266
267 private $currentid = null;
268 private $back = null;
269 public $initback = null;
270
271 //public $cache = null;
272 private $object_id = null;
273
274 private $icount = 0;
275 public $exceed = false;
276
277 public $initalign = true;
278
279 public $portmulti = 0;
280 public $prevportmulti = 0;
281
282 private $initport = false;
283
284 private $lids = array(); // link ids porta portb linktype
285
286 public $pids = null; // port ids
287 public $oids = null; // object ids
288
289 /* $back = null follow front and back
290 * true follow back only
291 * false follow front only
292 * $prevport first port
293 */
294 function __construct($port_id, $back = null, $prevport = null, $reverse = false, &$pids = null, &$oids = null)
fdc74cf0 295 {
3badc888 296 global $lc_cache;
fdc74cf0 297
3badc888 298 $this->init = $port_id;
fdc74cf0 299
3badc888 300 $this->initback = $back;
fdc74cf0 301
3badc888 302 if($pids === null)
303 $this->pids = array();
304 else
305 $this->pids = &$pids;
306
307 if($oids === null)
308 $this->oids = array();
309 else
310 $this->oids = &$oids;
311
312 if($back !== null)
fdc74cf0 313 {
3badc888 314 $prevport_id = $prevport['id'];
315 $prevlinktype = $this->getlinktype(!$back);
316 //if($this->setlinkid($port_id, $prevport_id, $prevlinktype))
317 {
318 $this->last = $this->_getlinks($port_id, $back, null, $reverse);
319 }
320 //else
321 {
322 // $this->loop = true;
323 // echo "SUBCHAIN LID exists $port_id $prevlinktype $prevport_id<br>";
324 }
fdc74cf0 325
3badc888 326 // TODO set previous port ..use _set... function()
327 $this->ports[$port_id][$this->getlinktype(!$back)]['portcount'] = 1;
328 $this->ports[$port_id][$this->getlinktype(!$back)]['linked'] = 1;
329 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_id'] = $prevport['id'];
330 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_object_id'] = $prevport['object_id'];
331 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_name'] = $prevport['name'];
332 $this->ports[$port_id][$this->getlinktype(!$back)]['remote_object_name'] = $prevport['object_name'];
333 $this->ports[$port_id][$this->getlinktype(!$back)]['cableid'] = $prevport['cableid'];
fdc74cf0 334
fdc74cf0 335
3badc888 336 $prevport = $this->_setportlink($prevport, $this->getlinktype(!$back));
337 $prevport[$this->getlinktype($back)]['portcount'] = null;
338 $prevport[$this->getlinktype($back)]['remote_id'] = null;
339 $prevport[$this->getlinktype($back)]['remote_object_id'] = null;
fdc74cf0 340
3badc888 341 $this->ports[$prevport_id] = $prevport;
342 $this->first = $prevport_id;
343 $this->linkcount++;
fdc74cf0 344
3badc888 345 // self::var_dump_html($this->ports[$port_id], "PORT");
346 // self::var_dump_html($prevport, "PREVPORT");
347 $this->object_id = $_GET['object_id'];
348 }
349 else
350 {
351 // Link
352 $this->last = $this->_getlinks($port_id, false);
fdc74cf0 353
3badc888 354 if(!$this->loop)
355 $this->first = $this->_getlinks($port_id, true, null, true);
fdc74cf0 356
3badc888 357 $this->object_id = $this->ports[$this->init]['object_id'];
358 }
fdc74cf0 359
3badc888 360 if(0)
361 if($this->loop)
362 {
363 $linktype = $this->getlinktype($back);
364 $prevlinktype = $this->getlinktype(!$back);
365
366 /* set first object */
367 $object_id = $this->ports[$port_id]['object_id'];
368 $object = $lc_cache->getobject($object_id);
fdc74cf0 369
3badc888 370 if($object['IPV4OBJ'])
371 $this->lastipobjport = $port_id;
372
373 $this->first = $this->lastipobjport;
fdc74cf0 374 }
fdc74cf0 375
3badc888 376 $this->linked = ($this->linkcount > 0);
377
378 if($reverse)
379 $this->reverse();
fdc74cf0 380
381 }
fdc74cf0 382
3badc888 383 function setlinkid($porta, $portb, $linktype)
384 {
385 if($porta > $portb)
386 $lid = "${portb}_${porta}";
387 else
388 $lid = "${porta}_${portb}";
fdc74cf0 389
3badc888 390 $lid .= $linktype;
fdc74cf0 391
3badc888 392 if(isset($this->lids[$lid]))
393 return false;
fdc74cf0 394
3badc888 395 $this->lids[$lid] = true;
fdc74cf0 396
3badc888 397 return true;
fdc74cf0 398 }
399
3badc888 400 function hasport_id($port_id)
fdc74cf0 401 {
3badc888 402 if(isset($this->pids[$port_id]))
403 return true;
404 else
405 return false;
fdc74cf0 406 }
407
3badc888 408 function hasobject_id($object_id)
409 {
410 if(isset($this->oids[$object_id]))
411 return true;
412 else
413 return false;
414 }
fdc74cf0 415
3badc888 416 function _setportprevlink($port, $linktype, $prevport)
417 {
418 $port[$linktype] = array(
419 'cableid' => $prevport[$linktype]['cableid'],
420 'linked' => $prevport[$linktype]['linked'],
421 'portcount' => null, //$prevport[$linktype]['linked'],
422 'remote_id' => $prevport['id'],
423 'remote_name' => $prevport['name'],
424 'remote_object_id' => $prevport['object_id'],
425 'remote_object_name' => $prevport['object_name'],
426 );
fdc74cf0 427
3badc888 428 return $port;
429 }
fdc74cf0 430
3badc888 431 function _setportlink($port, $linktype)
432 {
433 $port[$linktype] = array(
434 'cableid' => $port['cableid'],
435 'linked' => $port['linked'],
436 'portcount' => $port['portcount'],
437 'remote_id' => $port['remote_id'],
438 'remote_name' => $port['remote_name'],
439 'remote_object_id' => $port['remote_object_id'],
440 'remote_object_name' => $port['remote_object_name'],
441 );
fdc74cf0 442
3badc888 443 unset($port['cableid']);
444 unset($port['linked']);
445 unset($port['portcount']);
446 unset($port['remote_id']);
447 unset($port['remote_name']);
448 unset($port['remote_object_id']);
449 unset($port['remote_object_name']);
fdc74cf0 450
3badc888 451 return $port;
452 }
fdc74cf0 453
3badc888 454 function _getportlink($port)
455 {
456 $linktype = $this->getlinktype($this->back);
457 return array_merge($port, $port[$linktype], array('linktype' => $linktype));
458 }
fdc74cf0 459
3badc888 460 function isback($linktable)
461 {
462 if($linktable == 'back')
463 return true;
464 else
465 return false;
466 }
fdc74cf0 467
3badc888 468 function getlinktype($back)
469 {
470 return ($back ? 'back' : 'front' );
471 }
fdc74cf0 472
3badc888 473 function reverse()
474 {
475 $tmp = $this->first;
476 $this->first = $this->last;
477 $this->last = $tmp;
fdc74cf0 478
fdc74cf0 479 }
480
fdc74cf0 481
3badc888 482 //recursive
483 function _getlinks($port_id, $back = false, $prevport_id = null, $reverse = false)
484 {
485 global $lc_cache;
486 $linktype = $this->getlinktype($back);
fdc74cf0 487
3badc888 488 if($port_id == $this->init)
489 $this->initport = true;
fdc74cf0 490
3badc888 491 $ports = lm_getPortInfo($port_id, $back);
fdc74cf0 492
3badc888 493 $portcount = count($ports);
fdc74cf0 494
3badc888 495 $port = $ports[0];
fdc74cf0 496
3badc888 497 // check for loops on multilinked ports
498 // set main port to looping one
499 if($portcount > 1)
500 foreach($ports as $mport)
501 {
502 if(isset($this->ports[$mport['remote_id']]))
503 {
504 $port = $mport;
505 break;
506 }
507 }
fdc74cf0 508
3badc888 509 $remote_id = $port['remote_id'];
fdc74cf0 510
3badc888 511 $port['portcount'] = $portcount;
fdc74cf0 512
3badc888 513 $object_id = $port['object_id'];
fdc74cf0 514
3badc888 515 $rack = null;
516 $object = $lc_cache->getobject($object_id, $rack);
fdc74cf0 517
3badc888 518 $this->oids[$object_id] = true;
fdc74cf0 519
3badc888 520 if($object['IPV4OBJ'])
521 $this->lastipobjport = $port_id;
fdc74cf0 522
3badc888 523 if($object)
524 if(isset($object['portip'][$port['name']]))
525 $port['portip'] = $object['portip'][$port['name']];
fdc74cf0 526
3badc888 527 $port = $this->_setportlink($port, $linktype);
fdc74cf0 528
3badc888 529 if($prevport_id)
fdc74cf0 530 {
3badc888 531 $prevlinktype = $this->getlinktype(!$back);
532 $port = $this->_setportprevlink($port, $prevlinktype, $this->ports[$prevport_id]);
fdc74cf0 533 }
534
3badc888 535 //if(!$back)
536 {
537 if($prevport_id)
538 {
539 /* mutlilink: multiple previous links */
540 $prevports = lm_getPortInfo($port_id, !$back);
fdc74cf0 541
3badc888 542 $prevportcount = count($prevports);
fdc74cf0 543
3badc888 544 if($prevportcount > 1)
545 {
546 if($this->initport)
547 $this->initalign = false;
fdc74cf0 548
3badc888 549 $port[$this->getlinktype(!$back)]['portcount'] = $prevportcount;
fdc74cf0 550
3badc888 551 $lcs = array();
552 foreach($prevports as $mport)
553 {
554 if($prevport_id != $mport['remote_id'])
555 {
556 if(isset($this->pids[$mport['remote_id']]))
557 continue;
fdc74cf0 558
3badc888 559 $mport['portcount'] = 1;
560 $lc = new lm_linkchain($mport['remote_id'], $back, $mport, !$reverse, $this->pids, $this->oids);
561 $lcs[$mport['remote_id']] = $lc;
562 $this->linkcount += $lc->linkcount;
563 }
564 }
fdc74cf0 565
3badc888 566 $port[$this->getlinktype(!$back)]['chains'] = $lcs;
567 }
568 }
569 }
fdc74cf0 570
3badc888 571 if($portcount > 1)
fdc74cf0 572 {
3badc888 573 /* mutlilink: multiple links */
574
575 $lcs = array();
576 foreach($ports as $mport)
577 {
578 if($remote_id != $mport['remote_id'])
579 {
580 if(isset($this->pids[$mport['remote_id']]))
581 continue;
582
583 $mport['portcount'] = 1;
584 $lc = new lm_linkchain($mport['remote_id'], !$back, $mport, $reverse, $this->pids, $this->oids);
585 $lcs[$mport['remote_id']] = $lc;
586 $this->linkcount += $lc->linkcount;
587 }
588 }
589
590 $port[$linktype]['chains'] = $lcs;
fdc74cf0 591
fdc74cf0 592 }
593
3badc888 594 if(isset($this->ports[$port_id]))
595 {
596 if(!isset($this->ports[$port_id][$linktype]))
597 {
598 $this->ports[$port_id][$linktype] = $port[$linktype];
599 }
600 }
601 else
602 $this->ports[$port_id] = $port;
fdc74cf0 603
3badc888 604 $this->pids[$port_id] = true;
fdc74cf0 605
3badc888 606 if($remote_id)
607 {
608 if($this->setlinkid($port_id, $remote_id, $linktype))
609 {
610 $this->linkcount++;
611 return $this->_getlinks($remote_id, !$back, $port_id, $reverse);
612 }
613 else
614 {
615 $prevlinktype = $this->getlinktype(!$back);
616 $this->loop = true;
617 if(isset($port[$prevlinktype]))
618 $this->ports[$port_id][$prevlinktype] = $port[$prevlinktype];
619
620 $this->first = $remote_id;
621 }
622 }
623
624 return $port_id;
625 }
626
627 // TODO
628 function getchaintext()
629 {
630 //$this::var_dump_html($this->ports);
631 $chain = "";
632 foreach($this as $id => $port)
633 {
634 $linktype = $port['linktype']; //$this->getlinktype();
635 if($linktype == 'front')
636 $arrow = ' --> ';
637 else
638 $arrow = ' => ';
639
640 $text = $port['object_name']." [".$port['name']."]";
641
642 if($id == $this->init)
643 $chain .= "*$text*";
644 else
645 $chain .= "$text";
646
647 $remote_id = $port['remote_id'];
648
649 if($remote_id)
650 $chain .= $arrow;
651
652 if($this->loop && $remote_id == $this->first)
653 {
654 $chain .= "LOOP!";
655 break;
656 }
657 }
658 return $chain;
659 }
660
661 function getchainlabeltrstart($rowbgcolor = '#ffffff')
662 {
663
664 $port_id = $this->init;
665
666 $initport = $this->ports[$port_id];
fdc74cf0 667
668 $urlparams = array(
3badc888 669 'module' => 'redirect',
670 'page' => 'object',
671 'tab' => 'linkmgmt',
672 'op' => 'map',
673 'usemap' => 1,
674 'object_id' => $initport['object_id'],
fdc74cf0 675 );
676
3badc888 677 $hl_port_id = NULL;
678 if($hl_port_id !== NULL)
679 $urlparams['hl_port_id'] = $hl_port_id;
680 else
681 $urlparams['port_id'] = $port_id;
fdc74cf0 682
3badc888 683 $title = "linkcount: ".$this->linkcount."\nTypeID: ${initport['oif_id']}\nPortID: $port_id";
fdc74cf0 684
3badc888 685 $onclick = 'onclick=window.open("'.makeHrefProcess(array_merge($_GET, $urlparams)).'","Map","height=500,width=800,scrollbars=yes");';
fdc74cf0 686
3badc888 687 if($hl_port_id == $port_id)
688 {
689 $hlbgcolor = "bgcolor=".self::HL_PORT_BGCOLOR;
690 }
691 else
692 $hlbgcolor = "bgcolor=$rowbgcolor";
fdc74cf0 693
3badc888 694 if($rowbgcolor == '#ffffff')
695 $troutlinecolor = 'grey';
696 else
697 $troutlinecolor = 'white';
fdc74cf0 698
3badc888 699 /* Current Port */
700 $chainlabel = '<tr '.$hlbgcolor.' style="outline: thin solid '.$troutlinecolor.';"><td nowrap="nowrap" bgcolor='.($this->loop ? '#ff9966' : self::CURRENT_PORT_BGCOLOR).' title="'.$title.
701 '"><a '.$onclick.'>'.
702 $initport['name'].': </a></td>';
fdc74cf0 703
3badc888 704 return $chainlabel;
705 }
706
707 function getchainrow($allback = false, $rowbgcolor = '#ffffff', $isprev = false)
708 {
709 $port_id = $this->init;
710
711 $initport = $this->ports[$port_id];
712 $portmulti = 0;
713 $prevportmulti = 0;
714
715 $chain = "";
716
717 $remote_id = null;
718 $portalign = false;
719 $portmultis = array();
720 $i=0;
721 foreach($this as $id => $port)
fdc74cf0 722 {
3badc888 723 if($this->loop && (($isprev && $id == $this->last) || ($this->initback === null && !$isprev && $id == $this->first)))
724 $chain .= '<td bgcolor=#ff9966>LOOP</td>';
725
726 $object_text = $this->getprintobject($port);
727 $port_text = $this->getprintport($port);
728
729 if($this->initback !== null && (($isprev && $id == $this->last) || (!$isprev && $id == $this->first)))
fdc74cf0 730 {
3badc888 731 $object_text = "";
732 $port_text = "";
fdc74cf0 733 }
fdc74cf0 734
3badc888 735 $linktype = $port['linktype'];
736 $prevlinktype = ($linktype == 'front' ? 'back' : 'front');
fdc74cf0 737
3badc888 738 if($port[$prevlinktype]['portcount'] > 1)
739 {
740 $prevportmulti++;
741 /* mutlilink: multiple previous links */
fdc74cf0 742
3badc888 743 $notrowbgcolor = ($rowbgcolor == lm_linkchain::ALTERNATE_ROW_BGCOLOR ? '#ffffff' : lm_linkchain::ALTERNATE_ROW_BGCOLOR );
744 if($this->prevportmulti % 2)
745 {
746 $oddbgcolor = $rowbgcolor;
747 $evenbgcolor = $notrowbgcolor;
748 }
749 else
750 {
751 $oddbgcolor = $notrowbgcolor;
752 $evenbgcolor = $rowbgcolor;
753 }
fdc74cf0 754
3badc888 755 $chain = "<td><table id=pmp frame=box><tr id=main><td><table id=pmmain align=right><tr>$chain</tr></table></td></tr><!-- main tr--><tr><td><table id=plcs align=right width=100%>"; // end table pmmain; end main tr
fdc74cf0 756
3badc888 757 $mi = 0;
758 foreach($port[$prevlinktype]['chains'] as $mlc)
759 {
760 $mbgcolor = ($mi % 2 ? $evenbgcolor : $oddbgcolor);
761 $chain .= "<tr bgcolor=$mbgcolor align=right><td><table id=plc><tr>".$mlc->getchainrow($allback,$mbgcolor, true)."</tr></table><!-- end plc --></td></tr>"; // close table plc
762 $mi++;
763 }
764 $chain .= "</table><!--plcs--></td></tr></table><!--pmp--></td><td bgcolor=#ff0000></td>"; // clode tables plcs; pmp
fdc74cf0 765
3badc888 766 }
83b49cf7 767
3badc888 768 if($port_text && !$this->loop && $id == $this->first)
769 {
770 $chain .= $this->_printlinkportsymbol($id, $prevlinktype);
83b49cf7 771
3badc888 772 if($prevlinktype == 'front')
773 $chain .= $this->printcomment($port);
774 }
83b49cf7 775
3badc888 776 // current port align
777 if($this->initback === null && $port_text && !$portmulti && !$prevportmulti && $this->initalign && $id == $port_id)
778 {
779 $portalign = true;
780 $port_text = "</tr></table><!--t1 current--></td><!--end 1st td--><td id=2nd width=100%><table id=t2><tr>$port_text"; // close table t1 current; clode 1st td
781 }
83b49cf7 782
3badc888 783 $object_id = $port['object_id'];
83b49cf7 784
3badc888 785 $prevobject_id = $port[$prevlinktype]['remote_object_id'];
786 $remote_object_id = $port['remote_object_id'];
83b49cf7 787
3badc888 788 if($linktype == 'front')
789 {
790 // $arrow = ' ---> ';
791 if($object_text)
792 {
793 if($prevobject_id != $object_id || $allback)
794 $chain .= $object_text."<td>></td>";
83b49cf7 795
3badc888 796 $chain .= $port_text;
797 }
83b49cf7 798
3badc888 799 }
800 else
801 {
802 // $arrow = ' ===> ';
803 if($object_text)
804 $chain .= $port_text."<td><</td>".$object_text;
805 }
83b49cf7 806
3badc888 807 if($port['portcount'] > 1)
808 {
809
810 /* mutlilink: multiple links */
83b49cf7 811
3badc888 812 $notrowbgcolor = ($rowbgcolor == lm_linkchain::ALTERNATE_ROW_BGCOLOR ? '#ffffff' : lm_linkchain::ALTERNATE_ROW_BGCOLOR );
813 if($this->portmulti % 2)
814 {
815 $oddbgcolor = $rowbgcolor;
816 $evenbgcolor = $notrowbgcolor;
817 }
818 else
819 {
820 $oddbgcolor = $notrowbgcolor;
821 $evenbgcolor = $rowbgcolor;
822 }
823
824 $chain .= "<td bgcolor=#ff0000></td><td><table id=mp frame=box>";
83b49cf7 825
3badc888 826 $multichain = "<tr><td><table id=mlcs width=100%>";
83b49cf7 827
3badc888 828 $mi = 0;
829 foreach($port['chains'] as $mlc)
830 {
83b49cf7 831
3badc888 832 $mbgcolor = ($mi % 2 ? $evenbgcolor : $oddbgcolor);
833 $multichain .= "<tr bgcolor=$mbgcolor><td><table id=t9><tr>".$mlc->getchainrow(false, $mbgcolor)."</tr></table></td></tr>";
834 $mi++;
835 }
83b49cf7 836
3badc888 837 $multichain .= "</table><!-- mlcs --></td></tr>"; // close table mlcs
83b49cf7 838
3badc888 839 $portmultis[] = $multichain;
840
841 // main
842 $chain .= "<tr id=mm><td><table id=pmm><tr>";
83b49cf7 843
3badc888 844 $portmulti++;
83b49cf7 845
3badc888 846 }
847
848 $remote_id = $port['remote_id'];
849
850 if($remote_id)
851 if($object_id != $remote_object_id || $allback || $linktype == 'front')
852 $chain .= $this->printlink($port, $linktype);
853 else
854 $chain .= "<td>></td>";
855
856 if($port_text && !($remote_id && $this->loop) && $id == $this->last)
857 {
858 $chain .= $this->_printlinkportsymbol($id, $linktype);
859
860 if($linktype == 'front')
861 $chain .= $this->printcomment($port);
862 }
863
864 $i++;
865 } // foreach port
866
867 if($this->loop && $remote_id)
868 {
869 $chain .= '<td bgcolor=#ff9966>LOOP</td>';
870 showWarning("Possible Loop on Port ($linktype) ".$initport['name']);
83b49cf7 871 }
83b49cf7 872
3badc888 873 if($this->exceed)
874 {
875 $chain .= '<td bgcolor=#ff9966>LINKCOUNT EXCEEDED</td>';
876 $linktype = $this->getlinktype($this->back);
877 showWarning("Possible Loop linkcount(".$this->linkcount.") exceeded on Port ($linktype) ".$initport['name']);
878 }
83b49cf7 879
3badc888 880 foreach(array_reverse($portmultis) as $multitr)
881 {
882 $chain .= "</tr><!-- tr mm --></table><!-- pmm --></td></tr>$multitr</table><!-- mp--></td>"; // close table pmm; mp
883 }
83b49cf7 884
3badc888 885 if($this->initback === null)
886 {
887 // TODO width..
888 $chain = "<td id=1st".(!$portalign ? " colspan=2" : " width=1%")."><table id=t1".(!$portalign ? "" : " align=right")."><tr>$chain";
889 $chain .= "</tr></table><!-- end t1/t2 --></td><!--1st/2nd td-->"; // close table t1/t2; close 1st/2nd td
890 }
891
892 $this->portmulti += $portmulti;
893 $this->prevportmulti += $prevportmulti;
894
895 return $chain;
896 }
897
898 /*
899 */
900 function getprintobject($port) {
901 global $lc_cache;
902
903 $object_id = $port['object_id'];
904
905 if($object_id == $this->object_id) {
906 $color='color: '.self::CURRENT_OBJECT_BGCOLOR;
907 } else {
908 $color='';
909 }
910
911 $style = "font-size: 80%;";
912
913 $rack = null;
914 $object = $lc_cache->getobject($object_id, $rack);
915
5ddfc8cc 916 $rackinfo = $this->_getprintrack($rack, $style);
917
918 if($object['container_id'])
919 {
920 $container_rack = null;
921 $container = $lc_cache->getobject($object['container_id'], $container_rack);
922 $container_rackinfo = $this->_getprintrack($container_rack, $style);
923
924 $txt = '<a style="font-weight:bold;'
925 .$color.'" href="'.makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $container['id']))
926 .'"><pre>'.$container['name'].'</pre></a><pre>'.$container_rackinfo
927 .'</pre>';
928
929 $txt = "<tr><td><label style=\"font-size: 60%;\">Container:</label>$txt<hr></td></tr>";
930 }
931 else
932 $txt = '';
933
934
935 return '<td><table frame=box align=center cellpadding=5 cellspacing=0>'.$txt.'<tr><td align=center><a style="font-weight:bold;'
936 .$color.'" href="'.makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $object_id))
937 .'"><pre>'.$object['name'].'</pre></a><pre>'.$rackinfo
938 .'</pre></td></tr></table></td>';
939
940 } /* getprintobject */
941
942 function _getprintrack($rack, $style)
943 {
3badc888 944 if(!$rack)
945 $rackinfo = '<span style="'.$style.'">Unmounted</span>';
946 else
947 {
948 $rackinfo = '<a style="'.$style.'" href='.makeHref(array('page'=>'row', 'row_id'=>$rack['row_id'])).'>'.$rack['row_name']
949 .'</a>/<a style="'.$style.'" href='.makeHref(array('page'=>'rack', 'rack_id'=>$rack['id'])).'>'
950 .$rack['name'].'</a>';
951 }
952
5ddfc8cc 953 return $rackinfo;
954 }
3badc888 955
956 function getprintport($port, $multilink = false) {
957 global $lm_cache, $lm_multilink_port_types;
958
959 /* multilink port */
960 $multilink = in_array($port['oif_id'], $lm_multilink_port_types);
961
962 /* set bgcolor for current port */
963 if($this->initback === null && $port['id'] == $this->init) {
964 $bgcolor = 'bgcolor='.self::CURRENT_PORT_BGCOLOR;
965 $idtag = ' id='.$port['id'];
966 } else {
967 $bgcolor = 'bgcolor=#e0e0f8';
968 $idtag = '';
969 }
970
971 $mac = trim(preg_replace('/(..)/','$1:',$port['l2address']),':');
972
973 $title = "Label: ${port['label']}\nMAC: $mac\nTypeID: ${port['oif_id']}\nPortID: ${port['id']}";
974
975 if(isset($port['portip']))
976 $ip = "<br><p style=\"font-size: 80%\">".$port['portip']."</p>";
977 else
978 $ip = "";
979
980 return '<td><table><tr><td'.$idtag.' align=center '.$bgcolor.' title="'.$title.'"><pre>[<a href="'
981 .makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $port['object_id'], 'hl_port_id' => $port['id']))
982 .'#'.$port['id']
983 .'">'.$port['name'].'</a>]'.$ip.'</pre>'.($multilink && $lm_cache['allowbacklink'] ? $this->_getlinkportsymbol($port['id'], 'back') : '' ).'</td></tr></table></td>';
984
985 } /* printport */
986
987 /*
988 */
989 function printlink($port, $linktype) {
990
3badc888 991 if($linktype == 'back')
3badc888 992 $arrow = '====>';
3badc888 993 else
994 $arrow = '---->';
995
996 $port_id = $port['id'];
997
998 /* link */
999 return '<td align=center>'
16ec374c 1000 .'<pre><span class="editable id1-'.$port_id.' id2-'.$port['remote_id'].' op-lm-upd-reservation-cable linktype-'.$linktype.'">'.$port['cableid']
1001 ."</span></pre><pre>$arrow</pre>"
3badc888 1002 .$this->_printUnLinkPort($port, $linktype)
1003 .'</td>';
1004 } /* printlink */
1005
1006 /*
1007 * return link symbol
1008 */
1009 function _getlinkportsymbol($port_id, $linktype) {
1010 $retval = '<span onclick=window.open("'.makeHrefProcess(array_merge($_GET,
1011 array('op' => 'PortLinkDialog','port' => $port_id,'linktype' => $linktype ))).'","name","height=800,width=800");'
1012 .'>';
1013
1014 $img = getImageHREF ('plug', $linktype.' Link this port');
1015
1016 if($linktype == 'back')
1017 $img = str_replace('<img',
1018 '<img style="transform:rotate(180deg);-o-transform:rotate(180deg);-ms-transform:rotate(180deg);-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);"',
1019 $img);
1020
1021 $retval .= $img;
1022 $retval .= "</span>";
1023 return $retval;
1024
1025 } /* _getlinkportsymbol */
1026
1027 /*
1028 * print link symbol
1029 *
1030 */
1031 function _printlinkportsymbol($port_id, $linktype = 'front') {
1032 global $lm_cache;
1033
1034 if($linktype == 'front' && !$lm_cache['allowlink'])
1035 return;
1036
1037 if($linktype != 'front' && !$lm_cache['allowbacklink'])
1038 return;
1039
1040 return "<td align=center>"
1041 .$this->_getlinkportsymbol($port_id, $linktype)
1042 ."</td>";
1043
1044 } /* _printlinkportsymbol */
1045
1046 /*
1047 */
1048 function printcomment($port) {
1049
1050 if(!empty($port['reservation_comment'])) {
1051 $prefix = '<b>Reserved: </b>';
1052 } else
1053 $prefix = '';
1054
16ec374c 1055 return '<td>'.$prefix.'</td><td><i><span class="editable op-upd-reservation-port id-'.$port['id'].'">'.$port['reservation_comment'].'</span</i></td>';
3badc888 1056
1057 } /* printComment */
1058
1059 /*
1060 * return link cut symbol
1061 *
1062 * TODO $opspec_list
1063 */
1064 function _printUnLinkPort($src_port, $linktype) {
1065 global $lm_cache;
1066
1067 if($linktype == 'front' && !$lm_cache['allowlink'])
1068 return;
1069
1070 if($linktype != 'front' && !$lm_cache['allowbacklink'])
1071 return;
1072
1073 $dst_port = $this->ports[$src_port['remote_id']];
1074
1075 return '<a href='.
1076 makeHrefProcess(array(
1077 'op'=>'unlinkPort',
1078 'port_id'=>$src_port['id'],
1079 'remote_id' => $dst_port['id'],
1080 'object_id'=> $this->object_id, //$this->ports[$this->init]['object_id'],
1081 'tab' => 'linkmgmt',
1082 'linktype' => $linktype)).
1083 ' onclick="return confirm(\'unlink ports '.$src_port['name']. ' -> '.$dst_port['name']
1084 .' ('.$linktype.') with cable ID: '.$src_port['cableid'].'?\');">'.
1085 getImageHREF ('cut', $linktype.' Unlink this port').'</a>';
1086
1087 } /* _printUnLinkPort */
1088
3d9d3e22 1089 function printports()
1090 {
1091
1092 $back = isset($this->ports[$this->first]['back']['remote_id']);
d894f236 1093 //$this->getporttds($this->first, $back);
1094 foreach($this as $port_id => $port)
1095 {
1096 $class = 'hidden';
1097 if($port_id == $this->first || $port_id == $this->last)
1098 $class = '';
1099
1100 $this->getporttds($port, $class);
1101 }
3d9d3e22 1102 }
1103
d894f236 1104 function getporttds($port, $class = '')
569219fa 1105 {
d894f236 1106 $back = ($port['linktype'] == 'front' ? false : true );
569219fa
CR
1107
1108 // see interface.php renderObjectPortRow()
1109 // highlight port name with yellow if it's name is not canonical
1110 $canon_pn = shortenPortName ($port['name'], $port['object_id']);
1111 $name_class = $canon_pn == $port['name'] ? '' : 'trwarning';
1112
1113 echo "<td class='tdleft $class'>" .
1114 formatPortLink ($port['object_id'], $port['object_name'], $port['id'], NULL) .
1115 "</td>";
1116
1117 $a_class = isEthernetPort ($port) ? 'port-menu' : '';
1118 echo "<td class='tdleft $class' NOWRAP><a name='port-${port['id']}' class='interactive-portname nolink $a_class'>${port['name']}</a></td>";
1119 echo "<td class='tdleft $class'>${port['label']}</td>";
1120 echo "<td class='tdleft $class'>" . formatPortIIFOIF ($port) . "</td><td class='tdleft $class'><tt>${port['l2address']}</tt></td>";
1121
1122 $remote_port = $port[( $back ? 'back' : 'front')];
1123 if ($remote_port['remote_id'])
1124 {
3d9d3e22 1125 if($port['id'] == $this->first)
1126 {
1127 echo "<td class='tdcenter nothidden'>";
1128
1129 if( $this->linkcount == 1 )
1130 if($back)
1131 echo "=${remote_port['cableid']}=>";
1132 else
1133 echo "-${remote_port['cableid']}->";
1134 else
1135 if( $this->linkcount > 1 )
1136 echo "*".$this->linkcount."*>";
1137 echo "</td>";
1138 }
1139
569219fa 1140 $editable = permitted ('object', 'ports', 'editPort') ? 'editable' : '';
d894f236 1141 echo "<td class='tdcenter hidden'>";
569219fa
CR
1142 if($back)
1143 echo "=${remote_port['cableid']}=>";
1144 else
1145 echo "-${remote_port['cableid']}->";
1146
d894f236 1147 echo "</td>";
569219fa
CR
1148 }
1149 else
d894f236 1150 {
1151 if(!empty($port['reservation_comment']))
1152 echo implode ('', formatPortReservation ($port));
1153 }
569219fa 1154
569219fa
CR
1155 } // getporttds
1156
3badc888 1157 // TODO
1158 // html table
1159 function getchainhtml()
1160 {
1161 $remote_id = $this->first;
1162
1163 // if not Link use LinkBackend
1164 $back = $this->ports[$remote_id]['front']['remote_id'];
1165
1166 $chain = "<table>";
1167
1168 for(;$remote_id;)
1169 {
1170 $back = !$back;
1171 $linktype = $port['linktype'];
1172
1173 if($back)
1174 {
1175 // $linktable = 'LinkBackend';
1176 $arrow = ' => ';
1177 }
1178 else
1179 {
1180 // $linktable = 'Link';
1181 $arrow = ' --> ';
1182 }
1183
1184 $port = $this->ports[$remote_id][$linktype];
1185
1186 if($this->init == $remote_id)
1187 $chain .= "<tr><td><b>".$port['object_name']."</b></td><td><b> [".$port['name']."]</b></td>";
1188 else
1189 $chain .= "<tr><td>".$port['object_name']."</td><td> [".$port['name']."]</td>";
1190
1191 if($remote_id == $this->first || $remote_id == $this->last)
1192 $chain .= "<td><div name=\"port${remote_id}-status\"></div></td>";
1193 else
1194 $chain .= "<td></td>";
1195
1196 $remote_id = $port['remote_id'];
1197
1198 if($remote_id)
1199 $chain .= "<td>$arrow</td></tr>";
1200 else
1201 $chain .= "<td></td></tr>";
1202
1203 if($this->loop && $remote_id == $this->first)
1204 {
1205 $chain .= "LOOP!<br>";
1206 break;
1207 }
1208
1209 }
1210
1211 $chain .= "</table>";
1212
1213 return $chain;
1214 }
1215
1216 function getport($id)
1217 {
1218 return $this->ports[$id];
1219 }
1220
1221 /* Iterator */
1222 function rewind() {
1223 $this->currentid = $this->first;
1224
1225 $this->back = isset($this->ports[$this->currentid]['back']['remote_id']);
1226
1227 $this->icount = 0;
1228 }
1229
1230 function current() {
1231 return $this->_getportlink($this->ports[$this->currentid]);
1232 }
1233
1234 function key() {
1235 return $this->currentid;
1236 }
1237
1238 function next() {
1239 $port = $this->current();
1240 $remote_id = $port['remote_id'];
1241
1242 if($this->loop && $remote_id == $this->first)
1243 $this->currentid = false;
1244 else
1245 $this->currentid = $remote_id;
1246
1247 $this->back = !$this->back;
1248
1249 $this->icount++;
1250 }
1251
1252 function valid() {
1253
1254 /* linkcout exceeded */
1255 if($this->icount > $this->linkcount+1)
1256 {
1257 $this->exceed = true;
1258 return false;
1259 }
1260
1261 return $this->currentid;
1262 }
1263
1264 /* for debugging only */
1265 function var_dump_html(&$var, $msg = "") {
1266 echo "<pre>------------------Start Var Dump -------------$msg------------\n";
1267 var_dump($var);
1268 echo "\n---------------------END Var Dump -----------$msg-------------</pre>";
1269 }
1270} // lm_linkchain
1271
1272/*
1273 * from RT database.php fetchPortList()
1274 * with Link table selection
1275 * and multilink changes
1276 */
1277function lm_fetchPortList ($sql_where_clause, $query_params = array(), $linktable = 'Link')
1278{
1279 $query = <<<END
1280SELECT
1281 Port.id,
1282 Port.name,
1283 Port.object_id,
1284 Object.name AS object_name,
1285 Port.l2address,
1286 Port.label,
1287 Port.reservation_comment,
1288 Port.iif_id,
1289 Port.type AS oif_id,
1290 (SELECT PortInnerInterface.iif_name FROM PortInnerInterface WHERE PortInnerInterface.id = Port.iif_id) AS iif_name,
1291 (SELECT PortOuterInterface.oif_name FROM PortOuterInterface WHERE PortOuterInterface.id = Port.type) AS oif_name,
1292
1293 lk.cable AS cableid,
1294 IF(lk.porta = Port.id, pb.id, pa.id) AS remote_id,
1295 IF(lk.porta = Port.id, pb.name, pa.name) AS remote_name,
1296 IF(lk.porta = Port.id, pb.object_id, pa.object_id) AS remote_object_id,
1297 IF(lk.porta = Port.id, ob.name, oa.name) AS remote_object_name,
1298
1299 (SELECT COUNT(*) FROM PortLog WHERE PortLog.port_id = Port.id) AS log_count,
1300 PortLog.user,
1301 UNIX_TIMESTAMP(PortLog.date) as time
1302FROM
1303 Port
1304 INNER JOIN Object ON Port.object_id = Object.id
1305
1306 LEFT JOIN $linktable AS lk ON lk.porta = Port.id or lk.portb = Port.id
1307 LEFT JOIN Port AS pa ON pa.id = lk.porta
1308 LEFT JOIN Object AS oa ON pa.object_id = oa.id
1309 LEFT JOIN Port AS pb ON pb.id = lk.portb
1310 LEFT JOIN Object AS ob ON pb.object_id = ob.id
1311
1312 LEFT JOIN PortLog ON PortLog.id = (SELECT id FROM PortLog WHERE PortLog.port_id = Port.id ORDER BY date DESC LIMIT 1)
1313WHERE
1314 $sql_where_clause
1315END;
1316
1317 $result = usePreparedSelectBlade ($query, $query_params);
1318
1319 $ret = array();
1320 while ($row = $result->fetch (PDO::FETCH_ASSOC))
1321 {
1322 $row['l2address'] = l2addressFromDatabase ($row['l2address']);
1323 $row['linked'] = isset ($row['remote_id']) ? 1 : 0;
1324
1325 // last changed log
1326 $row['last_log'] = array();
1327 if ($row['log_count'])
1328 {
1329 $row['last_log']['user'] = $row['user'];
1330 $row['last_log']['time'] = $row['time'];
1331 }
1332 unset ($row['user']);
1333 unset ($row['time']);
1334
1335 $ret[] = $row;
1336 }
1337 return $ret;
1338} /* lm_fetchPortList */
1339
1340function lm_getPortInfo ($port_id, $back = false)
1341{
1342 $linktable = ($back ? 'LinkBackend' : 'Link');
1343 if($back)
1344 $result = lm_fetchPortList ('Port.id = ?', array ($port_id), $linktable);
1345 else
1346 $result = fetchPortList ('Port.id = ?', array ($port_id));
1347
1348 //return empty ($result) ? NULL : $result[0];
1349 return $result;
1350} /* lm_getPortInfo */
1351
1352/* -------------------------------------------------------------------------- */
1353/* -------------------------------------------------- */
1354
1355function linkmgmt_opHelp() {
1356?>
1357 <table cellspacing=10><tr><th>Help</th><tr>
1358 <tr><td width=150></td><td width=150 style="font-weight:bold;color:<?php echo portlist::CURRENT_OBJECT_BGCOLOR; ?>">Current Object</td></tr>
1359 <tr><td></td><td bgcolor=<?php echo portlist::CURRENT_PORT_BGCOLOR; ?>>[current port]</td></tr>
1360 <tr><td>front link</td><td>[port]<(Object)</td><td>back link</td></tr>
1361 <tr><td>back link</td><td>(Object)>[port]</td><td>front link</td></tr>
1362 <tr><td></td><td><pre>----></pre></td><td>Front link</td></tr>
1363 <tr><td></td><td><pre>====></pre></td><td>Backend link</td></tr>
1364 <tr><td></td><td>Link Symbol</td><td>Create new link</td></tr>
1365 <tr><td></td><td>Cut Symbol</td><td>Delete link</td></tr>
1366
1367 </table>
1368
1369<?php
1370 exit;
1371} /* opHelp */
1372
1373/* -------------------------------------------------- */
1374
1375function linkmgmt_ajax_mapinfo() {
1376
1377 $object_id = NULL;
1378 $port_id = NULL;
1379 $remote_id = NULL;
1380 $linktype = NULL;
1381
1382 if(isset($_REQUEST['object_id']))
1383 $object_id = $_REQUEST['object_id'];
1384
1385 if(isset($_REQUEST['port_id']))
1386 $port_id = $_REQUEST['port_id'];
1387
1388 if(isset($_REQUEST['remote_id']))
1389 $remote_id = $_REQUEST['remote_id'];
1390
1391 if(isset($_REQUEST['linktype']))
1392 $linktype = $_REQUEST['linktype'];
1393
1394 $debug = NULL;
1395 if(isset($_REQUEST['debug']))
1396 $debug['value'] = $_REQUEST['debug'];
1397
1398 $info = array();
1399
1400 echo "<table style=\"font-size:12;\"><tr>";
1401
1402 if($port_id != NULL)
1403 {
1404 $port = new linkmgmt_RTport($port_id);
1405
1406 echo "<td>";
1407 $port->printtable('both');
1408 echo "</td>";
1409
1410 if($debug)
1411 $debug['port'] = &$port;
1412
1413 if($remote_id != NULL)
1414 {
1415
1416 $remote_port = new linkmgmt_RTport($remote_id);
1417
1418 echo "<td><table align=\"center\">";
1419
1420 // TODO cableid
1421 echo "<tr><td><pre>".($linktype == 'back' ? ' ===> ' : ' ---> ')."</pre></td></tr>";
1422
1423 $port->printunlinktr($linktype, $remote_port);
1424
1425 echo "</table></td>";
1426
1427
1428 echo "<td>";
1429 $remote_port->printtable('both');
1430 echo "</td>";
1431
1432 if($debug)
1433 $debug['remote_port'] = &$remote_port;
1434
1435 }
1436 else
1437 $port->printunlinktr();
1438
1439
1440 }
1441 echo "</tr><tr>";
1442
1443 echo "<td>";
1444 $object = linkmgmt_RTport::printobjecttable($object_id);
1445 echo "</td>";
1446
1447 if($debug)
1448 $debug['object'] = &$object;
1449
1450 if($remote_id != NULL)
1451 {
1452
1453 echo "<td></td>"; /* link */
1454 echo "<td>";
1455 $remote_object = linkmgmt_RTport::printobjecttable($remote_port->port['object_id']);
1456 echo "</td>";
1457
1458 if($debug)
1459 $debug['remote_object'] = &$remote_object;
1460 }
1461
1462 echo "</tr></table>";
1463
1464 if($debug)
1465 {
1466 echo "<pre>--- Debug ---";
1467 var_dump($debug);
1468 echo "</pre>";
1469 }
1470
1471 exit;
1472}
1473
1474/* -------------------------------------- */
1475function lm_renderObjectCell ($cell)
1476{
1477 echo "<table class='slbcell vscell'><tr><td rowspan=2 width='5%'>";
1478 printImageHREF ('OBJECT');
1479 echo '</td><td>';
1480 echo mkA ('<strong>' . niftyString ($cell['dname']) . '</strong>', 'object', $cell['id']);
1481 echo '</td></tr><tr><td>';
1482 echo count ($cell['etags']) ? ("<small>" . serializeTags ($cell['etags']) . "</small>") : '&nbsp;';
1483 echo "</td></tr></table>";
1484}
1485/* -------------------------------------- */
1486
1487// TODO replace..
1488class linkmgmt_RTport {
1489
1490 private $port_id = NULL;
1491
1492 public $port = false;
1493
1494 function __construct($port_id) {
1495
1496 $this->port = getPortInfo($port_id);
1497
1498 if($this->port === false)
1499 return;
1500
1501 /* successfully get port info */
1502 $this->port_id = $port_id;
1503
1504 } /* __construct */
1505
1506 function isvalid() {
1507 return ($port_id !== NULL);
1508 }
1509
1510 function getlinks($type = 'front') {
1511 } /* getlinks */
1512
1513 function printtable($linktype = 'front') {
1514
1515 if($this->port_id == NULL)
1516 return;
1517
1518 echo "<table>";
1519
1520 $urlparams = array(
1521 'module' => 'redirect',
1522 'page' => 'object',
1523 'tab' => 'linkmgmt',
1524 'op' => 'map',
1525 'object_id' => $this->port['object_id'],
1526 'port_id' => $this->port_id,
1527 'usemap' => 1,
1528 );
1529
1530 echo '<tr><td><a title="don\'t highlight port" href="?'.http_build_query($urlparams).'">-phl</a></td>';
1531
1532 $urlparams['hl'] = 'p';
1533 echo '<td><a title="highlight port" href="?'.http_build_query($urlparams).'">+phl</a></td></tr>';
1534
1535 $this->_printinforow($this->port,
1536 array(
1537 'id' => 'Port ID',
1538 'name' => 'Port Name',
1539 'oif_name' => 'Port Type',
1540 'l2address' => 'MAC',
1541 'reservation_comment' => 'comment',
1542 )
1543 ); /* printinforow */
1544
1545 $this->printlinktr($linktype);
1546
1547 echo "</table>";
1548 } /* printtable */
1549
1550 function printlinktr($linktype = 'front') {
1551 if($this->port_id === NULL)
1552 return;
1553
1554 $urlparams = array(
1555 'tab' => 'linkmgmt',
1556 'page' => 'object',
1557 'op'=>'PortLinkDialog',
1558 'port'=>$this->port_id,
1559 'object_id'=>$this->port['object_id'],
1560 'linktype' => $linktype,
1561 );
1562
1563 echo "<tr><td align=\"center\"><a href='".
1564 makeHrefProcess($urlparams).
1565 "'>";
1566 printImageHREF ('plug', 'Link this port');
1567 echo "</a></td></tr>";
1568 } /* link */
1569
1570 function printunlinktr($linktype = 'front', $remote_port = NULL) {
1571 if($this->port_id === NULL)
1572 return;
1573
1574 $urlparams = array(
1575 'tab' => 'linkmgmt',
1576 'op'=>'unlinkPort',
1577 'port_id'=>$this->port_id,
1578 'object_id'=>$this->port['object_id'],
1579 'linktype' => $linktype,
1580 );
1581
1582 $confirmmsg = "unlink port ".$this->port['name'];
1583
1584 if($remote_port !== NULL)
1585 {
1586 $urlparams['remote_id'] = $remote_port->port['id'];
1587 $confirmmsg .= ' -> '.$remote_port->port['name'];
1588 }
1589
1590 $confirmmsg .= " ($linktype)"; // TODO cableid
1591
1592 echo "<tr><td align=\"center\"><a href='".makeHrefProcess($urlparams).
1593 "' onclick=\"return confirm('$confirmmsg');\">";
1594 printImageHREF ('cut', 'Unlink this port');
1595 echo "</a></td></tr>";
1596
1597 } /* unlink */
1598
1599 /* TODO move to object class */
1600 static function printobjecttable($object_id = NULL) {
1601
1602 if($object_id === NULL)
1603 return;
1604
1605 $object = spotEntity ('object', $object_id);
1606
1607 if($object === false)
1608 return;
1609
1610 if($object['rack_id'])
1611 {
1612 $rack = spotEntity('rack', $object['rack_id']);
1613
1614 $object['row_name'] = $rack['row_name'];
1615 $object['rack_name'] = $rack['name'];
1616 }
1617
1618 echo "<table><tr><td>";
1619 lm_renderObjectCell($object);
1620 echo "</td></tr><tr><td><table>";
1621
1622 self::_printinforow($object,
1623 array(
1624 'id' => 'ID',
1625 'dname' => 'Name',
1626 'label' => 'Label',
1627 'rack_name' => 'Rack',
1628 'row_name' => 'Row',
1629 )
1630
1631 ); /* printinforow */
1632
1633 $urlparams = array(
1634 'module' => 'redirect',
1635 'page' => 'object',
1636 'tab' => 'linkmgmt',
1637 'op' => 'map',
1638 'object_id' => $object_id,
1639 'usemap' => 1,
1640 );
1641
1642 echo '<tr><td><a title="don\'t highlight object" href="?'.http_build_query($urlparams).'">-ohl</a></td>';
1643
1644 $urlparams['hl'] = 'o';
1645 echo '<td><a title="highlight object" href="?'.http_build_query($urlparams).'">+ohl</a></td></tr>';
1646
1647 echo "</table></td></tr></table>";
1648
1649 return $object;
1650
1651 } /* printobjecttable */
1652
1653 static function _printinforow(&$data, $config) {
1654
1655 foreach($config as $key => $name)
1656 {
1657 if(isset($data[$key]))
1658 {
1659 $value = $data[$key];
1660 if(!empty($value))
1661 echo "<tr><td align=\"right\" nowrap=\"nowrap\" style=\"font-size:10;\">$name:</td><td nowrap=\"nowrap\">$value</td></tr>";
1662 }
1663 }
1664
1665 } /* _printinforow */
1666} /* class RTport */
1667
1668/* -------------------------------------------------- */
1669
1670function linkmgmt_opmap() {
1671
1672 /* display require errors "white screen of death" */
1673 $errorlevel = error_reporting();
1674 error_reporting(E_ALL);
1675
1676 require_once 'Image/GraphViz.php';
1677
1678/*
1679 *
1680 */
1681class lm_Image_GraphViz extends Image_GraphViz {
1682
1683 /* extend renderDotFile with additional output file
1684 */
1685 function renderDotFile($dotfile, $outputfile, $format = 'svg',
1686 $command = null, $outputfile2 = null, $format2 = null)
1687 {
1688 if (!file_exists($dotfile)) {
1689 if ($this->_returnFalseOnError) {
1690 return false;
1691 }
1692 $error = PEAR::raiseError('Could not find dot file');
1693 return $error;
1694 }
1695
1696 $oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0;
1697
1698 switch ($command) {
1699 case 'dot':
1700 case 'neato':
1701 break;
1702 default:
1703 $command = $this->graph['directed'] ? 'dot' : 'neato';
1704 }
1705 $command_orig = $command;
1706
1707 $command = $this->binPath.(($command == 'dot') ? $this->dotCommand
1708 : $this->neatoCommand);
1709
1710 $command .= ' -T'.escapeshellarg($format)
1711 .' -o'.escapeshellarg($outputfile)
1712 .($format2 !== null && $outputfile2 !== null ? ' -T'.escapeshellarg($format2).' -o'.escapeshellarg($outputfile2) : '')
1713 .' '.escapeshellarg($dotfile)
1714 .' 2>&1';
1715
1716 exec($command, $msg, $return_val);
1717
1718 clearstatcache();
1719 if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime
1720 && $return_val == 0) {
1721 return true;
1722 } elseif ($this->_returnFalseOnError) {
1723 return false;
1724 }
1725 $error = PEAR::raiseError($command_orig.' command failed: '
1726 .implode("\n", $msg));
1727 return $error;
1728 }
1729 // renderDotFile
1730
1731
1732 /*
1733 */
1734 function fetch($format = 'svg', $command = null, $format2 = null, &$data2 = null)
1735 {
1736
1737 $file = $this->saveParsedGraph();
1738 if (!$file || PEAR::isError($file)) {
1739 return $file;
1740 }
1741
1742 $outputfile = $file . '.' . $format;
1743
1744 if($format2 != null && $data2 !== null)
1745 $outputfile2 = $file . '.' . $format2;
1746 else
1747 $outputfile2 = null;
1748
1749 $rendered = $this->renderDotFile($file, $outputfile, $format,
1750 $command, $outputfile2, $format2);
1751 if ($rendered !== true) {
1752 return $rendered;
1753 }
1754
1755 @unlink($file);
1756
1757 if($format2 !== null && $data2 !== null) {
1758 $fp = fopen($outputfile2, 'rb');
1759
1760 if ($fp) {
1761 $data = fread($fp, filesize($outputfile2));
1762 fclose($fp);
1763 @unlink($outputfile2);
1764
1765 $data2 = $data;
1766 } else {
1767 return $error;
1768 }
1769 }
1770
1771
1772 $fp = fopen($outputfile, 'rb');
1773
1774 if (!$fp) {
1775 if ($this->_returnFalseOnError) {
1776 return false;
83b49cf7 1777 }
1778 $error = PEAR::raiseError('Could not read rendered file');
1779 return $error;
1780 }
1781
3badc888 1782 $data = fread($fp, filesize($outputfile));
1783 fclose($fp);
1784 @unlink($outputfile);
1785
1786 return $data;
1787 }
1788 // fetch
1789
1790
1791
1792} /* class lm_Image_GraphViz */
1793
1794 error_reporting($errorlevel);
1795
1796 $object_id = NULL;
1797 $port_id = NULL;
1798 $remote_id = NULL;
1799 $allports = false;
1800 $usemap = false;
1801 $command = NULL;
1802
1803 /* highlight object */
1804 $hl = NULL;
1805 if(isset($_REQUEST['hl']))
1806 {
1807 $hl = $_REQUEST['hl'];
1808 unset($_REQUEST['hl_object_id']);
1809 unset($_REQUEST['hl_port_id']);
1810
1811 if($hl == 'o')
1812 {
1813 unset($_GET['port_id']);
1814 unset($_GET['remote_id']);
1815 }
1816
1817 }
1818
1819 if(!$hl && isset($_REQUEST['hl_object_id']))
1820 {
1821 $hl = 'o';
1822 $object_id = $_REQUEST['hl_object_id'];
1823 $_REQUEST['object_id'] = $object_id;
1824 unset($_REQUEST['hl_object_id']);
1825 unset($_REQUEST['hl_port_id']);
1826 unset($_REQUEST['port_id']);
1827 }
1828
1829 if(isset($_REQUEST['object_id']))
1830 $object_id = $_REQUEST['object_id'];
1831
1832 if(isset($_REQUEST['type']))
1833 {
1834 $type = $_REQUEST['type'];
1835 }
1836 else
1837 $type = 'gif';
1838
1839 /* highlight port */
1840 if(!$hl && isset($_REQUEST['hl_port_id']))
1841 {
1842 $hl = 'p';
1843 $port_id = $_REQUEST['hl_port_id'];
1844 $_REQUEST['port_id'] = $port_id;
1845 unset($_REQUEST['hl_port_id']);
1846 }
1847
1848 if(isset($_REQUEST['allports']))
1849 {
1850 $allports = $_REQUEST['allports'];
1851 }
1852
1853 if(isset($_REQUEST['port_id']))
1854 {
1855 $port_id = $_REQUEST['port_id'];
1856 }
1857
1858 if(isset($_REQUEST['usemap']))
1859 $usemap = $_REQUEST['usemap'];
1860
1861 if($hl == 'p' && $port_id === NULL)
1862 {
1863 unset($_GET['hl']);
1864 unset($_GET['port_id']);
1865 unset($_GET['remote_id']);
1866 }
1867
1868 if($hl == 'o')
1869 unset($_GET['remote_id']);
1870
1871 if(isset($_REQUEST['remote_id']))
1872 $remote_id = $_REQUEST['remote_id'];
1873
1874 /* show all objects */
1875 if(isset($_REQUEST['all']))
1876 {
1877 $object_id = NULL;
1878 $port_id = NULL;
1879 $hl = NULL;
1880 unset($_GET['hl']);
1881 }
1882
1883 if(isset($_REQUEST['cmd']))
1884 $command = $_REQUEST['cmd'];
1885
1886 if(isset($_REQUEST['debug']))
1887 $debug = $_REQUEST['debug'];
1888 else
1889 $debug = False;
1890
1891 if($debug) echo "-- DEBUG --<br>";
1892
1893
1894 switch($type) {
1895 case 'gif':
1896 case 'png':
1897 case 'bmp':
1898 case 'jpeg':
1899 case 'tif':
1900 case 'wbmp':
1901 $ctype = "image/$type";
1902 break;
1903 case 'jpg':
1904 $ctype = "image/jpeg";
1905 break;
1906 case 'svg':
1907 $ctype = 'image/svg+xml';
1908 break;
1909 case 'pdf':
1910 $ctype = 'application/pdf';
1911 break;
1912 case 'cmapx':
1913 $ctype = 'text/plain';
1914 break;
1915 }
1916
1917 $start = microtime(true);
1918 $gvmap = new linkmgmt_gvmap($object_id, $port_id, $allports, $hl, $remote_id);
1919 $stop = microtime(true);
1920
1921 if($debug)
1922 echo "gvmap Time: ".( $stop - $start )."<br>";
1923
1924 if($debug) echo "-- after gvmap --<br>";
1925
1926 if($usemap)
1927 {
1928
1929 if($debug) echo "-- usemap --<br>";
1930
1931 /* add context menu to Ports, Objects, Links, ...
1932 */
1933
1934 echo "<script>
1935 function initcontextmenu() {
1936 var maps = document.getElementsByTagName('map');
1937 for(var i=0;i<maps.length;i++) {
1938 var areas = maps[i].childNodes;
1939
1940 for(j=0;j<areas.length;j++) {
1941 if(areas[j].nodeType == 1)
1942 {
1943 // console.log(areas[j].id);
1944 // attr = document.createAttribute('onmouseover','ahh');
1945 // areas[j].setAttribute(attr);
1946 // areas[j].onmouseover = 'menu(this);';
1947
1948 areas[j].addEventListener('contextmenu',menu,false);
1949 // areas[j].oncontextmenu = 'menu(this, event);';
1950 // console.log(areas[j].oncontextmenu);
1951 }
1952 }
1953
1954 }
1955
1956 };
1957
1958 function menu(event) {
1959
1960 // console.log('Menu');
1961
1962 if(!event)
1963 event = window.event;
1964
1965 var parent = event.target;
1966
1967 // console.log('--' + parent);
1968
1969 var ids = parent.id.split('-');
1970
1971 if(ids[0] == 'graph1')
1972 return false;
1973
1974 var object_id = ids[0];
1975
1976 var url ='?module=ajax&ac=lm_mapinfo&object_id=' + object_id;
1977
1978 // links ='<li><a href=' + object_id + '>Object</a></li>';
1979
1980 if(ids[1] != '')
1981 {
1982 var port_id = ids[1];
1983 url += '&port_id=' + port_id;
1984 // links += '<li><a href=' + port_id + '>Port</a></li>';
1985
1986 if(ids[2] != '')
1987 {
1988 var remote_id = ids[2];
1989
1990 if(ids[3] != '')
1991 {
1992 var linktype = ids[3];
1993 url += '&remote_id=' + remote_id + '&linktype=' + linktype;
1994 // links += '<li><a href=' + port_id + '_' + remote_id + '_' + linktype + '>Unlink</a></li>';
1995 }
1996 }
1997
1998 }
1999
2000
2001 var xmlHttp = new XMLHttpRequest();
2002 xmlHttp.open('GET', url, false);
2003 xmlHttp.send(null);
2004
2005 var infodiv = document.getElementById('info');
2006 infodiv.innerHTML = xmlHttp.responseText;
2007
2008 // linkdiv = document.getElementById('link');
2009 // linkdiv.innerHTML = links;
2010
2011 var menudiv = document.getElementById('menu');
2012 menudiv.style.position = 'absolute';
2013 menudiv.style.top = (event.clientY + document.body.scrollTop) + 'px';
2014 menudiv.style.left = (event.clientX + document.body.scrollLeft) + 'px';
2015 menudiv.style.display = '';
2016
2017 return false;
2018 };
2019
2020 function mousedown(event) {
2021 // console.log('mouse down');
2022
2023 if(!event)
2024 event = window.event;
2025
2026 if(event.button != 2)
2027 return true;
2028
2029 var menudiv = document.getElementById('menu');
2030
2031 menudiv.style.display = 'none';
2032
2033 return false;
2034 };
2035
2036 </script>";
2037
2038 echo "<body oncontextmenu=\"return false\" onmousedown=\"mousedown(event);\" onload=\"initcontextmenu();\">";
2039
2040 echo "<div id=\"menu\" style=\"display:none; background-color:#ffff90\">
2041 <div id=\"info\"></div>
2042 <ul id=\"link\" style=\"list-style-type:none\"></ul>
2043 </div>";
2044
2045 if($debug)
2046 $gvmap->setFalseOnError(False);
2047
2048 $data2 = '';
2049
2050 $start = microtime(true);
2051 $data = $gvmap->fetch($type, $command, 'cmapx', $data2);
2052 $stop = microtime(true);
2053
2054 if($debug)
2055 echo "DOT time: ".( $stop - $start )."<br>";
2056
2057 if($data === false)
2058 echo "ERROR Fetching image data!<br>";
2059
2060 if(PEAR::isError($data))
2061 echo $data->getMessage();
2062
2063 //echo $gvmap->fetch('cmapx', $command);
2064 echo $data2;
2065
2066 if($debug) echo "-- after map gvmap --<br>";
2067
2068 echo "<img src=\"data:$ctype;base64,".
2069 base64_encode($data).
2070 "\" usemap=#map$object_id />";
2071
2072 if($debug)
2073 {
2074 echo "<pre>";
2075 echo $gvmap->export();
2076 echo "</pre>";
2077
2078 echo "<pre>".$gvmap->parse()."</pre>";
2079 }
2080 }
2081 else
2082 {
2083 $gvmap->image($type);
2084 }
2085
2086 exit;
2087
2088} /* linkmgmt_opmap */
2089
2090class cytoscapedata
2091{
2092 public $objects = array();
2093
2094 public $pids = array();
2095
2096 public $parents = array();
2097 public $nodes = array();
2098
2099 public $edges = NULL;
2100
2101 public $debug = null;
2102
2103 function __construct()
2104 {
2105
2106 $this->edges['parents'] = array();
2107 $this->edges['nodes'] = array();
2108 }
2109
2110 function addnode($id, $values = NULL, &$arr = NULL)
2111 {
2112 $data = array( 'id' => $id );
2113
2114 if($values != NULL)
2115 $data = $data + $values;
2116
2117 $node['data'] = $data;
2118
2119 $this->objects[] = array('group' => 'nodes') + $node;
2120
2121 switch($values['type'])
2122 {
2123 case 'port':
2124 $this->nodes[$id] = array('group' => 'nodes') + $node;
2125 break;
2126 case 'object':
2127 case 'container':
2128 $this->parents[$id] = array('group' => 'nodes') + $node;
2129 break;
2130 }
2131
2132 if($arr !== NULL)
2133 $arr[] = array('group' => 'nodes') + $node;
2134 }
2135
2136 function addedge($id, $source, $target, $values = NULL, &$arr = NULL)
2137 {
2138 $data = array(
2139 'id' => $id,
2140 'source' => $source,
2141 'target' => $target
2142 );
2143
2144 if($values != NULL)
2145 $data = $data + $values;
2146
2147 $edge['data'] = $data;
2148
2149 //$this->elements['edges'][] = $edge;
2150
2151 if($arr !== NULL)
2152 {
2153 $arr[$id] = array('group' => 'edges') + $edge;
2154 }
2155 else
2156 $this->objects[] = array('group' => 'edges') + $edge;
2157
2158 //$this->edges[] = array('group' => 'edges') + $edge;
2159 }
2160
2161 function _addobjectnode($object_id, $type = 'object')
2162 {
2163 global $lc_cache;
2164
2165 if(!isset($this->parents["o$object_id"]))
2166 {
2167 $rack = null;
2168 $object = $lc_cache->getobject($object_id, $rack);
2169
2170 $clustertitle = "${object['dname']}";
2171
2172 //has_problems
2173 //if($object['has_problems'] != 'no')
2174
2175 $rack_text = "";
2176 if(!empty($rack['row_name']) || !empty($rack['name']))
2177 {
2178 $rack_text = "${rack['row_name']} / ${rack['name']}";
2179 }
2180
2181 $data = array('label' => $object['name'], 'text' => $rack_text, 'type' => $type, 'has_problems' => $object['has_problems']);
2182
2183 $container_id = $object['container_id'];
2184 if($container_id)
2185 {
2186 $data['parent'] = "o$container_id";
2187 $this->_addobjectnode($container_id, 'container');
2188 }
2189
2190 $this->addnode('o'.$object_id, $data);
2191 }
2192
2193 }
2194
2195 // cytoscape
2196 function addlinkchain($linkchain, $index) {
2197
2198 foreach($linkchain as $id => $port)
2199 {
2200
2201 if(!$linkchain->linked)
2202 continue;
2203
2204 $this->_addobjectnode($port['object_id']);
2205
2206 $text = (isset($port['portip']) ? $port['portip'] : "" );
2207 $nodedata = array('label' => $port['name'], 'parent' => 'o'.$port['object_id'], 'text' => $text, 'type' => 'port', 'index' => $index , 'loop' => ($linkchain->loop ? '1' : '0'));
2208
2209 //$this->addnode('l_'.$port['id'], array( 'label' => $port['name'], 'parent' => 'p'.$port['id'], 'text' => $text ));
2210
2211 if($port['portcount'] > 1)
2212 foreach($port['chains'] as $mlc)
2213 {
2214 $this->addlinkchain($mlc, 0); // TODO index
2215 }
2216
2217 $prevlinktype = ($port['linktype'] == 'front' ? 'back' : 'front');
2218 if($port[$prevlinktype]['portcount'] > 1)
2219 {
2220 foreach($port[$prevlinktype]['chains'] as $mlc)
2221 {
2222 $this->addlinkchain($mlc, 0, false); // TODO index
2223 }
2224 }
83b49cf7 2225
3badc888 2226 if($port['remote_id'])
2227 {
83b49cf7 2228
3badc888 2229 $this->_addobjectnode($port['remote_object_id']);
83b49cf7 2230
3badc888 2231 $linktype = $port['linktype'];
2232 $edgedata = array('label' => $port['cableid'], 'type' => $linktype, 'loop' => ($linkchain->loop ? '1' : '0'));
83b49cf7 2233
3badc888 2234 if($linkchain->loop && $port['remote_id'] == $linkchain->first)
2235 {
2236 $nodedata['loopedge'] = array('group' => 'edges', 'data' => array( 'id' => 'le'.$port['id']."_".$port['remote_id'], 'source' => 'p'.$port['id'], 'target' => 'p'.$port['remote_id']) + $edgedata);
2237 }
2238 else
2239 {
83b49cf7 2240
3badc888 2241 $this->addedge('e'.$port['id']."_".$port['remote_id'], 'p'.$port['id'], 'p'.$port['remote_id'], $edgedata);
2242 $this->addedge('e'.$port['id']."_".$port['remote_id'], 'p'.$port['id'], 'p'.$port['remote_id'], $edgedata, $this->edges['nodes']);
2243 $id1 = $port['object_id'];
2244 if($id1 > $port['remote_object_id'])
2245 {
2246 $id1 = $port['remote_object_id'];
2247 $id2 = $port['object_id'];
2248 }
2249 else
2250 $id2 = $port['remote_object_id'];
fdc74cf0 2251
3badc888 2252 $peid = "pe".$id1."_".$id2;
fdc74cf0 2253
3badc888 2254 if(isset($this->edges['parents'][$peid]))
2255 $this->edges['parents'][$peid]['data']['linkcount']++;
2256 else
2257 $this->addedge($peid, 'o'.$id1, 'o'.$id2, array('type' => $linktype, 'linkcount' => 1), $this->edges['parents']);
2258 }
2259 }
fdc74cf0 2260
3badc888 2261 $this->addnode('p'.$port['id'], $nodedata);
2262 }
2263
2264 if(0)
2265 if($linkchain->first != $linkchain->last )
fdc74cf0 2266 {
3badc888 2267 $first = $linkchain->first;
2268 $last = $linkchain->last;
2269 $this->addedge("l${first}_${last}",'p'.$first, 'p'.$last, array('type' => 'logical', 'label' => "logical"));
fdc74cf0 2270 }
2271
3badc888 2272 //portlist::var_dump_html($this->parents);
fdc74cf0 2273 }
2274
3badc888 2275 function getlinkchains($object_id) {
fdc74cf0 2276
3badc888 2277 $this->objects = array();
2278 $this->parents = array();
2279 $this->nodes = array();
2280 $this->edges['parents'] = array();
2281 $this->edges['nodes'] = array();
fdc74cf0 2282
3badc888 2283 $this->_getlinkchains($object_id);
fdc74cf0 2284 }
fdc74cf0 2285
3badc888 2286 function getelements()
fdc74cf0 2287 {
3badc888 2288 //lm_linkchain::var_dump_html($this);
2289 return array('parents' => array_values($this->parents),
2290 'nodes' => array_values($this->nodes),
2291 'edges' => array(
2292 'parents' => array_values($this->edges['parents']),
2293 'nodes' => array_values($this->edges['nodes'])
2294 ),
2295 'debug' => $this->debug
2296 );
fdc74cf0 2297
fdc74cf0 2298 }
2299
3badc888 2300 function gettest()
fdc74cf0 2301 {
3badc888 2302 return array_merge(array_values($this->parents), array_values($this->edges['parents']));
2303 //return array_merge(array_values($this->parents), array_values($this->edges['parents']), array_values($this->nodes), array_values($this->edges['nodes']));
fdc74cf0 2304 }
2305
3badc888 2306 function _getlinkchains($object_id) {
fdc74cf0 2307
fdc74cf0 2308
3badc888 2309 // container
2310 // $object = spotEntity('object', $object_id);
2311 $object['ports'] = getObjectPortsAndLinks ($object_id);
fdc74cf0 2312
3badc888 2313 $i = 0;
2314 foreach($object['ports'] as $key => $port)
2315 {
fdc74cf0 2316
3badc888 2317 if(isset($this->pids[$port['id']]))
2318 continue;
2319
2320 $i++;
2321 $lc = new lm_linkchain($port['id']);
2322
2323 $this->pids += $lc->pids;
2324
2325 $this->addlinkchain($lc, $i);
2326
2327 }
2328
2329 $children = getEntityRelatives ('children', 'object', $object_id);
2330
2331 foreach($children as $child)
2332 $this->_getlinkchains($child['entity_id']);
2333 }
2334
2335 function allobjects()
fdc74cf0 2336 {
3badc888 2337
2338 $this->objects = array();
2339 $this->parents = array();
2340 $this->nodes = array();
2341 $this->edges['parents'] = array();
2342 $this->edges['nodes'] = array();
2343
2344 $objects = listCells('object');
2345
2346 $i = 0;
2347 foreach($objects as $object)
2348 {
2349 //echo $object['id']."<br>";
2350 $this->_getlinkchains($object['id']);
2351 $i++;
2352 if($i > 20 ) break;
2353 }
fdc74cf0 2354 }
3badc888 2355}
fdc74cf0 2356
3badc888 2357function linkmgmt_cytoscapemap() {
fdc74cf0 2358
fdc74cf0 2359
3badc888 2360 $object_id = $_GET['object_id'];
fdc74cf0 2361
3badc888 2362 if(isset($_GET['json']))
2363 {
2364 ob_start();
2365 $data = new cytoscapedata();
2366 $data->getlinkchains($object_id);
2367 //$data->allobjects(); // ugly graph;
2368 //echo json_encode($data->objects);
fdc74cf0 2369
3badc888 2370 if(ob_get_length())
2371 $data->debug = ob_get_contents();
fdc74cf0 2372
3badc888 2373 ob_end_clean();
2374
2375 echo json_encode($data->getelements());
fdc74cf0 2376
3badc888 2377 exit;
fdc74cf0 2378 }
2379
3badc888 2380 echo (<<<HTMLEND
2381<!DOCTYPE html>
2382<html>
2383<head>
2384<style>
2385body {
2386 font: 14px helvetica neue, helvetica, arial, sans-serif;
2387}
fdc74cf0 2388
3badc888 2389#cy {
2390 height: 100%;
2391 width: 100%;
2392 position: absolute;
2393 left: 0;
2394 top: 0;
2395}
2396</style>
2397<meta charset=utf-8 />
2398<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
2399<title>Compound nodes</title>
2400<!--<script src="js/jquery-1.4.4.min.js"></script>-->
2401
383230c5 2402<script src="?module=chrome&uri=linkmgmt/jquery-1.11.3.min.js"></script>
2403<script src="?module=chrome&uri=linkmgmt/cytoscape.min.js"></script>
2404<script src="?module=chrome&uri=linkmgmt/dagre.min.js"></script>
2405<script src="?module=chrome&uri=linkmgmt/cytoscape-dagre.js"></script>
2406<link rel="stylesheet" type="text/css" href="?module=chrome&uri=linkmgmt/jquery.qtip.min.css">
2407<script src="?module=chrome&uri=linkmgmt/jquery.qtip.min.js"></script>
2408<script src="?module=chrome&uri=linkmgmt/cytoscape-qtip.js"></script>
3badc888 2409
383230c5 2410<!--<script src="?module=chrome&uri=linkmgmt/cytoscape-css-renderer_mod.js"></script>-->
2411<!--<script src="?module=chrome&uri=linkmgmt/cytoscape.js-navigator.js_mod"></script>-->
3badc888 2412<script>
2413var cy = null;
2414var cy2 = null;
2415var layout = null;
2416var data = null;
2417
2418$(function(){ // on dom ready
2419 var cystyle = [
2420 {
2421 selector: 'node',
2422 css: {
2423 'content': 'data(id)',
2424 'text-wrap': 'wrap'
2425 },
2426 style: {
2427 'background-color': '#666',
2428 'label': 'data(label)',
2429 'width': 'label',
2430 'min-zoomed-font-size' : 8,
2431 'text-valign': 'center',
2432 'text-halign': 'center',
2433 'text-wrap': 'wrap',
2434 'shape': function(ele) {
2435 if(ele.data('type') != 'port')
2436 return 'rectangle';
2437 else
2438 return 'ellipse';
2439 },
fdc74cf0 2440
3badc888 2441 }
2442 },
2443 {
2444 selector: '\$node > node',
2445 css: {
2446 'padding-top': '10px',
2447 'padding-left': '10px',
2448 'padding-bottom': '10px',
2449 'padding-right': '10px',
2450 'text-valign': 'top',
2451 'text-halign': 'center',
2452 'background-color': '#bbb',
2453 'min-zoomed-font-size' : 6,
2454 }
2455 },
2456 {
2457 selector: 'edge',
2458 css: {
2459 'target-arrow-shape': 'triangle',
2460 'line-color': function(ele){
2461 if(ele.data('type') == 'front')
2462 return 'black';
2463 else
2464 return 'grey';
2465 },
2466 'line-style': function(ele){
2467 if(ele.data('type') == 'front')
2468 return 'solid';
2469 else
2470 return 'dashed';
2471 },
2472 'width': function(ele){
2473 var ret = 1;
2474 if(ele.data('type') == 'front')
2475 ret = 3;
2476 else
2477 ret = 5;
fdc74cf0 2478
3badc888 2479 if(1)
2480 if(ele.data('linkcount'))
2481 {
2482 ret = (ele.data('linkcount') * ret);
2483 }
fdc74cf0 2484
3badc888 2485 //console.log(ele.data('id') + ret);
2486 return ret;
2487 },
2488// 'curve-style': 'segments',
2489 'font-size': '8',
2490 'min-zoomed-font-size' : 8,
2491 'label': 'data(label)',
2492 'edge-text-rotation': 'autorotate',
2493 'source-arrow-shape': 'none',
2494 'target-arrow-shape': 'none'
2495 }
2496 },
2497 {
2498 selector: '.logical',
2499 css: {
2500 'line-color': '#0000ff',
2501 'width': 5,
2502 'curve-style': 'segments',
2503 'z-index': 0
2504 }
2505 },
2506 {
2507 selector: ':selected',
2508 css: {
2509 'background-color': 'black',
2510 'line-color': 'black',
2511 'target-arrow-color': 'black',
2512 'source-arrow-color': 'black'
2513 }
2514 },
2515 {
2516 selector: '.highlighted',
2517 css: {
2518 'line-color': '#ff0000',
2519 'background-color': '#ff0000'
2520 }
2521 },
2522 {
2523 selector: '.clhighlighted',
2524 css: {
2525 'line-color': '#00ff00',
2526 'background-color': '#00ff00'
2527 }
2528 }
2529 ];
fdc74cf0 2530
3badc888 2531var cylayout = { name: 'dagre', nodeSep: 3, ready: layoutready, stop: layoutstop };
fdc74cf0 2532
3badc888 2533function highlight(evt) {
fdc74cf0 2534
3badc888 2535 //cy = evt.cy;
fdc74cf0 2536
3badc888 2537 var hlclass = evt.data.hlclass;
2538 var ele = evt.cyTarget;
fdc74cf0 2539
3badc888 2540 if(!ele.data)
2541 return;
fdc74cf0 2542
3badc888 2543 if(ele.data('source'))
2544 ele = cy.$( '#' + ele.data('source'));
fdc74cf0 2545
3badc888 2546 var id = ele.data('id');
fdc74cf0 2547
3badc888 2548 if(ele.isParent())
2549 {
2550 // var childs = cy.$('#' + id).children()
2551 // childs.layout({name:'grid', cols: 2, condense: true});
2552 return;
2553 }
fdc74cf0 2554
fdc74cf0 2555
3badc888 2556// console.log('Event ' + hlclass + ' ' + id );
fdc74cf0 2557
3badc888 2558 // remove existing highlights
2559 cy.$('.' + hlclass).removeClass(hlclass);
fdc74cf0 2560
3badc888 2561 var hleles = ele.closedNeighborhood();
fdc74cf0 2562
3badc888 2563 var hlcount = hleles.length;
fdc74cf0 2564
3badc888 2565 // max 100
2566 for(i=0;i<100;i++)
2567 {
2568 hleles = hleles.closedNeighborhood();
fdc74cf0 2569
3badc888 2570 if(hlcount == hleles.length)
2571 break;
fdc74cf0 2572
3badc888 2573 hlcount = hleles.length;
fdc74cf0 2574
3badc888 2575 }
2576 //console.log('End Loop ' + i );
2577 hleles.addClass(hlclass);
fdc74cf0 2578
3badc888 2579// console.log('Event Type: ' + evt.originalEvent.type)
2580// console.log('Event Type: ' + evt.type)
fdc74cf0 2581
3badc888 2582 if(evt.type != 'click')
2583 return;
fdc74cf0 2584
3badc888 2585 var hleles2 = hleles.clone();
2586 hleles2 = hleles2.add(hleles.parents().clone());
fdc74cf0 2587
3badc888 2588 //var cy2 = evt.data.cy2;
fdc74cf0 2589
3badc888 2590 //var ret = evt.data.ret;
fdc74cf0 2591
3badc888 2592 //var j = ret.parents.concat(ret.nodes).concat(ret.edges.nodes);
fdc74cf0 2593
3badc888 2594 cy2.remove(cy2.elements());
2595 cy2.add(hleles2);
2596 //cy2.add(j);
2597 cy2.layout({name: 'dagre', rankDir: 'LR', ready: layoutready});
2598}
fdc74cf0 2599
3badc888 2600function cytoscapeswitch(evt)
2601{
2602 var b = evt.target;
fdc74cf0 2603
3badc888 2604 var j = null;
2605 if(b.value == "1")
2606 {
2607 j = data.parents.concat(data.edges.parents);
2608 b.value = "0";
2609 b.innerHTML = "Ports";
2610 }
2611 else
2612 {
2613 j = data.parents.concat(data.nodes).concat(data.edges.nodes);
2614 b.value = "1";
2615 b.innerHTML = "Objects";
2616 }
fdc74cf0 2617
3badc888 2618 //layout.stop();
2619 cy.remove(cy.elements());
fdc74cf0 2620
3badc888 2621 cy.add(j);
fdc74cf0 2622
3badc888 2623 cy.layout(cylayout);
2624 //layout.run();
2625}
fdc74cf0 2626
3badc888 2627$.ajax({
2628 type: "GET",
2629 url: "{$_SERVER['PHP_SELF']}",
2630 data: { module: 'redirect',
2631 page: 'object',
2632 tab: 'linkmgmt',
2633 object_id: $object_id,
2634 op: 'cytoscapemap',
2635 json: 'json'
2636 },
2637 dataTye: 'json',
2638 error: function(){ alert("Error loading"); },
2639 success: function(jdata) {
2640
2641 data = JSON.parse(jdata);
2642
2643 if(data.debug)
2644 $('#debug').html(data.debug);
2645
2646 //j = data.parents.concat(data.edges.parents);
2647 j = data.parents.concat(data.nodes).concat(data.edges.nodes);
2648
2649 if(j.length == 0)
2650 {
2651 alert("No Links to display. Closing Window");
2652 window.close();
2653 return;
2654 }
fdc74cf0 2655
3badc888 2656 cy2 = cytoscape({
2657 container: document.getElementById('cy2'),
2658 //renderer: { name: 'css' },
2659
2660 boxSelectionEnabled: false,
2661 autounselectify: true,
2662 style: cystyle,
2663 wheelSensitivity: 0.1,
2664 });
2665 cy2.style().selector('node').style('label', function(node) { return node.data('label') + '\\n' + node.data('text'); });
2666
2667 cystyle.push({
2668 selector: '.loopedge',
2669 css: {
2670 'curve-style': 'segments',
2671 }
2672 });
2673
2674 cy = cytoscape({
2675 container: document.getElementById('cy'),
2676
2677 boxSelectionEnabled: false,
2678 autounselectify: true,
2679 style: cystyle,
2680 wheelSensitivity: 0.1,
2681 elements: j,
2682 layout: cylayout,
2683 });
2684
2685 cy.on('mouseover', { hlclass: 'highlighted' }, highlight );
2686 cy.on('click', { hlclass: 'clhighlighted' }, highlight );
2687
2688 $('#switch').click(cytoscapeswitch);
2689
2690 /*
2691 TODO: node ranking
2692 */
2693
2694 if(0)
2695 cy.layout({
2696 name: 'dagre',
2697 /*
2698 nodeSep: 5,
2699 rankDir: 'TB',
2700 */
2701 //edgeSep: 10,
2702 /*
2703 edgeWeight: function(edge) {
2704 if(edge.data('type') == 'front')
2705 return 10;
2706 else
2707 return 1;
2708
2709 },
2710 */
2711 ready: layoutready,
2712 stop: layoutstop
2713 });
2714
2715 if(1)
2716 cy.$(':child').qtip({
2717
2718 content: function() {
2719
2720 return this.id() + '<br>'
2721 + 'Index: ' + this.data('index') + '<br>'
2722 + 'Name: ' + this.data('label') + '<br>'
2723 + 'Text: ' + this.data('text');
2724 },
2725 position: {
2726 my: 'top center',
2727 at: 'bottom center'
2728 },
2729 style: {
2730 classes: 'qtip-bootstrap',
2731 tip: {
2732 width: 16,
2733 height: 8
2734 },
2735 }
2736 }); // qtip
2737 } // success function
2738 });
2739
2740
2741}); // on dom ready
2742
2743function layoutready(evt) {
2744 var _cy = evt.cy;
fdc74cf0 2745
3badc888 2746 // highlight current object
2747 var object = _cy.$('#o$object_id');
2748 if(object.data('has_problems') == 'no')
2749 object.style('background-color','#ffcccc');
fdc74cf0 2750
3badc888 2751 // highlight object with problems
2752 _cy.$('node[type = "object"][has_problems != "no"]').style('background-color','#ff0000');
de3d42dc 2753
3badc888 2754 var e = _cy.$('node[loop = "1"]').style('background-color','#ff6666');
83b49cf7 2755
3badc888 2756// console.log(e[0].data('loop'));
de3d42dc 2757
3badc888 2758// cy.add({group: 'edges', data: {id:'l691_2991', source: 'p691', target: 'p2991', label: 'test'}, classes: 'logical'});
de3d42dc 2759
3badc888 2760}
fdc74cf0 2761
3badc888 2762function layoutstop(evt) {
2763 var _cy = evt.cy;
2764// cy.elements().locked = true;
2765// cy.add({group: 'nodes', data: {id:'l2493', parent:'p2943', label: 'test'}});
fdc74cf0 2766
fdc74cf0 2767
3badc888 2768 var les = _cy.$('[loopedge]');
fdc74cf0 2769
3badc888 2770 if(les)
fdc74cf0 2771 {
3badc888 2772 _cy.batch( function() {
2773 les.each(function(i, ele) {
2774 var le = ele.data('loopedge');
2775 var edge = _cy.add(le);
2776 edge.addClass('loopedge');
2777 //edge.style('line-color', '#ffffff'); // TypeError text-transform undefined
2778 });
2779 });
2780 _cy.$('edge[loop = "1"]').style('line-color','#ff6666');
fdc74cf0 2781 }
3badc888 2782}
2783</script>
2784</head>
2785<body>
2786<div id="cy" style="position: absolute; height: 80%; width: 100%; left: 0; top: 20%;"></div>
2787<div id="cy2" style="position: absolute; height: 20%; width: 100%; left: 0; top: 0%;"></div>
2788<div id="debug"></div>
2789<button type="button" id="switch" style="position: absolute;" value="1">Object</button>
2790</body>
2791</html>
2792HTMLEND
2793); // echo
fdc74cf0 2794 exit;
3badc888 2795}
fdc74cf0 2796
2797/* ------------------------------------- */
2798class linkmgmt_gvmap {
2799
2800 private $object_id = NULL;
2801 private $port_id = NULL;
2802 private $remote_id = NULL;
3badc888 2803 private $hl = NULL;
fdc74cf0 2804
2805 private $gv = NULL;
2806
2807 private $ports = array();
2808
2809 private $allports = false;
2810 private $back = NULL;
2811
adc1e20b 2812 private $alpha = 'ff';
fdc74cf0 2813
2814 private $errorlevel = NULL;
2815
3badc888 2816 public $data = NULL;
2817
2818 private $pids = array();
2819
2820 function addlinkchainsobject($object_id)
2821 {
2822
2823 $object['ports'] = getObjectPortsAndLinks ($object_id);
2824
2825 if(empty($object['ports']))
2826 {
2827
2828 $hl = false;
2829 $alpha = $this->alpha;
2830 if($this->hl == 'o' && $this->object_id == $object_id)
2831 {
2832 $hl = true;
2833 $this->alpha = 'ff';
2834 }
2835
2836 $this->_addCluster($object_id, $hl, empty($object['ports']));
2837
2838 $this->alpha = $alpha;
2839 return;
2840 }
2841
2842 $i = 0;
2843 foreach($object['ports'] as $key => $port)
2844 {
2845
2846 if(isset($this->pids[$port['id']]))
2847 continue;
2848
2849 $i++;
2850 $lc = new lm_linkchain($port['id']);
2851
2852 $this->pids += $lc->pids;
2853
2854 if($this->allports ||($lc->linkcount > 0))
2855 $this->addlinkchain($lc, $i);
2856 }
2857
2858 }
2859
fdc74cf0 2860 function __construct($object_id = NULL, $port_id = NULL, $allports = false, $hl = NULL, $remote_id = NULL) {
2861 $this->allports = $allports;
2862
4eeb9282 2863 $this->object_id = $object_id;
2864 $this->port_id = $port_id;
2865 $this->remote_id = $remote_id;
3badc888 2866 $this->hl = $hl;
fdc74cf0 2867
2868 $hllabel = "";
2869
2870 /* suppress strict standards warnings for Image_GraphViz and PHP 5.4.0
2871 * output would corrupt image data
2872 */
2873 $this->errorlevel = error_reporting();
857ba778 2874 error_reporting($this->errorlevel & ~E_STRICT);
fdc74cf0 2875
2876 $graphattr = array(
3badc888 2877 'rankdir' => 'LR',
fdc74cf0 2878 // 'ranksep' => '0',
2879 'nodesep' => '0',
2880 // 'overlay' => false,
2881 );
2882
2883 unset($_GET['module']);
2884
2885 $_GET['all'] = 1;
2886
2dbca6fb 2887 $graphattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 2888
2889 unset($_GET['all']);
2890
3badc888 2891 switch($hl)
2892 {
2893 case 'o':
2894 case 'p':
2895 $this->alpha = '30';
2896 break;
2897 }
2898
83b49cf7 2899 //$this->gv = new Image_GraphViz(true, $graphattr, "map".$object_id);
2900 $this->gv = new lm_Image_GraphViz(true, $graphattr, "map".$object_id);
fdc74cf0 2901
3badc888 2902 /* --------------------------- */
fdc74cf0 2903 if($object_id === NULL)
2904 {
4eeb9282 2905 /* all objects ! */
fdc74cf0 2906 unset($_GET['all']);
2907 $_GET['hl'] = 'o';
2908
2909 $this->gv->addAttributes(array(
2910 'label' => 'Showing all objects'.$hllabel,
2911 'labelloc' => 't',
2912 )
2913 );
2914
2915 $objects = listCells('object');
2916
2917 foreach($objects as $obj)
3badc888 2918 //$this->addlinkchainsobject($obj['id']); // longer runtimes !!
2919 $this->_add($this->gv, $obj['id'], NULL); // for all still faster and nicer looking graph
4eeb9282 2920
2921 return;
fdc74cf0 2922 }
2923 else
2924 {
2925 $object = spotEntity ('object', $object_id);
2926
2927 $this->gv->addAttributes(array(
2928 'label' => "Graph for ${object['dname']}$hllabel",
2929 'labelloc' => 't',
2930 )
2931 );
2932
3badc888 2933 $this->addlinkchainsobject($object_id);
2934 //$this->_add($this->gv, $object_id, $port_id);
fdc74cf0 2935
2936 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
2937
2938 foreach($children as $child)
3badc888 2939 $this->addlinkchainsobject($child['entity_id']);
2940 // $this->_add($this->gv, $child['entity_id'], NULL);
fdc74cf0 2941 }
2942
4eeb9282 2943 /* add hl label */
2944 $this->gv->addAttributes(array(
2945 'label' => $this->gv->graph['attributes']['label'].$hllabel,
2946 ));
2947
fdc74cf0 2948 // portlist::var_dump_html($this->gv);
3badc888 2949// portlist::var_dump_html($this->data);
2950
2951// echo json_encode($this->data);
fdc74cf0 2952
83b49cf7 2953 // $this->gv->saveParsedGraph('/tmp/graph.txt');
fdc74cf0 2954 // error_reporting( E_ALL ^ E_NOTICE);
2955 } /* __construct */
2956
2957 function __destruct() {
2958 error_reporting($this->errorlevel);
2959 }
2960
3badc888 2961 function _addCluster($object_id, $hl = false, $adddummy = false)
2962 {
2963 global $lc_cache;
2964
2965 $cluster_id = "c".$object_id;
2966
2967 if(
2968 !isset($this->gv->graph['clusters'][$cluster_id]) &&
2969 !isset($this->gv->graph['subgraphs'][$cluster_id])
2970 || $hl
2971 ) {
2972 $rack = null;
2973 $object = $lc_cache->getobject($object_id, $rack);
2974
2975 // $object['attr'] = getAttrValues($object_id);
2976
2977 $clusterattr = array();
2978
2979 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
2980 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
2981
2982 if($this->object_id == $object_id)
2983 {
2984 $clusterattr['rank'] = 'source';
2985
2986 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
2987 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
2988 }
2989
2990 $clustertitle = htmlspecialchars($object['dname']);
2991 $clusterattr['tooltip'] = $clustertitle;
2992
2993 unset($_GET['module']); // makeHrefProcess adds this
2994 unset($_GET['port_id']);
2995 unset($_GET['remote_id']);
2996 $_GET['object_id'] = $object_id;
2997 //$_GET['hl'] = 'o';
2998
2999 $clusterattr['URL'] = $this->_makeHrefProcess($_GET);
3000
3001 //has_problems
3002 if($object['has_problems'] != 'no')
3003 {
3004 $clusterattr['style'] = 'filled';
3005 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
3006 }
3007
3008 if(!empty($object['container_name']))
3009 $clustertitle .= "<BR/>(${object['container_name']})";
3010
3011 if(!empty($rack['row_name']) || !empty($rack['name']))
3012 $clustertitle .= "<BR/><FONT point-size=\"10\">{$rack['row_name']} / {$rack['name']}</FONT>";
3013
3014 $embedin = $object['container_id'];
3015 if(empty($embedin))
3016 $embedin = 'default';
3017 else
3018 {
3019 $embedin = "c$embedin"; /* see cluster_id */
3020
3021 // TODO
3022 /* add container / cluster if not already exists */
3023 $this->addlinkchainsobject($object['container_id']);
3024 }
3025
3026 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
3027
3028 $this->gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
3029
3030 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
3031 if($adddummy)
3032 $this->gv->addNode("dummy$cluster_id", array(
3033 // 'label' =>'No Ports found/connected',
3034 'label' =>'',
3035 'fontsize' => 0,
3036 'size' => 0,
3037 'width' => 0,
3038 'height' => 0,
3039 'shape' => 'point',
3040 'style' => 'invis',
3041 ), $cluster_id);
3042
3043
3044 }
3045 } // _addCluster
3046
3047 function _addEdge($port, $linkchain, $loopedge = false)
3048 {
3049 global $lm_multilink_port_types;
3050 if(
3051 !isset($this->gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
3052 !isset($this->gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
3053 || $loopedge
3054 ) {
3055 $remote_id = $port['remote_id'];
3056
3057 $linktype = $port['linktype'];
3058
3059 $edgetooltip = $port['object_name'].':'.$port['name'].
3060 ' - '.$port['cableid'].' -> '.
3061 $port['remote_name'].':'.$port['remote_object_name'];
3062
3063 $edgeattr = array(
3064 'fontsize' => 8,
3065 'label' => htmlspecialchars($port['cableid']),
3066 'tooltip' => $edgetooltip,
3067 'sametail' => $linktype,
3068 'samehead' => $linktype,
3069 'arrowhead' => 'none',
3070 'arrowtail' => 'none',
3071 );
3072
3073 $this->_getcolor('edge', ($linkchain->loop ? 'loop' : 'default'), $this->alpha, $edgeattr, 'color');
3074 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
3075
3076 if($linktype == 'back' )
3077 {
3078 $edgeattr['style'] = 'dashed';
3079
3080 /* multilink ports */
3081 if(in_array($port['oif_id'], $lm_multilink_port_types))
3082 {
3083 $edgeattr['dir'] = 'both';
3084 $edgeattr['arrowtail'] = 'dot';
3085 }
3086
3087 if(in_array($linkchain->ports[$remote_id]['oif_id'], $lm_multilink_port_types))
3088 {
3089 $edgeattr['dir'] = 'both';
3090 $edgeattr['arrowhead'] = 'dot';
3091 }
3092 }
3093
3094 if(
3095 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
3096 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
3097 )
3098 {
3099 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
3100 $edgeattr['penwidth'] = 2; /* bold */
3101 }
3102
3103 unset($_GET['module']);
3104 $_GET['object_id'] = $port['object_id'];
3105 $_GET['port_id'] = $port['id'];
3106 $_GET['remote_id'] = $port['remote_id'];
3107
3108 $edgeattr['URL'] = $this->_makeHrefProcess($_GET);
3109
3110 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
3111
3112 if($loopedge)
3113 {
3114 $edgeattr = array_merge($edgeattr, array(
3115 'sametail' => 'loop',
3116 'samehead' => 'loop',
3117 'dir' => 'both',
3118 'arrowhead' => 'invodot',
3119 'arrowtail' => 'invodot',
3120 ));
3121 }
3122
3123 $this->gv->addEdge(array($port['id'] => $port['remote_id']),
3124 $edgeattr,
3125 array(
3126 $port['id'] => $linktype,
3127 $port['remote_id'] => $linktype,
3128 )
3129 );
3130
3131 }
3132 } // _addEdge
3133
3134 function addlinkchain($linkchain, $index)
3135 {
3136 global $lm_multilink_port_types;
3137
3138 $hl = false;
3139 $alpha = $this->alpha;
3140 if(
3141 ($this->hl == 'p' && $linkchain->hasport_id($this->port_id))
3142 || ($this->hl == 'o' && $linkchain->hasobject_id($this->object_id))
3143 )
3144 {
3145 $hl = true;
3146 $this->alpha = 'ff';
3147 }
3148
3149 $remote_id = null;
3150 foreach($linkchain as $id => $port)
3151 {
3152 $this->_addCluster($port['object_id'], $hl);
3153
3154 $nodelabel = htmlspecialchars("${port['name']}");
3155 $text = $nodelabel;
3156
3157 if($port['iif_id'] != '1' )
3158 {
3159 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
3160 $text .= "\n".$port['iif_name'];
3161 }
3162
3163 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
3164 $text .= "\n".$port['oif_name'];
3165
3166 // add ip address
3167 if(isset($port['portip']))
3168 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">".$port['portip']."</FONT>";
3169
3170 $nodeattr = array(
3171 'label' => $nodelabel,
3172 );
3173
3174 $this->_getcolor('port', ($linkchain->loop ? 'loop' : 'default'),$this->alpha, $nodeattr, 'fontcolor');
3175 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
3176
3177 if($this->port_id == $port['id']) {
3178 $nodeattr['style'] = 'filled';
3179 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
3180 }
3181
3182 if($this->remote_id == $port['id']) {
3183 $nodeattr['style'] = 'filled';
3184 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
3185 }
3186
3187 $nodeattr['tooltip'] = htmlspecialchars("${port['name']}");
3188
3189 unset($_GET['module']);
3190 unset($_GET['remote_id']);
3191 $_GET['object_id'] = $port['object_id'];
3192 $_GET['port_id'] = $port['id'];
3193 $_GET['hl'] = 'p';
3194
3195 $nodeattr['URL'] = $this->_makeHrefProcess($_GET);
3196 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
3197
3198 $this->gv->addNode($port['id'],
3199 $nodeattr,
3200 "c${port['object_id']}"); /* see cluster_id */
3201
3202 $remote_id = $port['remote_id'];
3203
3204 if($port['portcount'] > 1)
3205 foreach($port['chains'] as $mlc)
3206 {
3207 $this->addlinkchain($mlc, 0); // TODO index
3208 }
3209
3210 $prevlinktype = ($port['linktype'] == 'front' ? 'back' : 'front');
3211 if($port[$prevlinktype]['portcount'] > 1)
3212 {
3213 foreach($port[$prevlinktype]['chains'] as $mlc)
3214 {
3215 $this->addlinkchain($mlc, 0, false); // TODO index
3216 }
3217 }
3218
3219 if($remote_id)
3220 $this->_addEdge($port, $linkchain);
3221
3222 } //foreach
3223
3224 if($linkchain->loop && $remote_id)
3225 {
3226 // TODO separate loop link
3227 // add loop edge
3228 $this->_addEdge($port, $linkchain, true);
3229 }
3230
3231 // reset alpha to start value
3232 $this->alpha = $alpha;
3233 }
3234
de3d42dc 3235 function setFalseOnError($newvalue)
3236 {
3237 $this->gv->_returnFalseOnError = $newvalue;
3238 }
3239
2dbca6fb 3240 function _makeHrefProcess($array)
3241 {
3242 return str_replace('&','&amp;',makeHrefProcess($array));
3243 }
3244
fdc74cf0 3245 // !!!recursiv !!!
3246 function _add($gv, $object_id, $port_id = NULL) {
3247 global $lm_multilink_port_types;
3248
4eeb9282 3249 if($port_id !== NULL) {
3250 if(isset($this->ports[$port_id])) {
fdc74cf0 3251 return;
4eeb9282 3252 }
fdc74cf0 3253 }
3254
2d257b3a 3255 if($this->back != 'front' || $port_id === NULL || $this->allports)
3256 $front = $this->_getObjectPortsAndLinks($object_id, 'front', $port_id, $this->allports);
3257 else
3258 $front = array();
3259
3260 if($this->back != 'back' || $port_id === NULL || $this->allports)
3261 $backend = $this->_getObjectPortsAndLinks($object_id, 'back', $port_id, $this->allports);
3262 else
3263 $backend = array();
3264
3265 $ports = array_merge($front,$backend);
3266
630a86c8 3267 /* used only for Graphviz ...
3268 * !! numeric ids cause Image_Graphviz problems on nested clusters !!
3269 */
3270 $cluster_id = "c$object_id";
3271
2d257b3a 3272 if(empty($ports))
3273 {
3274 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
da6d05d0 3275 $gv->addNode("dummy$cluster_id", array(
2d257b3a 3276 // 'label' =>'No Ports found/connected',
3277 'label' =>'',
3278 'fontsize' => 0,
3279 'size' => 0,
3280 'width' => 0,
3281 'height' => 0,
3282 'shape' => 'point',
3283 'style' => 'invis',
3284 ), $cluster_id);
3285
3286 /* show objects without ports */
3287 if($object_id === NULL)
3288 return;
3289 }
3290
3badc888 3291 $object = NULL;
2d257b3a 3292 if($object_id !== NULL) {
4eeb9282 3293 if(
3294 !isset($gv->graph['clusters'][$cluster_id]) &&
3295 !isset($gv->graph['subgraphs'][$cluster_id])
3296 ) {
fdc74cf0 3297
4eeb9282 3298 $object = spotEntity ('object', $object_id);
3badc888 3299
3300 // ip addresses
3301 amplifyCell($object);
3302 $object['portip'] = array();
3303 foreach($object['ipv4'] as $ipv4)
3304 {
3305 $object['portip'][$ipv4['osif']] = $ipv4['addrinfo']['ip'];
3306 }
3307
4eeb9282 3308 // $object['attr'] = getAttrValues($object_id);
fdc74cf0 3309
4eeb9282 3310 $clusterattr = array();
fdc74cf0 3311
4eeb9282 3312 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
3313 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
fdc74cf0 3314
4eeb9282 3315 if($this->object_id == $object_id)
3316 {
3317 $clusterattr['rank'] = 'source';
fdc74cf0 3318
4eeb9282 3319 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
3320 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
3321 }
fdc74cf0 3322
4eeb9282 3323 $clustertitle = "${object['dname']}";
3badc888 3324 $text = "${object['dname']}";
4eeb9282 3325 $clusterattr['tooltip'] = $clustertitle;
fdc74cf0 3326
4eeb9282 3327 unset($_GET['module']); // makeHrefProcess adds this
3328 unset($_GET['port_id']);
3329 unset($_GET['remote_id']);
3330 $_GET['object_id'] = $object_id;
3331 //$_GET['hl'] = 'o';
fdc74cf0 3332
2dbca6fb 3333 $clusterattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 3334
4eeb9282 3335 //has_problems
3336 if($object['has_problems'] != 'no')
3337 {
3338 $clusterattr['style'] = 'filled';
3339 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
3340 }
fdc74cf0 3341
4eeb9282 3342 if(!empty($object['container_name']))
3badc888 3343 {
4eeb9282 3344 $clustertitle .= "<BR/>${object['container_name']}";
3badc888 3345 $text .= "\n${object['container_name']}";
3346 }
fdc74cf0 3347
4eeb9282 3348 if($object['rack_id'])
3349 {
3350 $rack = spotEntity('rack', $object['rack_id']);
fdc74cf0 3351
4eeb9282 3352 if(!empty($rack['row_name']) || !empty($rack['name']))
3badc888 3353 {
4eeb9282 3354 $clustertitle .= "<BR/>${rack['row_name']} / ${rack['name']}";
3badc888 3355 $text .= "\n${rack['row_name']} / ${rack['name']}";
3356 }
4eeb9282 3357 }
3358
3359 $embedin = $object['container_id'];
3360 if(empty($embedin))
3361 $embedin = 'default';
3362 else
3363 {
3364 $embedin = "c$embedin"; /* see cluster_id */
3365
3366 /* add container / cluster if not already exists */
3367 $this->_add($gv, $object['container_id'], NULL);
3368 }
fdc74cf0 3369
2d257b3a 3370 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
fdc74cf0 3371
2d257b3a 3372 $gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
fdc74cf0 3373
2d257b3a 3374 } /* isset cluster_id */
3375 } /* object_id !== NULL */
fca4e38a 3376
fdc74cf0 3377 foreach($ports as $key => $port) {
3378
3379 $this->back = $port['linktype'];
3380
4eeb9282 3381 if(!isset($this->ports[$port['id']])) {
fdc74cf0 3382
fdc74cf0 3383
c613fc73 3384 $nodelabel = htmlspecialchars("${port['name']}");
3badc888 3385 $text = $nodelabel;
fdc74cf0 3386
4eeb9282 3387 if($port['iif_id'] != '1' )
3badc888 3388 {
4eeb9282 3389 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
3badc888 3390 $text .= "\n".$port['iif_name'];
3391 }
fdc74cf0 3392
4eeb9282 3393 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
3badc888 3394 $text .= "\n".$port['oif_name'];
3395
3396 // add ip address
3397 if($object)
3398 if(isset($object['portip'][$port['name']]))
3399 {
3400 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">".$object['portip'][$port['name']]."</FONT>";
3401 $text .= "\n".$object['portip'][$port['name']];
3402 }
fdc74cf0 3403
4eeb9282 3404 $nodeattr = array(
3405 'label' => $nodelabel,
3406 );
fdc74cf0 3407
4eeb9282 3408 $this->_getcolor('port', 'default',$this->alpha, $nodeattr, 'fontcolor');
3409 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
fdc74cf0 3410
4eeb9282 3411 if($this->port_id == $port['id']) {
3412 $nodeattr['style'] = 'filled';
3413 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
3414 }
fdc74cf0 3415
4eeb9282 3416 if($this->remote_id == $port['id']) {
3417 $nodeattr['style'] = 'filled';
3418 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
3419 }
3420
c613fc73 3421 $nodeattr['tooltip'] = htmlspecialchars("${port['name']}");
4eeb9282 3422
3423 unset($_GET['module']);
3424 unset($_GET['remote_id']);
3425 $_GET['object_id'] = $port['object_id'];
3426 $_GET['port_id'] = $port['id'];
3427 $_GET['hl'] = 'p';
fdc74cf0 3428
2dbca6fb 3429 $nodeattr['URL'] = $this->_makeHrefProcess($_GET);
4eeb9282 3430 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
fdc74cf0 3431
4eeb9282 3432 $gv->addNode($port['id'],
fdc74cf0 3433 $nodeattr,
3434 "c${port['object_id']}"); /* see cluster_id */
3435
4eeb9282 3436 $this->ports[$port['id']] = true;
2d257b3a 3437
4eeb9282 3438 } /* isset port */
fdc74cf0 3439
3440 if(!empty($port['remote_id'])) {
3441
bb000ce6 3442 if($this->object_id !== NULL)
3443 $this->_add($gv, $port['remote_object_id'], $port['remote_id']);
fdc74cf0 3444
3445 if(
3446 !isset($gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
3447 !isset($gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
3448 ) {
3449
3450 $linktype = $port['linktype'];
3451
3452 $edgetooltip = $port['object_name'].':'.$port['name'].
3453 ' - '.$port['cableid'].' -> '.
3454 $port['remote_name'].':'.$port['remote_object_name'];
3455
3456 $edgeattr = array(
3457 'fontsize' => 8,
c613fc73 3458 'label' => htmlspecialchars($port['cableid']),
fdc74cf0 3459 'tooltip' => $edgetooltip,
3460 'sametail' => $linktype,
3461 'samehead' => $linktype,
3462 );
3463
3464 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'color');
3465 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
3466
3467 if($linktype == 'back' )
3468 {
3469 $edgeattr['style'] = 'dashed';
3470 $edgeattr['arrowhead'] = 'none';
3471 $edgeattr['arrowtail'] = 'none';
3472
3473 /* multilink ports */
3474 if(in_array($port['oif_id'], $lm_multilink_port_types))
3475 {
3476 $edgeattr['dir'] = 'both';
3477 $edgeattr['arrowtail'] = 'dot';
3478 }
3479
3480 if(in_array($port['remote_oif_id'], $lm_multilink_port_types))
3481 {
3482 $edgeattr['dir'] = 'both';
3483 $edgeattr['arrowhead'] = 'dot';
3484 }
3485 }
3486
3487 if(
3488 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
3489 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
3490 )
3491 {
3492 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
3493 $edgeattr['penwidth'] = 2; /* bold */
3494 }
3495
3496 unset($_GET['module']);
3497 $_GET['object_id'] = $port['object_id'];
3498 $_GET['port_id'] = $port['id'];
3499 $_GET['remote_id'] = $port['remote_id'];
3500
2dbca6fb 3501 $edgeattr['URL'] = $this->_makeHrefProcess($_GET);
fdc74cf0 3502
3503 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
3504
3505 $gv->addEdge(array($port['id'] => $port['remote_id']),
3506 $edgeattr,
3507 array(
3508 $port['id'] => $linktype,
3509 $port['remote_id'] => $linktype,
3510 )
3511 );
3badc888 3512
fdc74cf0 3513 }
3514 }
3515
3516 }
3517
3518 // portlist::var_dump_html($port);
3519 }
3520
83b49cf7 3521 function fetch($type = 'png', $command = NULL, $format2 = NULL, &$data2 = NULL) {
3522
3523 $tmpdata = $data2;
3524 $ret = $this->gv->fetch($type, $command, $format2, $tmpdata);
3525 if($data2 !== NULL)
3526 $data2 = $tmpdata;
fdc74cf0 3527 return $ret;
3528 }
3529
3530 function image($type = 'png', $command = NULL) {
3531 $this->gv->image($type, $command);
3532 }
3533
3534 function parse() {
3535 return $this->gv->parse();
3536 }
3537
3538 /* should be compatible with getObjectPortsAndLinks from RT database.php */
3539 function _getObjectPortsAndLinks($object_id, $linktype = 'front', $port_id = NULL, $allports = false) {
3540
3541 if($linktype == 'front')
3542 $linktable = 'Link';
3543 else
3544 $linktable = 'LinkBackend';
3545
3546 $qparams = array();
3547
3548 $query = "SELECT
3549 '$linktype' as linktype,
3550 Port.*,
3551 Port.type AS oif_id,
3552 PortInnerInterface.iif_name as iif_name,
262da468 3553 POI.oif_name as oif_name,
fdc74cf0 3554 Object.id as object_id, Object.name as object_name,
2a5cd259 3555 IFNULL(LinkTable_a.cable,LinkTable_b.cable) as cableid,
fdc74cf0 3556 remoteObject.id as remote_object_id, remoteObject.name as remote_object_name,
3557 remotePort.id as remote_id, remotePort.name as remote_name,
3558 remotePort.type AS remote_oif_id,
3559 remotePortInnerInterface.iif_name as remote_iif_name,
262da468 3560 remotePOI.oif_name as remote_oif_name
fdc74cf0 3561 FROM Port";
3562
3563 // JOIN
3564 $join = " LEFT JOIN PortInnerInterface on PortInnerInterface.id = Port.iif_id
262da468 3565 LEFT JOIN PortOuterInterface AS POI on POI.id = Port.type
2be16f85 3566 LEFT JOIN $linktable as LinkTable_a on Port.id = LinkTable_a.porta
2a5cd259 3567 LEFT JOIN $linktable as LinkTable_b on Port.id = LinkTable_b.portb
fdc74cf0 3568 LEFT JOIN Object on Object.id = Port.object_id
2a5cd259 3569 LEFT JOIN Port as remotePort on remotePort.id = IFNULL(LinkTable_a.portb, LinkTable_b.porta)
fdc74cf0 3570 LEFT JOIN Object as remoteObject on remoteObject.id = remotePort.object_id
3571 LEFT JOIN PortInnerInterface as remotePortInnerInterface on remotePortInnerInterface.id = remotePort.iif_id
262da468 3572 LEFT JOIN PortOuterInterface as remotePOI on remotePOI.id = remotePort.type
fdc74cf0 3573 ";
3574
3575 // WHERE
3576 if($port_id === NULL)
3577 {
3578 $where = " WHERE Object.id = ?";
3579 $qparams[] = $object_id;
3580
09749fb9 3581 if(!$allports) {
fdc74cf0 3582 $where .= " AND remotePort.id is not NULL";
09749fb9 3583
3584 if($linktype != 'front') {
2a5cd259 3585 $join .= "
3586 LEFT JOIN Link as FrontLink_a on Port.id = FrontLink_a.porta
3587 LEFT JOIN Link as FrontLink_b on Port.id = FrontLink_b.portb
3588 LEFT JOIN Link as FrontRemoteLink_a on remotePort.id = FrontRemoteLink_a.porta
3589 LEFT JOIN Link as FrontRemoteLink_b on remotePort.id = FrontRemoteLink_b.portb
09749fb9 3590 ";
2a5cd259 3591 $where .= " AND ( (FrontLink_a.porta is not NULL or FrontLink_b.portb is not NULL )
3592 OR (FrontRemoteLink_a.porta is not NULL or FrontRemoteLink_b.portb is not NULL) )";
09749fb9 3593 }
3594 }
fdc74cf0 3595 }
3596 else
3597 {
3598 // $where = " WHERE Port.id = ? and remotePort.id is not NULL";
3599 $where = " WHERE Port.id = ?";
3600 $qparams[] = $port_id;
3601 }
3602
3603 // ORDER
3604 $order = " ORDER by oif_name, Port.Name";
3605
3606 $query .= $join.$where.$order;
3607
2a5cd259 3608 //echo "$port_id: $query<br><br>";
3609
fdc74cf0 3610 $result = usePreparedSelectBlade ($query, $qparams);
3611
3612 $row = $result->fetchAll(PDO::FETCH_ASSOC);
3613
3614 $result->closeCursor();
3615
3616 return $row;
3617 }
3618
3619 function _getcolor($type = 'object', $key = 'default', $alpha = 'ff', &$array = NULL , $arraykey = 'color') {
3620
3621 $object = array(
3622 'current' => '#ff0000',
3623 );
3624 $port = array(
3625 'current' => '#ffff90',
3626 'remote' => '#ffffD0',
3badc888 3627 'loop' => '#ff6666',
fdc74cf0 3628 );
3629
3630 $cluster = array(
3631 'current' => '#ff0000',
3632 'problem' => '#ff3030',
3633 );
3634
3635 $edge = array (
3636 'highlight' => '#ff0000',
3badc888 3637 'loop' => '#ff6666',
fdc74cf0 3638 );
3639
3640 $oif_id = array(
3641 '16' => '#800000', /* AC-in */
3642 '1322' => '#ff4500', /* AC-out */
3643 '24' => '#000080', /* 1000base-t */
3644 );
3645
3646 $defaultcolor = '#000000'; /* black */
3647 $default = true;
3648
3649 if(isset(${$type}[$key]))
3650 {
3651 $default = false;
3652 $color = ${$type}[$key];
3653 }
3654 else
3655 $color = $defaultcolor;
3656
3657
3658 if($alpha != 'ff' || $default == false)
3659 {
3660 $color .= $alpha;
3661
3662 if($array !== NULL)
3663 $array[$arraykey] = $color;
3664 else
3665 return $color;
3666 }
3667 else
3668 return $defaultcolor;
3669
3670 } /* _getcolor */
3671
3672 function dump() {
3673 var_dump($this->gv);