1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
#!/usr/bin/perl
# Author: An-Cheng Huang <ancheng@vyatta.com>
# Date: 2007
# Description: vyatta configuration parser
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2006, 2007, 2008 Vyatta, Inc.
# All Rights Reserved.
# **** End License ****
package Vyatta::Config;
use strict;
use Vyatta::ConfigDOMTree;
my %fields = (
_changes_only_dir_base => $ENV{VYATTA_CHANGES_ONLY_DIR},
_new_config_dir_base => $ENV{VYATTA_TEMP_CONFIG_DIR},
_active_dir_base => $ENV{VYATTA_ACTIVE_CONFIGURATION_DIR},
_vyatta_template_dir => $ENV{VYATTA_CONFIG_TEMPLATE},
_current_dir_level => "/",
_level => undef,
);
sub new {
my $that = shift;
my $class = ref ($that) || $that;
my $self = {
%fields,
};
bless $self, $class;
return $self;
}
sub _set_current_dir_level {
my ($self) = @_;
my $level = $self->{_level};
$level =~ s/\//%2F/g;
$level =~ s/\s+/\//g;
$self->{_current_dir_level} = "/$level";
return $self->{_current_dir_level};
}
## setLevel("level")
# if "level" is supplied, set the current level of the hierarchy we are working on
# return the current level
sub setLevel {
my ($self, $level) = @_;
$self->{_level} = $level if defined($level);
$self->_set_current_dir_level();
return $self->{_level};
}
## listNodes("level")
# return array of all nodes at "level"
# level is relative
sub listNodes {
my ($self, $path) = @_;
my @nodes = ();
if (defined $path) {
$path =~ s/\//%2F/g;
$path =~ s/\s+/\//g;
$path = $self->{_new_config_dir_base} . $self->{_current_dir_level} . "/" . $path;
}
else {
$path = $self->{_new_config_dir_base} . $self->{_current_dir_level};
}
#print "DEBUG Vyatta::Config->listNodes(): path = $path\n";
opendir my $dir, "$path" or return ();
@nodes = grep !/^\./, readdir $dir;
closedir $dir;
my @nodes_modified = ();
while (@nodes) {
my $tmp = pop (@nodes);
$tmp =~ s/\n//g;
$tmp =~ s/%2F/\//g;
#print "DEBUG Vyatta::Config->listNodes(): node = $tmp\n";
push @nodes_modified, $tmp;
}
return @nodes_modified;
}
## listOrigNodes("level")
# return array of all original nodes (i.e., before any current change; i.e.,
# in "working") at "level"
# level is relative
sub listOrigNodes {
my ($self, $path) = @_;
my @nodes = ();
if (defined $path) {
$path =~ s/\//%2F/g;
$path =~ s/\s+/\//g;
$path = $self->{_active_dir_base} . $self->{_current_dir_level} . "/"
. $path;
}
else {
$path = $self->{_active_dir_base} . $self->{_current_dir_level};
}
#print "DEBUG Vyatta::Config->listNodes(): path = $path\n";
opendir my $dir, "$path" or return ();
@nodes = grep !/^\./, readdir $dir;
closedir $dir;
my @nodes_modified = ();
while (@nodes) {
my $tmp = pop (@nodes);
$tmp =~ s/\n//g;
$tmp =~ s/%2F/\//g;
#print "DEBUG Vyatta::Config->listNodes(): node = $tmp\n";
push @nodes_modified, $tmp;
}
return @nodes_modified;
}
## listOrigNodes("level")
# return array of all original nodes (i.e., before any current change; i.e.,
# in "working") at "level"
# level is relative
sub listOrigNodesNoDef {
my ($self, $path) = @_;
my @nodes = ();
if (defined $path) {
$path =~ s/\//%2F/g;
$path =~ s/\s+/\//g;
$path = $self->{_active_dir_base} . $self->{_current_dir_level} . "/"
. $path;
}
else {
$path = $self->{_active_dir_base} . $self->{_current_dir_level};
}
#print "DEBUG Vyatta::Config->listNodes(): path = $path\n";
opendir my $dir, "$path" or return ();
@nodes = grep !/^\./, readdir $dir;
closedir $dir;
my @nodes_modified = ();
while (@nodes) {
my $tmp = pop (@nodes);
$tmp =~ s/\n//g;
$tmp =~ s/%2F/\//g;
#print "DEBUG Vyatta::Config->listNodes(): node = $tmp\n";
if ($tmp ne 'def') {
push @nodes_modified, $tmp;
}
}
return @nodes_modified;
}
## returnParent("level")
# return the name of parent node relative to the current hierarchy
# in this case "level" is set to the parent dir ".. .."
# for example
sub returnParent {
my ($self, $node) = @_;
my @x, my $tmp;
# split our hierarchy into vars on a stack
my @level = split /\s+/, $self->{_level};
# count the number of parents we need to lose
# and then pop 1 less
@x = split /\s+/, $node;
for ($tmp = 1; $tmp < @x; $tmp++) {
pop @level;
}
# return the parent
$tmp = pop @level;
return $tmp;
}
## returnValue("node")
# returns the value of "node" or undef if the node doesn't exist .
# node is relative
sub returnValue {
my ( $self, $node ) = @_;
my $tmp;
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
return unless
open my $file, '<',
"$self->{_new_config_dir_base}$self->{_current_dir_level}/$node/node.val";
read $file, $tmp, 16384;
close $file;
$tmp =~ s/\n$//;
return $tmp;
}
## returnOrigValue("node")
# returns the original value of "node" (i.e., before the current change; i.e.,
# in "working") or undef if the node doesn't exist.
# node is relative
sub returnOrigValue {
my ( $self, $node ) = @_;
my $tmp;
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
my $filepath = "$self->{_active_dir_base}$self->{_current_dir_level}/$node";
return unless open my $file, '<', "$filepath/node.val";
read $file, $tmp, 16384;
close $file;
$tmp =~ s/\n$//;
return $tmp;
}
## returnValues("node")
# returns an array of all the values of "node", or an empty array if the values do not exist.
# node is relative
sub returnValues {
my $val = returnValue(@_);
my @values = ();
if (defined($val)) {
@values = split("\n", $val);
}
return @values;
}
## returnOrigValues("node")
# returns an array of all the original values of "node" (i.e., before the
# current change; i.e., in "working"), or an empty array if the values do not
# exist.
# node is relative
sub returnOrigValues {
my $val = returnOrigValue(@_);
my @values = ();
if (defined($val)) {
@values = split("\n", $val);
}
return @values;
}
## exists("node")
# Returns true if the "node" exists.
sub exists {
my ( $self, $node ) = @_;
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
if ( -d "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node" ) {
#print "DEBUG: the dir is there\n";
return !0;
} else {
return undef;
}
}
## existsOrig("node")
# Returns true if the "original node" exists.
sub existsOrig {
my ( $self, $node ) = @_;
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
if ( -d "$self->{_active_dir_base}$self->{_current_dir_level}/$node" ) {
return 1;
} else {
return undef;
}
}
## isDeleted("node")
# is the "node" deleted. node is relative. returns true or false
sub isDeleted {
my ($self, $node) = @_;
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
my $filepathAct
= "$self->{_active_dir_base}$self->{_current_dir_level}/$node";
my $filepathNew
= "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node";
return ((-e $filepathAct) && !(-e $filepathNew));
}
## listDeleted("level")
# return array of deleted nodes in the "level"
# "level" defaults to current
sub listDeleted {
my ($self, $path) = @_;
my @new_nodes = $self->listNodes("$path");
my @orig_nodes = $self->listOrigNodes("$path");
my %new_hash = map { $_ => 1 } @new_nodes;
my @deleted = grep { !defined($new_hash{$_}) } @orig_nodes;
return @deleted;
}
## isChanged("node")
# will check the change_dir to see if the "node" has been changed from a previous
# value. returns true or false.
sub isChanged {
my ($self, $node) = @_;
# let's setup the filepath for the change_dir
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
my $filepath = "$self->{_changes_only_dir_base}$self->{_current_dir_level}/$node";
# if the node exists in the change dir, it's modified.
return ( ! -e $filepath);
}
## isChangedOrDeleted("node")
# is the "node" changed or deleted. node is relative. returns true or false
sub isChangedOrDeleted {
my ($self, $node) = @_;
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
my $filepathChg
= "$self->{_changes_only_dir_base}$self->{_current_dir_level}/$node";
if (-e $filepathChg) {
return 1;
}
my $filepathAct
= "$self->{_active_dir_base}$self->{_current_dir_level}/$node";
my $filepathNew
= "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node";
return ((-e $filepathAct) && !(-e $filepathNew));
}
## isAdded("node")
# will compare the new_config_dir to the active_dir to see if the "node" has
# been added. returns true or false.
sub isAdded {
my ($self, $node) = @_;
#print "DEBUG Vyatta::Config->isAdded(): node $node\n";
# let's setup the filepath for the modify dir
$node =~ s/\//%2F/g;
$node =~ s/\s+/\//g;
my $filepathNewConfig = "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node";
#print "DEBUG Vyatta::Config->isAdded(): filepath $filepathNewConfig\n";
# if the node doesn't exist in the modify dir, it's not
# been added. so short circuit and return false.
return unless (-e $filepathNewConfig);
# now let's setup the path for the working dir
my $filepathActive = "$self->{_active_dir_base}$self->{_current_dir_level}/$node";
# if the node is in the active_dir it's not new
return (! -e $filepathActive);
}
## listNodeStatus("level")
# return a hash of the status of nodes at the current config level
# node name is the hash key. node status is the hash value.
# node status can be one of deleted, added, changed, or static
sub listNodeStatus {
my ($self, $path) = @_;
my @nodes = ();
my %nodehash = ();
# find deleted nodes first
@nodes = $self->listDeleted("$path");
foreach my $node (@nodes) {
if ($node =~ /.+/) { $nodehash{$node} = "deleted" };
}
@nodes = ();
@nodes = $self->listNodes("$path");
foreach my $node (@nodes) {
if ($node =~ /.+/) {
#print "DEBUG Vyatta::Config->listNodeStatus(): node $node\n";
# No deleted nodes -- added, changed, ot static only.
if ($self->isAdded("$path $node")) { $nodehash{$node} = "added"; }
elsif ($self->isChanged("$path $node")) { $nodehash{$node} = "changed"; }
else { $nodehash{$node} = "static"; }
}
}
return %nodehash;
}
############ DOM Tree ################
#Create active DOM Tree
sub createActiveDOMTree {
my $self = shift;
my $tree = new Vyatta::ConfigDOMTree($self->{_active_dir_base} . $self->{_current_dir_level},"active");
return $tree;
}
#Create changes only DOM Tree
sub createChangesOnlyDOMTree {
my $self = shift;
my $tree = new Vyatta::ConfigDOMTree($self->{_changes_only_dir_base} . $self->{_current_dir_level},
"changes_only");
return $tree;
}
#Create new config DOM Tree
sub createNewConfigDOMTree {
my $self = shift;
my $level = $self->{_new_config_dir_base} . $self->{_current_dir_level};
return new Vyatta::ConfigDOMTree($level, "new_config");
}
###### functions for templates ######
# $1: array representing the config node path.
# returns the filesystem path to the template of the specified node,
# or undef if the specified node path is not valid.
sub getTmplPath {
my $self = shift;
my @cfg_path = @{$_[0]};
my $tpath = $self->{_vyatta_template_dir};
for my $p (@cfg_path) {
if (-d "$tpath/$p") {
$tpath .= "/$p";
next;
}
if (-d "$tpath/node.tag") {
$tpath .= "/node.tag";
next;
}
# the path is not valid!
return;
}
return $tpath;
}
sub isTagNode {
my $self = shift;
my $cfg_path_ref = shift;
my $tpath = $self->getTmplPath($cfg_path_ref);
return unless $tpath;
return (-d "$tpath/node.tag");
}
sub hasTmplChildren {
my $self = shift;
my $cfg_path_ref = shift;
my $tpath = $self->getTmplPath($cfg_path_ref);
return unless $tpath;
opendir (my $tdir, $tpath) or return;
my @tchildren = grep !/^node\.def$/, (grep !/^\./, (readdir $tdir));
closedir $tdir;
return (scalar(@tchildren) > 0);
}
# $cfg_path_ref: ref to array containing the node path.
# returns ($is_multi, $is_text, $default),
# or undef if specified node is not valid.
sub parseTmpl {
my $self = shift;
my $cfg_path_ref = shift;
my ($is_multi, $is_text, $default) = (0, 0, undef);
my $tpath = $self->getTmplPath($cfg_path_ref);
return unless $tpath;
if (! -r "$tpath/node.def") {
return ($is_multi, $is_text);
}
open(TMPL, "<$tpath/node.def") or return ($is_multi, $is_text);
foreach (<TMPL>) {
if (/^multi:/) {
$is_multi = 1;
}
if (/^type:\s+txt\s*$/) {
$is_text = 1;
}
if (/^default:\s+(\S+)\s*$/) {
$default = $1;
}
}
close TMPL;
return ($is_multi, $is_text, $default);
}
###### misc functions ######
# compare two value lists and return "deleted" and "added" lists.
# since this is for multi-value nodes, there is no "changed" (if a value's
# ordering changed, it is deleted then added).
# $0: \@orig_values
# $1: \@new_values
sub compareValueLists {
my $self = shift;
my @ovals = @{$_[0]};
my @nvals = @{$_[1]};
my %comp_hash = (
'deleted' => [],
'added' => [],
);
my $idx = 0;
my %ohash = map { $_ => ($idx++) } @ovals;
$idx = 0;
my %nhash = map { $_ => ($idx++) } @nvals;
my $min_changed_idx = 2**31;
my %dhash = ();
foreach (@ovals) {
if (!defined($nhash{$_})) {
push @{$comp_hash{'deleted'}}, $_;
$dhash{$_} = 1;
if ($ohash{$_} < $min_changed_idx) {
$min_changed_idx = $ohash{$_};
}
}
}
foreach (@nvals) {
if (defined($ohash{$_})) {
if ($ohash{$_} != $nhash{$_}) {
if ($ohash{$_} < $min_changed_idx) {
$min_changed_idx = $ohash{$_};
}
}
}
}
foreach (@nvals) {
if (defined($ohash{$_})) {
if ($ohash{$_} != $nhash{$_}) {
if (!defined($dhash{$_})) {
push @{$comp_hash{'deleted'}}, $_;
$dhash{$_} = 1;
}
push @{$comp_hash{'added'}}, $_;
} elsif ($ohash{$_} >= $min_changed_idx) {
# ordering unchanged, but something before it is changed.
if (!defined($dhash{$_})) {
push @{$comp_hash{'deleted'}}, $_;
$dhash{$_} = 1;
}
push @{$comp_hash{'added'}}, $_;
} else {
# this is before any changed value. do nothing.
}
} else {
push @{$comp_hash{'added'}}, $_;
}
}
return %comp_hash;
}
1;
|