GeniXML.pm.in 25.7 KB
Newer Older
1
2
#!/usr/bin/perl -w
#
Leigh B Stoller's avatar
Leigh B Stoller committed
3
# Copyright (c) 2010-2013 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
29
30
31
32
33
34
35
36
#
package GeniXML;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA = "Exporter";
37
@EXPORT = qw(Parse ParseFile GetXmlVersion IsVersion0 FindNodes FindNodesNS
38
FindFirst FindElement FindAttr GetNodeByVirtualId GetLinkByVirtualId
39
IsLanNode IsLocalNode IsTunnel GetExpires GetBandwidth GetIp GetVnodeId
40
GetNodeId GetVirtualId GetInterfaceId GetInterfaceNodeId GetSliverId
41
GetManagerId GetColocate GetSubnodeOf GetStartupCommand GetTarball
42
GetVirtualizationType SetVirtualizationSubtype GetVirtualizationSubtype
Jonathon Duerig's avatar
Jonathon Duerig committed
43
GetExclusive SetExclusive GetLinkManager SetText GetText Serialize
44
CreateDocument AddElement RemoveChild PolicyExists GetMask
45
46
GetDiskImage IsUntaggedLan IsTaggedLan GetHopLinkID GetCapabilitySection
GetSuggestedVlanFromHop SetVlanTagInHop);
47
48

use English;
Leigh B Stoller's avatar
Leigh B Stoller committed
49
use Data::Dumper;
50
use XML::LibXML;
51
52
use XML::LibXML::XPathContext;
use XML::LibXML::NodeList;
53
use GeniHRN;
54
use GeniResponse;
55
use GeniUtil;
56
use Carp qw(cluck carp);
57
use Scalar::Util qw(looks_like_number);
58

59
use vars qw($RSPEC_0_1 $RSPEC_0_2 $RSPEC_2 $RSPEC_3
60
            $RSPEC_0_1_NS $RSPEC_0_2_NS $RSPEC_2_NS
61
            $EMULAB_NS $XSI_NS $STITCH_NS $SHAREDVLAN_NS
62
63
            $REQUEST_2_URL $MANIFEST_2_URL
            $REQUEST_3_URL $MANIFEST_3_URL);
64
$RSPEC_0_1 = "0.1";
Jonathon Duerig's avatar
Jonathon Duerig committed
65
$RSPEC_0_2 = "0.2";
Jonathon Duerig's avatar
Jonathon Duerig committed
66
$RSPEC_2 = "2";
67
$RSPEC_3 = "3";
68

69
70
71
our $RSPEC_0_1_NS = "http://www.protogeni.net/resources/rspec/0.1";
our $RSPEC_0_2_NS = "http://www.protogeni.net/resources/rspec/0.2";
our $RSPEC_2_NS = "http://www.protogeni.net/resources/rspec/2";
72
our $RSPEC_3_NS = "http://www.geni.net/resources/rspec/3";
73
74
75
our $EMULAB_NS = "http://www.protogeni.net/resources/rspec/ext/emulab/1";
our $XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
our $STITCH_NS = "http://hpn.east.isi.edu/rspec/ext/stitch/0.1/";
76
77
our $SHAREDVLAN_NS = "http://www.geni.net/resources/rspec/ext/shared-vlan/1";
our $SHAREDVLAN_PG_NS = "http://www.protogeni.net/resources/rspec/ext/shared-vlan/1";
78

79
80
81
82
$REQUEST_2_URL = "http://www.protogeni.net/resources/rspec/2/request.xsd";
$MANIFEST_2_URL = "http://www.protogeni.net/resources/rspec/2/manifest.xsd";
$REQUEST_3_URL = "http://www.geni.net/resources/rspec/3/request.xsd";
$MANIFEST_3_URL = "http://www.geni.net/resources/rspec/3/manifest.xsd";
83

84
85
86
87
# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";

88
89
90
# Name Space stuff
my $DELAY_NS = "http://www.protogeni.net/resources/rspec/ext/delay/1";

91
92
93
94
95
96
97
98
99
100
101
# Returns the document element by parsing a given string. If the
# string fails to parse, returns undefined.
sub Parse($)
{
    my ($xml) = @_;
    my $parser = XML::LibXML->new;
    my $doc;
    eval {
        $doc = $parser->parse_string($xml);
    };
    if ($@) {
102
	carp("Failed to parse xml string: $@\nXML: $xml\n\n");
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	return undef;
    } else {
	return $doc->documentElement();
    }
}

