GeniXML.pm.in 18 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
#
# GENIPUBLIC-COPYRIGHT
4
# Copyright (c) 2010-2011 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
# All rights reserved.
#
package GeniXML;

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

@ISA = "Exporter";
Jonathon Duerig's avatar
Jonathon Duerig committed
14
@EXPORT = qw(Parse ParseFile IsVersion0 FindNodes FindNodesNS
15
FindFirst FindElement FindAttr GetNodeByVirtualId GetLinkByVirtualId
16
IsLanNode IsLocalNode IsTunnel GetExpires GetBandwidth GetIp GetVnodeId
17
GetNodeId GetVirtualId GetInterfaceId GetInterfaceNodeId GetSliverId
18
GetManagerId GetColocate GetSubnodeOf GetStartupCommand GetTarball
19
GetVirtualizationType SetVirtualizationSubtype GetVirtualizationSubtype
Jonathon Duerig's avatar
Jonathon Duerig committed
20
GetExclusive SetExclusive GetLinkManager SetText GetText Serialize
21
CreateDocument AddElement RemoveChild PolicyExists GetMask);
22
23
24

use English;
use XML::LibXML;
25
26
use XML::LibXML::XPathContext;
use XML::LibXML::NodeList;
27
use GeniHRN;
28
use GeniUtil;
29
30
use Carp qw(cluck carp);

31
use vars qw($RSPEC_0_1 $RSPEC_0_2 $RSPEC_2 $EMULAB_NS);
32
$RSPEC_0_1 = "0.1";
Jonathon Duerig's avatar
Jonathon Duerig committed
33
$RSPEC_0_2 = "0.2";
Jonathon Duerig's avatar
Jonathon Duerig committed
34
$RSPEC_2 = "2";
35

36
37
$EMULAB_NS = "http://www.protogeni.net/resources/rspec/ext/emulab/1";

38
39
40
41
# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";

42
43
44
# Name Space stuff
my $DELAY_NS = "http://www.protogeni.net/resources/rspec/ext/delay/1";

45
46
47
48
49
50
51
52
53
54
55
# 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 ($@) {
56
	carp("Failed to parse xml string: $@\nXML: $xml\n\n");
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	return undef;
    } else {
	return $doc->documentElement();
    }
}

sub ParseFile($)
{
    my ($file) = @_;
    my $parser = XML::LibXML->new;
    my $doc;
    eval {
        $doc = $parser->parse_file($file);
    };
    if ($@) {
72
	carp("Failed to parse xml string: $@");
73
74
75
76
77
78
	return undef;
    } else {
	return $doc->documentElement();
    }
}

79
80
81
82
83
84
85
# Determines the rspec version of a node by examining is namespace URI
sub GetVersion($)
{
    my ($node) = @_;
    my $result = $RSPEC_0_1;
    my $ns = $node->namespaceURI();
    if (defined($ns)) {
86
	if ($ns =~ /protogeni.net\/resources\/rspec\/0.1$/) {
87
	    $result = $RSPEC_0_1;
88
	} elsif ($ns =~ /protogeni.net\/resources\/rspec\/0.2$/) {
Jonathon Duerig's avatar
Jonathon Duerig committed
89
	    $result = $RSPEC_0_2;
90
	} elsif ($ns =~ /protogeni.net\/resources\/rspec\/2$/) {
91
92
93
94
95
96
97
98
99
	    $result = $RSPEC_2;
	} else {
	    carp("Unknown rspec namespace: " . $ns);
	    $result = $RSPEC_0_1;
	}
    }
    return $result;
}

Jonathon Duerig's avatar
Jonathon Duerig committed
100
101
102
103
104
105
sub IsVersion0($)
{
    my $version = GetVersion($_[0]);
    return $version eq $RSPEC_0_1 || $version eq $RSPEC_0_2;
}

106
107
108
109
110
111
112
113
114
115
116
117
# 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)) {
118
	    $xc->registerNs('n', $ns);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	} 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;
    }
}

135
136
137
138
139
140
# 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;
141
142
    return XML::LibXML::NodeList->new()
        if (!defined($node));
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    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;
    } 
}


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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);
}

198
199
200
201
202
sub GetElementByVirtualId($$$)
{
    my ($name, $type, $node) = @_;
    my @list = FindNodes('n:'.$type.'[@virtual_id = "'.$name.'"]',
			 $node)->get_nodelist();
203
204
205
206
207
208
209
210
    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();
    }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    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);    
}

