File Coverage

script/ospf2dot
Criterion Covered Total %
statement 78 93 83.8
branch 15 30 50.0
condition 1 3 33.3
subroutine 28 31 90.3
pod n/a
total 122 157 77.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             ##########################################################################
4             # Copyright (c) 2010-2012 Alexander Bluhm
5             #
6             # Permission to use, copy, modify, and distribute this software for any
7             # purpose with or without fee is hereby granted, provided that the above
8             # copyright notice and this permission notice appear in all copies.
9             #
10             # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11             # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12             # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13             # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14             # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15             # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16             # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17             ##########################################################################
18              
19 7     7   1419083 use strict;
  7     5   30  
  7         282  
  5         15555609  
  5         147  
  5         784  
20 7     7   56 use warnings;
  7     5   23  
  7         297  
  5         133  
  5         70  
  5         788  
21 7     7   719 use File::Slurp;
  7     5   33173  
  7         653  
  5         135  
  5         77  
  5         1751  
22 7     7   4760 use Getopt::Long qw(:config posix_default bundling);
  7     5   66679  
  7         37  
  5         118  
  5         64  
  5         245  
23 7     7   7498 use OSPF::LSDB::View;
  7     5   23  
  7         386  
  5         3466  
  5         40  
  5         294  
24 7     7   5646 use OSPF::LSDB::View6;
  7     5   36  
  7         386  
  5         47  
  5         72  
  5         372  
25 7     7   4511 use OSPF::LSDB::YAML;
  7     5   28  
  7         4911  
  5         71  
  5         69  
  5         9068  
26              
27             sub usage(@) {
28 6 100   6   231 print STDERR "Error: @_\n" if @_;
29 6         133 print STDERR <
30             Convert OSPF LSDB in YAML format to dot file used by Graphviz.
31              
32             Usage: $0 [-46bBceEhlpPsSwWv] [ospf.yaml] [ospf.dot]
33             -4 disable IPv6
34             -6 enable IPv6
35             -b generate other area AS boundary router summary
36             -B aggregate other area AS boundary router summary
37             -c cluster identical networks
38             -e generate AS external networks
39             -E aggregate AS external networks
40             -h help, print usage
41             -l generate legend
42             -p generate link and intra-area-prefix
43             -P generate intra-area-prefix
44             -s generate other area network summary
45             -S aggregate other area network summary
46             -w show most serious warning in dot graph
47             -W show all warnings and areas in dot graph
48             -v be verbose, print warnings to stderr
49             ospf.yaml input file, default stdin
50             ospf.dot output file, default stdout
51             EOF
52 6         608 exit(2);
53             }
54              
55             sub main() {
56 7     7   48 my %todo;
57             my $ipv6;
58 7         0 my $legend;
59             GetOptions(
60 0     0   0 '4' => sub { $ipv6 = 0 },
61 0     0   0 '6' => sub { $ipv6 = 1 },
62 1     1   1189 'b' => sub { $todo{boundary}{generate} = 1 },
63 2     2   192 'B' => sub { $todo{boundary}{aggregate} = 1 },
64 1     1   1015 'c' => sub { $todo{cluster} = 1 },
65 1     1   1237 'e' => sub { $todo{external}{generate} = 1 },
66 2     2   179 'E' => sub { $todo{external}{aggregate} = 1 },
67 1     1   992 'h' => sub { usage() },
68             'l' => \$legend,
69 1     1   1461 'p' => sub { $todo{prefix}{generate} = 1 },
70 1     1   101 'P' => sub { $todo{prefix}{aggregate} = 1 },
71 1     1   1195 's' => sub { $todo{summary}{generate} = 1 },
72 2     2   177 'S' => sub { $todo{summary}{aggregate} = 1 },
73 1     1   1333 'w' => sub { $todo{warning}{single} = 1 },
74 1     1   168 'W' => sub { $todo{warning}{all} = 1 },
75 0     0   0 'v' => sub { $todo{verbose} = 1 },
76 7 50       244 ) or usage("Bad option");
77 6 50       394 usage("Only two arguments allowed") if @ARGV > 2;
78              
79 6         46 foreach my $option (qw(boundary external prefix summary warning)) {
80 20 100       30 if (keys %{$todo{$option} || {}} > 1) {
  20 100       592  
81 5         23 my $opt = substr($option, 0, 1);
82 5         38 usage("Options -$opt and -".uc($opt)." used together");
83             }
84             }
85              
86 1 50       3 if ($todo{prefix}) {
87 0         0 $todo{intra}{generate} = 1;
88 0 0       0 $todo{link}{generate} = 1 if $todo{prefix}{generate};
89             }
90              
91 1 50       3 if ($legend) {
92 0 0       0 my $class = $ipv6 ? 'OSPF::LSDB::View6' : 'OSPF::LSDB::View';
93 0         0 print $class->legend();
94 0         0 exit 0;
95             }
96              
97 1         14 my $yaml = OSPF::LSDB::YAML->new();
98 1 50       7 if (@ARGV > 0) {
99 1         8 $yaml->LoadFile($ARGV[0]);
100             } else {
101 0         0 local $/;
102 0         0 $yaml->Load();
103             }
104              
105 1 50 33     1083 if (defined $ipv6 && $ipv6 != $yaml->ipv6) {
106 0         0 die "Address family of yaml file does not match -4 and -6 options.\n";
107             }
108 1 50       6 my $class = $yaml->ipv6 ? 'OSPF::LSDB::View6' : 'OSPF::LSDB::View';
109 1         17 my $view = $class->new($yaml);
110 1         9 my $dot = $view->graph(%todo);
111 1 50       6 if ($todo{verbose}) {
112 0         0 my @errors = $view->get_errors;
113 0 0       0 warn map { "$_\n" } @errors if @errors;
  0         0  
114             }
115 1 50       5 if (@ARGV > 1) {
116 1         10 write_file($ARGV[1], $dot);
117             } else {
118 0           print $dot;
119             }
120             }
121              
122             main();