| 1 | package perfSONAR_PS::Services::MA::SNMP; | ||
| 2 | |||
| 3 | use base 'perfSONAR_PS::Services::Base'; | ||
| 4 | |||
| 5 | use fields 'LS_CLIENT', 'NAMESPACES', 'METADATADB', 'LOGGER'; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | our $VERSION = 0.09; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | perfSONAR_PS::Services::MA::SNMP - A module that provides methods for the | ||
| 15 | perfSONAR-PS SNMP based Measurement Archive (MA). | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This module, in conjunction with other parts of the perfSONAR-PS framework, | ||
| 20 | handles specific messages from interested actors in search of SNMP data. There | ||
| 21 | are three major message types that this service can act upon: | ||
| 22 | |||
| 23 | - MetadataKeyRequest - Given some metadata about a specific measurement, | ||
| 24 | request a re-playable 'key' to faster access | ||
| 25 | underlying data. | ||
| 26 | - SetupDataRequest - Given either metadata or a key regarding a specific | ||
| 27 | measurement, retrieve data values. | ||
| 28 | - MetadataStorageRequest - Store data into the archive (unsupported) | ||
| 29 | |||
| 30 | The module is capable of dealing with several characteristic and tool based | ||
| 31 | eventTypes related to the underlying data as well as the aforementioned message | ||
| 32 | types. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | use Log::Log4perl qw(get_logger); | ||
| 37 | use Module::Load; | ||
| 38 | use Digest::MD5 qw(md5_hex); | ||
| 39 | use English qw( -no_match_vars ); | ||
| 40 | use Params::Validate qw(:all); | ||
| 41 | use Date::Manip; | ||
| 42 | |||
| 43 | use perfSONAR_PS::Services::MA::General; | ||
| 44 | use perfSONAR_PS::Common; | ||
| 45 | use perfSONAR_PS::Messages; | ||
| 46 | use perfSONAR_PS::Client::LS::Remote; | ||
| 47 | use perfSONAR_PS::Error_compat qw/:try/; | ||
| 48 | use perfSONAR_PS::DB::File; | ||
| 49 | use perfSONAR_PS::DB::RRD; | ||
| 50 | use perfSONAR_PS::DB::SQL; | ||
| 51 | use perfSONAR_PS::ParameterValidation; | ||
| 52 | |||
| 53 | my %ma_namespaces = ( | ||
| 54 | nmwg => "http://ggf.org/ns/nmwg/base/2.0/", | ||
| 55 | nmtm => "http://ggf.org/ns/nmwg/time/2.0/", | ||
| 56 | snmp => "http://ggf.org/ns/nmwg/tools/snmp/2.0/", | ||
| 57 | netutil => "http://ggf.org/ns/nmwg/characteristic/utilization/2.0/", | ||
| 58 | neterr => "http://ggf.org/ns/nmwg/characteristic/errors/2.0/", | ||
| 59 | netdisc => "http://ggf.org/ns/nmwg/characteristic/discards/2.0/", | ||
| 60 | select => "http://ggf.org/ns/nmwg/ops/select/2.0/", | ||
| 61 | average => "http://ggf.org/ns/nmwg/ops/average/2.0/", | ||
| 62 | perfsonar => "http://ggf.org/ns/nmwg/tools/org/perfsonar/1.0/", | ||
| 63 | psservice => "http://ggf.org/ns/nmwg/tools/org/perfsonar/service/1.0/", | ||
| 64 | nmwgt => "http://ggf.org/ns/nmwg/topology/2.0/", | ||
| 65 | nmwgtopo3 => "http://ggf.org/ns/nmwg/topology/base/3.0/", | ||
| 66 | nmtb => "http://ogf.org/schema/network/topology/base/20070828/", | ||
| 67 | nmtl2 => "http://ogf.org/schema/network/topology/l2/20070828/", | ||
| 68 | nmtl3 => "http://ogf.org/schema/network/topology/l3/20070828/", | ||
| 69 | nmtl4 => "http://ogf.org/schema/network/topology/l4/20070828/", | ||
| 70 | nmtopo => "http://ogf.org/schema/network/topology/base/20070828/", | ||
| 71 | nmtb => "http://ogf.org/schema/network/topology/base/20070828/", | ||
| 72 | nmwgr => "http://ggf.org/ns/nmwg/result/2.0/" | ||
| 73 | ); | ||
| 74 | |||
| 75 | =head1 API | ||
| 76 | |||
| 77 | The offered API is not meant for external use as many of the functions are | ||
| 78 | relied upon by internal aspects of the perfSONAR-PS framework. | ||
| 79 | |||
| 80 | =head2 init($self, $handler) | ||
| 81 | |||
| 82 | Called at startup by the daemon when this particular module is loaded into | ||
| 83 | the perfSONAR-PS deployment. Checks the configuration file for the necessary | ||
| 84 | items and fills in others when needed. Initializes the backed metadata storage | ||
| 85 | (DBXML or a simple XML file) and builds the internal 'key hash' for the | ||
| 86 | MetadataKey exchanges. Finally the message handler loads the appropriate | ||
| 87 | message types and eventTypes for this module. Any other 'pre-startup' tasks | ||
| 88 | should be placed in this function. | ||
| 89 | |||
| 90 | Due to performance issues, the database access must be handled in two different | ||
| 91 | ways: | ||
| 92 | |||
| 93 | - File Database - it is expensive to continuously open the file and store it as | ||
| 94 | a DOM for each access. Therefore it is opened once by the | ||
| 95 | daemon and used by each connection. A $self object can | ||
| 96 | be used for this. | ||
| 97 | - XMLDB - File handles are opened and closed for each connection. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub init { | ||
| 102 | my ( $self, $handler ) = @_; | ||
| 103 | $self->{LOGGER} = get_logger("perfSONAR_PS::Services::MA::SNMP"); | ||
| 104 | |||
| 105 | unless ( exists $self->{CONF}->{"snmp"}->{"metadata_db_type"} | ||
| 106 | and $self->{CONF}->{"snmp"}->{"metadata_db_type"} ) | ||
| 107 | { | ||
| 108 | $self->{LOGGER}->error("Value for 'metadata_db_type' is not set."); | ||
| 109 | return -1; | ||
| 110 | } | ||
| 111 | |||
| 112 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 113 | unless ( exists $self->{CONF}->{"snmp"}->{"metadata_db_file"} | ||
| 114 | and $self->{CONF}->{"snmp"}->{"metadata_db_file"} ) | ||
| 115 | { | ||
| 116 | $self->{LOGGER}->error("Value for 'metadata_db_file' is not set."); | ||
| 117 | return -1; | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | if ( defined $self->{DIRECTORY} ) { | ||
| 121 | unless ( $self->{CONF}->{"snmp"}->{"metadata_db_file"} =~ "^/" ) { | ||
| 122 | $self->{CONF}->{"snmp"}->{"metadata_db_file"} = $self->{DIRECTORY} . "/" . $self->{CONF}->{"snmp"}->{"metadata_db_file"}; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 128 | eval { load perfSONAR_PS::DB::XMLDB; }; | ||
| 129 | if ($EVAL_ERROR) { | ||
| 130 | $self->{LOGGER}->error("Couldn't load perfSONAR_PS::DB::XMLDB: $EVAL_ERROR"); | ||
| 131 | return -1; | ||
| 132 | } | ||
| 133 | |||
| 134 | unless ( exists $self->{CONF}->{"snmp"}->{"metadata_db_file"} | ||
| 135 | and $self->{CONF}->{"snmp"}->{"metadata_db_file"} ) | ||
| 136 | { | ||
| 137 | $self->{LOGGER}->error("Value for 'metadata_db_file' is not set."); | ||
| 138 | return -1; | ||
| 139 | } | ||
| 140 | unless ( exists $self->{CONF}->{"snmp"}->{"metadata_db_name"} | ||
| 141 | and $self->{CONF}->{"snmp"}->{"metadata_db_name"} ) | ||
| 142 | { | ||
| 143 | $self->{LOGGER}->error("Value for 'metadata_db_name' is not set."); | ||
| 144 | return -1; | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | if ( defined $self->{DIRECTORY} ) { | ||
| 148 | unless ( $self->{CONF}->{"snmp"}->{"metadata_db_name"} =~ "^/" ) { | ||
| 149 | $self->{CONF}->{"snmp"}->{"metadata_db_name"} = $self->{DIRECTORY} . "/" . $self->{CONF}->{"snmp"}->{"metadata_db_name"}; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | $self->{LOGGER}->error("Wrong value for 'metadata_db_type' set."); | ||
| 156 | return -1; | ||
| 157 | } | ||
| 158 | |||
| 159 | unless ( exists $self->{CONF}->{"snmp"}->{"rrdtool"} | ||
| 160 | and $self->{CONF}->{"snmp"}->{"rrdtool"} ) | ||
| 161 | { | ||
| 162 | $self->{LOGGER}->error("Value for 'rrdtool' is not set."); | ||
| 163 | return -1; | ||
| 164 | } | ||
| 165 | |||
| 166 | unless ( exists $self->{CONF}->{"snmp"}->{"default_resolution"} | ||
| 167 | and $self->{CONF}->{"snmp"}->{"default_resolution"} ) | ||
| 168 | { | ||
| 169 | $self->{CONF}->{"snmp"}->{"default_resolution"} = "300"; | ||
| 170 | $self->{LOGGER}->warn("Setting 'default_resolution' to '300'."); | ||
| 171 | } | ||
| 172 | |||
| 173 | unless ( exists $self->{CONF}->{"snmp"}->{"enable_registration"} | ||
| 174 | and $self->{CONF}->{"snmp"}->{"enable_registration"} ) | ||
| 175 | { | ||
| 176 | $self->{CONF}->{"snmp"}->{"enable_registration"} = 0; | ||
| 177 | } | ||
| 178 | |||
| 179 | if ( $self->{CONF}->{"snmp"}->{"enable_registration"} ) { | ||
| 180 | unless ( exists $self->{CONF}->{"snmp"}->{"service_accesspoint"} | ||
| 181 | and $self->{CONF}->{"snmp"}->{"service_accesspoint"} ) | ||
| 182 | { | ||
| 183 | $self->{LOGGER}->error("No access point specified for SNMP service"); | ||
| 184 | return -1; | ||
| 185 | } | ||
| 186 | |||
| 187 | unless ( exists $self->{CONF}->{"snmp"}->{"ls_instance"} | ||
| 188 | and $self->{CONF}->{"snmp"}->{"ls_instance"} ) | ||
| 189 | { | ||
| 190 | if ( defined $self->{CONF}->{"ls_instance"} | ||
| 191 | and $self->{CONF}->{"ls_instance"} ) | ||
| 192 | { | ||
| 193 | $self->{CONF}->{"snmp"}->{"ls_instance"} = $self->{CONF}->{"ls_instance"}; | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $self->{LOGGER}->error("No LS instance specified for SNMP service"); | ||
| 197 | return -1; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | unless ( exists $self->{CONF}->{"snmp"}->{"ls_registration_interval"} | ||
| 202 | and $self->{CONF}->{"snmp"}->{"ls_registration_interval"} ) | ||
| 203 | { | ||
| 204 | if ( defined $self->{CONF}->{"ls_registration_interval"} | ||
| 205 | and $self->{CONF}->{"ls_registration_interval"} ) | ||
| 206 | { | ||
| 207 | $self->{CONF}->{"snmp"}->{"ls_registration_interval"} = $self->{CONF}->{"ls_registration_interval"}; | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | $self->{LOGGER}->warn("Setting registration interval to 30 minutes"); | ||
| 211 | $self->{CONF}->{"snmp"}->{"ls_registration_interval"} = 1800; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | unless ( exists $self->{CONF}->{"snmp"}->{"service_accesspoint"} | ||
| 216 | and $self->{CONF}->{"snmp"}->{"service_accesspoint"} ) | ||
| 217 | { | ||
| 218 | $self->{CONF}->{"snmp"}->{"service_accesspoint"} = "http://localhost:" . $self->{PORT} . "/" . $self->{ENDPOINT}; | ||
| 219 | $self->{LOGGER}->warn( "Setting 'service_accesspoint' to 'http://localhost:" . $self->{PORT} . "/" . $self->{ENDPOINT} . "'." ); | ||
| 220 | } | ||
| 221 | |||
| 222 | unless ( exists $self->{CONF}->{"snmp"}->{"service_description"} | ||
| 223 | and $self->{CONF}->{"snmp"}->{"service_description"} ) | ||
| 224 | { | ||
| 225 | $self->{CONF}->{"snmp"}->{"service_description"} = "perfSONAR_PS SNMP MA"; | ||
| 226 | $self->{LOGGER}->warn("Setting 'service_description' to 'perfSONAR_PS SNMP MA'."); | ||
| 227 | } | ||
| 228 | |||
| 229 | unless ( exists $self->{CONF}->{"snmp"}->{"service_name"} | ||
| 230 | and $self->{CONF}->{"snmp"}->{"service_name"} ) | ||
| 231 | { | ||
| 232 | $self->{CONF}->{"snmp"}->{"service_name"} = "SNMP MA"; | ||
| 233 | $self->{LOGGER}->warn("Setting 'service_name' to 'SNMP MA'."); | ||
| 234 | } | ||
| 235 | |||
| 236 | unless ( exists $self->{CONF}->{"snmp"}->{"service_type"} | ||
| 237 | and $self->{CONF}->{"snmp"}->{"service_type"} ) | ||
| 238 | { | ||
| 239 | $self->{CONF}->{"snmp"}->{"service_type"} = "MA"; | ||
| 240 | $self->{LOGGER}->warn("Setting 'service_type' to 'MA'."); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | $handler->registerMessageHandler( "SetupDataRequest", $self ); | ||
| 245 | $handler->registerMessageHandler( "MetadataKeyRequest", $self ); | ||
| 246 | |||
| 247 | my $error = q{}; | ||
| 248 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 249 | $self->{METADATADB} = new perfSONAR_PS::DB::File( { file => $self->{CONF}->{"snmp"}->{"metadata_db_file"} } ); | ||
| 250 | $self->{METADATADB}->openDB( { error => \$error } ); | ||
| 251 | unless ( $self->{METADATADB} ) { | ||
| 252 | $self->{LOGGER}->error("Couldn't initialize store file: $error"); | ||
| 253 | return -1; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 257 | my $error = q{}; | ||
| 258 | my $metadatadb = $self->prepareDatabases; | ||
| 259 | unless ($metadatadb) { | ||
| 260 | $self->{LOGGER}->error( "There was an error opening \"" . $self->{CONF}->{"ls"}->{"metadata_db_name"} . "/" . $self->{CONF}->{"ls"}->{"metadata_db_file"} . "\": " . $error ); | ||
| 261 | return -1; | ||
| 262 | } | ||
| 263 | |||
| 264 | if ( $self->{CONF}->{"snmp"}->{"db_autoload"} and $self->{CONF}->{"snmp"}->{"autoload_metadata_db_file"} ) { | ||
| 265 | my $status = $self->loadXMLDB( { metadatadb => $metadatadb } ); | ||
| 266 | return -1 if $status == -1; | ||
| 267 | } | ||
| 268 | |||
| 269 | $metadatadb->closeDB( { error => \$error } ); | ||
| 270 | $self->{METADATADB} = q{}; | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | $self->{LOGGER}->error("Wrong value for 'metadata_db_type' set."); | ||
| 274 | return -1; | ||
| 275 | } | ||
| 276 | |||
| 277 | unless ( $self->buildHashedKeys == 0 ) { | ||
| 278 | $self->{LOGGER}->error("Error building key database."); | ||
| 279 | return -1; | ||
| 280 | } | ||
| 281 | |||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | |||
| 285 | =head2 prepareDatabases($self, { doc }) | ||
| 286 | |||
| 287 | Opens the XMLDB and returns the handle if there was not an error. The optional | ||
| 288 | argument can be used to pass an error message to the given message and | ||
| 289 | return this in response to a request. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub prepareDatabases { | ||
| 294 | my ( $self, @args ) = @_; | ||
| 295 | my $parameters = validateParams( @args, { doc => 0 } ); | ||
| 296 | |||
| 297 | my $error = q{}; | ||
| 298 | my $metadatadb = new perfSONAR_PS::DB::XMLDB( { env => $self->{CONF}->{"snmp"}->{"metadata_db_name"}, cont => $self->{CONF}->{"snmp"}->{"metadata_db_file"}, ns => \%ma_namespaces, } ); | ||
| 299 | unless ( $metadatadb->openDB( { txn => q{}, error => \$error } ) == 0 ) { | ||
| 300 | throw perfSONAR_PS::Error_compat( "error.ls.xmldb", "There was an error opening \"" . $self->{CONF}->{"ls"}->{"metadata_db_name"} . "/" . $self->{CONF}->{"ls"}->{"metadata_db_file"} . "\": " . $error ); | ||
| 301 | return; | ||
| 302 | } | ||
| 303 | return $metadatadb; | ||
| 304 | } | ||
| 305 | |||
| 306 | =head2 loadXMLDB( { metadatadb } ) | ||
| 307 | |||
| 308 | If the deployment has an existing store file, but would like to utilize an XML | ||
| 309 | DB instance, this function will load the old data into an existing XML DB. This | ||
| 310 | operation is non-destructive. | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | sub loadXMLDB { | ||
| 315 | my ( $self, @args ) = @_; | ||
| 316 | my $parameters = validateParams( @args, { metadatadb => 1 } ); | ||
| 317 | |||
| 318 | my $sourceError = q{}; | ||
| 319 | my $sourceDB = new perfSONAR_PS::DB::File( { file => $self->{CONF}->{"snmp"}->{"autoload_metadata_db_file"} } ); | ||
| 320 | $sourceDB->openDB( { error => \$sourceError } ); | ||
| 321 | unless ($sourceDB) { | ||
| 322 | $self->{LOGGER}->error("Couldn't initialize store file: $sourceError"); | ||
| 323 | return -1; | ||
| 324 | } | ||
| 325 | |||
| 326 | my $dom = $sourceDB->getDOM; | ||
| 327 | if ($dom) { | ||
| 328 | my $error = q{}; | ||
| 329 | my $errorFlag = 0; | ||
| 330 | my $dbTr = q{}; | ||
| 331 | $dbTr = $parameters->{metadatadb}->getTransaction( { error => \$error } ); | ||
| 332 | unless ($dbTr) { | ||
| 333 | $parameters->{metadatadb}->abortTransaction( { txn => $dbTr, error => \$error } ) if $dbTr; | ||
| 334 | undef $dbTr; | ||
| 335 | $self->{LOGGER}->error( "Database error: \"" . $error . "\", aborting." ); | ||
| 336 | return -1; | ||
| 337 | } | ||
| 338 | |||
| 339 | foreach my $data ( $dom->getDocumentElement->getChildrenByTagNameNS( "http://ggf.org/ns/nmwg/base/2.0/", "data" ) ) { | ||
| 340 | my $dHash = md5_hex( $data->toString ); | ||
| 341 | $parameters->{metadatadb}->insertIntoContainer( { content => $parameters->{metadatadb}->wrapStore( { content => $data->toString, type => "MAStore" } ), name => $dHash, txn => $dbTr, error => \$error } ); | ||
| 342 | $self->{LOGGER}->debug( "Inserting \"" . $data->toString . "\" as \"" . $dHash . "\"." ); | ||
| 343 | |||
| 344 | my $metadata = $dom->getDocumentElement->find( "./nmwg:metadata[\@id=\"" . $data->getAttribute("metadataIdRef") . "\"]" )->get_node(1); | ||
| 345 | my $mdHash = md5_hex( $metadata->toString ); | ||
| 346 | $parameters->{metadatadb}->insertIntoContainer( { content => $parameters->{metadatadb}->wrapStore( { content => $metadata->toString, type => "MAStore" } ), name => $mdHash, txn => $dbTr, error => \$error } ); | ||
| 347 | $self->{LOGGER}->debug( "Inserting \"" . $metadata->toString . "\" as \"" . $mdHash . "\"." ); | ||
| 348 | } | ||
| 349 | |||
| 350 | if ($errorFlag) { | ||
| 351 | $parameters->{metadatadb}->abortTransaction( { txn => $dbTr, error => \$error } ) if $dbTr; | ||
| 352 | undef $dbTr; | ||
| 353 | $self->{LOGGER}->error( "Database error: \"" . $error . "\", aborting." ); | ||
| 354 | return -1; | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | my $status = $parameters->{metadatadb}->commitTransaction( { txn => $dbTr, error => \$error } ); | ||
| 358 | if ( $status == 0 ) { | ||
| 359 | undef $dbTr; | ||
| 360 | } | ||
| 361 | else { | ||
| 362 | $parameters->{metadatadb}->abortTransaction( { txn => $dbTr, error => \$error } ) if $dbTr; | ||
| 363 | undef $dbTr; | ||
| 364 | $self->{LOGGER}->error( "Database error: \"" . $error . "\", aborting." ); | ||
| 365 | return -1; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | } | ||
| 369 | else { | ||
| 370 | $self->{LOGGER}->error( "Source file \"" . $self->{CONF}->{"snmp"}->{"autoload_metadata_db_file"} . "\" error, aborting." ); | ||
| 371 | return -1; | ||
| 372 | } | ||
| 373 | return 0; | ||
| 374 | } | ||
| 375 | |||
| 376 | =head2 buildHashedKeys($self {}) | ||
| 377 | |||
| 378 | With the backend storage known we can search through looking for key | ||
| 379 | structures. Once we have these in hand each will be examined and Digest::MD5 | ||
| 380 | will be utilized to create MD5 hex based fingerprints of each key. We then | ||
| 381 | map these to the key ids in the metadata database for easy lookup. | ||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | sub buildHashedKeys { | ||
| 386 | my ( $self, @args ) = @_; | ||
| 387 | my $parameters = validateParams( @args, {} ); | ||
| 388 | |||
| 389 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 390 | my $results = $self->{METADATADB}->querySet( { query => "/nmwg:store/nmwg:data" } ); | ||
| 391 | if ( $results->size() > 0 ) { | ||
| 392 | foreach my $data ( $results->get_nodelist ) { | ||
| 393 | if ( $data->getAttribute("id") ) { | ||
| 394 | my $hash = md5_hex( $data->toString ); | ||
| 395 | $self->{CONF}->{"snmp"}->{"hashToId"}->{$hash} = $data->getAttribute("id"); | ||
| 396 | $self->{CONF}->{"snmp"}->{"idToHash"}->{ $data->getAttribute("id") } = $hash; | ||
| 397 | $self->{LOGGER}->debug( "Key id $hash maps to data element " . $data->getAttribute("id") ); | ||
| 398 | } | ||
| 399 | } | ||
| 400 | } | ||
| 401 | } | ||
| 402 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 403 | my $metadatadb = $self->prepareDatabases( { doc => $parameters->{output} } ); | ||
| 404 | my $error = q{}; | ||
| 405 | unless ($metadatadb) { | ||
| 406 | $self->{LOGGER}->error("Database could not be opened."); | ||
| 407 | return -1; | ||
| 408 | } | ||
| 409 | |||
| 410 | my $parser = XML::LibXML->new(); | ||
| 411 | my @results = $metadatadb->query( { query => "/nmwg:store[\@type=\"MAStore\"]/nmwg:data", txn => q{}, error => \$error } ); | ||
| 412 | |||
| 413 | my $len = $#results; | ||
| 414 | if ( $len == -1 ) { | ||
| 415 | $self->{LOGGER}->error("Nothing returned for database search."); | ||
| 416 | return -1; | ||
| 417 | } | ||
| 418 | |||
| 419 | for my $x ( 0 .. $len ) { | ||
| 420 | my $hash = md5_hex( $results[$x] ); | ||
| 421 | my $data = $parser->parse_string( $results[$x] ); | ||
| 422 | $self->{CONF}->{"snmp"}->{"hashToId"}->{$hash} = $data->getDocumentElement->getAttribute("id"); | ||
| 423 | $self->{CONF}->{"snmp"}->{"idToHash"}->{ $data->getDocumentElement->getAttribute("id") } = $hash; | ||
| 424 | $self->{LOGGER}->debug( "Key id $hash maps to data element " . $data->getDocumentElement->getAttribute("id") ); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | else { | ||
| 428 | $self->{LOGGER}->error("Wrong value for 'metadata_db_type' set."); | ||
| 429 | return -1; | ||
| 430 | } | ||
| 431 | return 0; | ||
| 432 | } | ||
| 433 | |||
| 434 | =head2 needLS($self {}) | ||
| 435 | |||
| 436 | This particular service (SNMP MA) should register with a lookup service. This | ||
| 437 | function simply returns the value set in the configuration file (either yes or | ||
| 438 | no, depending on user preference) to let other parts of the framework know if | ||
| 439 | LS registration is required. | ||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub needLS { | ||
| 444 | my ( $self, @args ) = @_; | ||
| 445 | my $parameters = validateParams( @args, {} ); | ||
| 446 | |||
| 447 | return ( $self->{CONF}->{"snmp"}->{"enable_registration"} ); | ||
| 448 | } | ||
| 449 | |||
| 450 | =head2 registerLS($self $sleep_time) | ||
| 451 | |||
| 452 | Given the service information (specified in configuration) and the contents of | ||
| 453 | our metadata database, we can contact the specified LS and register ourselves. | ||
| 454 | We then sleep for some amount of time and do it again. | ||
| 455 | |||
| 456 | =cut | ||
| 457 | |||
| 458 | sub registerLS { | ||
| 459 | my ( $self, $sleep_time ) = validateParamsPos( @_, 1, { type => SCALARREF }, ); | ||
| 460 | |||
| 461 | # my ( $self, @args ) = @_; | ||
| 462 | # my $parameters = validateParams( @args, { sleep_time => 0 } ); | ||
| 463 | |||
| 464 | my ( $status, $res ); | ||
| 465 | my $ls = q{}; | ||
| 466 | |||
| 467 | if ( !defined $self->{LS_CLIENT} ) { | ||
| 468 | my %ls_conf = ( | ||
| 469 | SERVICE_TYPE => $self->{CONF}->{"snmp"}->{"service_type"}, | ||
| 470 | SERVICE_NAME => $self->{CONF}->{"snmp"}->{"service_name"}, | ||
| 471 | SERVICE_DESCRIPTION => $self->{CONF}->{"snmp"}->{"service_description"}, | ||
| 472 | SERVICE_ACCESSPOINT => $self->{CONF}->{"snmp"}->{"service_accesspoint"}, | ||
| 473 | ); | ||
| 474 | $self->{LS_CLIENT} = new perfSONAR_PS::Client::LS::Remote( $self->{CONF}->{"snmp"}->{"ls_instance"}, \%ls_conf, $self->{NAMESPACES} ); | ||
| 475 | } | ||
| 476 | |||
| 477 | $ls = $self->{LS_CLIENT}; | ||
| 478 | |||
| 479 | my $error = q{}; | ||
| 480 | my @resultsString = (); | ||
| 481 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 482 | @resultsString = $self->{METADATADB}->query( { query => "/nmwg:store/nmwg:metadata", error => \$error } ); | ||
| 483 | } | ||
| 484 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 485 | my $metadatadb = $self->prepareDatabases; | ||
| 486 | unless ($metadatadb) { | ||
| 487 | $self->{LOGGER}->error("Database could not be opened."); | ||
| 488 | return -1; | ||
| 489 | } | ||
| 490 | @resultsString = $metadatadb->query( { query => "/nmwg:store[\@type=\"MAStore\"]/nmwg:metadata", txn => q{}, error => \$error } ); | ||
| 491 | $metadatadb->closeDB( { error => \$error } ); | ||
| 492 | } | ||
| 493 | else { | ||
| 494 | $self->{LOGGER}->error("Wrong value for 'metadata_db_type' set."); | ||
| 495 | return -1; | ||
| 496 | } | ||
| 497 | |||
| 498 | if ( $#resultsString == -1 ) { | ||
| 499 | $self->{LOGGER}->error("No data to register with LS"); | ||
| 500 | return -1; | ||
| 501 | } | ||
| 502 | $ls->registerStatic( \@resultsString ); | ||
| 503 | return 0; | ||
| 504 | } | ||
| 505 | |||
| 506 | =head2 handleMessageBegin($self, { ret_message, messageId, messageType, msgParams, request, retMessageType, retMessageNamespaces }) | ||
| 507 | |||
| 508 | Stub function that is currently unused. Will be used to interact with the | ||
| 509 | daemon's message handler. | ||
| 510 | |||
| 511 | =cut | ||
| 512 | |||
| 513 | sub handleMessageBegin { | ||
| 514 | my ( $self, $ret_message, $messageId, $messageType, $msgParams, $request, $retMessageType, $retMessageNamespaces ) = @_; | ||
| 515 | |||
| 516 | # my ($self, @args) = @_; | ||
| 517 | # my $parameters = validateParams(@args, | ||
| 518 | # { | ||
| 519 | # ret_message => 1, | ||
| 520 | # messageId => 1, | ||
| 521 | # messageType => 1, | ||
| 522 | # msgParams => 1, | ||
| 523 | # request => 1, | ||
| 524 | # retMessageType => 1, | ||
| 525 | # retMessageNamespaces => 1 | ||
| 526 | # }); | ||
| 527 | |||
| 528 | return 0; | ||
| 529 | } | ||
| 530 | |||
| 531 | =head2 handleMessageEnd($self, { ret_message, messageId }) | ||
| 532 | |||
| 533 | Stub function that is currently unused. Will be used to interact with the | ||
| 534 | daemon's message handler. | ||
| 535 | |||
| 536 | =cut | ||
| 537 | |||
| 538 | sub handleMessageEnd { | ||
| 539 | my ( $self, $ret_message, $messageId ) = @_; | ||
| 540 | |||
| 541 | # my ($self, @args) = @_; | ||
| 542 | # my $parameters = validateParams(@args, | ||
| 543 | # { | ||
| 544 | # ret_message => 1, | ||
| 545 | # messageId => 1 | ||
| 546 | # }); | ||
| 547 | |||
| 548 | return 0; | ||
| 549 | } | ||
| 550 | |||
| 551 | =head2 handleEvent($self, { output, messageId, messageType, messageParameters, eventType, subject, filterChain, data, rawRequest, doOutputMetadata }) | ||
| 552 | |||
| 553 | Current workaround to the daemon's message handler. All messages that enter | ||
| 554 | will be routed based on the message type. The appropriate solution to this | ||
| 555 | problem is to route on eventType and message type and will be implemented in | ||
| 556 | future releases. | ||
| 557 | |||
| 558 | =cut | ||
| 559 | |||
| 560 | sub handleEvent { | ||
| 561 | my ( $self, @args ) = @_; | ||
| 562 | my $parameters = validateParams( | ||
| 563 | @args, | ||
| 564 | { | ||
| 565 | output => 1, | ||
| 566 | messageId => 1, | ||
| 567 | messageType => 1, | ||
| 568 | messageParameters => 1, | ||
| 569 | eventType => 1, | ||
| 570 | subject => 1, | ||
| 571 | filterChain => 1, | ||
| 572 | data => 1, | ||
| 573 | rawRequest => 1, | ||
| 574 | doOutputMetadata => 1, | ||
| 575 | } | ||
| 576 | ); | ||
| 577 | |||
| 578 | my @subjects = @{ $parameters->{subject} }; | ||
| 579 | my @filters = @{ $parameters->{filterChain} }; | ||
| 580 | my $md = $subjects[0]; | ||
| 581 | |||
| 582 | # this module outputs its own metadata so it needs to turn off the daemon's | ||
| 583 | # metadata output routines. | ||
| 584 | ${ $parameters->{doOutputMetadata} } = 0; | ||
| 585 | |||
| 586 | my %timeSettings = (); | ||
| 587 | |||
| 588 | # go through the main subject and select filters looking for parameters. | ||
| 589 | my $new_timeSettings = getFilterParameters( { m => $md, namespaces => $parameters->{rawRequest}->getNamespaces(), default_resolution => $self->{CONF}->{"snmp"}->{"default_resolution"} } ); | ||
| 590 | |||
| 591 | $timeSettings{"CF"} = $new_timeSettings->{"CF"} if ( defined $new_timeSettings->{"CF"} ); | ||
| 592 | $timeSettings{"RESOLUTION"} = $new_timeSettings->{"RESOLUTION"} if ( defined $new_timeSettings->{"RESOLUTION"} and $timeSettings{"RESOLUTION_SPECIFIED"} ); | ||
| 593 | $timeSettings{"RESOLUTION_SPECIFIED"} = $new_timeSettings->{"RESOLUTION_SPECIFIED"} if ( $new_timeSettings->{"RESOLUTION_SPECIFIED"} ); | ||
| 594 | |||
| 595 | if ( exists $new_timeSettings->{"START"}->{"value"} ) { | ||
| 596 | if ( exists $new_timeSettings->{"START"}->{"type"} and lc( $new_timeSettings->{"START"}->{"type"} ) eq "unix" ) { | ||
| 597 | $new_timeSettings->{"START"}->{"internal"} = $new_timeSettings->{"START"}->{"value"}; | ||
| 598 | } | ||
| 599 | elsif ( exists $new_timeSettings->{"START"}->{"type"} and lc( $new_timeSettings->{"START"}->{"type"} ) eq "iso" ) { | ||
| 600 | $new_timeSettings->{"START"}->{"internal"} = UnixDate( $new_timeSettings->{"START"}->{"value"}, "%s" ); | ||
| 601 | } | ||
| 602 | else { | ||
| 603 | $new_timeSettings->{"START"}->{"internal"} = $new_timeSettings->{"START"}->{"value"}; | ||
| 604 | } | ||
| 605 | } | ||
| 606 | $timeSettings{"START"} = $new_timeSettings->{"START"}; | ||
| 607 | |||
| 608 | if ( exists $new_timeSettings->{"END"}->{"value"} ) { | ||
| 609 | if ( exists $new_timeSettings->{"END"}->{"type"} and lc( $new_timeSettings->{"END"}->{"type"} ) eq "unix" ) { | ||
| 610 | $new_timeSettings->{"END"}->{"internal"} = $new_timeSettings->{"END"}->{"value"}; | ||
| 611 | } | ||
| 612 | elsif ( exists $new_timeSettings->{"START"}->{"type"} and lc( $new_timeSettings->{"END"}->{"type"} ) eq "iso" ) { | ||
| 613 | $new_timeSettings->{"END"}->{"internal"} = UnixDate( $new_timeSettings->{"END"}->{"value"}, "%s" ); | ||
| 614 | } | ||
| 615 | else { | ||
| 616 | $new_timeSettings->{"END"}->{"internal"} = $new_timeSettings->{"END"}->{"value"}; | ||
| 617 | } | ||
| 618 | } | ||
| 619 | $timeSettings{"END"} = $new_timeSettings->{"END"}; | ||
| 620 | |||
| 621 | if ( $#filters > -1 ) { | ||
| 622 | foreach my $filter_arr (@filters) { | ||
| 623 | my @filters = @{$filter_arr}; | ||
| 624 | my $filter = $filters[-1]; | ||
| 625 | |||
| 626 | $new_timeSettings = getFilterParameters( { m => $filter, namespaces => $parameters->{rawRequest}->getNamespaces(), default_resolution => $self->{CONF}->{"snmp"}->{"default_resolution"} } ); | ||
| 627 | |||
| 628 | $timeSettings{"CF"} = $new_timeSettings->{"CF"} if ( defined $new_timeSettings->{"CF"} ); | ||
| 629 | $timeSettings{"RESOLUTION"} = $new_timeSettings->{"RESOLUTION"} if ( defined $new_timeSettings->{"RESOLUTION"} and $new_timeSettings->{"RESOLUTION_SPECIFIED"} ); | ||
| 630 | $timeSettings{"RESOLUTION_SPECIFIED"} = $new_timeSettings->{"RESOLUTION_SPECIFIED"} if ( $new_timeSettings->{"RESOLUTION_SPECIFIED"} ); | ||
| 631 | |||
| 632 | if ( exists $new_timeSettings->{"START"}->{"value"} ) { | ||
| 633 | if ( exists $new_timeSettings->{"START"}->{"type"} and lc( $new_timeSettings->{"START"}->{"type"} ) eq "unix" ) { | ||
| 634 | $new_timeSettings->{"START"}->{"internal"} = $new_timeSettings->{"START"}->{"value"}; | ||
| 635 | } | ||
| 636 | elsif ( exists $new_timeSettings->{"START"}->{"type"} and lc( $new_timeSettings->{"START"}->{"type"} ) eq "iso" ) { | ||
| 637 | $new_timeSettings->{"START"}->{"internal"} = UnixDate( $new_timeSettings->{"START"}->{"value"}, "%s" ); | ||
| 638 | } | ||
| 639 | else { | ||
| 640 | $new_timeSettings->{"START"}->{"internal"} = $new_timeSettings->{"START"}->{"value"}; | ||
| 641 | } | ||
| 642 | } | ||
| 643 | else { | ||
| 644 | $new_timeSettings->{"START"}->{"internal"} = q{}; | ||
| 645 | } | ||
| 646 | |||
| 647 | if ( exists $new_timeSettings->{"END"}->{"value"} ) { | ||
| 648 | if ( exists $new_timeSettings->{"END"}->{"type"} and lc( $new_timeSettings->{"END"}->{"type"} ) eq "unix" ) { | ||
| 649 | $new_timeSettings->{"END"}->{"internal"} = $new_timeSettings->{"END"}->{"value"}; | ||
| 650 | } | ||
| 651 | elsif ( exists $new_timeSettings->{"END"}->{"type"} and lc( $new_timeSettings->{"END"}->{"type"} ) eq "iso" ) { | ||
| 652 | $new_timeSettings->{"END"}->{"internal"} = UnixDate( $new_timeSettings->{"END"}->{"value"}, "%s" ); | ||
| 653 | } | ||
| 654 | else { | ||
| 655 | $new_timeSettings->{"END"}->{"internal"} = $new_timeSettings->{"END"}->{"value"}; | ||
| 656 | } | ||
| 657 | } | ||
| 658 | else { | ||
| 659 | $new_timeSettings->{"END"}->{"internal"} = q{}; | ||
| 660 | } | ||
| 661 | |||
| 662 | # we conditionally replace the START/END settings since under the | ||
| 663 | # theory of filter, if a later element specifies an earlier start | ||
| 664 | # time, the later start time that appears higher in the filter chain | ||
| 665 | # would have filtered out all times earlier than itself leaving | ||
| 666 | # nothing to exist between the earlier start time and the later | ||
| 667 | # start time. XXX I'm not sure how the resolution and the | ||
| 668 | # consolidation function should work in this context. | ||
| 669 | |||
| 670 | if ( exists $new_timeSettings->{"START"}->{"internal"} and ( ( not exists $timeSettings{"START"}->{"internal"} ) or $new_timeSettings->{"START"}->{"internal"} > $timeSettings{"START"}->{"internal"} ) ) { | ||
| 671 | $timeSettings{"START"} = $new_timeSettings->{"START"}; | ||
| 672 | } | ||
| 673 | |||
| 674 | if ( exists $new_timeSettings->{"END"}->{"internal"} and ( ( not exists $timeSettings{"END"}->{"internal"} ) or $new_timeSettings->{"END"}->{"internal"} < $timeSettings{"END"}->{"internal"} ) ) { | ||
| 675 | $timeSettings{"END"} = $new_timeSettings->{"END"}; | ||
| 676 | } | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | # If no resolution was listed in the filters, go with the default | ||
| 681 | if ( not defined $timeSettings{"RESOLUTION"} ) { | ||
| 682 | $timeSettings{"RESOLUTION"} = $self->{CONF}->{"snmp"}->{"default_resolution"}; | ||
| 683 | $timeSettings{"RESOLUTION_SPECIFIED"} = 0; | ||
| 684 | } | ||
| 685 | |||
| 686 | my $cf = q{}; | ||
| 687 | my $resolution = q{}; | ||
| 688 | my $start = q{}; | ||
| 689 | my $end = q{}; | ||
| 690 | |||
| 691 | $cf = $timeSettings{"CF"} if ( $timeSettings{"CF"} ); | ||
| 692 | $resolution = $timeSettings{"RESOLUTION"} if ( $timeSettings{"RESOLUTION"} ); | ||
| 693 | $start = $timeSettings{"START"}->{"internal"} if ( $timeSettings{"START"}->{"internal"} ); | ||
| 694 | $end = $timeSettings{"END"}->{"internal"} if ( $timeSettings{"END"}->{"internal"} ); | ||
| 695 | |||
| 696 | $self->{LOGGER}->debug("Request filter parameters: cf: $cf resolution: $resolution start: $start end: $end"); | ||
| 697 | |||
| 698 | if ( $parameters->{messageType} eq "MetadataKeyRequest" ) { | ||
| 699 | return $self->maMetadataKeyRequest( | ||
| 700 | { | ||
| 701 | output => $parameters->{output}, | ||
| 702 | metadata => $md, | ||
| 703 | filters => \@filters, | ||
| 704 | time_settings => \%timeSettings, | ||
| 705 | request => $parameters->{rawRequest}, | ||
| 706 | message_parameters => $parameters->{messageParameters} | ||
| 707 | } | ||
| 708 | ); | ||
| 709 | } | ||
| 710 | elsif ( $parameters->{messageType} eq "SetupDataRequest" ) { | ||
| 711 | return $self->maSetupDataRequest( | ||
| 712 | { | ||
| 713 | output => $parameters->{output}, | ||
| 714 | metadata => $md, | ||
| 715 | filters => \@filters, | ||
| 716 | time_settings => \%timeSettings, | ||
| 717 | request => $parameters->{rawRequest}, | ||
| 718 | message_parameters => $parameters->{messageParameters} | ||
| 719 | } | ||
| 720 | ); | ||
| 721 | } | ||
| 722 | else { | ||
| 723 | throw perfSONAR_PS::Error_compat( "error.ma.message_type", "Invalid Message Type" ); | ||
| 724 | return; | ||
| 725 | } | ||
| 726 | return; | ||
| 727 | } | ||
| 728 | |||
| 729 | =head2 maMetadataKeyRequest($self, { output, metadata, time_settings, filters, request, message_parameters }) | ||
| 730 | |||
| 731 | Main handler of MetadataKeyRequest messages. Based on contents (i.e. was a | ||
| 732 | key sent in the request, or not) this will route to one of two functions: | ||
| 733 | |||
| 734 | - metadataKeyRetrieveKey - Handles all requests that enter with a | ||
| 735 | key present. | ||
| 736 | - metadataKeyRetrieveMetadataData - Handles all other requests | ||
| 737 | |||
| 738 | The goal of this message type is to return a pointer (i.e. a 'key') to the data | ||
| 739 | so that the more expensive operation of XPath searching the database is avoided | ||
| 740 | with a simple hashed key lookup. The key currently can be replayed repeatedly | ||
| 741 | currently because it is not time sensitive. | ||
| 742 | |||
| 743 | =cut | ||
| 744 | |||
| 745 | sub maMetadataKeyRequest { | ||
| 746 | my ( $self, @args ) = @_; | ||
| 747 | my $parameters = validateParams( | ||
| 748 | @args, | ||
| 749 | { | ||
| 750 | output => 1, | ||
| 751 | metadata => 1, | ||
| 752 | time_settings => 1, | ||
| 753 | filters => 1, | ||
| 754 | request => 1, | ||
| 755 | message_parameters => 1 | ||
| 756 | } | ||
| 757 | ); | ||
| 758 | |||
| 759 | my $mdId = q{}; | ||
| 760 | my $dId = q{}; | ||
| 761 | my $error = q{}; | ||
| 762 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 763 | $self->{METADATADB} = $self->prepareDatabases( { doc => $parameters->{output} } ); | ||
| 764 | unless ( $self->{METADATADB} ) { | ||
| 765 | throw perfSONAR_PS::Error_compat("Database could not be opened."); | ||
| 766 | return; | ||
| 767 | } | ||
| 768 | } | ||
| 769 | unless ( ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) | ||
| 770 | or ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) ) | ||
| 771 | { | ||
| 772 | throw perfSONAR_PS::Error_compat("Wrong value for 'metadata_db_type' set."); | ||
| 773 | return; | ||
| 774 | } | ||
| 775 | |||
| 776 | my $nmwg_key = find( $parameters->{metadata}, "./nmwg:key", 1 ); | ||
| 777 | if ($nmwg_key) { | ||
| 778 | $self->metadataKeyRetrieveKey( | ||
| 779 | { | ||
| 780 | metadatadb => $self->{METADATADB}, | ||
| 781 | key => $nmwg_key, | ||
| 782 | metadata => $parameters->{metadata}, | ||
| 783 | filters => $parameters->{filters}, | ||
| 784 | request_namespaces => $parameters->{request}->getNamespaces(), | ||
| 785 | output => $parameters->{output} | ||
| 786 | } | ||
| 787 | ); | ||
| 788 | } | ||
| 789 | else { | ||
| 790 | $self->metadataKeyRetrieveMetadataData( | ||
| 791 | { | ||
| 792 | metadatadb => $self->{METADATADB}, | ||
| 793 | time_settings => $parameters->{time_settings}, | ||
| 794 | metadata => $parameters->{metadata}, | ||
| 795 | filters => $parameters->{filters}, | ||
| 796 | request_namespaces => $parameters->{request}->getNamespaces(), | ||
| 797 | output => $parameters->{output} | ||
| 798 | } | ||
| 799 | ); | ||
| 800 | |||
| 801 | } | ||
| 802 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 803 | $self->{METADATADB}->closeDB( { error => \$error } ); | ||
| 804 | } | ||
| 805 | return; | ||
| 806 | } | ||
| 807 | |||
| 808 | =head2 metadataKeyRetrieveKey($self, { metadatadb, key, metadata, filters, request_namespaces, output }) | ||
| 809 | |||
| 810 | Because the request entered with a key, we must handle it in this particular | ||
| 811 | function. We first attempt to extract the 'maKey' hash and check for validity. | ||
| 812 | An invalid or missing key will trigger an error instantly. If the key is found | ||
| 813 | we see if any chaining needs to be done (and appropriately 'cook' the key), then | ||
| 814 | return the response. | ||
| 815 | |||
| 816 | =cut | ||
| 817 | |||
| 818 | sub metadataKeyRetrieveKey { | ||
| 819 | my ( $self, @args ) = @_; | ||
| 820 | my $parameters = validateParams( | ||
| 821 | @args, | ||
| 822 | { | ||
| 823 | metadatadb => 1, | ||
| 824 | key => 1, | ||
| 825 | metadata => 1, | ||
| 826 | filters => 1, | ||
| 827 | request_namespaces => 1, | ||
| 828 | output => 1 | ||
| 829 | } | ||
| 830 | ); | ||
| 831 | |||
| 832 | my $mdId = "metadata." . genuid(); | ||
| 833 | my $dId = "data." . genuid(); | ||
| 834 | my $hashKey = extract( find( $parameters->{key}, ".//nmwg:parameter[\@name=\"maKey\"]", 1 ), 0 ); | ||
| 835 | unless ($hashKey) { | ||
| 836 | my $msg = "Key error in metadata storage."; | ||
| 837 | $self->{LOGGER}->error($msg); | ||
| 838 | throw perfSONAR_PS::Error_compat( "error.ma.storage_result", $msg ); | ||
| 839 | return; | ||
| 840 | } | ||
| 841 | |||
| 842 | my $hashId = $self->{CONF}->{"snmp"}->{"hashToId"}->{$hashKey}; | ||
| 843 | unless ($hashId) { | ||
| 844 | my $msg = "Key error in metadata storage."; | ||
| 845 | $self->{LOGGER}->error($msg); | ||
| 846 | throw perfSONAR_PS::Error_compat( "error.ma.storage_result", $msg ); | ||
| 847 | return; | ||
| 848 | } | ||
| 849 | |||
| 850 | my $query = q{}; | ||
| 851 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 852 | $query = "/nmwg:store/nmwg:data[\@id=\"" . $hashId . "\"]"; | ||
| 853 | } | ||
| 854 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 855 | $query = "/nmwg:store[\@type=\"MAStore\"]/nmwg:data[\@id=\"" . $hashId . "\"]"; | ||
| 856 | } | ||
| 857 | |||
| 858 | if ( $parameters->{metadatadb}->count( { query => $query } ) != 1 ) { | ||
| 859 | my $msg = "Key error in metadata storage."; | ||
| 860 | $self->{LOGGER}->error($msg); | ||
| 861 | throw perfSONAR_PS::Error_compat( "error.ma.storage_result", $msg ); | ||
| 862 | return; | ||
| 863 | } | ||
| 864 | |||
| 865 | my $mdIdRef; | ||
| 866 | my @filters = @{ $parameters->{filters} }; | ||
| 867 | if ( $#filters > -1 ) { | ||
| 868 | $mdIdRef = $filters[-1][0]->getAttribute("id"); | ||
| 869 | } | ||
| 870 | else { | ||
| 871 | $mdIdRef = $parameters->{metadata}->getAttribute("id"); | ||
| 872 | } | ||
| 873 | |||
| 874 | createMetadata( $parameters->{output}, $mdId, $mdIdRef, $parameters->{key}->toString, undef ); | ||
| 875 | my $key2 = $parameters->{key}->cloneNode(1); | ||
| 876 | my $params = find( $key2, ".//nmwg:parameters", 1 ); | ||
| 877 | $self->addSelectParameters( { parameter_block => $params, filters => $parameters->{filters} } ); | ||
| 878 | createData( $parameters->{output}, $dId, $mdId, $key2->toString, undef ); | ||
| 879 | return; | ||
| 880 | } | ||
| 881 | |||
| 882 | =head2 metadataKeyRetrieveMetadataData($self, $metadatadb, $metadata, $chain, | ||
| 883 | $id, $request_namespaces, $output) | ||
| 884 | |||
| 885 | Similar to 'metadataKeyRetrieveKey' we are looking to return a valid key. The | ||
| 886 | input will be partially or fully specified metadata. If this matches something | ||
| 887 | in the database we will return a key matching the description (in the form of | ||
| 888 | an MD5 fingerprint). If this metadata was a part of a chain the chaining will | ||
| 889 | be resolved and used to augment (i.e. 'cook') the key. | ||
| 890 | |||
| 891 | =cut | ||
| 892 | |||
| 893 | sub metadataKeyRetrieveMetadataData { | ||
| 894 | my ( $self, @args ) = @_; | ||
| 895 | my $parameters = validateParams( | ||
| 896 | @args, | ||
| 897 | { | ||
| 898 | metadatadb => 1, | ||
| 899 | time_settings => 1, | ||
| 900 | metadata => 1, | ||
| 901 | filters => 1, | ||
| 902 | request_namespaces => 1, | ||
| 903 | output => 1 | ||
| 904 | } | ||
| 905 | ); | ||
| 906 | |||
| 907 | my $mdId = q{}; | ||
| 908 | my $dId = q{}; | ||
| 909 | my $queryString = q{}; | ||
| 910 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 911 | $queryString = "/nmwg:store/nmwg:metadata[" . getMetadataXQuery( { node => $parameters->{metadata} } ) . "]"; | ||
| 912 | } | ||
| 913 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 914 | $queryString = "/nmwg:store[\@type=\"MAStore\"]/nmwg:metadata[" . getMetadataXQuery( { node => $parameters->{metadata} } ) . "]"; | ||
| 915 | } | ||
| 916 | |||
| 917 | my $results = $parameters->{metadatadb}->querySet( { query => $queryString } ); | ||
| 918 | my %et = (); | ||
| 919 | my $eventTypes = find( $parameters->{metadata}, "./nmwg:eventType", 0 ); | ||
| 920 | my $supportedEventTypes = find( $parameters->{metadata}, ".//nmwg:parameter[\@name=\"supportedEventType\" or \@name=\"eventType\"]", 0 ); | ||
| 921 | foreach my $e ( $eventTypes->get_nodelist ) { | ||
| 922 | my $value = extract( $e, 0 ); | ||
| 923 | if ($value) { | ||
| 924 | $et{$value} = 1; | ||
| 925 | } | ||
| 926 | } | ||
| 927 | foreach my $se ( $supportedEventTypes->get_nodelist ) { | ||
| 928 | my $value = extract( $se, 0 ); | ||
| 929 | if ($value) { | ||
| 930 | $et{$value} = 1; | ||
| 931 | } | ||
| 932 | } | ||
| 933 | |||
| 934 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 935 | $queryString = "/nmwg:store/nmwg:data"; | ||
| 936 | } | ||
| 937 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 938 | $queryString = "/nmwg:store[\@type=\"MAStore\"]/nmwg:data"; | ||
| 939 | } | ||
| 940 | |||
| 941 | if ( $eventTypes->size() or $supportedEventTypes->size() ) { | ||
| 942 | $queryString = $queryString . "[./nmwg:key/nmwg:parameters/nmwg:parameter[(\@name=\"supportedEventType\" or \@name=\"eventType\")"; | ||
| 943 | foreach my $e ( sort keys %et ) { | ||
| 944 | $queryString = $queryString . " and (\@value=\"" . $e . "\" or text()=\"" . $e . "\")"; | ||
| 945 | } | ||
| 946 | $queryString = $queryString . "]]"; | ||
| 947 | } | ||
| 948 | my $dataResults = $parameters->{metadatadb}->querySet( { query => $queryString } ); | ||
| 949 | if ( $results->size() > 0 and $dataResults->size() > 0 ) { | ||
| 950 | my %mds = (); | ||
| 951 | foreach my $md ( $results->get_nodelist ) { | ||
| 952 | my $curr_md_id = $md->getAttribute("id"); | ||
| 953 | next if not $curr_md_id; | ||
| 954 | $mds{$curr_md_id} = $md; | ||
| 955 | } | ||
| 956 | |||
| 957 | foreach my $d ( $dataResults->get_nodelist ) { | ||
| 958 | my $curr_d_mdIdRef = $d->getAttribute("metadataIdRef"); | ||
| 959 | next if ( not $curr_d_mdIdRef or not exists $mds{$curr_d_mdIdRef} ); | ||
| 960 | my $curr_md = $mds{$curr_d_mdIdRef}; | ||
| 961 | |||
| 962 | my $dId = "data." . genuid(); | ||
| 963 | my $mdId = "metadata." . genuid(); | ||
| 964 | |||
| 965 | my $md_temp = $curr_md->cloneNode(1); | ||
| 966 | $md_temp->setAttribute( "metadataIdRef", $curr_d_mdIdRef ); | ||
| 967 | $md_temp->setAttribute( "id", $mdId ); | ||
| 968 | |||
| 969 | $parameters->{output}->addExistingXMLElement($md_temp); | ||
| 970 | |||
| 971 | my $hashId = $d->getAttribute("id"); | ||
| 972 | my $hashKey = $self->{CONF}->{"snmp"}->{"idToHash"}->{$hashId}; | ||
| 973 | |||
| 974 | next if ( not defined $hashKey ); | ||
| 975 | |||
| 976 | startData( $parameters->{output}, $dId, $mdId, undef ); | ||
| 977 | $parameters->{output}->startElement( { prefix => "nmwg", tag => "key", namespace => "http://ggf.org/ns/nmwg/base/2.0/" } ); | ||
| 978 | startParameters( $parameters->{output}, "params.0" ); | ||
| 979 | addParameter( $parameters->{output}, "maKey", $hashKey ); | ||
| 980 | |||
| 981 | my %attrs = (); | ||
| 982 | $attrs{"type"} = $parameters->{time_settings}->{"START"}->{"type"} if $parameters->{time_settings}->{"START"}->{"type"}; | ||
| 983 | addParameter( $parameters->{output}, "startTime", $parameters->{time_settings}->{"START"}->{"value"}, \%attrs ) if ( defined $parameters->{time_settings}->{"START"}->{"value"} ); | ||
| 984 | |||
| 985 | %attrs = (); | ||
| 986 | $attrs{"type"} = $parameters->{time_settings}->{"END"}->{"type"} if $parameters->{time_settings}->{"END"}->{"type"}; | ||
| 987 | addParameter( $parameters->{output}, "endTime", $parameters->{time_settings}->{"END"}->{"value"}, \%attrs ) if ( defined $parameters->{time_settings}->{"END"}->{"value"} ); | ||
| 988 | |||
| 989 | if ( defined $parameters->{time_settings}->{"RESOLUTION"} and $parameters->{time_settings}->{"RESOLUTION_SPECIFIED"} ) { | ||
| 990 | addParameter( $parameters->{output}, "resolution", $parameters->{time_settings}->{"RESOLUTION"} ); | ||
| 991 | } | ||
| 992 | addParameter( $parameters->{output}, "consolidationFunction", $parameters->{time_settings}->{"CF"} ) if ( defined $parameters->{time_settings}->{"CF"} ); | ||
| 993 | endParameters( $parameters->{output} ); | ||
| 994 | $parameters->{output}->endElement("key"); | ||
| 995 | endData( $parameters->{output} ); | ||
| 996 | } | ||
| 997 | } | ||
| 998 | else { | ||
| 999 | my $msg = "Database \"" . $self->{CONF}->{"snmp"}->{"metadata_db_file"} . "\" returned 0 results for search"; | ||
| 1000 | $self->{LOGGER}->error($msg); | ||
| 1001 | throw perfSONAR_PS::Error_compat( "error.ma.storage", $msg ); | ||
| 1002 | } | ||
| 1003 | return; | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | =head2 maSetupDataRequest($self, $output, $md, $request, $message_parameters) | ||
| 1007 | |||
| 1008 | Main handler of SetupDataRequest messages. Based on contents (i.e. was a | ||
| 1009 | key sent in the request, or not) this will route to one of two functions: | ||
| 1010 | |||
| 1011 | - setupDataRetrieveKey - Handles all requests that enter with a | ||
| 1012 | key present. | ||
| 1013 | - setupDataRetrieveMetadataData - Handles all other requests | ||
| 1014 | |||
| 1015 | Chaining operations are handled internally, although chaining will eventually | ||
| 1016 | be moved to the overall message handler as it is an important operation that | ||
| 1017 | all services will need. | ||
| 1018 | |||
| 1019 | The goal of this message type is to return actual data, so after the metadata | ||
| 1020 | section is resolved the appropriate data handler will be called to interact | ||
| 1021 | with the database of choice (i.e. rrdtool, mysql, sqlite). | ||
| 1022 | |||
| 1023 | =cut | ||
| 1024 | |||
| 1025 | sub maSetupDataRequest { | ||
| 1026 | my ( $self, @args ) = @_; | ||
| 1027 | my $parameters = validateParams( | ||
| 1028 | @args, | ||
| 1029 | { | ||
| 1030 | output => 1, | ||
| 1031 | metadata => 1, | ||
| 1032 | filters => 1, | ||
| 1033 | time_settings => 1, | ||
| 1034 | request => 1, | ||
| 1035 | message_parameters => 1 | ||
| 1036 | } | ||
| 1037 | ); | ||
| 1038 | |||
| 1039 | my $mdId = q{}; | ||
| 1040 | my $dId = q{}; | ||
| 1041 | my $error = q{}; | ||
| 1042 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 1043 | $self->{METADATADB} = $self->prepareDatabases( { doc => $parameters->{output} } ); | ||
| 1044 | unless ( $self->{METADATADB} ) { | ||
| 1045 | throw perfSONAR_PS::Error_compat("Database could not be opened."); | ||
| 1046 | return; | ||
| 1047 | } | ||
| 1048 | } | ||
| 1049 | unless ( ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) | ||
| 1050 | or ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) ) | ||
| 1051 | { | ||
| 1052 | throw perfSONAR_PS::Error_compat("Wrong value for 'metadata_db_type' set."); | ||
| 1053 | return; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | my $nmwg_key = find( $parameters->{metadata}, "./nmwg:key", 1 ); | ||
| 1057 | if ($nmwg_key) { | ||
| 1058 | $self->setupDataRetrieveKey( | ||
| 1059 | { | ||
| 1060 | metadatadb => $self->{METADATADB}, | ||
| 1061 | metadata => $nmwg_key, | ||
| 1062 | filters => $parameters->{filters}, | ||
| 1063 | message_parameters => $parameters->{message_parameters}, | ||
| 1064 | time_settings => $parameters->{time_settings}, | ||
| 1065 | request_namespaces => $parameters->{request}->getNamespaces(), | ||
| 1066 | output => $parameters->{output} | ||
| 1067 | } | ||
| 1068 | ); | ||
| 1069 | } | ||
| 1070 | else { | ||
| 1071 | $self->setupDataRetrieveMetadataData( | ||
| 1072 | { | ||
| 1073 | metadatadb => $self->{METADATADB}, | ||
| 1074 | metadata => $parameters->{metadata}, | ||
| 1075 | filters => $parameters->{filters}, | ||
| 1076 | time_settings => $parameters->{time_settings}, | ||
| 1077 | message_parameters => $parameters->{message_parameters}, | ||
| 1078 | output => $parameters->{output} | ||
| 1079 | } | ||
| 1080 | ); | ||
| 1081 | } | ||
| 1082 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 1083 | $self->{METADATADB}->closeDB( { error => \$error } ); | ||
| 1084 | } | ||
| 1085 | return; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | =head2 setupDataRetrieveKey($self, $metadatadb, $metadata, $chain, $id, | ||
| 1089 | $message_parameters, $request_namespaces, $output) | ||
| 1090 | |||
| 1091 | Because the request entered with a key, we must handle it in this particular | ||
| 1092 | function. We first attempt to extract the 'maKey' hash and check for validity. | ||
| 1093 | An invalid or missing key will trigger an error instantly. If the key is found | ||
| 1094 | we see if any chaining needs to be done. We finally call the handle data | ||
| 1095 | function, passing along the useful pieces of information from the metadata | ||
| 1096 | database to locate and interact with the backend storage (i.e. rrdtool, mysql, | ||
| 1097 | sqlite). | ||
| 1098 | |||
| 1099 | =cut | ||
| 1100 | |||
| 1101 | sub setupDataRetrieveKey { | ||
| 1102 | my ( $self, @args ) = @_; | ||
| 1103 | my $parameters = validateParams( | ||
| 1104 | @args, | ||
| 1105 | { | ||
| 1106 | metadatadb => 1, | ||
| 1107 | metadata => 1, | ||
| 1108 | filters => 1, | ||
| 1109 | time_settings => 1, | ||
| 1110 | message_parameters => 1, | ||
| 1111 | request_namespaces => 1, | ||
| 1112 | output => 1 | ||
| 1113 | } | ||
| 1114 | ); | ||
| 1115 | |||
| 1116 | my $mdId = q{}; | ||
| 1117 | my $dId = q{}; | ||
| 1118 | my $results = q{}; | ||
| 1119 | |||
| 1120 | my $hashKey = extract( find( $parameters->{metadata}, ".//nmwg:parameter[\@name=\"maKey\"]", 1 ), 0 ); | ||
| 1121 | unless ($hashKey) { | ||
| 1122 | my $msg = "Key error in metadata storage."; | ||
| 1123 | $self->{LOGGER}->error($msg); | ||
| 1124 | throw perfSONAR_PS::Error_compat( "error.ma.storage_result", $msg ); | ||
| 1125 | return; | ||
| 1126 | } | ||
| 1127 | |||
| 1128 | my $hashId = $self->{CONF}->{"snmp"}->{"hashToId"}->{$hashKey}; | ||
| 1129 | $self->{LOGGER}->debug("Received hash key $hashKey which maps to $hashId"); | ||
| 1130 | unless ($hashId) { | ||
| 1131 | my $msg = "Key error in metadata storage."; | ||
| 1132 | $self->{LOGGER}->error($msg); | ||
| 1133 | throw perfSONAR_PS::Error_compat( "error.ma.storage_result", $msg ); | ||
| 1134 | return; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | my $query = q{}; | ||
| 1138 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 1139 | $query = "/nmwg:store/nmwg:data[\@id=\"" . $hashId . "\"]"; | ||
| 1140 | } | ||
| 1141 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 1142 | $query = "/nmwg:store[\@type=\"MAStore\"]/nmwg:data[\@id=\"" . $hashId . "\"]"; | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | $results = $parameters->{metadatadb}->querySet( { query => $query } ); | ||
| 1146 | if ( $results->size() != 1 ) { | ||
| 1147 | my $msg = "Key error in metadata storage."; | ||
| 1148 | $self->{LOGGER}->error($msg); | ||
| 1149 | throw perfSONAR_PS::Error_compat( "error.ma.storage_result", $msg ); | ||
| 1150 | return; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | my $sentKey = $parameters->{metadata}->cloneNode(1); | ||
| 1154 | my $results_temp = $results->get_node(1)->cloneNode(1); | ||
| 1155 | my $storedKey = find( $results_temp, "./nmwg:key", 1 ); | ||
| 1156 | |||
| 1157 | my %l_et = (); | ||
| 1158 | my $l_supportedEventTypes = find( $storedKey, ".//nmwg:parameter[\@name=\"supportedEventType\" or \@name=\"eventType\"]", 0 ); | ||
| 1159 | foreach my $se ( $l_supportedEventTypes->get_nodelist ) { | ||
| 1160 | my $value = extract( $se, 0 ); | ||
| 1161 | if ($value) { | ||
| 1162 | $l_et{$value} = 1; | ||
| 1163 | } | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | $mdId = "metadata." . genuid(); | ||
| 1167 | $dId = "data." . genuid(); | ||
| 1168 | |||
| 1169 | my $mdIdRef = $parameters->{metadata}->getAttribute("id"); | ||
| 1170 | |||
| 1171 | my @filters = @{ $parameters->{filters} }; | ||
| 1172 | |||
| 1173 | if ( $#filters > -1 ) { | ||
| 1174 | $self->addSelectParameters( { parameter_block => find( $sentKey, ".//nmwg:parameters", 1 ), filters => \@filters } ); | ||
| 1175 | |||
| 1176 | $mdIdRef = $filters[-1][0]->getAttribute("id"); | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | createMetadata( $parameters->{output}, $mdId, $mdIdRef, $sentKey->toString, undef ); | ||
| 1180 | $self->handleData( | ||
| 1181 | { | ||
| 1182 | id => $mdId, | ||
| 1183 | data => $results_temp, | ||
| 1184 | output => $parameters->{output}, | ||
| 1185 | time_settings => $parameters->{time_settings}, | ||
| 1186 | et => \%l_et, | ||
| 1187 | message_parameters => $parameters->{message_parameters} | ||
| 1188 | } | ||
| 1189 | ); | ||
| 1190 | |||
| 1191 | return; | ||
| 1192 | } | ||
| 1193 | |||
| 1194 | =head2 setupDataRetrieveMetadataData($self, $metadatadb, $metadata, $id, | ||
| 1195 | $message_parameters, $output) | ||
| 1196 | |||
| 1197 | Similar to 'setupDataRetrieveKey' we are looking to return data. The input | ||
| 1198 | will be partially or fully specified metadata. If this matches something in | ||
| 1199 | the database we will return a data matching the description. If this metadata | ||
| 1200 | was a part of a chain the chaining will be resolved passed along to the data | ||
| 1201 | handling function. | ||
| 1202 | |||
| 1203 | =cut | ||
| 1204 | |||
| 1205 | sub setupDataRetrieveMetadataData { | ||
| 1206 | my ( $self, @args ) = @_; | ||
| 1207 | my $parameters = validateParams( | ||
| 1208 | @args, | ||
| 1209 | { | ||
| 1210 | metadatadb => 1, | ||
| 1211 | metadata => 1, | ||
| 1212 | filters => 1, | ||
| 1213 | time_settings => 1, | ||
| 1214 | message_parameters => 1, | ||
| 1215 | output => 1 | ||
| 1216 | } | ||
| 1217 | ); | ||
| 1218 | |||
| 1219 | my $mdId = q{}; | ||
| 1220 | my $dId = q{}; | ||
| 1221 | |||
| 1222 | my $queryString = q{}; | ||
| 1223 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 1224 | $queryString = "/nmwg:store/nmwg:metadata[" . getMetadataXQuery( { node => $parameters->{metadata} } ) . "]"; | ||
| 1225 | } | ||
| 1226 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 1227 | $queryString = "/nmwg:store[\@type=\"MAStore\"]/nmwg:metadata[" . getMetadataXQuery( { node => $parameters->{metadata} } ) . "]"; | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | my $results = $parameters->{metadatadb}->querySet( { query => $queryString } ); | ||
| 1231 | |||
| 1232 | my %et = (); | ||
| 1233 | my $eventTypes = find( $parameters->{metadata}, "./nmwg:eventType", 0 ); | ||
| 1234 | my $supportedEventTypes = find( $parameters->{metadata}, ".//nmwg:parameter[\@name=\"supportedEventType\" or \@name=\"eventType\"]", 0 ); | ||
| 1235 | foreach my $e ( $eventTypes->get_nodelist ) { | ||
| 1236 | my $value = extract( $e, 0 ); | ||
| 1237 | if ($value) { | ||
| 1238 | $et{$value} = 1; | ||
| 1239 | } | ||
| 1240 | } | ||
| 1241 | foreach my $se ( $supportedEventTypes->get_nodelist ) { | ||
| 1242 | my $value = extract( $se, 0 ); | ||
| 1243 | if ($value) { | ||
| 1244 | $et{$value} = 1; | ||
| 1245 | } | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | if ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "file" ) { | ||
| 1249 | $queryString = "/nmwg:store/nmwg:data"; | ||
| 1250 | } | ||
| 1251 | elsif ( $self->{CONF}->{"snmp"}->{"metadata_db_type"} eq "xmldb" ) { | ||
| 1252 | $queryString = "/nmwg:store[\@type=\"MAStore\"]/nmwg:data"; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | if ( $eventTypes->size() or $supportedEventTypes->size() ) { | ||
| 1256 | $queryString = $queryString . "[./nmwg:key/nmwg:parameters/nmwg:parameter[(\@name=\"supportedEventType\" or \@name=\"eventType\")"; | ||
| 1257 | foreach my $e ( sort keys %et ) { | ||
| 1258 | $queryString = $queryString . " and (\@value=\"" . $e . "\" or text()=\"" . $e . "\")"; | ||
| 1259 | } | ||
| 1260 | $queryString = $queryString . "]]"; | ||
| 1261 | } | ||
| 1262 | my $dataResults = $parameters->{metadatadb}->querySet( { query => $queryString } ); | ||
| 1263 | |||
| 1264 | my %used = (); | ||
| 1265 | for my $x ( 0 .. $dataResults->size() ) { | ||
| 1266 | $used{$x} = 0; | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | my $base_id = $parameters->{metadata}->getAttribute("id"); | ||
| 1270 | my @filters = @{ $parameters->{filters} }; | ||
| 1271 | if ( $#filters > -1 ) { | ||
| 1272 | my @filter_arr = @{ $filters[-1] }; | ||
| 1273 | |||
| 1274 | $base_id = $filter_arr[0]->getAttribute("id"); | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | if ( $results->size() > 0 and $dataResults->size() > 0 ) { | ||
| 1278 | my %mds = (); | ||
| 1279 | foreach my $md ( $results->get_nodelist ) { | ||
| 1280 | next if not $md->getAttribute("id"); | ||
| 1281 | |||
| 1282 | my %l_et = (); | ||
| 1283 | my $l_eventTypes = find( $md, "./nmwg:eventType", 0 ); | ||
| 1284 | my $l_supportedEventTypes = find( $md, ".//nmwg:parameter[\@name=\"supportedEventType\" or \@name=\"eventType\"]", 0 ); | ||
| 1285 | foreach my $e ( $l_eventTypes->get_nodelist ) { | ||
| 1286 | my $value = extract( $e, 0 ); | ||
| 1287 | if ($value) { | ||
| 1288 | $l_et{$value} = 1; | ||
| 1289 | } | ||
| 1290 | } | ||
| 1291 | foreach my $se ( $l_supportedEventTypes->get_nodelist ) { | ||
| 1292 | my $value = extract( $se, 0 ); | ||
| 1293 | if ($value) { | ||
| 1294 | $l_et{$value} = 1; | ||
| 1295 | } | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | my %hash = (); | ||
| 1299 | $hash{"md"} = $md; | ||
| 1300 | $hash{"et"} = \%l_et; | ||
| 1301 | $mds{ $md->getAttribute("id") } = \%hash; | ||
| 1302 | } | ||
| 1303 | |||
| 1304 | foreach my $d ( $dataResults->get_nodelist ) { | ||
| 1305 | my $idRef = $d->getAttribute("metadataIdRef"); | ||
| 1306 | |||
| 1307 | next if ( not defined $idRef or not defined $mds{$idRef} ); | ||
| 1308 | |||
| 1309 | my $md_temp = $mds{$idRef}->{"md"}->cloneNode(1); | ||
| 1310 | my $d_temp = $d->cloneNode(1); | ||
| 1311 | $mdId = "metadata." . genuid(); | ||
| 1312 | $md_temp->setAttribute( "metadataIdRef", $base_id ); | ||
| 1313 | $md_temp->setAttribute( "id", $mdId ); | ||
| 1314 | $parameters->{output}->addExistingXMLElement($md_temp); | ||
| 1315 | $self->handleData( | ||
| 1316 | { | ||
| 1317 | id => $mdId, | ||
| 1318 | data => $d_temp, | ||
| 1319 | output => $parameters->{output}, | ||
| 1320 | time_settings => $parameters->{time_settings}, | ||
| 1321 | et => $mds{$idRef}->{"et"}, | ||
| 1322 | message_parameters => $parameters->{message_parameters} | ||
| 1323 | } | ||
| 1324 | ); | ||
| 1325 | } | ||
| 1326 | } | ||
| 1327 | else { | ||
| 1328 | my $msg = "Database \"" . $self->{CONF}->{"snmp"}->{"metadata_db_file"} . "\" returned 0 results for search"; | ||
| 1329 | $self->{LOGGER}->error($msg); | ||
| 1330 | throw perfSONAR_PS::Error_compat( "error.ma.storage", $msg ); | ||
| 1331 | } | ||
| 1332 | return; | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | =head2 handleData($self, $id, $data, $output, $et, $message_parameters) | ||
| 1336 | |||
| 1337 | Directs the data retrieval operations based on a value found in the metadata | ||
| 1338 | database's representation of the key (i.e. storage 'type'). Current offerings | ||
| 1339 | only interact with rrd files and sql databases. | ||
| 1340 | |||
| 1341 | =cut | ||
| 1342 | |||
| 1343 | sub handleData { | ||
| 1344 | my ( $self, @args ) = @_; | ||
| 1345 | my $parameters = validateParams( | ||
| 1346 | @args, | ||
| 1347 | { | ||
| 1348 | id => 1, | ||
| 1349 | data => 1, | ||
| 1350 | output => 1, | ||
| 1351 | et => 1, | ||
| 1352 | time_settings => 1, | ||
| 1353 | message_parameters => 1 | ||
| 1354 | } | ||
| 1355 | ); | ||
| 1356 | |||
| 1357 | my $type = extract( find( $parameters->{data}, "./nmwg:key/nmwg:parameters/nmwg:parameter[\@name=\"type\"]", 1 ), 0 ); | ||
| 1358 | if ( $type eq "rrd" ) { | ||
| 1359 | $self->retrieveRRD( | ||
| 1360 | { | ||
| 1361 | d => $parameters->{data}, | ||
| 1362 | mid => $parameters->{id}, | ||
| 1363 | output => $parameters->{output}, | ||
| 1364 | time_settings => $parameters->{time_settings}, | ||
| 1365 | et => $parameters->{et}, | ||
| 1366 | message_parameters => $parameters->{et} | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | ); | ||
| 1370 | } | ||
| 1371 | elsif ( $type eq "sqlite" ) { | ||
| 1372 | $self->retrieveSQL( | ||
| 1373 | |||
| 1374 | { | ||
| 1375 | d => $parameters->{data}, | ||
| 1376 | mid => $parameters->{id}, | ||
| 1377 | output => $parameters->{output}, | ||
| 1378 | time_settings => $parameters->{time_settings}, | ||
| 1379 | et => $parameters->{et}, | ||
| 1380 | message_parameters => $parameters->{et} | ||
| 1381 | } | ||
| 1382 | ); | ||
| 1383 | } | ||
| 1384 | else { | ||
| 1385 | my $msg = "Database \"" . $type . "\" is not yet supported"; | ||
| 1386 | $self->{LOGGER}->error($msg); | ||
| 1387 | getResultCodeData( $parameters->{output}, "data." . genuid(), $parameters->{id}, $msg, 1 ); | ||
| 1388 | } | ||
| 1389 | return; | ||
| 1390 | } | ||
| 1391 | |||
| 1392 | =head2 retrieveSQL($self, $d, $mid, $output, $et, $message_parameters) | ||
| 1393 | |||
| 1394 | Given some 'startup' knowledge such as the name of the database and any | ||
| 1395 | credentials to connect with it, we start a connection and query the database | ||
| 1396 | for given values. These values are prepared into XML response content and | ||
| 1397 | return in the response message. | ||
| 1398 | |||
| 1399 | =cut | ||
| 1400 | |||
| 1401 | sub retrieveSQL { | ||
| 1402 | my ( $self, @args ) = @_; | ||
| 1403 | my $parameters = validateParams( | ||
| 1404 | @args, | ||
| 1405 | { | ||
| 1406 | d => 1, | ||
| 1407 | mid => 1, | ||
| 1408 | time_settings => 1, | ||
| 1409 | output => 1, | ||
| 1410 | et => 1, | ||
| 1411 | message_parameters => 1 | ||
| 1412 | } | ||
| 1413 | ); | ||
| 1414 | |||
| 1415 | my $datumns = 0; | ||
| 1416 | my $timeType = q{}; | ||
| 1417 | |||
| 1418 | if ( defined $parameters->{message_parameters}->{"eventNameSpaceSynchronization"} | ||
| 1419 | and lc( $parameters->{message_parameters}->{"eventNameSpaceSynchronization"} ) eq "true" ) | ||
| 1420 | { | ||
| 1421 | $datumns = 1; | ||
| 1422 | } | ||
| 1423 | |||
| 1424 | if ( defined $parameters->{message_parameters}->{"timeType"} ) { | ||
| 1425 | if ( lc( $parameters->{message_parameters}->{"timeType"} ) eq "unix" ) { | ||
| 1426 | $timeType = "unix"; | ||
| 1427 | } | ||
| 1428 | elsif ( lc( $parameters->{message_parameters}->{"timeType"} ) eq "iso" ) { | ||
| 1429 | $timeType = "iso"; | ||
| 1430 | } | ||
| 1431 | } | ||
| 1432 | |||
| 1433 | $self->{LOGGER}->error("No data element") if ( not defined $parameters->{d} ); | ||
| 1434 | |||
| 1435 | my $file = extract( find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"file\"]", 1 ), 1 ); | ||
| 1436 | my $table = extract( find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"table\"]", 1 ), 1 ); | ||
| 1437 | my $dbuser = extract( find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"user\"]", 1 ), 1 ); | ||
| 1438 | my $dbpass = extract( find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"pass\"]", 1 ), 1 ); | ||
| 1439 | |||
| 1440 | unless ( $file and $table ) { | ||
| 1441 | $self->{LOGGER}->error( "Data element " . $parameters->{d}->getAttribute("id") . " is missing some SQL elements" ); | ||
| 1442 | throw perfSONAR_PS::Error_compat( "error.ma.storage", "Unable to open associated database" ); | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | print "\n\nFILE: ", $file, "\n\n"; | ||
| 1446 | |||
| 1447 | my $backup = $file; | ||
| 1448 | if ( $backup =~ m/^DBI:SQLite:dbname=/mx ) { | ||
| 1449 | $backup =~ s/^DBI:SQLite:dbname=//mx; | ||
| 1450 | } | ||
| 1451 | if ( $self->{DIRECTORY} ) { | ||
| 1452 | if ( !( $backup =~ "^/" ) ) { | ||
| 1453 | $backup = $self->{DIRECTORY} . "/" . $backup; | ||
| 1454 | } | ||
| 1455 | } | ||
| 1456 | $file = "dbi:SQLite:dbname=" . $backup; | ||
| 1457 | |||
| 1458 | print "\n\nFILE: ", $file, "\n\n"; | ||
| 1459 | |||
| 1460 | my $query = {}; | ||
| 1461 | if ( $parameters->{time_settings}->{"START"}->{"internal"} or $parameters->{time_settings}->{"END"}->{"internal"} ) { | ||
| 1462 | $query = "select * from " . $table . " where id=\"" . $parameters->{d}->getAttribute("metadataIdRef") . "\" and"; | ||
| 1463 | my $queryCount = 0; | ||
| 1464 | if ( $parameters->{time_settings}->{"START"}->{"internal"} ) { | ||
| 1465 | $query = $query . " time > " . $parameters->{time_settings}->{"START"}->{"internal"}; | ||
| 1466 | $queryCount++; | ||
| 1467 | } | ||
| 1468 | if ( $parameters->{time_settings}->{"END"}->{"internal"} ) { | ||
| 1469 | if ($queryCount) { | ||
| 1470 | $query = $query . " and time < " . $parameters->{time_settings}->{"END"}->{"internal"} . ";"; | ||
| 1471 | } | ||
| 1472 | else { | ||
| 1473 | $query = $query . " time < " . $parameters->{time_settings}->{"END"}->{"internal"} . ";"; | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | } | ||
| 1477 | else { | ||
| 1478 | $query = "select * from " . $table . " where id=\"" . $parameters->{d}->getAttribute("metadataIdRef") . "\";"; | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | my @dbSchema = ( "id", "time", "value", "eventtype", "misc" ); | ||
| 1482 | my $datadb = new perfSONAR_PS::DB::SQL( { name => $file, schema => \@dbSchema, user => $dbuser, pass => $dbpass } ); | ||
| 1483 | |||
| 1484 | $datadb->openDB; | ||
| 1485 | my $result = $datadb->query( { query => $query } ); | ||
| 1486 | $datadb->closeDB; | ||
| 1487 | |||
| 1488 | my $id = "data." . genuid(); | ||
| 1489 | if ( $#{$result} == -1 ) { | ||
| 1490 | my $msg = "Query returned 0 results"; | ||
| 1491 | $self->{LOGGER}->error($msg); | ||
| 1492 | getResultCodeData( $parameters->{output}, $id, $parameters->{mid}, $msg, 1 ); | ||
| 1493 | } | ||
| 1494 | else { | ||
| 1495 | my $prefix = "nmwg"; | ||
| 1496 | my $uri = "http://ggf.org/ns/nmwg/base/2.0/"; | ||
| 1497 | |||
| 1498 | if ($datumns) { | ||
| 1499 | if ( defined $parameters->{et} and $parameters->{et} ne q{} ) { | ||
| 1500 | foreach my $e ( sort keys %{ $parameters->{et} } ) { | ||
| 1501 | next if $e eq "http://ggf.org/ns/nmwg/tools/snmp/2.0/"; | ||
| 1502 | $uri = $e; | ||
| 1503 | } | ||
| 1504 | } | ||
| 1505 | if ( $uri ne "http://ggf.org/ns/nmwg/base/2.0" ) { | ||
| 1506 | foreach my $r ( sort keys %{ $self->{NAMESPACES} } ) { | ||
| 1507 | if ( ( $uri . "/" ) eq $self->{NAMESPACES}->{$r} ) { | ||
| 1508 | $prefix = $r; | ||
| 1509 | last; | ||
| 1510 | } | ||
| 1511 | } | ||
| 1512 | if ( !$prefix ) { | ||
| 1513 | $prefix = "nmwg"; | ||
| 1514 | $uri = "http://ggf.org/ns/nmwg/base/2.0/"; | ||
| 1515 | } | ||
| 1516 | } | ||
| 1517 | } | ||
| 1518 | |||
| 1519 | startData( $parameters->{output}, $id, $parameters->{mid}, undef ); | ||
| 1520 | my $len = $#{$result}; | ||
| 1521 | for my $a ( 0 .. $len ) { | ||
| 1522 | my %attrs = (); | ||
| 1523 | if ( $timeType eq "iso" ) { | ||
| 1524 | my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime( $result->[$a][1] ); | ||
| 1525 | $attrs{"timeType"} = "ISO"; | ||
| 1526 | $attrs{ $dbSchema[1] . "Value" } = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ\n", ( $year + 1900 ), ( $mon + 1 ), $mday, $hour, $min, $sec; | ||
| 1527 | } | ||
| 1528 | else { | ||
| 1529 | $attrs{"timeType"} = "unix"; | ||
| 1530 | $attrs{ $dbSchema[1] . "Value" } = $result->[$a][1]; | ||
| 1531 | } | ||
| 1532 | $attrs{ $dbSchema[2] } = $result->[$a][2]; | ||
| 1533 | |||
| 1534 | my @misc = split( /,/xm, $result->[$a][4] ); | ||
| 1535 | foreach my $m (@misc) { | ||
| 1536 | my @pair = split( /=/xm, $m ); | ||
| 1537 | $attrs{ $pair[0] } = $pair[1]; | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | $parameters->{output}->createElement( | ||
| 1541 | prefix => $prefix, | ||
| 1542 | namespace => $uri, | ||
| 1543 | tag => "datum", | ||
| 1544 | attributes => \%attrs | ||
| 1545 | ); | ||
| 1546 | } | ||
| 1547 | endData( $parameters->{output} ); | ||
| 1548 | } | ||
| 1549 | return; | ||
| 1550 | } | ||
| 1551 | |||
| 1552 | =head2 retrieveRRD($self, $d, $mid, $output, $et, $message_parameters) | ||
| 1553 | |||
| 1554 | Given some 'startup' knowledge such as the name of the database and any | ||
| 1555 | credentials to connect with it, we start a connection and query the database | ||
| 1556 | for given values. These values are prepared into XML response content and | ||
| 1557 | return in the response message. | ||
| 1558 | |||
| 1559 | =cut | ||
| 1560 | |||
| 1561 | sub retrieveRRD { | ||
| 1562 | my ( $self, @args ) = @_; | ||
| 1563 | my $parameters = validateParams( | ||
| 1564 | @args, | ||
| 1565 | { | ||
| 1566 | d => 1, | ||
| 1567 | mid => 1, | ||
| 1568 | time_settings => 1, | ||
| 1569 | output => 1, | ||
| 1570 | et => 1, | ||
| 1571 | message_parameters => 1 | ||
| 1572 | } | ||
| 1573 | ); | ||
| 1574 | |||
| 1575 | my $timeSettings = $parameters->{time_settings}; | ||
| 1576 | |||
| 1577 | my ( $sec, $frac ) = Time::HiRes::gettimeofday; | ||
| 1578 | my $datumns = 0; | ||
| 1579 | my $timeType = q{}; | ||
| 1580 | |||
| 1581 | if ( defined $parameters->{message_parameters}->{"eventNameSpaceSynchronization"} | ||
| 1582 | and lc( $parameters->{message_parameters}->{"eventNameSpaceSynchronization"} ) eq "true" ) | ||
| 1583 | { | ||
| 1584 | $datumns = 1; | ||
| 1585 | } | ||
| 1586 | |||
| 1587 | if ( defined $parameters->{message_parameters}->{"timeType"} ) { | ||
| 1588 | if ( lc( $parameters->{message_parameters}->{"timeType"} ) eq "unix" ) { | ||
| 1589 | $timeType = "unix"; | ||
| 1590 | } | ||
| 1591 | elsif ( lc( $parameters->{message_parameters}->{"timeType"} ) eq "iso" ) { | ||
| 1592 | $timeType = "iso"; | ||
| 1593 | } | ||
| 1594 | } | ||
| 1595 | |||
| 1596 | my $file_element = find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"file\"]", 1 ); | ||
| 1597 | |||
| 1598 | my $rrd_file = extract( $file_element, 1 ); | ||
| 1599 | |||
| 1600 | if ( not $rrd_file ) { | ||
| 1601 | $self->{LOGGER}->error( "Data element " . $parameters->{d}->getAttribute("id") . " is missing some RRD file" ); | ||
| 1602 | throw perfSONAR_PS::Error_compat( "error.ma.storage", "Unable to open associated RRD file" ); | ||
| 1603 | } | ||
| 1604 | |||
| 1605 | adjustRRDTime( { timeSettings => $timeSettings } ); | ||
| 1606 | my $id = "data." . genuid(); | ||
| 1607 | my %rrd_result = getDataRRD( { directory => $self->{DIRECTORY}, file => $rrd_file, timeSettings => $timeSettings, rrdtool => $self->{CONF}->{"snmp"}->{"rrdtool"} } ); | ||
| 1608 | if ( $rrd_result{ERROR} ) { | ||
| 1609 | $self->{LOGGER}->error( "RRD error seen: " . $rrd_result{ERROR} ); | ||
| 1610 | getResultCodeData( $parameters->{output}, $id, $parameters->{mid}, $rrd_result{ERROR}, 1 ); | ||
| 1611 | } | ||
| 1612 | else { | ||
| 1613 | my $prefix = "nmwg"; | ||
| 1614 | my $uri = "http://ggf.org/ns/nmwg/base/2.0/"; | ||
| 1615 | if ($datumns) { | ||
| 1616 | if ( defined $parameters->{et} and $parameters->{et} ne q{} ) { | ||
| 1617 | foreach my $e ( sort keys %{ $parameters->{et} } ) { | ||
| 1618 | next if $e eq "http://ggf.org/ns/nmwg/tools/snmp/2.0/"; | ||
| 1619 | $uri = $e; | ||
| 1620 | } | ||
| 1621 | } | ||
| 1622 | if ( $uri ne "http://ggf.org/ns/nmwg/base/2.0" ) { | ||
| 1623 | foreach my $r ( sort keys %{ $self->{NAMESPACES} } ) { | ||
| 1624 | if ( ( $uri . "/" ) eq $self->{NAMESPACES}->{$r} ) { | ||
| 1625 | $prefix = $r; | ||
| 1626 | last; | ||
| 1627 | } | ||
| 1628 | } | ||
| 1629 | if ( !$prefix ) { | ||
| 1630 | $prefix = "nmwg"; | ||
| 1631 | $uri = "http://ggf.org/ns/nmwg/base/2.0/"; | ||
| 1632 | } | ||
| 1633 | } | ||
| 1634 | } | ||
| 1635 | |||
| 1636 | my $dataSource = extract( find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"dataSource\"]", 1 ), 0 ); | ||
| 1637 | my $valueUnits = extract( find( $parameters->{d}, "./nmwg:key//nmwg:parameter[\@name=\"valueUnits\"]", 1 ), 0 ); | ||
| 1638 | |||
| 1639 | startData( $parameters->{output}, $id, $parameters->{mid}, undef ); | ||
| 1640 | foreach my $a ( sort( keys(%rrd_result) ) ) { | ||
| 1641 | if ( $a < $sec ) { | ||
| 1642 | my %attrs = (); | ||
| 1643 | if ( $timeType eq "iso" ) { | ||
| 1644 | my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime($a); | ||
| 1645 | $attrs{"timeType"} = "ISO"; | ||
| 1646 | $attrs{"timeValue"} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ\n", ( $year + 1900 ), ( $mon + 1 ), $mday, $hour, $min, $sec; | ||
| 1647 | } | ||
| 1648 | else { | ||
| 1649 | $attrs{"timeType"} = "unix"; | ||
| 1650 | $attrs{"timeValue"} = $a; | ||
| 1651 | } | ||
| 1652 | $attrs{"value"} = $rrd_result{$a}{$dataSource}; | ||
| 1653 | $attrs{"valueUnits"} = $valueUnits; | ||
| 1654 | |||
| 1655 | $parameters->{output}->createElement( | ||
| 1656 | prefix => $prefix, | ||
| 1657 | namespace => $uri, | ||
| 1658 | tag => "datum", | ||
| 1659 | attributes => \%attrs | ||
| 1660 | ); | ||
| 1661 | } | ||
| 1662 | } | ||
| 1663 | endData( $parameters->{output} ); | ||
| 1664 | } | ||
| 1665 | return; | ||
| 1666 | } | ||
| 1667 | |||
| 1668 | =head2 addSelectParameters($self, { parameter_block, filters }) | ||
| 1669 | |||
| 1670 | Re-construct the parameters block. | ||
| 1671 | |||
| 1672 | =cut | ||
| 1673 | |||
| 1674 | sub addSelectParameters { | ||
| 1675 | my ( $self, @args ) = @_; | ||
| 1676 | my $parameters = validateParams( | ||
| 1677 | @args, | ||
| 1678 | { | ||
| 1679 | parameter_block => 1, | ||
| 1680 | filters => 1, | ||
| 1681 | } | ||
| 1682 | ); | ||
| 1683 | |||
| 1684 | my $params = $parameters->{parameter_block}; | ||
| 1685 | my @filters = @{ $parameters->{filters} }; | ||
| 1686 | my %paramsByName = (); | ||
| 1687 | |||
| 1688 | foreach my $p ( $params->childNodes ) { | ||
| 1689 | if ( $p->localname and $p->localname eq "parameter" and $p->getAttribute("name") ) { | ||
| 1690 | $paramsByName{ $p->getAttribute("name") } = $p; | ||
| 1691 | } | ||
| 1692 | } | ||
| 1693 | |||
| 1694 | foreach my $filter_arr (@filters) { | ||
| 1695 | my @filters = @{$filter_arr}; | ||
| 1696 | my $filter = $filters[-1]; | ||
| 1697 | |||
| 1698 | $self->{LOGGER}->debug( "Filter: " . $filter->toString ); | ||
| 1699 | |||
| 1700 | my $select_params = find( $filter, "./select:parameters", 1 ); | ||
| 1701 | if ($select_params) { | ||
| 1702 | foreach my $p ( $select_params->childNodes ) { | ||
| 1703 | if ( $p->localname and $p->localname eq "parameter" and $p->getAttribute("name") ) { | ||
| 1704 | my $newChild = $p->cloneNode(1); | ||
| 1705 | if ( $paramsByName{ $p->getAttribute("name") } ) { | ||
| 1706 | $params->replaceChild( $newChild, $paramsByName{ $p->getAttribute("name") } ); | ||
| 1707 | } | ||
| 1708 | else { | ||
| 1709 | $params->addChild($newChild); | ||
| 1710 | } | ||
| 1711 | $paramsByName{ $p->getAttribute("name") } = $newChild; | ||
| 1712 | } | ||
| 1713 | } | ||
| 1714 | } | ||
| 1715 | } | ||
| 1716 | return; | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | 1; | ||
| 1720 | |||
| 1721 | __END__ |