235
236
237
238
239
240
# 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") {
241
	foreach my $lan (FindNodes("n:hardware_type",
242
				   $node)->get_nodelist()) {
243
	    my $typeName = GetFirstText($lan, "type_name", "name");
244
	    if (defined($typeName) && $typeName eq "lan") {
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
		$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") {
261
	my $manager_uuid  = GetManagerId($node);
262
263
264
265
266
267
268
269
270
271
	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
272
273
274
275
276
277
278
279
sub IsTunnel($)
{
    my ($link) = @_;
    my $result = 0;
    if (IsVersion0($link)) {
	my $link_type = GetText("link_type", $link);
	$result = (defined($link_type) && $link_type eq "tunnel");
    } else {
280
	my @types = FindNodes("n:link_type", $link)->get_nodelist();
Jonathon Duerig's avatar
Jonathon Duerig committed
281
282
	foreach my $current (@types) {
	    my $name = GetText("name", $current);
283
	    if (defined($name) && $name eq "gre-tunnel") {
Jonathon Duerig's avatar
Jonathon Duerig committed
284
285
286
287
288
289
290
291
292
293
294
		$result = 1;
		last;
	    }
	}
    }
    return $result;
}

sub GetExpires($)
{
    my ($node) = @_;
295
    return GetFirstText($node, "valid_until", "expires");
Jonathon Duerig's avatar
Jonathon Duerig committed
296
297
298
299
300
301
302
303
304
305
306
307
}

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

308
309
310
311
312
313
314
315
sub GetBandwidth($)
{
    my ($link) = @_;
    my $result = undef;
    if (IsVersion0($link)) {
	$result = GeniXML::GetText("bandwidth", $link);
    } else {
	my $prop = FindFirst("n:property", $link);
316
317
318
	if (defined($prop)) {
	    $result = GeniXML::GetText("capacity", $prop);
	}
319
320
321
322
    }
    return $result;
}

323
sub GetMask($$)
324
325
326
327
328
329
{
    my ($ifaceref, $node) = @_;
    my $result = undef;
    if (IsVersion0($ifaceref)) {
	$result = GetText("tunnel_ip", $ifaceref);
    } else {
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
	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("mask", $ip);
		}
		last;
	    }
	}
    }
    return $result;
}

sub GetIp($$)
{
    my ($ifaceref, $node) = @_;
    my $result = undef;
350
351
352
    if (IsVersion0($ifaceref)) {
	$result = GetText("tunnel_ip", $ifaceref);
    } else {
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	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;
}

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# 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 {
	my $vnoderef = GeniXML::FindNodesNS("vnode", $node,
					    $EMULAB_NS)->pop();
	if (defined($vnoderef)) {
	    $result = GetText("name", $vnoderef);
	}
	if (! defined($result)) {
	    $result = GetNodeId($node);
	}
    }
    return $result;
}

389
390
391
392
# Returns the uuid or urn of an RSpec node or undef if it is not a node.
sub GetNodeId($)
{
    my ($node) = @_;
393
394
    my $result = GetFirstText($node, "component_urn", "component_id",
			      "component_uuid", "uuid");
395
    if (defined($result) && $result eq "") {
396
397
398
	$result = undef;
    }
    return $result;
399
400
401
402
403
}

sub GetVirtualId($)
{
    my ($node) = @_;
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    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");
418
419
}

420
421
422
sub GetSliverId($)
{
    my ($node) = @_;
423
    return GetFirstText($node, "sliver_urn", "sliver_id");
424
425
}

426
427
428
sub GetManagerId($)
{
    my ($node) = @_;
429
430
431
    my $result = GetFirstText($node, "component_manager_urn",
			      "component_manager_id",
			      "component_manager_uuid");
432
    if (defined($result) && $result eq "") {
433
434
435
	$result = undef;
    }
    return $result;
436
437
}

Jonathon Duerig's avatar
Jonathon Duerig committed
438
439
440
sub GetColocate($)
{
    my ($node) = @_;
441
    my $result = GetFirstText($node, "colocate", "phys_nickname");
Jonathon Duerig's avatar
Jonathon Duerig committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    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;
}

463
464
465
466
467
468
469
470
sub GetStartupCommand($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
        $result = GetText("startup_command", $node);
    } else {
	my $execute = FindFirst("n:services/n:execute", $node);
471
472
473
	if (defined($execute)) {
	    $result = GetText("command", $execute);
	}
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
    }
    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;
	    }
	}
500
	if (defined($result) && $result eq "") {
501
502
503
504
505
506
	    $result = undef;
	}
    }
    return $result;
}

Jonathon Duerig's avatar
Jonathon Duerig committed
507
508
509
510
511
512
513
514
515
516
sub GetVirtualizationType($)
{
    my ($node) = @_;
    my $result = "emulab-vnode";
    if (IsVersion0($node)) {
	$result = GetText("virtualization_type", $node);
    }
    return $result;
}

517
sub SetVirtualizationSubtype($$)
Jonathon Duerig's avatar
Jonathon Duerig committed
518
519
520
521
{
    my ($node, $arg) = @_;
    my $result = 1;
    if (IsVersion0($node)) {
522
523
524
525
526
527
528
	$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
529
530
531
532
533
534
535
536
537
    }
    return $result;
}

