예제 #1
0
function amplifyAllocationList($alloc_list)
{
    $ret = array();
    $sorted = array();
    foreach ($alloc_list as $ip_bin => $alloc) {
        $sorted[$alloc['osif']][$ip_bin] = $alloc;
    }
    foreach (sortPortList($sorted) as $osif => $subarray) {
        foreach ($subarray as $ip_bin => $alloc) {
            $alloc['addrinfo'] = getIPAddress($ip_bin);
            $ret[$ip_bin] = $alloc;
        }
    }
    return $ret;
}
예제 #2
0
function findSparePorts($port_info, $filter)
{
    $qparams = array();
    $query = "\nSELECT\n\tp.id,\n\tp.name,\n\tp.reservation_comment,\n\tp.iif_id,\n\tp.type as oif_id,\n\tpii.iif_name,\n\tpoi.oif_name,\n\tp.object_id,\n\to.name as object_name\nFROM Port p\nINNER JOIN Object o ON o.id = p.object_id\nINNER JOIN PortInnerInterface pii ON p.iif_id = pii.id\nINNER JOIN PortOuterInterface poi ON poi.id = p.type\n";
    // porttype filter (non-strict match)
    $query .= "\nINNER JOIN (\n\tSELECT Port.id FROM Port\n\tINNER JOIN\n\t(\n\t\tSELECT DISTINCT\tpic2.iif_id\n\t\tFROM PortInterfaceCompat pic2\n\t\tINNER JOIN PortCompat pc ON pc.type2 = pic2.oif_id\n";
    if ($port_info['iif_id'] != 1) {
        $query .= " INNER JOIN PortInterfaceCompat pic ON pic.oif_id = pc.type1 WHERE pic.iif_id = ? AND ";
        $qparams[] = $port_info['iif_id'];
    } else {
        $query .= " WHERE pc.type1 = ? AND ";
        $qparams[] = $port_info['oif_id'];
    }
    $query .= "\n\t\t\tpic2.iif_id <> 1\n\t) AS sub1 USING (iif_id)\n\tUNION\n\tSELECT Port.id\n\tFROM Port\n\tINNER JOIN PortCompat ON type1 = type\n\tWHERE\n\t\tiif_id = 1 and type2 = ?\n) AS sub2 ON sub2.id = p.id\n";
    $qparams[] = $port_info['oif_id'];
    // self and linked ports filter
    $query .= " WHERE p.id <> ? " . "AND p.id NOT IN (SELECT porta FROM Link) " . "AND p.id NOT IN (SELECT portb FROM Link) ";
    $qparams[] = $port_info['id'];
    // rack filter
    if (!empty($filter['racks'])) {
        // objects directly mounted in the racks
        $query .= sprintf('AND p.object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (%s) ', questionMarks(count($filter['racks'])));
        // children of objects directly mounted in the racks
        $query .= sprintf("UNION SELECT child_entity_id FROM EntityLink WHERE parent_entity_type='object' AND child_entity_type = 'object' AND parent_entity_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (%s)) ", questionMarks(count($filter['racks'])));
        // zero-U objects mounted to the racks
        $query .= sprintf("UNION SELECT child_entity_id FROM EntityLink WHERE parent_entity_type='rack' AND child_entity_type='object' AND parent_entity_id IN (%s)) ", questionMarks(count($filter['racks'])));
        $qparams = array_merge($qparams, $filter['racks']);
        $qparams = array_merge($qparams, $filter['racks']);
        $qparams = array_merge($qparams, $filter['racks']);
    }
    // objectname filter
    if (!empty($filter['objects'])) {
        $query .= 'AND o.name like ? ';
        $qparams[] = '%' . $filter['objects'] . '%';
    }
    // asset_no filter
    if (!empty($filter['asset_no'])) {
        $query .= 'AND o.asset_no like ? ';
        $qparams[] = '%' . $filter['asset_no'] . '%';
    }
    // portname filter
    if (!empty($filter['ports'])) {
        $query .= 'AND p.name LIKE ? ';
        $qparams[] = '%' . $filter['ports'] . '%';
    }
    // ordering
    $query .= ' ORDER BY o.name';
    $ret = array();
    $result = usePreparedSelectBlade($query, $qparams);
    $rows_by_pn = array();
    $prev_object_id = NULL;
    // fetch port rows from the DB
    while (TRUE) {
        $row = $result->fetch(PDO::FETCH_ASSOC);
        if (isset($prev_object_id) and (!$row or $row['object_id'] != $prev_object_id)) {
            // handle sorted object's portlist
            foreach (sortPortList($rows_by_pn) as $ports_subarray) {
                foreach ($ports_subarray as $port_row) {
                    $port_description = $port_row['object_name'] . ' --  ' . $port_row['name'];
                    if (count($ports_subarray) > 1) {
                        $if_type = $port_row['iif_id'] == 1 ? $port_row['oif_name'] : $port_row['iif_name'];
                        $port_description .= " ({$if_type})";
                    }
                    if (!empty($port_row['reservation_comment'])) {
                        $port_description .= '  --  ' . $port_row['reservation_comment'];
                    }
                    $ret[$port_row['id']] = $port_description;
                }
            }
            $rows_by_pn = array();
        }
        $prev_object_id = $row['object_id'];
        if ($row) {
            $rows_by_pn[$row['name']][] = $row;
        } else {
            break;
        }
    }
    return $ret;
}
function getObjectBackendPortsAndLinks($object_id)
{
    $ret = fetchBackendPortList("Port.object_id = ?", array($object_id));
    return sortPortList($ret, TRUE);
}
예제 #4
0
파일: interface.php 프로젝트: xtha/salt
function renderDiscoveredNeighbors($object_id)
{
    global $tabno;
    $opcode_by_tabno = array('livecdp' => 'getcdpstatus', 'livelldp' => 'getlldpstatus');
    try {
        $neighbors = queryDevice($object_id, $opcode_by_tabno[$tabno]);
        $neighbors = sortPortList($neighbors);
    } catch (RTGatewayError $e) {
        showError($e->getMessage());
        return;
    }
    $mydevice = spotEntity('object', $object_id);
    amplifyCell($mydevice);
    // reindex by port name
    $myports = array();
    foreach ($mydevice['ports'] as $port) {
        if (mb_strlen($port['name'])) {
            $myports[$port['name']][] = $port;
        }
    }
    // scroll to selected port
    if (isset($_REQUEST['hl_port_id'])) {
        assertUIntArg('hl_port_id');
        $hl_port_id = intval($_REQUEST['hl_port_id']);
        addAutoScrollScript("port-{$hl_port_id}");
    }
    switchportInfoJS($object_id);
    // load JS code to make portnames interactive
    printOpFormIntro('importDPData');
    echo '<br><table cellspacing=0 cellpadding=5 align=center class=widetable>';
    echo '<tr><th colspan=2>local port</th><th></th><th>remote device</th><th colspan=2>remote port</th><th><input type="checkbox" checked id="cb-toggle"></th></tr>';
    $inputno = 0;
    foreach ($neighbors as $local_port => $remote_list) {
        $initial_row = TRUE;
        // if port has multiple neighbors, the first table row is initial
        // array of local ports with the name specified by DP
        $local_ports = isset($myports[$local_port]) ? $myports[$local_port] : array();
        foreach ($remote_list as $dp_neighbor) {
            $error_message = NULL;
            $link_matches = FALSE;
            $portinfo_local = NULL;
            $portinfo_remote = NULL;
            $variants = array();
            do {
                // once-cyle fake loop used only to break out of it
                if (!empty($local_ports)) {
                    $portinfo_local = $local_ports[0];
                }
                // find remote object by DP information
                $dp_remote_object_id = searchByMgmtHostname($dp_neighbor['device']);
                if (!$dp_remote_object_id) {
                    $dp_remote_object_id = lookupEntityByString('object', $dp_neighbor['device']);
                }
                if (!$dp_remote_object_id) {
                    $error_message = "No such neighbor <i>{$dp_neighbor['device']}</i>";
                    break;
                }
                $dp_remote_object = spotEntity('object', $dp_remote_object_id);
                amplifyCell($dp_remote_object);
                $dp_neighbor['port'] = shortenIfName($dp_neighbor['port'], NULL, $dp_remote_object['id']);
                // get list of ports that have name matching CDP portname
                $remote_ports = array();
                // list of remote (by DP info) ports
                foreach ($dp_remote_object['ports'] as $port) {
                    if ($port['name'] == $dp_neighbor['port']) {
                        $portinfo_remote = $port;
                        $remote_ports[] = $port;
                    }
                }
                // check if ports with such names exist on devices
                if (empty($local_ports)) {
                    $error_message = "No such local port <i>{$local_port}</i>";
                    break;
                }
                if (empty($remote_ports)) {
                    $error_message = "No such port on " . formatPortLink($dp_remote_object['id'], $dp_remote_object['name'], NULL, NULL);
                    break;
                }
                // determine match or mismatch of local link
                foreach ($local_ports as $portinfo_local) {
                    if ($portinfo_local['remote_id']) {
                        if ($portinfo_local['remote_object_id'] == $dp_remote_object_id and $portinfo_local['remote_name'] == $dp_neighbor['port']) {
                            // set $portinfo_remote to corresponding remote port
                            foreach ($remote_ports as $portinfo_remote) {
                                if ($portinfo_remote['id'] == $portinfo_local['remote_id']) {
                                    break;
                                }
                            }
                            $link_matches = TRUE;
                            unset($error_message);
                        } elseif ($portinfo_local['remote_object_id'] != $dp_remote_object_id) {
                            $error_message = "Remote device mismatch - port linked to " . formatLinkedPort($portinfo_local);
                        } else {
                            // ($portinfo_local['remote_name'] != $dp_neighbor['port'])
                            $error_message = "Remote port mismatch - port linked to " . formatPortLink($portinfo_local['remote_object_id'], NULL, $portinfo_local['remote_id'], $portinfo_local['remote_name']);
                        }
                        break 2;
                    }
                }
                // no local links found, try to search for remote links
                foreach ($remote_ports as $portinfo_remote) {
                    if ($portinfo_remote['remote_id']) {
                        $remote_link_html = formatLinkedPort($portinfo_remote);
                        $remote_port_html = formatPortLink($portinfo_remote['object_id'], NULL, $portinfo_remote['id'], $portinfo_remote['name']);
                        $error_message = "Remote port {$remote_port_html} is already linked to {$remote_link_html}";
                        break 2;
                    }
                }
                // no links found on both sides, search for a compatible port pair
                $port_types = array();
                foreach (array('left' => $local_ports, 'right' => $remote_ports) as $side => $port_list) {
                    foreach ($port_list as $portinfo) {
                        $tmp_types = $portinfo['iif_id'] == 1 ? array($portinfo['oif_id'] => $portinfo['oif_name']) : getExistingPortTypeOptions($portinfo['id']);
                        foreach ($tmp_types as $oif_id => $oif_name) {
                            $port_types[$side][$oif_id][] = array('id' => $oif_id, 'name' => $oif_name, 'portinfo' => $portinfo);
                        }
                    }
                }
                foreach ($port_types['left'] as $left_id => $left) {
                    foreach ($port_types['right'] as $right_id => $right) {
                        if (arePortTypesCompatible($left_id, $right_id)) {
                            foreach ($left as $left_port) {
                                foreach ($right as $right_port) {
                                    $variants[] = array('left' => $left_port, 'right' => $right_port);
                                }
                            }
                        }
                    }
                }
                if (!count($variants)) {
                    // no compatible ports found
                    $error_message = "Incompatible port types";
                }
            } while (FALSE);
            // do {
            $tr_class = $link_matches ? 'trok' : (isset($error_message) ? 'trerror' : 'trwarning');
            echo "<tr class=\"{$tr_class}\">";
            if ($initial_row) {
                $count = count($remote_list);
                $td_class = '';
                if (isset($hl_port_id) and $hl_port_id == $portinfo_local['id']) {
                    $td_class = "class='border_highlight'";
                }
                echo "<td rowspan=\"{$count}\" {$td_class} NOWRAP>" . ($portinfo_local ? formatPortLink($mydevice['id'], NULL, $portinfo_local['id'], $portinfo_local['name'], 'interactive-portname port-menu') : "<a class='interactive-portname port-menu nolink'>{$local_port}</a>") . ($count > 1 ? "<br> ({$count} neighbors)" : '') . '</td>';
                $initial_row = FALSE;
            }
            echo "<td>" . ($portinfo_local ? formatPortIIFOIF($portinfo_local) : '&nbsp;') . "</td>";
            echo "<td>" . formatIfTypeVariants($variants, "ports_{$inputno}") . "</td>";
            echo "<td>{$dp_neighbor['device']}</td>";
            echo "<td>" . ($portinfo_remote ? formatPortLink($dp_remote_object_id, NULL, $portinfo_remote['id'], $portinfo_remote['name']) : $dp_neighbor['port']) . "</td>";
            echo "<td>" . ($portinfo_remote ? formatPortIIFOIF($portinfo_remote) : '&nbsp;') . "</td>";
            echo "<td>";
            if (!empty($variants)) {
                echo "<input type=checkbox name=do_{$inputno} class='cb-makelink'>";
                $inputno++;
            }
            echo "</td>";
            if (isset($error_message)) {
                echo "<td style=\"background-color: white; border-top: none\">{$error_message}</td>";
            }
            echo "</tr>";
        }
    }
    if ($inputno) {
        echo "<input type=hidden name=nports value={$inputno}>";
        echo '<tr><td colspan=7 align=center>' . getImageHREF('CREATE', 'import selected', TRUE) . '</td></tr>';
    }
    echo '</table></form>';
    addJS(<<<END
\$(document).ready(function () {
\t\$('#cb-toggle').click(function (event) {
\t\tvar list = \$('.cb-makelink');
\t\tfor (var i in list) {
\t\t\tvar cb = list[i];
\t\t\tcb.checked = event.target.checked;
\t\t}
\t}).triggerHandler('click');
});
END
, TRUE);
}
예제 #5
0
function usort_portlist(&$array)
{
    $temp_array = array();
    foreach ($array as $portname) {
        $temp_array[$portname] = 1;
    }
    $array = array_keys(sortPortList($temp_array, FALSE));
}