sub ParseFile($)
{
    my ($file) = @_;
    my $parser = XML::LibXML->new;
    my $doc;
    eval {
        $doc = $parser->parse_file($file);
    };
    if ($@) {
118
	carp("Failed to parse xml string: $@");
119
120
121
122
123
124
	return undef;
    } else {
	return $doc->documentElement();
    }
}

125
# Determines the rspec version of a node by examining is namespace URI
126
sub GetXmlVersion($)
127
128
{
    my ($node) = @_;
129
    my $result = undef;
130
131
    my $ns = $node->namespaceURI();
    if (defined($ns)) {
132
	if ($ns =~ /protogeni.net\/resources\/rspec\/0.1$/) {
133
	    $result = $RSPEC_0_1;
134
	} elsif ($ns =~ /protogeni.net\/resources\/rspec\/0.2$/) {
Jonathon Duerig's avatar
Jonathon Duerig committed
135
	    $result = $RSPEC_0_2;
136
	} elsif ($ns =~ /protogeni.net\/resources\/rspec\/2$/) {
137
	    $result = $RSPEC_2;
138
139
	} elsif ($ns =~ /geni.net\/resources\/rspec\/3$/) {
	    $result = $RSPEC_3;
140
141
142
143
144
145
146
147
	} else {
	    carp("Unknown rspec namespace: " . $ns);
	    $result = $RSPEC_0_1;
	}
    }
    return $result;
}

Jonathon Duerig's avatar
Jonathon Duerig committed
148
149
sub IsVersion0($)
{
150
151
152
    my $version = GetXmlVersion($_[0]);
    return defined($version)
	&& ($version eq $RSPEC_0_1 || $version eq $RSPEC_0_2);
Jonathon Duerig's avatar
Jonathon Duerig committed
153
154
}

155
156
157
158
159
160
161
162
163
164
165
166
# Returns a NodeList for a given XPath using a given node as
# context. 'n' is defined to be the prefix for the namespace of the
# node.
sub FindNodes($$)
{
    my ($path, $node) = @_;
    my $result = undef;
    my $ns = undef;
    eval {
	my $xc = XML::LibXML::XPathContext->new();
	$ns = $node->namespaceURI();
	if (defined($ns)) {
167
	    $xc->registerNs('n', $ns);
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	} else {
	    $path =~ s/\bn://g;
	}
	$result = $xc->findnodes($path, $node);
    };
    if ($@) {
	if (! defined($ns)) {
	    $ns = "undefined";
	}
        cluck "Failed to find nodes using XPath path='$path', ns='$ns': $@\n";
	return XML::LibXML::NodeList->new();
    } else {
	return $result;
    }
}

184
185
186
187
188
189
# Returns a NodeList for a given XPath using a given namespace as
# context. 'n' is defined to be the prefix for the given namespace.
sub FindNodesNS($$$)
{
    my ($path, $node, $nsURI) = @_;
    my $result = undef;
190
191
    return XML::LibXML::NodeList->new()
        if (!defined($node));
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    eval { 
  my $xc = XML::LibXML::XPathContext->new();
  if (defined($nsURI)) {
      $xc->registerNs('n', $nsURI);
  } else {
      $path =~ s/\bn://g;
  }
  $result = $xc->findnodes($path, $node);
    };
    if ($@) {
  if (! defined($nsURI)) {
      $nsURI = "undefined";
  }
        cluck "Failed to find nodes using XPath path='$path', nsURI='$nsURI': $@\n";        
  return XML::LibXML::NodeList->new();
    } else { 
  return $result;
    } 
}


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Returns the first Node which matches a given XPath against a given
# node. If that node is not of the specified type, returns
# undefined. Works like FindNodes.
sub FindNodeType($$$)
{
    my ($path, $node, $type) = @_;
    my $result = FindNodes($path, $node)->pop();
    if (defined($result) && $result->nodeType() != $type) {
	$result = undef;
    }
    return $result;
}

# Returns the first Node which matches a given XPath.
sub FindFirst($$)
{
    my ($path, $node) = @_;
    return FindNodes($path, $node)->pop();
}

