| 1 | package perfSONAR_PS::Services::MA::Topology; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | perfSONAR_PS::Services::MA::Topology - A module that provides methods for a | ||
| 6 | Topology Service. The Topology Service can be used to make Topology Data | ||
| 7 | available to individuals via webservice interface. | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This module, in conjunction with other parts of the perfSONAR-PS framework, | ||
| 12 | handles specific messages from interested actors in search of Topology data. | ||
| 13 | |||
| 14 | There are two major message types that this service can act upon: | ||
| 15 | QueryRequest/SetupDataRequest - Allows queries to the database | ||
| 16 | TopologyChangeRequest - Allows updates to the topology database | ||
| 17 | =head1 API | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use base 'perfSONAR_PS::Services::Base'; | ||
| 22 | |||
| 23 | use fields 'CLIENT', 'LS_CLIENT', 'LOGGER'; | ||
| 24 | |||
| 25 | use warnings; | ||
| 26 | use strict; | ||
| 27 | use Log::Log4perl qw(get_logger); | ||
| 28 | use Params::Validate qw(:all); | ||
| 29 | |||
| 30 | use perfSONAR_PS::Common; | ||
| 31 | use perfSONAR_PS::Messages; | ||
| 32 | use perfSONAR_PS::Topology::Common; | ||
| 33 | use perfSONAR_PS::Client::Topology::XMLDB; | ||
| 34 | use perfSONAR_PS::Client::LS::Remote; | ||
| 35 | use perfSONAR_PS::ParameterValidation; | ||
| 36 | |||
| 37 | our $VERSION = 0.09; | ||
| 38 | |||
| 39 | =head2 init | ||
| 40 | Called at startup by the daemon when this particular module is loaded into | ||
| 41 | the perfSONAR-PS deployment. Checks the configuration file for the necessary | ||
| 42 | items and fills in others when needed. Initializes the backed metadata storage | ||
| 43 | (Oracle Sleepycat XML Database). Finally the message handler registers the | ||
| 44 | appropriate message types and eventTypes for this module. Any other | ||
| 45 | 'pre-startup' tasks should be placed in this function. | ||
| 46 | =cut | ||
| 47 | sub init { | ||
| 48 | my ($self, $handler) = @_; | ||
| 49 | |||
| 50 | $self->{LOGGER} = get_logger("perfSONAR_PS::Services::MA::Topology"); | ||
| 51 | |||
| 52 | if (not defined $self->{CONF}->{"topology"}->{"db_type"} or $self->{CONF}->{"topology"}->{"db_type"} eq q{}) { | ||
| 53 | $self->{LOGGER}->error("No database type specified"); | ||
| 54 | return -1; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (lc($self->{CONF}->{"topology"}->{"db_type"}) eq "xml") { | ||
| 58 | if (not defined $self->{CONF}->{"topology"}->{"db_file"} or $self->{CONF}->{"topology"}->{"db_file"} eq q{}) { | ||
| 59 | $self->{LOGGER}->error("You specified a Sleepycat XML DB Database, but then did not specify a database file (db_file)"); | ||
| 60 | return -1; | ||
| 61 | } | ||
| 62 | |||
| 63 | if (not defined $self->{CONF}->{"topology"}->{"db_environment"} or $self->{CONF}->{"topology"}->{"db_environment"} eq q{}) { | ||
| 64 | $self->{LOGGER}->error("You specified a Sleepycat XML DB Database, but then did not specify a database name (db_environment)"); | ||
| 65 | return -1; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $environment = $self->{CONF}->{"topology"}->{"db_environment"}; | ||
| 69 | if (defined $self->{DIRECTORY}) { | ||
| 70 | if (!($environment =~ "^/")) { | ||
| 71 | $environment = $self->{DIRECTORY}."/".$environment; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | my $read_only = 0; | ||
| 76 | |||
| 77 | if (defined $self->{CONF}->{"topology"}->{"read_only"} and $self->{CONF}->{"topology"}->{"read_only"} == 1) { | ||
| 78 | $read_only = 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | my $file = $self->{CONF}->{"topology"}->{"db_file"}; | ||
| 82 | my %ns = getTopologyNamespaces(); | ||
| 83 | |||
| 84 | $self->{CLIENT}= new perfSONAR_PS::Client::Topology::XMLDB($environment, $file, \%ns, $read_only); | ||
| 85 | } else { | ||
| 86 | $self->{LOGGER}->error("Invalid database type specified"); | ||
| 87 | return -1; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($self->{CONF}->{"topology"}->{"enable_registration"}) { | ||
| 91 | if (not defined $self->{CONF}->{"topology"}->{"service_accesspoint"} or $self->{CONF}->{"topology"}->{"service_accesspoint"} eq q{}) { | ||
| 92 | $self->{LOGGER}->error("No access point specified for SNMP service"); | ||
| 93 | return -1; | ||
| 94 | } | ||
| 95 | |||
| 96 | if (not defined $self->{CONF}->{"topology"}->{"ls_instance"} or $self->{CONF}->{"topology"}->{"ls_instance"} eq q{}) { | ||
| 97 | if (defined $self->{CONF}->{"ls_instance"} and $self->{CONF}->{"ls_instance"} ne q{}) { | ||
| 98 | $self->{CONF}->{"topology"}->{"ls_instance"} = $self->{CONF}->{"ls_instance"}; | ||
| 99 | } else { | ||
| 100 | $self->{LOGGER}->error("No LS instance specified for SNMP service"); | ||
| 101 | return -1; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | if (not defined $self->{CONF}->{"topology"}->{"ls_registration_interval"} or $self->{CONF}->{"topology"}->{"ls_registration_interval"} eq q{}) { | ||
| 106 | if (defined $self->{CONF}->{"ls_registration_interval"} and $self->{CONF}->{"ls_registration_interval"} ne q{}) { | ||
| 107 | $self->{CONF}->{"topology"}->{"ls_registration_interval"} = $self->{CONF}->{"ls_registration_interval"}; | ||
| 108 | } else { | ||
| 109 | $self->{LOGGER}->warn("Setting registration interval to 30 minutes"); | ||
| 110 | $self->{CONF}->{"topology"}->{"ls_registration_interval"} = 1800; | ||
| 111 | } | ||
| 112 | } else { | ||
| 113 | # turn the registration interval from minutes to seconds | ||
| 114 | $self->{CONF}->{"topology"}->{"ls_registration_interval"} *= 60; | ||
| 115 | } | ||
| 116 | |||
| 117 | if(not defined $self->{CONF}->{"topology"}->{"service_description"} or | ||
| 118 | $self->{CONF}->{"topology"}->{"service_description"} eq q{}) { | ||
| 119 | $self->{CONF}->{"topology"}->{"service_description"} = "perfSONAR_PS Topology MA"; | ||
| 120 | $self->{LOGGER}->warn("Setting 'service_description' to 'perfSONAR_PS Topology MA'."); | ||
| 121 | } | ||
| 122 | |||
| 123 | if(not defined $self->{CONF}->{"topology"}->{"service_name"} or | ||
| 124 | $self->{CONF}->{"topology"}->{"service_name"} eq q{}) { | ||
| 125 | $self->{CONF}->{"topology"}->{"service_name"} = "Topology MA"; | ||
| 126 | $self->{LOGGER}->warn("Setting 'service_name' to 'Topology MA'."); | ||
| 127 | } | ||
| 128 | |||
| 129 | if(not defined $self->{CONF}->{"topology"}->{"service_type"} or | ||
| 130 | $self->{CONF}->{"topology"}->{"service_type"} eq q{}) { | ||
| 131 | $self->{CONF}->{"topology"}->{"service_type"} = "MA"; | ||
| 132 | $self->{LOGGER}->warn("Setting 'service_type' to 'MA'."); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | $handler->registerEventHandler("QueryRequest", "http://ggf.org/ns/nmwg/topology/20070809", $self); | ||
| 137 | # $handler->registerEventHandler("KeepaliveRequest", "http://ggf.org/ns/nmwg/topology/20070809", $self); | ||
| 138 | # $handler->registerEventHandler("AddRequest", "http://ggf.org/ns/nmwg/topology/20070809", $self); | ||
| 139 | # $handler->registerEventHandler("UpdateRequest", "http://ggf.org/ns/nmwg/topology/20070809", $self); | ||
| 140 | # $handler->registerEventHandler("RemoveRequest", "http://ggf.org/ns/nmwg/topology/20070809", $self); | ||
| 141 | |||
| 142 | $handler->registerEventHandler("SetupDataRequest", "http://ggf.org/ns/nmwg/topology/query/xquery/20070809", $self); | ||
| 143 | $handler->registerEventHandler("SetupDataRequest", "http://ggf.org/ns/nmwg/topology/query/all/20070809", $self); | ||
| 144 | $handler->registerEventHandler("TopologyChangeRequest", "http://ggf.org/ns/nmwg/topology/change/add/20070809", $self); | ||
| 145 | $handler->registerEventHandler("TopologyChangeRequest", "http://ggf.org/ns/nmwg/topology/change/update/20070809", $self); | ||
| 146 | $handler->registerEventHandler("TopologyChangeRequest", "http://ggf.org/ns/nmwg/topology/change/replace/20070809", $self); | ||
| 147 | |||
| 148 | return 0; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub needLS { | ||
| 152 | my ($self) = @_; | ||
| 153 | |||
| 154 | return ($self->{CONF}->{"topology"}->{"enable_registration"}); | ||
| 155 | } | ||
| 156 | |||
| 157 | =head2 registerLS($self $sleep_time) | ||
| 158 | Given the service information (specified in configuration) and the contents | ||
| 159 | of our xmldb backend, we can contact the specified LS and register the | ||
| 160 | top-level identifiers (a summarized form of the elements in the database). | ||
| 161 | =cut | ||
| 162 | sub registerLS { | ||
| 163 | my ($self, $sleep_time) = @_; | ||
| 164 | my ($status, $res1); | ||
| 165 | |||
| 166 | if (not defined $self->{LS_CLIENT}) { | ||
| 167 | my %ls_conf = ( | ||
| 168 | LS_INSTANCE => $self->{CONF}->{"topology"}->{"ls_instance"}, | ||
| 169 | SERVICE_TYPE => $self->{CONF}->{"topology"}->{"service_type"}, | ||
| 170 | SERVICE_NAME => $self->{CONF}->{"topology"}->{"service_name"}, | ||
| 171 | SERVICE_DESCRIPTION => $self->{CONF}->{"topology"}->{"service_description"}, | ||
| 172 | SERVICE_ACCESSPOINT => $self->{CONF}->{"topology"}->{"service_accesspoint"}, | ||
| 173 | LS_REGISTRATION_INTERVAL => $self->{CONF}->{"topology"}->{"registration_interval"}, | ||
| 174 | ); | ||
| 175 | |||
| 176 | my %ns = getTopologyNamespaces(); | ||
| 177 | $self->{LS_CLIENT} = new perfSONAR_PS::Client::LS::Remote($self->{CONF}->{"topology"}->{"ls_instance"}, \%ls_conf, \%ns); | ||
| 178 | } | ||
| 179 | |||
| 180 | ($status, $res1) = $self->{CLIENT}->open; | ||
| 181 | if ($status != 0) { | ||
| 182 | my $msg = "Couldn't open from database: $res1"; | ||
| 183 | $self->{LOGGER}->error($msg); | ||
| 184 | return -1; | ||
| 185 | } | ||
| 186 | |||
| 187 | ($status, $res1) = $self->{CLIENT}->getUniqueIDs; | ||
| 188 | if ($status != 0) { | ||
| 189 | my $msg = "Couldn't get link nformation from database: $res1"; | ||
| 190 | $self->{LOGGER}->error($msg); | ||
| 191 | return -1; | ||
| 192 | } | ||
| 193 | |||
| 194 | my @mds = (); | ||
| 195 | my @md_ids = (); | ||
| 196 | |||
| 197 | foreach my $info (@{ $res1 }) { | ||
| 198 | my ($md, $md_id) = buildLSMetadata($info->{id}, $info->{type}, $info->{prefix}, $info->{uri}); | ||
| 199 | push @mds, $md; | ||
| 200 | } | ||
| 201 | |||
| 202 | $res1 = q{}; | ||
| 203 | |||
| 204 | my $n = $self->{LS_CLIENT}->registerDynamic(\@mds); | ||
| 205 | |||
| 206 | if (defined $sleep_time) { | ||
| 207 | ${$sleep_time} = $self->{CONF}->{"topology"}->{"ls_registration_interval"}; | ||
| 208 | } | ||
| 209 | |||
| 210 | return $n; | ||
| 211 | } | ||
| 212 | |||
| 213 | =head2 buildLSMetadata ($id, $type, $prefix, $url) | ||
| 214 | This function is used to build the metadata that is registered with the LS. | ||
| 215 | This element contains the prefix and the local name of the element to be | ||
| 216 | register as well as the id for that element. | ||
| 217 | =cut | ||
| 218 | sub buildLSMetadata { | ||
| 219 | my ($id, $type, $prefix, $uri) = @_; | ||
| 220 | my $md = q{}; | ||
| 221 | my $md_id = "meta".genuid(); | ||
| 222 | |||
| 223 | $md .= "<nmwg:metadata id=\"$md_id\">\n"; | ||
| 224 | $md .= "<nmwg:subject id=\"sub0\">\n"; | ||
| 225 | if (not defined $prefix or $prefix eq q{}) { | ||
| 226 | $md .= " <$type xmlns=\"$uri\" id=\"$id\" />\n"; | ||
| 227 | } else { | ||
| 228 | $md .= " <$prefix:$type xmlns:$prefix=\"$uri\" id=\"$id\" />\n"; | ||
| 229 | } | ||
| 230 | $md .= "</nmwg:subject>\n"; | ||
| 231 | $md .= "<nmwg:eventType>topology</nmwg:eventType>\n"; | ||
| 232 | $md .= "<nmwg:eventType>http://ggf.org/ns/nmwg/topology/query/all/20070809</nmwg:eventType>\n"; | ||
| 233 | $md .= "<nmwg:eventType>http://ggf.org/ns/nmwg/topology/query/xquery/20070809</nmwg:eventType>\n"; | ||
| 234 | $md .= "<nmwg:eventType>http://ggf.org/ns/nmwg/topology/change/add/20070809</nmwg:eventType>\n"; | ||
| 235 | $md .= "<nmwg:eventType>http://ggf.org/ns/nmwg/topology/change/update/20070809</nmwg:eventType>\n"; | ||
| 236 | $md .= "<nmwg:eventType>http://ggf.org/ns/nmwg/topology/change/replace/20070809</nmwg:eventType>\n"; | ||
| 237 | $md .= "</nmwg:metadata>\n"; | ||
| 238 | |||
| 239 | return ($md, $md_id); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 handleEvent | ||
| 243 | This is the function that is called by the daemon whenever a metadata/data | ||
| 244 | pair with one of our eventTypes is found in a message we've regeistered in. | ||
| 245 | =cut | ||
| 246 | sub handleEvent { | ||
| 247 | my ($self, @args) = @_; | ||
| 248 | my $parameters = validateParams(@args, | ||
| 249 | { | ||
| 250 | output => 1, | ||
| 251 | messageId => 1, | ||
| 252 | messageType => 1, | ||
| 253 | messageParameters => 1, | ||
| 254 | eventType => 1, | ||
| 255 | subject => 1, | ||
| 256 | filterChain => 1, | ||
| 257 | data => 1, | ||
| 258 | rawRequest => 1, | ||
| 259 | doOutputMetadata => 1, | ||
| 260 | }); | ||
| 261 | |||
| 262 | my $output = $parameters->{"output"}; | ||
| 263 | my $messageId = $parameters->{"messageId"}; | ||
| 264 | my $messageType = $parameters->{"messageType"}; | ||
| 265 | my $message_parameters = $parameters->{"messageParameters"}; | ||
| 266 | my $eventType = $parameters->{"eventType"}; | ||
| 267 | my $d = $parameters->{"data"}; | ||
| 268 | my $raw_request = $parameters->{"rawRequest"}; | ||
| 269 | my @subjects = @{ $parameters->{"subject"} }; | ||
| 270 | my $md = $subjects[0]; | ||
| 271 | |||
| 272 | my @filters = @{ $parameters->{"filterChain"} }; | ||
| 273 | if ($#filters > -1) { | ||
| 274 | throw perfSONAR_PS::Error_compat("error.ma.select", "Topology Service does not yet support filtering"); | ||
| 275 | } | ||
| 276 | |||
| 277 | my ($status, $res) = $self->{CLIENT}->open; | ||
| 278 | if ($status != 0) { | ||
| 279 | my $msg = "Couldn't open database"; | ||
| 280 | $self->{LOGGER}->error("Error changing topology: $res"); | ||
| 281 | throw perfSONAR_PS::Error_compat("error.topology.ma", $msg); | ||
| 282 | } | ||
| 283 | |||
| 284 | if ($messageType eq "SetupDataRequest") { | ||
| 285 | $self->handleSetupDataRequest($output, $eventType, $md, $d); | ||
| 286 | } elsif ($messageType eq "TopologyChangeRequest") { | ||
| 287 | $self->handleChangeTopologyRequest($output, $eventType, $md, $d); | ||
| 288 | } elsif ($messageType eq "QueryRequest") { | ||
| 289 | $self->handleQueryRequest($output, $eventType, $md, $d); | ||
| 290 | # } elsif ($messageType eq "AddRequest") { | ||
| 291 | # $self->handleChangeTopologyRequest($output, "add", $m, $d); | ||
| 292 | # } elsif ($messageType eq "UpdateRequest") { | ||
| 293 | # $self->handleChangeTopologyRequest($output, "update", $m, $d); | ||
| 294 | # } elsif ($messageType eq "RemoveRequest") { | ||
| 295 | # $self->handleChangeTopologyRequest($output, "remove", $m, $d); | ||
| 296 | } | ||
| 297 | |||
| 298 | return; | ||
| 299 | } | ||
| 300 | |||
| 301 | =head2 handleQueryRequest ($self, $output, $eventType, $m, $d) | ||
| 302 | This function handles the (hopefully to be standardized) QueryRequest | ||
| 303 | message. The semantics of the message are as follows: | ||
| 304 | |||
| 305 | If no subject is included, the semantics are "give me the whole database", | ||
| 306 | and a XQuery for "//*" (i.e. everything) is submitted to the backend database. | ||
| 307 | |||
| 308 | If an xquery subject is included, the semantics are "use the included | ||
| 309 | XQuery to query the database". It simply passes the included XQuery to the | ||
| 310 | backend database. | ||
| 311 | |||
| 312 | If any other subject is included, an invalid subject error is thrown. | ||
| 313 | =cut | ||
| 314 | sub handleQueryRequest { | ||
| 315 | my ($self, $output, $eventType, $m, $d) = @_; | ||
| 316 | |||
| 317 | my $subjects = find($m, "./*[local-name()='subject']", 0); | ||
| 318 | if ($subjects->size() > 1) { | ||
| 319 | throw perfSONAR_PS::Error_compat("error.ma.subject", "Multiple subjects specified"); | ||
| 320 | } | ||
| 321 | |||
| 322 | my $xquery; | ||
| 323 | |||
| 324 | if ($subjects->size() == 0) { | ||
| 325 | # no subject is the query all request | ||
| 326 | $xquery = "//*"; | ||
| 327 | } else{ | ||
| 328 | my $subject = $subjects->get_node(1); | ||
| 329 | if ($subject->namespaceURI() eq "http://ggf.org/ns/nmwg/tools/org/perfsonar/xquery/1.0/") { | ||
| 330 | $xquery = $subject->textContent; | ||
| 331 | } else { | ||
| 332 | throw perfSONAR_PS::Error_compat("error.ma.subject", "Invalid subject type: ".$subject->namespaceURI()); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | my ($status, $res) = $self->{CLIENT}->xQuery($xquery); | ||
| 337 | if ($status != 0) { | ||
| 338 | my $msg = "Database query failed: $res"; | ||
| 339 | $self->{LOGGER}->error($msg); | ||
| 340 | throw perfSONAR_PS::Error_compat("error.common.storage.query", $msg); | ||
| 341 | } | ||
| 342 | |||
| 343 | createData($output, "data.".genuid(), $m->getAttribute("id"), $res, undef); | ||
| 344 | |||
| 345 | return; | ||
| 346 | } | ||
| 347 | |||
| 348 | =head2 handleSetupDataRequest ($self, $output, $eventType, $m, $d) | ||
| 349 | This function handles the older protocol's query mechanism. | ||
| 350 | The semantics of the message are as follows: | ||
| 351 | |||
| 352 | If an "http://ggf.org/ns/nmwg/topology/query/all/20070809" eventType is | ||
| 353 | included in the metadata, the entire backend database is returned. | ||
| 354 | |||
| 355 | If an "http://ggf.org/ns/nmwg/topology/query/xquery/20070809" eventType is | ||
| 356 | included in the metadata, an xquery subject must be included as well. The | ||
| 357 | xquery inside the xquery subject will be passed to the backend database. | ||
| 358 | =cut | ||
| 359 | sub handleSetupDataRequest { | ||
| 360 | my ($self, $output, $eventType, $m, $d) = @_; | ||
| 361 | my ($status, $res); | ||
| 362 | my $dataContent; | ||
| 363 | |||
| 364 | if ($eventType eq "http://ggf.org/ns/nmwg/topology/query/all/20070809") { | ||
| 365 | ($status, $res) = $self->{CLIENT}->getAll; | ||
| 366 | if ($status != 0) { | ||
| 367 | my $msg = "Database dump failed: $res"; | ||
| 368 | $self->{LOGGER}->error($msg); | ||
| 369 | throw perfSONAR_PS::Error_compat("error.common.storage.fetch", $msg); | ||
| 370 | } | ||
| 371 | |||
| 372 | $dataContent = $res->toString; | ||
| 373 | } elsif ($eventType eq "http://ggf.org/ns/nmwg/topology/query/xquery/20070809") { | ||
| 374 | my $query = findvalue($m, "./xquery:subject"); | ||
| 375 | |||
| 376 | if (not defined $query or $query eq q{}) { | ||
| 377 | my $msg = "No query given in request"; | ||
| 378 | $self->{LOGGER}->error($msg); | ||
| 379 | throw perfSONAR_PS::Error_compat("error.topology.query.query_not_found", $msg); | ||
| 380 | } | ||
| 381 | |||
| 382 | my ($status, $res) = $self->{CLIENT}->xQuery($query); | ||
| 383 | if ($status != 0) { | ||
| 384 | my $msg = "Database query failed: $res"; | ||
| 385 | $self->{LOGGER}->error($msg); | ||
| 386 | throw perfSONAR_PS::Error_compat("error.common.storage.query", $msg); | ||
| 387 | } | ||
| 388 | |||
| 389 | $dataContent = $res; | ||
| 390 | } | ||
| 391 | |||
| 392 | createData($output, "data.".genuid(), $m->getAttribute("id"), $dataContent, undef); | ||
| 393 | |||
| 394 | return; | ||
| 395 | } | ||
| 396 | |||
| 397 | =head2 handleChangeTopologyRequest | ||
| 398 | The hope is to standardize the information changing protocol between the LS | ||
| 399 | and the Topology Service. In the interim, this handler handle metadata/data | ||
| 400 | pairs corresponding to the current TS protocol. | ||
| 401 | |||
| 402 | The metadata contains an eventType specifying how the data should modify | ||
| 403 | the backend database and the data contains a topology wrapper containing | ||
| 404 | the topology elements to add or update. | ||
| 405 | |||
| 406 | If the eventType is "http://ggf.org/ns/nmwg/topology/change/add/20070809", | ||
| 407 | the elements in the data segment are added to the database. If any of the | ||
| 408 | elements (based on identifiers), already exist in the database, an error is | ||
| 409 | returned. | ||
| 410 | |||
| 411 | If the eventType is | ||
| 412 | "http://ggf.org/ns/nmwg/topology/change/update/20070809", the elements in | ||
| 413 | the data segment are merged with the existing elements with the same | ||
| 414 | identifier in the database. If any element in the data segment does not | ||
| 415 | exist in the database, an error will be returned. | ||
| 416 | |||
| 417 | If the eventType is | ||
| 418 | "http://ggf.org/ns/nmwg/topology/change/replace/20070809", the elements in | ||
| 419 | the data segment are added to the database. If any of the elements already | ||
| 420 | exist in the database, they will be replaced with the element in the data | ||
| 421 | segment. | ||
| 422 | =cut | ||
| 423 | sub handleChangeTopologyRequest { | ||
| 424 | my ($self, $output, $eventType, $m, $d) = @_; | ||
| 425 | my ($status, $res); | ||
| 426 | my $changeType; | ||
| 427 | |||
| 428 | if ($eventType eq "http://ggf.org/ns/nmwg/topology/change/add/20070809") { | ||
| 429 | $changeType = "add"; | ||
| 430 | } elsif ($eventType eq "http://ggf.org/ns/nmwg/topology/change/update/20070809") { | ||
| 431 | $changeType = "update"; | ||
| 432 | } elsif ($eventType eq "http://ggf.org/ns/nmwg/topology/change/replace/20070809") { | ||
| 433 | $changeType = "replace"; | ||
| 434 | } | ||
| 435 | |||
| 436 | my $topology = find($d, "./*[local-name()='topology']", 1); | ||
| 437 | |||
| 438 | if (not defined $topology) { | ||
| 439 | my $msg = "No topology defined in change topology request for metadata: ".$m->getAttribute("id"); | ||
| 440 | $self->{LOGGER}->error("Error changing topology: $msg"); | ||
| 441 | throw perfSONAR_PS::Error_compat("error.topology.query.topology_not_found", $msg); | ||
| 442 | } | ||
| 443 | |||
| 444 | ($status, $res) = topologyNormalize($topology); | ||
| 445 | if ($status != 0) { | ||
| 446 | $self->{LOGGER}->error("Couldn't normalize topology"); | ||
| 447 | throw perfSONAR_PS::Error_compat("error.topology.invalid_topology", $res); | ||
| 448 | } | ||
| 449 | |||
| 450 | ($status, $res) = $self->{CLIENT}->changeTopology($changeType, $topology); | ||
| 451 | if ($status != 0) { | ||
| 452 | $self->{LOGGER}->error("Error handling topology request"); | ||
| 453 | throw perfSONAR_PS::Error_compat("error.topology.ma", $res); | ||
| 454 | } | ||
| 455 | |||
| 456 | my $changeDesc; | ||
| 457 | my $mdID = "metadata.".genuid(); | ||
| 458 | |||
| 459 | if ($changeType eq "add") { | ||
| 460 | $changeDesc = "added"; | ||
| 461 | } elsif ($changeType eq "replace") { | ||
| 462 | $changeDesc = "replaced"; | ||
| 463 | } elsif ($changeType eq "update") { | ||
| 464 | $changeDesc = "updated"; | ||
| 465 | } | ||
| 466 | |||
| 467 | getResultCodeMetadata($output, $mdID, $m->getAttribute("id"), "success.ma.".$changeDesc); | ||
| 468 | getResultCodeData($output, "data.".genuid(), $mdID, "data element(s) successfully $changeDesc", 1); | ||
| 469 | |||
| 470 | return; | ||
| 471 | } | ||
| 472 | |||
| 473 | 1; | ||
| 474 | |||
| 475 | __END__ |