";
- return true;
- } else {
- // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning
- printf ('
\n";
-}
-
-// interpret standard syntax for peerscope
-function plc_peer_info ($api,$peerscope) {
- switch ($peerscope) {
- case '':
- $peer_filter=array();
- $peer_label="all peers";
- break;
- case 'local':
- $peer_filter=array("peer_id"=>NULL);
- $peer_label=PLC_SHORTNAME;
- break;
- case 'foreign':
- $peer_filter=array("~peer_id"=>NULL);
- $peer_label="foreign peers";
- break;
- default:
- if (is_int ($peerscope)) {
- $peer_id=intval($peerscope);
- $peers=$api->GetPeers(array("peer_id"=>$peer_id));
- } else {
- $peers=$api->GetPeers(array("shortname"=>$peerscope));
- }
- if ($peers) {
- $peer=$peers[0];
- $peer_id=$peer['peer_id'];
- $peer_filter=array("peer_id"=>$peer_id);
- $peer_label='peer "' . $peer['shortname'] . '"';
- } else {
- $peer_filter=array();
- $peer_label="[no such peer " . $peerscope . "]";
+// because GetRoles does not correctly support filters, it's really painful to do this
+function sort_roles ($r1, $r2) { return $r2['role_id'] - $r1['role_id']; }
+function roles_except ($roles, $exception_ids) {
+ $result=array();
+ if ($roles) foreach ($roles as $role) {
+ if ( ! in_array ($role['role_id'],$exception_ids) ) {
+ $result[]=$role;
+ }
}
- break;
- }
- return array ($peer_filter,$peer_label);
+ usort($result,sort_roles);
+ return $result;
}
//////////////////////////////////////////////////////////// nodegroups
@@ -316,29 +346,148 @@ function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
//////////////////////////////////////////////////////////// titles
function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; }
function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; }
+function t_person ($person) { return " belonging to " . $person['email'] . " (" . $person['person_id'] . ")"; }
-//////////////////////////////////////////////////////////// nav tabs
-function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); }
-function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); }
-function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); }
+//////////////////////////////////////////////////////////// html fragments
+function plc_vertical_table ($messages, $class="") {
+ if ( empty( $messages) ) return "";
+ $formatted = "";
+ $formatted .= "
";
+ return $formatted;
+}
+function plc_itemize ($messages, $class="") {
+ if ( empty( $messages) ) return "";
+ $formatted = "";
+ $formatted .= "
";
+ return $formatted;
+}
-//////////////////////////////////////////////////////////// presentation
+////////// just return a truncated text
+function truncate ($text,$numb,$etc = "...") {
+ if (strlen($text) <= $numb) return $text;
+ return substr($text, 0, $numb).$etc;
+}
+// ditto but in case the text is too lare, returns a
with its 'title' set to the full value
+function truncate_and_popup ($text,$numb,$etc = "...") {
+ if (strlen($text) <= $numb) return $text;
+ $display=substr($text, 0, $numb).$etc;
+ return sprintf("%s",$text,$display);
+}
+
+// generates <(atom) class=(class)> (text) (atom)>
+function html_atom ($atom,$text,$class="") {
+ $html="<$atom";
+ if ($class) $html .= " class='$class'";
+ $html .= ">$text$atom>";
+ return $html;
+}
+function html_div ($text,$class="") { return html_atom ('div',$text,$class); }
+function html_span ($text,$class="") { return html_atom ('span',$text,$class); }
+
+// should use the same channel as the php errors..
+function plc_error_html ($text) { return html_div ($text,'plc-error'); }
+function plc_error ($text) { print plc_error_html ("Error " . $text); }
+
+function errors_init() { return array();}
+function errors_record ($adm, $errors) {
+ if ($adm->error()) {
+ $tmp=$adm->error();
+ $errors []= $tmp;
+ }
+ return $errors;
+}
+
+function errors_display ($errors) {
+ if ($errors) {
+ print( "" );
+ print( "
The following errors occured:
" );
+ print("
");
+ foreach( $errors as $error )
+ print( "- $error
\n" );
+ print( "
\n" );
+ }
+}
+
+function plc_warning_html ($text) { return html_span($text,'plc-warning'); }
+function plc_warning ($text) { print plc_warning_html("Warning " . $text); }
+
+function bold_html ($text) { return html_span($text,'bold'); }
+
+// shows a php variable verbatim with a heading message
+function plc_debug ($message,$object) {
+ print "
" . $message . "";
+ print_r ($object);
+ print "
";
+}
+
+function plc_debug_txt ($message,$txt) {
+ print "
" . $message . "";
+ $txt=str_replace(" ","<SPC>",$txt);
+ $txt=str_replace("\t","<TAB>",$txt);
+ $txt=str_replace("\n","<LF>",$txt);
+ $txt=str_replace("\r","<CR>",$txt);
+ print $txt . "<EOF>";
+ print "
";
+}
+
+$plc_prof_start=0.;
+$plc_prof_time=0.;
+$plc_prof_counter=0;
+function plc_debug_prof_start () {
+ global $plc_prof_counter, $plc_prof_start, $plc_prof_time;
+ $plc_prof_counter=0;
+ plc_debug(strftime("[0] %T (start)") ,"heating up");
+ $plc_prof_time=microtime(true);
+ $plc_prof_start=$plc_prof_time;
+}
+function plc_debug_prof ($message,$object) {
+ global $plc_prof_counter, $plc_prof_start, $plc_prof_time;
+ $plc_prof_counter+=1;
+ $now=microtime(true);
+ $timelabel=strftime("%T");
+ $prof_message=sprintf("[%d] %s (%2.3f s -- %2.3f s) ",$plc_prof_counter,$timelabel,
+ ($now-$plc_prof_time),($now-$plc_prof_start));
+ plc_debug($prof_message.$message,$object);
+ $plc_prof_time=$now;
+}
+function plc_debug_prof_end () {
+ plc_debug_prof ("end","cooling down");
+}
+
+if (! function_exists ("drupal_set_error")) {
+ function drupal_set_error ($text) {
+ drupal_set_message ("$text");
+ }
+ }
+
+//////////////////////////////////////////////////////////// sort out for obsolete / trash
// builds a table from an array of strings, with the given class
// attempt to normalize the delete buttons and confirmations
-function plc_delete_button($width=15) {
- return '';
+function plc_delete_icon($width=15) {
+ return "";
}
-function plc_js_confirm($message) {
- return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
+function plc_add_icon($width=15) {
+ return "";
}
-function plc_delete_link($url,$delete_message,$visible) {
- return "" . $visible . "";
+function plc_bubble($text,$bubble) {
+ return "$text";
}
-
-function plc_delete_link_button($url,$delete_message,$width=15) {
- return "" . plc_delete_button($width) . "";
+function plc_delete_icon_bubble ($bubble,$width=15) {
+ return plc_bubble(plc_delete_icon($width),$bubble);
}
function plc_event_button($type,$param,$id) {
@@ -355,56 +504,100 @@ function plc_comon_button ($id_name, $id_value,$target="") {
return $result;
}
-function plc_vertical_table ($messages, $class="") {
- // pretty print the cell
- if ( empty( $messages) ) return "";
- $formatted = "";
- $formatted .= "";
- return $formatted;
-}
-
-//////////////////////////////////////////////////////////// various mappers
-// could not figure how to use anonymous lambdas..
-function get_site_id ($site) { return $site['site_id'];}
-function get_tagname ($tag) { return $tag['tagname'];}
-
-////////////////////////////////////////////////////////////
-function plc_section ($text,$line=true) {
- if ($line) { print "
";}
- print " $text
\n";
+////////////////////
+function plc_redirect ($url) {
+ header ("Location: " . $url);
+ exit ();
}
-function plc_error ($text) {
- // should use the same channel as the php errors..
- print " Error " . $text . "
";
+//////////////////// the options for an nodetype - suitable for plekit/form
+global $builtin_node_types;
+$builtin_node_types = array ( "regular" => "Regular/Shared",
+ "reservable" => "Reservable (requires to get leases)");
+function node_type_display ($api, $node_type) {
+ global $builtin_node_types;
+ $val=$builtin_node_types[$node_type];
+ if ( ! $val) $val="??undefined??";
+ return $val;
}
-function plc_errors ($list) {
- print( "" );
- print( "
The following errors occured:
" );
- print("
");
- foreach( $errors as $err ) {
- print( "- $err
\n" );
+function node_type_selectors ($api,$node_type) {
+ global $builtin_node_types;
+ foreach ($builtin_node_types as $value=>$display) {
+ $selector=array('display'=>$display, 'value'=>$value);
+ if ($value == $node_type) $selector['selected']=true;
+ $selectors []= $selector;
}
- print( "
\n" );
+ return $selectors;
}
-function plc_warning_text ($text) { return "" . $text . "
";}
-function plc_warning ($text) { print plc_warning_text("Warning " . $text); }
-function plc_foreign_text($text) { return "$text
"; }
-
-// shows a php variable verbatim with a heading message
-function plc_debug ($message,$object) {
- print "
" . $message . "";
- print_r ($object);
- print "
";
+//////////////////// the options for an interface - suitable for plekit/form
+//>>> GetNetworkMethods()
+//[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
+function interface_method_selectors ($api, $method, $primary) {
+ if ($primary) {
+ $builtin_methods=array("static"=>"Static",
+ "dhcp"=>"DHCP");
+ } else {
+ $builtin_methods=array("static"=>"Static",
+ "dhcp"=>"DHCP",
+ "proxy"=>"Proxy",
+ "tap"=>"TUN/TAP",
+ "ipmi"=>"IPMI");
+ }
+ $selectors=array();
+ foreach ($builtin_methods as $value=>$display) {
+ $selector=array('display'=>$display, 'value'=>$value);
+ if ($value == $method) $selector['selected']=true;
+ $selectors []= $selector;
+ }
+ return $selectors;
+}
+
+// displays bandwidth with kbps Mbps Gbps as needed
+function pretty_bandwidth ($bw) {
+ if ($bw < 1000) return $bw;
+ if ($bw < 1000000) return strval($bw/1000) . " kbps";
+ if ($bw < 1000000000) return strval($bw/1000000) . " Mbps";
+ else return strval($bw/1000000000) . " Gbps";
+}
+
+////////////////////
+function instantiation_label ($slice) {
+ $instantiation_labels = array ('not-instantiated'=>'NOT',
+ 'plc-instantiated'=>'PLC',
+ 'delegated' => 'DEL',
+ 'nm-controller' => 'NM');
+ $result=$instantiation_labels[$slice['instantiation']];
+ if (!$result) $result = $slice['instantiation'];
+ if (!$result) $result = '??';
+ return $result;
}
+
+//////////////////// toggle areas
+// get_arg ('show_persons',false) returns $_GET['show_persons'] if set and false otherwise
+function get_arg ($name,$default=NULL,$method='get') {
+ if ($method == 'get') $var=$_GET; else $var=$_POST;
+ if (isset ($var[$name])) return $var[$name];
+ else return $default;
+}
+
+//////////////////// number of ...
+function count_english ($objs,$name) {
+ $count=count($objs);
+ if ($count == 0) return 'No ' . $name;
+ else if ($count == 1) return 'One ' . $name;
+ else return $count . ' ' . $name . 's';
+}
+function count_english_warning ($objs, $name) {
+ $x=count_english ($objs,$name);
+ if (count ($objs) == 0) $x=plc_warning_html($x . ' !!');
+ return $x;
+}
+
+//////////////////// outlining reservable nodes
+function reservable_mark () { return "-R-";}
+function reservable_legend () { return "reservable nodes are marked with " . reservable_mark (); }
?>