# Returns the first Element which matches a given XPath.
sub FindElement($$)
{
    my ($path, $node) = @_;
    return FindNodeType($path, $node, XML_ELEMENT_NODE);
}

# Returns the first Attribute which matches a given XPath.
sub FindAttr($$)
{
    my ($path, $node) = @_;
    return FindNodeType($path, $node, XML_ATTRIBUTE_NODE);
}

247
248
249
250
251
sub GetElementByVirtualId($$$)
{
    my ($name, $type, $node) = @_;
    my @list = FindNodes('n:'.$type.'[@virtual_id = "'.$name.'"]',
			 $node)->get_nodelist();
252
253
254
255
256
257
258
259
    if (scalar(@list) == 0) {
	@list = FindNodes('n:'.$type.'[@nickname = "'.$name.'"]',
			  $node)->get_nodelist();
    }
    if (scalar(@list) == 0) {
	@list = FindNodes('n:'.$type.'[@client_id = "'.$name.'"]',
			  $node)->get_nodelist();
    }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    my $result = undef;
    if (scalar(@list) > 0)
    {
	$result = $list[0];
    }
    if (scalar(@list) > 1)
    {
	cluck("WARNING: Multiple $type with virtual_id $name found");
    }
    return $result;
}

sub GetNodeByVirtualId($$)
{
    my ($name, $node) = @_;
    return GetElementByVirtualId($name, 'node', $node);
}

sub GetLinkByVirtualId($$)
{
    my ($name, $node) = @_;
    return GetElementByVirtualId($name, 'link', $node);    
}

284
285
286
287
288
289
# Returns true if a given XML Node is an RSpec node and is of type lan
sub IsLanNode($)
{
    my ($node) = @_;
    my $result = 0;
    if (defined($node) && $node->localname() eq "node") {
290
	foreach my $lan (FindNodes("n:hardware_type",
291
				   $node)->get_nodelist()) {
292
	    my $typeName = GetFirstText($lan, "type_name", "name");
293
	    if (defined($typeName) && $typeName eq "lan") {
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
		$result = 1;
		last;
	    }
	}
    }
    return $result;
}

# Returns true if a given XML Node is an RSpec node and either has
# the current CM as a 'component_manager_urn' or
# 'component_manager_uuid or no component_manager defined.
sub IsLocalNode($)
{
    my ($node) = @_;
    my $result = 0;
    if (defined($node) && $node->localname() eq "node") {
310
	my $manager_uuid  = GetManagerId($node);
311
312
313
314
315
316
317
318
319
320
	if (! defined($manager_uuid) ||
	    GeniHRN::Equal($manager_uuid, $ENV{'MYURN'}) ||
	    $manager_uuid eq $ENV{'MYUUID'}) {

	    $result = 1;
	}
    }
    return $result;
}

Jonathon Duerig's avatar
Jonathon Duerig committed
321
sub IsTunnel($)
322
323
324
325
326
327
328
329
330
331
332
333
{
    my ($link)  = @_;
    my $tunnel_type = TunnelType($link);

    return 1
	if (defined($tunnel_type) &&
	    $tunnel_type =~ /^(tunnel|gre-tunnel|egre-tunnel)$/);

    return 0;
}

sub TunnelType($)
Jonathon Duerig's avatar
Jonathon Duerig committed
334
335
{
    my ($link) = @_;
336
337
    my $result = undef;
    
Jonathon Duerig's avatar
Jonathon Duerig committed
338
339
    if (IsVersion0($link)) {
	my $link_type = GetText("link_type", $link);
340
341
342
	if (defined($link_type)) {
	    $result = "tunnel";
	}
Jonathon Duerig's avatar
Jonathon Duerig committed
343
    } else {
344
	my @types = FindNodes("n:link_type", $link)->get_nodelist();
Jonathon Duerig's avatar
Jonathon Duerig committed
345
346
	foreach my $current (@types) {
	    my $name = GetText("name", $current);
347
348
	    if (defined($name)) {
		$result = $name;
Jonathon Duerig's avatar
Jonathon Duerig committed
349
350
351
352
353
354
355
356
357
358
		last;
	    }
	}
    }
    return $result;
}

