/** * Function to add a node to a lab. * * @param Lab $lab Lab * @return Array Return code (JSend data) */ function apiGetLabTopology($lab) { // Printing topology $output['code'] = '200'; $output['status'] = 'success'; $output['message'] = 'Topology loaded'; $output['data'] = array(); foreach ($lab->getNodes() as $node_id => $node) { foreach ($node->getEthernets() as $interface) { if ($interface->getNetworkId() != '' && isset($lab->getNetworks()[$interface->getNetworkId()])) { // Interface is connected switch ($lab->getNetworks()[$interface->getNetworkId()]->getCount()) { default: // More than two connected nodes $output['data'][] = array('type' => 'ethernet', 'source' => 'node' . $node_id, 'source_type' => 'node', 'source_label' => $interface->getName(), 'destination' => 'network' . $interface->getNetworkId(), 'destination_type' => 'network', 'destination_label' => ''); break; case 0: // Network not used break; case 1: // Only one connected node $output['data'][] = array('type' => 'ethernet', 'source' => 'node' . $node_id, 'source_type' => 'node', 'source_label' => $interface->getName(), 'destination' => 'network' . $interface->getNetworkId(), 'destination_type' => 'network', 'destination_label' => ''); break; case 2: // P2P Link if ($lab->getNetworks()[$interface->getNetworkId()]->isCloud()) { // Cloud are never printed as P2P link $output['data'][] = array('type' => 'ethernet', 'source' => 'node' . $node_id, 'source_type' => 'node', 'source_label' => $interface->getName(), 'destination' => 'network' . $interface->getNetworkId(), 'destination_type' => 'network', 'destination_label' => ''); } else { foreach ($lab->getNodes() as $remote_node_id => $remote_node) { foreach ($remote_node->getEthernets() as $remote_interface) { if ($interface->getNetworkId() == $remote_interface->getNetworkId()) { // To avoid duplicates, only print if source node_id > destination node_id if ($node_id > $remote_node_id) { $output['data'][] = array('type' => 'ethernet', 'source' => 'node' . $node_id, 'source_type' => 'node', 'source_label' => $interface->getName(), 'destination' => 'node' . $remote_node_id, 'destination_type' => 'node', 'destination_label' => $remote_interface->getName()); } break; } } } } break; } } } foreach ($node->getSerials() as $interface) { if ($interface->getRemoteID() != '' && $node_id > $interface->getRemoteId()) { $output['data'][] = array('type' => 'serial', 'source' => 'node' . $node_id, 'source_type' => 'node', 'source_label' => $interface->getName(), 'destination' => 'node' . $interface->getRemoteID(), 'destination_type' => 'node', 'destination_label' => $lab->getNodes()[$interface->getRemoteID()]->getSerials()[$interface->getRemoteIf()]->getName()); } } } return $output; }
/** * Function to get all lab networks. * * @param Lab $lab Lab * @return Array Lab networks (JSend data) */ function apiGetLabNetworks($lab) { // Getting network(s) $networks = $lab->getNetworks(); // Printing networks $output['code'] = 200; $output['status'] = 'success'; $output['message'] = $GLOBALS['messages'][60004]; $output['data'] = array(); foreach ($networks as $network_id => $network) { $output['data'][$network_id] = array('id' => $network_id, 'count' => $network->getCount(), 'left' => $network->getLeft(), 'name' => $network->getName(), 'top' => $network->getTop(), 'type' => $network->getNType()); } return $output; }