/** * 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; }
usage(); error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][6]); exit(6); } try { $lab = new Lab($options['F'], $tenant); } catch (Exception $e) { // Lab file is invalid error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][$e->getMessage()]); error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][7]); exit(7); } } // Checking -D (Node ID) if (isset($options['D'])) { if ((int) $options['D'] > 0 && isset($lab->getNodes()[$options['D']])) { $node_id = (int) $options['D']; } else { // Node ID must be numeric, greater than 0 and exists on lab usage(); error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][8]); exit(8); } } switch ($action) { default: // Invalid action usage(); error_log(date('M d H:i:s ') . date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][9]); exit(9); case 'delete':
/** * Function to get all node interfaces. * * @param Lab $lab Lab * @param int $id Node ID * @return Array Node interfaces (JSend data) */ function apiGetLabNodeInterfaces($lab, $id) { // Getting node if (isset($lab->getNodes()[$id])) { $node = $lab->getNodes()[$id]; // Printing node $output['code'] = 200; $output['status'] = 'success'; $output['message'] = $GLOBALS['messages'][60025]; $output['data'] = array(); // Getting interfaces $ethernets = array(); foreach ($lab->getNodes()[$id]->getEthernets() as $interface_id => $interface) { $ethernets[$interface_id] = array('name' => $interface->getName(), 'network_id' => $interface->getNetworkId()); } $serials = array(); foreach ($lab->getNodes()[$id]->getSerials() as $interface_id => $interface) { $serials[$interface_id] = array('name' => $interface->getName(), 'remote_id' => $interface->getRemoteId(), 'remote_if' => $interface->getRemoteIf()); } $output['data']['ethernet'] = $ethernets; $output['data']['serial'] = $serials; $output['code'] = 200; $output['status'] = 'success'; $output['message'] = $GLOBALS['messages'][60030]; } else { // Node not found $output['code'] = 404; $output['status'] = 'fail'; $output['message'] = $GLOBALS['messages'][20024]; } return $output; }
/** * Function to get all startup-configs. * * @param Lab $lab Lab * @return Array startup-configs (JSend data) */ function apiGetLabConfigs($lab) { // Getting startup-configs $nodes = $lab->getNodes(); // Printing startup-configs $output['code'] = 200; $output['status'] = 'success'; $output['message'] = $GLOBALS['messages'][60055]; $output['data'] = array(); foreach ($nodes as $node_id => $node) { if (isset($GLOBALS['node_config'][$node->getTemplate()])) { $output['data'][$node_id] = array('config' => $node->getConfig(), 'name' => $node->getName()); } } return $output; }