sub GetExpires($)
{
    my ($node) = @_;
359
360
361
362
363
364
365
366
367
368
369
370
    my $result = undef;
    my $text = GetFirstText($node, "valid_until", "expires");
    if (defined($text)) {
	if ($text =~
	  /[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}Z/) {
	    $result = $text;
	} else {
	    $result = GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					   "$text: Invalid Datetime Format");
	}
    }
    return $result;
Jonathon Duerig's avatar
Jonathon Duerig committed
371
372
373
374
375
376
377
378
379
380
381
382
}

sub SetExpires($$)
{
    my ($node, $arg) = @_;
    if (IsVersion0($node)) {
	SetText("valid_until", $node, $arg);
    } else {
	SetText("expires", $node, $arg);
    }
}

383
384
385
386
387
388
389
390
sub GetBandwidth($)
{
    my ($link) = @_;
    my $result = undef;
    if (IsVersion0($link)) {
	$result = GeniXML::GetText("bandwidth", $link);
    } else {
	my $prop = FindFirst("n:property", $link);
391
392
393
	if (defined($prop)) {
	    $result = GeniXML::GetText("capacity", $prop);
	}
394
395
396
397
    }
    return $result;
}

398
sub GetMask($$)
399
400
401
402
403
404
{
    my ($ifaceref, $node) = @_;
    my $result = undef;
    if (IsVersion0($ifaceref)) {
	$result = GetText("tunnel_ip", $ifaceref);
    } else {
405
406
407
408
409
410
411
	my $id = GetInterfaceId($ifaceref);
	my @ifaces = FindNodes("n:interface", $node)->get_nodelist();
	foreach my $iface (@ifaces) {
	    my $testid = GetInterfaceId($iface);
	    if (defined($id) && defined($testid) && $testid eq $id) {
		my $ip = FindFirst("n:ip", $iface);
		if (defined($ip)) {
412
		    $result = GetFirstText($ip, "netmask", "mask");
413
414
415
416
417
418
419
420
421
422
423
424
		}
		last;
	    }
	}
    }
    return $result;
}

sub GetIp($$)
{
    my ($ifaceref, $node) = @_;
    my $result = undef;
425
426
427
    if (IsVersion0($ifaceref)) {
	$result = GetText("tunnel_ip", $ifaceref);
    } else {
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
	my $id = GetInterfaceId($ifaceref);
	my @ifaces = FindNodes("n:interface", $node)->get_nodelist();
	foreach my $iface (@ifaces) {
	    my $testid = GetInterfaceId($iface);
	    if (defined($id) && defined($testid) && $testid eq $id) {
		my $ip = FindFirst("n:ip", $iface);
		if (defined($ip)) {
		    $result = GetText("address", $ip);
		}
		last;
	    }
	}
    }
    return $result;
}

444
445
446
447
448
449
450
451
# Returns the vnode id in the emulab extension or failing that the component_id
sub GetVnodeId($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
	$result = GetNodeId($node);
    } else {
452
	my $vnoderef = GeniXML::FindNodesNS("n:vnode", $node,
453
454
455
456
457
458
459
460
461
462
463
					    $EMULAB_NS)->pop();
	if (defined($vnoderef)) {
	    $result = GetText("name", $vnoderef);
	}
	if (! defined($result)) {
	    $result = GetNodeId($node);
	}
    }
    return $result;
}

464
465
466
467
# Returns the uuid or urn of an RSpec node or undef if it is not a node.
sub GetNodeId($)
{
    my ($node) = @_;
468
469
    my $result = GetFirstText($node, "component_urn", "component_id",
			      "component_uuid", "uuid");
470
    if (defined($result) && $result eq "") {
471
472
473
	$result = undef;
    }
    return $result;
474
475
476
477
478
}

sub GetVirtualId($)
{
    my ($node) = @_;
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    return GetFirstText($node, "virtual_id", "nickname", "client_id");
}

sub GetInterfaceId($)
{
    my ($node) = @_;
    return GetFirstText($node, "virtual_interface_id", "iface_name",
			"client_id", "virtual_port_id", "virtual_id");
}

sub GetInterfaceNodeId($)
{
    my ($node) = @_;
    return GetFirstText($node, "virtual_node_id", "node_nickname");
493
494
}

495
496
497
sub GetSliverId($)
{
    my ($node) = @_;
498
    return GetFirstText($node, "sliver_urn", "sliver_id");
499
500
}

