#*********************************************************************************** # protected _process_attributes # ***************************** # Uses: Recursively searches through a tree structure to process/find attributes # Params: HASH with device and interface attributes # Return: The updated command and result set after template processing #*********************************************************************************** sub _process_attributes{ my $self = shift; my $child_tree = shift; my $attributes = shift || {}; my $attribute_values; foreach my $child (@$child_tree){ my $attribute_key = $child->{id}; my @attribute_array = split('-',$attribute_key); my $key; my $current_key; my $next_key; $attributes = hash_maker($attributes,@attribute_array); if($child->{children}){ $self->_process_attributes($child->{children},$attributes) } if($child->{leaf}){ nested_keys($attributes,\@attribute_array,$child->{value}); } } return $attributes; } sub nested_keys{ my $attributes = shift; my $attribute_array = shift; my $value = shift; foreach my $attr (@$attribute_array){ foreach my $k (keys %$attributes){ unless($k eq $attr){ next; } my $num_keys = keys %{$attributes->{$k}}; if(ref($attributes->{$k}) eq 'HASH' && $num_keys > 0){ shift(@$attribute_array); nested_keys($attributes->{$k},\@$attribute_array,$value); } else { $attributes->{$k} = $value; return; } } } } sub hash_maker { my $ref = shift; my $key = shift; return undef unless ref $ref eq 'HASH'; # # If $key is empty, we're building an empty hash. Return undef. # return undef unless $key; # # This is the passed in hash, so we need to duplicate it in our new # data structure. We also have to be sure that we're adding if necessary. # if ( ref $key eq 'HASH' ) { my $hash; my $count = keys %$key; my $cnt = 1; foreach my $k ( keys %{$key} ) { $ref->{$k} = $key->{$k}; } return $ref; } # # Reference was not a hashref, we don't know how to handle it. # return undef if ref $key; # # Nothing above was met, there's more to build. # $ref->{$key} = {} unless exists $ref->{$key}; #shift(@_); hash_maker( $ref->{$key}, @_ ); # # Pass that which we hath groweth to those who groweth before us... # return $ref; }