sub GetVirtualizationSubtype($)
{
    my ($node) = @_;
    my $result = undef;
    if (IsVersion0($node)) {
538
	$result = GetText("virtualization_subtype", $node);
Jonathon Duerig's avatar
Jonathon Duerig committed
539
    } else {
540
	my $type = FindFirst("n:sliver_type", $node);
541
542
543
	if (defined($type)) {
	    $result = GetText("name", $type);
	}
Jonathon Duerig's avatar
Jonathon Duerig committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    }
    return $result;
}

sub GetExclusive($)
{
    my ($node) = @_;
    my $exclusive = GetText("exclusive", $node);
    my $result = $exclusive;
    if (defined($exclusive) && ! IsVersion0($node)) {
	$result = ($exclusive eq "true");
    }
    return $result;
}

559
sub SetExclusive($$)
Jonathon Duerig's avatar
Jonathon Duerig committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
{
    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) = @_;
578
    return GetFirstText($link, "id", "name");
Jonathon Duerig's avatar
Jonathon Duerig committed
579
580
}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
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;
}

599
600
601
602
603
604
605
606
607
# 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;
608
609
610
611
    my $child = FindFirst('@n:'.$name, $node);
    if (! defined($child)) {
	$child = FindFirst('@'.$name, $node);
    }
612
613
614
615
    if (defined($child)) {
	if ($child->nodeType() == XML_ATTRIBUTE_NODE) {
	    $child->setValue($text);
	    $result = 1;
616
617
	}
    } else {
618
	$child = FindFirst('n:'.$name, $node);
619
	if (defined($child)) {
620
621
622
623
624
625
626
627
	    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);
628
		$result = 1;
629
	    }
630
	} elsif ($node->nodeType() == XML_ELEMENT_NODE) {
631
632
	    my $ns = $node->namespaceURI();
	    if (defined($ns)) {
633
634
635
# TODO: Submit bug report for the library. This call is bugged.
#		$node->setAttributeNS($ns, "rs:$name", $text);
		$node->setAttribute($name, $text);
636
637
638
	    } else {
		$node->setAttribute($name, $text);
	    }
639
	    $result = 1;
640
641
642
643
644
	}
    }
    return $result;
}

645
646
# Get the text contents of a child of a node with a particular
# name. This can be either an attribute or an element.
647
648
sub GetText($$)
{
649
    my ($name, $node) = @_;
650
    my $result = undef;
651
652
653
654
    my $child = FindFirst('@n:'.$name, $node);
    if (! defined($child)) {
	$child = FindFirst('@'.$name, $node);
    }
655
656
657
    if (! defined($child)) {
	$child = FindFirst('n:'.$name, $node);
    }
658
659
660
661
662
663
    if (defined($child)) {
	$result = $child->textContent();
    }
    return $result;
}

664
665
666
667
668
669
670
671
# 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;
672
    foreach my $name (@_) {
673
674
675
676
677
678
679
680
	$result = GetText($name, $node);
	if (defined($result)) {
	    last;
	}
    }
    return $result;
}

681
682
# Converts the XML representation of a node to a UTF-8 string and
# outputs it as a complete XML document.
683
sub Serialize($;$)
684
{
685
686
    my ($node, $format) = @_;
    $format = 0 if (!defined($format));
687
    my $newnode = $node->cloneNode(1);
688
    return $newnode->toString($format);
689
690
691
692
}

# Create a new XML document with a given namespace URI and document
# element name.
693
694
695
696
sub CreateDocument($$)
{
    my ($ns, $name) = @_;
    my $doc = XML::LibXML::Document->createDocument("1.0", "UTF-8");
697
698
699
    my $root = $doc->createElementNS($ns, "rs:$name");
    $doc->setDocumentElement($root);
    return $doc;
700
701
}

702
703
# Add a new element to a node. The new element will have the given
# name and be otherwise empty.
704
sub AddElement($$;$)
705
{
706
707
708
709
710
    my ($name, $node, $namespace) = @_;
    my $ns = $namespace;
    if (! defined($namespace)) {
	$ns = $node->namespaceURI();
    }
711
712
    my $child = $node->addNewChild($ns, "rs:$name");
    return $child;
713
714
}

715
716
# 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.
717
718
719
720
721
722
723
724
725
726
727
728
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);
    }
}

729
730
731
732
733
734
# Replaces a node (oldnode) with a copy of another node (newnode)
sub ReplaceNode($$)
{
    my ($oldnode, $newnode) = @_;
    my $copy = $newnode->cloneNode(1);
    $oldnode->replaceNode($copy);
735
    return $copy;
736
737
}

738
739
740
741
# checks for the existense of policy in extensions of the given
# credential.
sub PolicyExists($$)
{
742
743
    my ($policy, $credential) = @_;
    my $exists = 0;
744

745
746
747
748
749
750
    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/*",
751
          $extensions_elem, $GeniUtil::EXTENSIONS_NS);
752
753
754
755
756
757
    foreach my $epolicy ($policies->get_nodelist) {
        if ($policy eq $epolicy->string_value) {
            $exists = 1;
            last;
        }      
    }       
758
  
759
    return $exists;
760
761
}

762
763
# _Always_ make sure that this 1 is at the end of the file...
1;