501
502
503
sub GetManagerId($)
{
    my ($node) = @_;
504
505
506
    my $result = GetFirstText($node, "component_manager_urn",
			      "component_manager_id",
			      "component_manager_uuid");
507
    if (defined($result) && $result eq "") {
508
509
510
	$result = undef;
    }
    return $result;
511
512
}

Jonathon Duerig's avatar
Jonathon Duerig committed
513
514
515
sub GetColocate($)
{
    my ($node) = @_;
516
    my $result = GetFirstText($node, "colocate", "phys_nickname");
Jonathon Duerig's avatar
Jonathon Duerig committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    return $result;
}

sub GetSubnodeOf($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
	$result = GetText("subnode_of", $node);
    } else {
	my @relations = FindNodes("n:relation", $node)->get_nodelist();
	foreach my $current (@relations) {
	    if (GetText("type", $current) == "subnode_of") {
		$result = GetText("client_id", $current);
		last;
	    }
	}
    }
    return $result;
}

538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
sub GetServices($)
{
    my ($node) = @_;
    my @result = ();
    my @services = FindNodes("n:services", $node)->get_nodelist();

    foreach my $service (@services) {
	foreach my $current ($service->childNodes()) {
	    my $item;
	    my $type = $current->nodeName();
	
	    if ($type eq "execute") {
		$type = "execute";
		my $cmd  = GetText("command", $current);
		my $shell= GetText("shell", $current);
		$item = {"type"   => $type,
			 "cmd"    => $cmd,
			 "shell"  => $shell};
	    }
	    elsif ($type eq "install") {
		$type = "install";
		my $dir = GetText("install_path", $current);
		my $url = GetText("url", $current);
		$item = {"type"   => $type,
			 "dir"    => $dir,
			 "url"    => $url};
	    }
	    else {
		next;
	    }
	    push(@result, $item);
	}
    }
    return @result;
}

574
575
576
577
578
579
580
581
sub GetStartupCommand($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
        $result = GetText("startup_command", $node);
    } else {
	my $execute = FindFirst("n:services/n:execute", $node);
582
583
584
	if (defined($execute)) {
	    $result = GetText("command", $execute);
	}
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
    }
    return $result;
}

sub GetTarball($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
        $result = GetText("tarfiles", $node);
    } else {
	$result = "";
	my @tarlist = FindNodes("n:services/n:install",
				$node)->get_nodelist();
	foreach my $current (@tarlist) {
	    my $url = GetText("url", $current);
	    my $install = GetText("install_path", $current);
	    if (defined($url) && defined($install)) {
		# TODO: Make sure url/install don't have semicolons or spaces
		# TODO: Flag an error if either one is not defined
		if ($result ne "") {
		    $result .= ";";
		}
		$result .= $install . " " . $url;
	    }
	}
611
	if (defined($result) && $result eq "") {
612
613
614
615
616
617
	    $result = undef;
	}
    }
    return $result;
}

Jonathon Duerig's avatar
Jonathon Duerig committed
618
619
620
621
622
623
624
625
626
627
sub GetVirtualizationType($)
{
    my ($node) = @_;
    my $result = "emulab-vnode";
    if (IsVersion0($node)) {
	$result = GetText("virtualization_type", $node);
    }
    return $result;
}

628
sub SetVirtualizationSubtype($$)
Jonathon Duerig's avatar
Jonathon Duerig committed
629
630
631
632
{
    my ($node, $arg) = @_;
    my $result = 1;
    if (IsVersion0($node)) {
633
634
635
636
637
638
639
	$result = SetText("virtualization_subtype", $node, $arg);
    } else {
	my $type = FindFirst("n:sliver_type", $node);
	if (! defined($type)) {
	    $type = AddElement("sliver_type", $node);
	}
	SetText("name", $type, $arg);
Jonathon Duerig's avatar
Jonathon Duerig committed
640
641
642
643
644
645
646
647
648
    }
    return $result;
}

sub GetVirtualizationSubtype($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
649
	$result = GetText("virtualization_subtype", $node);
Jonathon Duerig's avatar
Jonathon Duerig committed
650
    } else {
651
	my $type = FindFirst("n:sliver_type", $node);
652
653
654
	if (defined($type)) {
	    $result = GetText("name", $type);
	}
Jonathon Duerig's avatar
Jonathon Duerig committed
655
656
657
658
    }
    return $result;
}

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
sub GetDiskImage($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
	$result = FindFirst("n:disk_image", $node);
    } else {
	my $type = FindFirst("n:sliver_type", $node);
	if (defined($type)) {
	    $result = FindFirst("n:disk_image", $type);
	}
    }
    return $result;
}

