| 1 | # | ||
| 2 | # $Id: Methods.pm,v 1.22 2004/01/09 19:03:35 rjohnst Exp $ | ||
| 3 | # | ||
| 4 | # COPYRIGHT AND LICENSE | ||
| 5 | # Copyright (c) 2001-2004, Juniper Networks, Inc. | ||
| 6 | # All rights reserved. | ||
| 7 | # Redistribution and use in source and binary forms, with or without | ||
| 8 | # modification, are permitted provided that the following conditions are | ||
| 9 | # met: | ||
| 10 | # 1. Redistributions of source code must retain the above | ||
| 11 | # copyright notice, this list of conditions and the following | ||
| 12 | # disclaimer. | ||
| 13 | # 2. Redistributions in binary form must reproduce the above | ||
| 14 | # copyright notice, this list of conditions and the following disclaimer | ||
| 15 | # in the documentation and/or other materials provided with the | ||
| 16 | # distribution. | ||
| 17 | # 3. The name of the copyright owner may not be used to | ||
| 18 | # endorse or promote products derived from this software without specific | ||
| 19 | # prior written permission. | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
| 21 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 22 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 23 | # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | ||
| 24 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 25 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 26 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 28 | # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 29 | # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | # | ||
| 32 | |||
| 33 | |||
| 34 | package JUNOS::Methods; | ||
| 35 | |||
| 36 | use File::Basename; | ||
| 37 | use strict; | ||
| 38 | use vars qw(@EXPORT_OK $AUTOLOAD | ||
| 39 | $NO_ARGS $TOGGLE $TOGGLE_NO $STRING $DOM $ATTRIBUTE %methods); | ||
| 40 | |||
| 41 | require Carp; | ||
| 42 | require Exporter; | ||
| 43 | @EXPORT_OK = qw($NO_ARGS $TOGGLE $TOGGLE_NO $STRING $DOM $ATTRIBUTE %methods); | ||
| 44 | |||
| 45 | use JUNOS::Trace; | ||
| 46 | |||
| 47 | $NO_ARGS = bless {}, "NO_ARGS"; | ||
| 48 | $TOGGLE = bless { 1 => 1 }, "TOGGLE"; | ||
| 49 | $TOGGLE_NO = bless {}, "TOGGLE"; | ||
| 50 | $STRING = bless {}, "STRING"; | ||
| 51 | $DOM = bless {}, "DOM"; | ||
| 52 | $ATTRIBUTE = bless {}, "ATTRIBUTE"; | ||
| 53 | |||
| 54 | # These are hardcoded methods | ||
| 55 | %methods = ( | ||
| 56 | lock_configuration => { | ||
| 57 | rollback => $ATTRIBUTE | ||
| 58 | }, | ||
| 59 | open_configuration => $NO_ARGS, | ||
| 60 | get_configuration => { | ||
| 61 | configuration => $DOM, | ||
| 62 | format => $ATTRIBUTE, | ||
| 63 | inherit => $ATTRIBUTE, | ||
| 64 | database => $ATTRIBUTE, | ||
| 65 | }, | ||
| 66 | load_configuration => { | ||
| 67 | action => $ATTRIBUTE, | ||
| 68 | format => $ATTRIBUTE, | ||
| 69 | rollback => $ATTRIBUTE, | ||
| 70 | url => $ATTRIBUTE, | ||
| 71 | configuration => $DOM | ||
| 72 | }, | ||
| 73 | commit_configuration => { | ||
| 74 | check => $TOGGLE, | ||
| 75 | confirmed => $TOGGLE, | ||
| 76 | "confirm-timeout" => $STRING | ||
| 77 | }, | ||
| 78 | close_configuration => $NO_ARGS, | ||
| 79 | unlock_configuration => $NO_ARGS, | ||
| 80 | |||
| 81 | get_xnm_information => { | ||
| 82 | type => $STRING, | ||
| 83 | namespace => $STRING | ||
| 84 | }, | ||
| 85 | |||
| 86 | get_database_status_information => $NO_ARGS, | ||
| 87 | ); | ||
| 88 | |||
| 89 | # | ||
| 90 | # add_methods: extra cheesy hack to add two hashes without doing any real work | ||
| 91 | # | ||
| 92 | sub add_methods { @_; } | ||
| 93 | |||
| 94 | sub init_methods | ||
| 95 | { | ||
| 96 | my($name, $args); | ||
| 97 | |||
| 98 | %methods = add_methods(%methods, @_); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub invoke_method | ||
| 102 | { | ||
| 103 | my ($self, $fn, %args) = @_; | ||
| 104 | my $bindings = $methods{ $fn }; | ||
| 105 | my $output = ""; | ||
| 106 | my $tag = ""; | ||
| 107 | my $attrs = ""; | ||
| 108 | trace("Methods", "$fn --> ", join("...", @_), "\n"); | ||
| 109 | |||
| 110 | foreach my $field (keys(%args)) { | ||
| 111 | my $type = $bindings->{ $field }; | ||
| 112 | my $value = $args{ $field }; | ||
| 113 | |||
| 114 | ($tag = $field) =~ s/_/-/g; | ||
| 115 | |||
| 116 | if (ref($type) eq "TOGGLE" || ref($value) eq "TOGGLE") { | ||
| 117 | if ($value ne "0") { | ||
| 118 | $output .= " <$tag/>\n"; | ||
| 119 | } | ||
| 120 | |||
| 121 | } elsif (ref($value) eq "TOGGLE_NO") { | ||
| 122 | if ($value eq "0") { | ||
| 123 | $output .= " <no-$tag/>\n"; | ||
| 124 | } else { | ||
| 125 | $output .= " <$tag/>\n"; | ||
| 126 | } | ||
| 127 | |||
| 128 | } elsif (ref($type) eq "STRING") { | ||
| 129 | $output .= " <${tag}>${value}</${tag}>\n"; | ||
| 130 | |||
| 131 | } elsif ($type =~ /(\d)+\.\.(\d)+/) { | ||
| 132 | $output .= " <${tag}>${value}</${tag}>\n"; | ||
| 133 | |||
| 134 | } elsif (ref($type) eq "DOM") { | ||
| 135 | $output .= $value->toString; | ||
| 136 | $output .= "\n"; | ||
| 137 | |||
| 138 | } elsif (ref($type) eq "ATTRIBUTE") { | ||
| 139 | $attrs .= " ${tag}=\"${value}\"\n"; | ||
| 140 | |||
| 141 | } elsif (ref($value)) { | ||
| 142 | $output .= $value->toString; | ||
| 143 | $output .= "\n"; | ||
| 144 | |||
| 145 | } else { | ||
| 146 | $output .= " <${tag}>${value}</${tag}>\n"; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | ($tag = $fn) =~ s/_/-/g; | ||
| 151 | |||
| 152 | if ($output) { | ||
| 153 | $output = "<rpc>\n <${tag}${attrs}>\n${output} </${tag}>\n</rpc>\n"; | ||
| 154 | } else { | ||
| 155 | $output = "<rpc>\n <${tag}${attrs}/>\n</rpc>\n"; | ||
| 156 | } | ||
| 157 | |||
| 158 | $self->request($output); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub AUTOLOAD | ||
| 162 | { | ||
| 163 | my $name = substr($AUTOLOAD, rindex($AUTOLOAD, "::") + 2); | ||
| 164 | unless ($methods{$name}) { | ||
| 165 | Carp::croak("undefined function: $AUTOLOAD"); | ||
| 166 | } | ||
| 167 | |||
| 168 | eval "sub $AUTOLOAD { invoke_method(shift, \"$name\", \@_); };"; | ||
| 169 | |||
| 170 | # *$AUTOLOAD = sub { invoke_method($name, @_); }; | ||
| 171 | goto &$AUTOLOAD; | ||
| 172 | } | ||
| 173 | |||
| 174 | ## | ||
| 175 | # Load up methods via version | ||
| 176 | ## | ||
| 177 | sub init | ||
| 178 | { | ||
| 179 | my($version) = shift; | ||
| 180 | my $current_dir; | ||
| 181 | |||
| 182 | foreach my $dir (@INC) { | ||
| 183 | if (-f "$dir/JUNOS/Methods.pm") { | ||
| 184 | $current_dir = $dir; | ||
| 185 | last; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | my $methods_dir = "$current_dir/JUNOS/$version"; | ||
| 190 | my @files = <$methods_dir/*_methods.pl>; | ||
| 191 | |||
| 192 | for my $f (@files) { | ||
| 193 | my $name = fileparse($f, '.pl'); | ||
| 194 | require $f; | ||
| 195 | # use strict doesn't allow usage of bareword so have to | ||
| 196 | # use this block as a way around it. Can't even hide | ||
| 197 | # this inside init_methods because the init_methods | ||
| 198 | # subroutine doesn't see the hashtables in its scope. | ||
| 199 | no strict 'refs'; | ||
| 200 | init_methods(%$name); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | 1; | ||
| 205 | |||
| 206 | __END__ |