package Atlas::Incident::Network::Graph; use strict; use warnings; use English( -no_match_vars ); use Graph; use Scalar::Util qw(blessed); use Storable qw( nfreeze thaw ); use base 'Atlas::Config'; use Atlas::System; my $ID = '0000000001'; my $insert_graph = 0; my $graph; my $routers; my $self = {}; ################################################# sub network { return $graph } ################################################# sub routers { return $routers } ################################################# sub _fetch_network_graph { my $sql = "SELECT object FROM Impact_Graphs WHERE id = ?"; $graph = $self->dbh->selectrow_array( $sql, {}, $ID ); if (not $graph) { $insert_graph = 1; return; }; my $value = thaw( $graph ); return $value; } ################################################# sub build { $graph = Graph->new(); $routers = Atlas::System->record_search({ order_by => 'name', system_type => 'Router', }); return if not $routers; ROUTER: for my $router (@{$routers}) { $graph->add_vertex( $router ); next ROUTER if not $router->interfaces; for my $interface ( @{$router->interfaces} ) { $graph->add_edge( $router, $interface ); } } SYSTEM: for my $router ( @{$routers} ) { next SYSTEM if not $router->interfaces; INTERFACE: for my $interface ( @{$router->interfaces} ) { next INTERFACE if not $interface->connections; for my $connection ( @{$interface->connections} ) { $graph->add_weighted_path( $connection->parent_interface, $connection->get_weight, $connection->child_interface ); } } } return 1; } ################################################# sub new { return $self if blessed $self; bless $self, __PACKAGE__; _fetch_network_graph(); return $self; } ################################################# sub save { my $sql = $insert_graph ? "INSERT Impact_Graphs SET object = ?, id = ?" : "UPDATE Impact_Graphs SET object = ? WHERE id = ?"; $self->dbh->do( $sql, {}, nfreeze( $graph ), $ID ) or return; $insert_graph = 0; return 1; }