Jonathon Duerig's avatar
Jonathon Duerig committed
674
675
676
677
678
679
680
681
682
683
684
sub GetExclusive($)
{
    my ($node) = @_;
    my $exclusive = GetText("exclusive", $node);
    my $result = $exclusive;
    if (defined($exclusive) && ! IsVersion0($node)) {
	$result = ($exclusive eq "true");
    }
    return $result;
}

685
sub SetExclusive($$)
Jonathon Duerig's avatar
Jonathon Duerig committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
{
    my ($node, $arg) = @_;
    my $result = 1;
    if (IsVersion0($node)) {
	$result = SetText("exclusive", $node, $arg);
    } else {
	my $textArg = "false";
	if ($arg) {
	    $textArg = "true";
	}
	$result = SetText("exclusive", $node, $textArg);
    }
    return $result;
}

sub GetLinkManager($)
{
    my ($link) = @_;
704
    return GetFirstText($link, "id", "name");
Jonathon Duerig's avatar
Jonathon Duerig committed
705
706
}

707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
sub GetDelayPipes($)
{
    my ($node) = @_;
    my @result = ();
    
    if (! IsVersion0($node)) {
	my $type = FindFirst("n:sliver_type", $node);
	if (defined($type)) {
	    my $name = GetText("name", $type);
	    if (defined($name) && $name eq "delay") {
		@result = FindNodesNS("n:sliver_type_shaping/n:pipe", $type,
				      $DELAY_NS)->get_nodelist();
	    }
	}
    }
    return @result;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
725
726
727
728
729
730
731
732
733
734
735
736
737
sub HasXenSettings($)
{
    my ($node) = @_;

    my $type = FindFirst("n:sliver_type", $node);
    if (defined($type)) {
	my $settings = FindNodesNS("n:xen", $type, $EMULAB_NS)->pop();
	return 1
	    if (defined($settings));
    }
    return 0;
}

738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
sub XenPtype($)
{
    my ($node) = @_;

    my $type = FindFirst("n:sliver_type", $node);
    if (defined($type)) {
	my $ptype = FindNodesNS("n:xen_ptype", $type, $EMULAB_NS)->pop();
	if (defined($ptype)) {
	    my $name = GetText("name", $ptype);
	    return $name
		if (defined($name) && $name ne "");
	}
    }
    return undef;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
sub GetXenSettings($)
{
    my ($node) = @_;
    my $result = {};

    my $type = FindFirst("n:sliver_type", $node);
    return undef
	if (!defined($type));
    
    my $settings = FindNodesNS("n:xen", $type, $EMULAB_NS)->pop();
    return undef
	if (!defined($settings));
    
    my $tmp = GetText("cores", $settings);
    $result->{"cores"} = $tmp
	if (defined($tmp));
    $tmp = GetText("ram", $settings);
    $result->{"ram"} = $tmp
	if (defined($tmp));
    $tmp = GetText("disk", $settings);
    $result->{"disk"} = $tmp
	if (defined($tmp));

    return $result;
}

780
781
782
783
784
785
786
787
sub HasTagSetting($)
{
    my ($link) = @_;
    my @tagged = FindNodesNS("n:vlan_tagging", $link,
			     $EMULAB_NS)->get_nodelist();
    return scalar(@tagged);
}

788
789
790
sub IsUntaggedLan($)
{
    my ($link) = @_;
791
    my $result = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
792
    my @tagged = FindNodesNS("n:vlan_tagging", $link,
Jonathon Duerig's avatar
Jonathon Duerig committed
793
			     $EMULAB_NS)->get_nodelist();
794
795
796
797
798
799
800
801
802
803
804
    if (scalar(@tagged) > 0) {
	my $enabled = GetText("enabled", $tagged[0]);
	$result = defined($enabled) && $enabled eq "false";
    }
    return $result;
}

sub IsTaggedLan($)
{
    my ($link) = @_;
    my $result = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
805
    my @tagged = FindNodesNS("n:vlan_tagging", $link,
806
807
808
809
810
811
			     $EMULAB_NS)->get_nodelist();
    if (scalar(@tagged) > 0) {
	my $enabled = GetText("enabled", $tagged[0]);
	$result = defined($enabled) && $enabled eq "true";
    }
    return $result;
812
813
}

814
815
816
817
818
sub GetSharedLanName($)
{
    my ($iface) = @_;

    if (! IsVersion0($iface)) {
819
	my $interface_name = FindNodesNS("n:link_shared_vlan", $iface,
820
					 $SHAREDVLAN_NS)->pop();
821
	if (! defined($interface_name)) {
822
823
	    $interface_name = FindNodesNS("n:link_shared_vlan", $iface,
					  $SHAREDVLAN_PG_NS)->pop();
824
	}
825
826
827
828
829
830
831
832
833
834
	if (defined($interface_name)) {
	    my $name = GetText("name", $interface_name);
	    if (defined($name) and $name ne "") {
		return $name;
	    }
	}
    }
    return undef;
}

835
836
837
838
839
840
841
842
843
# Takes an attribute/element name, *NOT AN XPATH* and a node and sets
# the text of that node to a particular value. If the node is an
# attribute, the value is set. If it is an element with just a text
# node child, that node is replaced.
# Returns 1 on success and 0 on failure.
sub SetText($$$)
{
    my ($name, $node, $text) = @_;
    my $result = 0;
844
845
846
847
    my $child = FindFirst('@n:'.$name, $node);
    if (! defined($child)) {
	$child = FindFirst('@'.$name, $node);
    }
848
849
850
851
    if (defined($child)) {
	if ($child->nodeType() == XML_ATTRIBUTE_NODE) {
	    $child->setValue($text);
	    $result = 1;
852
853
	}
    } else {
854
	$child = FindFirst('n:'.$name, $node);
855
	if (defined($child)) {
856
857
858
859
860
861
862
863
	    my @grand = $child->childNodes();
	    if (scalar(@grand) == 1
		&& $grand[0]->nodeType() == XML_TEXT_NODE) {
		$grand[0]->setData($text);
		$result = 1;
	    } elsif (scalar(@grand) == 0
		     && $child->nodeType() == XML_ELEMENT_NODE) {
		$child->appendText($text);
864
		$result = 1;
865
	    }
866
	} elsif ($node->nodeType() == XML_ELEMENT_NODE) {
867
868
	    my $ns = $node->namespaceURI();
	    if (defined($ns)) {
869
870
871
# TODO: Submit bug report for the library. This call is bugged.
#		$node->setAttributeNS($ns, "rs:$name", $text);
		$node->setAttribute($name, $text);
872
873
874
	    } else {
		$node->setAttribute($name, $text);
	    }
875
	    $result = 1;
876
877
878
879
880
	}
    }
    return $result;
}

881
882
# Get the text contents of a child of a node with a particular
# name. This can be either an attribute or an element.
883
884
sub GetText($$)
{
885
    my ($name, $node) = @_;
886
    my $result = undef;
887
888
889
890
    my $child = FindFirst('@n:'.$name, $node);
    if (! defined($child)) {
	$child = FindFirst('@'.$name, $node);
    }
891
892
893
    if (! defined($child)) {
	$child = FindFirst('n:'.$name, $node);
    }
894
895
896
    if (defined($child)) {
	$result = $child->textContent();
    }
897
898
899
    if (defined($result)) {
	$result =~ s/^\s*(.*)\s*$/$1/s;
    }
900
901
902
    return $result;
}

903
904
905
906
907
908
909
910
# Run GetText serially using the initial argument as an XML node
# reference and the remaining arguments as names and return the first
# defined result. Returns undefined if there are no names or if all
# GetText calls return undefined.
sub GetFirstText($@)
{
    my $node = shift(@_);
    my $result = undef;
911
    foreach my $name (@_) {
912
913
914
915
916
917
918
919
	$result = GetText($name, $node);
	if (defined($result)) {
	    last;
	}
    }
    return $result;
}

920
921
# Converts the XML representation of a node to a UTF-8 string and
# outputs it as a complete XML document.
922
sub Serialize($;$)
923
{
924
925
    my ($node, $format) = @_;
    $format = 0 if (!defined($format));
926
    my $newnode = $node->cloneNode(1);
927
    return $newnode->toString($format);
928
929
930
931
}

# Create a new XML document with a given namespace URI and document
# element name.
932
933
934
935
sub CreateDocument($$)
{
    my ($ns, $name) = @_;
    my $doc = XML::LibXML::Document->createDocument("1.0", "UTF-8");
936
    my $root = $doc->createElementNS($ns, "$name");
937
938
    $doc->setDocumentElement($root);
    return $doc;
939
940
}

941
942
# Add a new element to a node. The new element will have the given
# name and be otherwise empty.
943
sub AddElement($$;$)
944
{
945
946
947
948
949
    my ($name, $node, $namespace) = @_;
    my $ns = $namespace;
    if (! defined($namespace)) {
	$ns = $node->namespaceURI();
    }
950
951
    my $child = $node->addNewChild($ns, "rs:$name");
    return $child;
952
953
}

954
955
# Remove a node with a given name from a node. It will be removed
# whether it is an attribute or an element. The name is not an xpath.
956
957
958
959
960
961
962
963
964
965
966
967
sub RemoveChild($$)
{
    my ($name, $node) = @_;
    my $child = FindFirst('@n:'.$name, $node);
    if (! defined($child)) {
	$child = FindFirst('n:'.$name, $node);
    }
    if (defined($child)) {
	$node->removeChild($child);
    }
}

968
969
970
971
972
973
# Replaces a node (oldnode) with a copy of another node (newnode)
sub ReplaceNode($$)
{
    my ($oldnode, $newnode) = @_;
    my $copy = $newnode->cloneNode(1);
    $oldnode->replaceNode($copy);
974
    return $copy;
975
976
}

977
978
979
980
# checks for the existense of policy in extensions of the given
# credential.
sub PolicyExists($$)
{
981
982
    my ($policy, $credential) = @_;
    my $exists = 0;
983

984
985
986
987
988
989
    return 0
        if (!ref($credential) or !defined($policy));
    my $extensions_elem = $credential->extensions();
    return 0
        if (!defined($extensions_elem));
    my $policies = GeniXML::FindNodesNS("//n:policy_exceptions/*",
990
          $extensions_elem, $GeniUtil::EXTENSIONS_NS);
991
992
993
994
995
996
    foreach my $epolicy ($policies->get_nodelist) {
        if ($policy eq $epolicy->string_value) {
            $exists = 1;
            last;
        }      
    }       
997
  
998
    return $exists;
999
1000
}

1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
sub GetHopLinkID($)
{
    my ($ref)  = @_;
    my $result = "";
			
    my $link = FindFirst("n:link", $ref);
    if (defined($link)) {
	$result = GetText("id", $link);
    }
    return $result;
}

sub GetHopCapabilitySection($)
{
    my ($hopref) = @_;
    
    #
    # Dig out the section we need from the hop.
    #
    my $tmp = FindFirst("n:link", $hopref);
    $tmp = (FindFirst("n:switchingCapabilityDescriptor", $tmp) ||
	    FindFirst("n:switchingCapabilityDescriptors", $tmp))
	if (defined($tmp));
    $tmp = FindFirst("n:switchingCapabilitySpecificInfo", $tmp)
	if (defined($tmp));
    if (defined($tmp) &&
	FindFirst("n:switchingCapabilitySpecificInfo_L2sc", $tmp)) {
	$tmp = FindFirst("n:switchingCapabilitySpecificInfo_L2sc", $tmp)
    }
    return $tmp;
}

sub GetSuggestedVlanFromHop($)
{
    my ($hopref) = @_;
    my $capref   = GetHopCapabilitySection($hopref);
    return undef
	if (!defined($capref));

    my $tag = GeniXML::GetText("suggestedVLANRange", $capref);
    $tag = undef
	if (defined($tag) && ! looks_like_number($tag));
    return $tag;
}

sub SetVlanTagInHop($$)
{
    my ($hopref, $tag) = @_;

    my $capref = GetHopCapabilitySection($hopref);
    return undef
	if (!defined($capref));

    GeniXML::SetText("vlanRangeAvailability", $capref, "$tag");
    GeniXML::SetText("suggestedVLANRange", $capref, "$tag");
    return 0;
}

1059
1060
# _Always_ make sure that this 1 is at the end of the file...
1;