Farly/Remove/Address.pm

filter: Statementstmt 90.2% 102 / 113 Branchbran 63.1% 24 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 75.0% 3 / 4 totaltotal 84.7% 144 / 170 CC 24 SCAR 17.2
CC
24
Cov
83%
_collect_garbage
13.5
remove
4.2
_reference_search
4.0
CRAP 5.6
« Farly/Object.pm ↑ Summary Farly/Transport/Object.pm »
1 package Farly::Remove::Address;
2
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 our $VERSION = '0.26';
9
10 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
11 1 my ( $class, $container ) = @_;
12
13 1 ▶ confess "firewall configuration container object required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $container 0 1 1
14 unless ( defined($container) );
15
16 1 ▶ confess "Farly::Object::List object required"
Branch: 1 branch
BranchTrueFalseTotal
unless $container->isa("Farly::Object::List") 0 1 1
17 unless ( $container->isa("Farly::Object::List") );
18
19 1 my $self = {
20 FW => $container,
21 REMOVE => Farly::Object::List->new(),
22 };
23 1 bless $self, $class;
24
25 1 return $self;
26 }
27
28 6 ▶sub fw { return $_[0]->{FW} }
Subroutine fw: called CC=1 SCAR=0.0
Pod undocumented
29
30 # remove can be called many times
31 # all objects in {REMOVE} need to be removed from {FW}
32 2 ▶sub remove {
Subroutine remove: called CC=4 SCAR=14.2
Pod documented
33 2 my ( $self, $ip ) = @_;
34
35 2 ▶ confess "ip not defined" unless defined($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 0 2 2
36
37 2 ▶ confess "Farly::IPv4::Address address required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip->isa("Farly::IPv4::Address") 0 2 2
38 unless defined( $ip->isa('Farly::IPv4::Address') );
39
40 #print "\nsearching for references to ", $ip->as_string, "...\n";
41
42 2 my $garbage_list = $self->_address_search($ip);
43
44 2 my $objects_for_cleanup = $self->_collect_garbage($garbage_list);
45
46 2 ▶ if ( $objects_for_cleanup->size() != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects_for_cleanup->size != 0) 2 0 2
47
48 #print "\nstoring removed objects in reverse order\n\n";
49 2 $self->_add_reversed_list($objects_for_cleanup);
50 2 $self->_cleanup();
51 }
52 }
53
54 5 ▶sub result { return $_[0]->{REMOVE} }
Subroutine result: called CC=1 SCAR=0.0
Pod documented
55
56 # all objects being removed will have been marked with the
57 # 'REMOVE' property. Update the $self->{CONFIG} container to
58 # exclude all objects which are being removed. This allows
59 # the remove() method to be called multiple times for the
60 # same configuration.
61 2 ▶sub _cleanup {
Subroutine _cleanup: called CC=3 SCAR=11.0
62 2 my ($self) = @_;
63
64 2 my $new_list = Farly::Object::List->new();
65
66 2 foreach my $object ( $self->fw->iter() ) {
67
68 #print $object->dump(),"\n";
69 129 ▶ if ( !$object->has_defined('REMOVE') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object->has_defined("REMOVE")) 125 4 129
70 125 $new_list->add($object);
71 }
72 }
73
74 2 $self->{FW} = $new_list;
75 }
76
77 2 ▶sub _address_search {
Subroutine _address_search: called CC=1 SCAR=0.0
78 2 my ( $self, $ip ) = @_;
79
80 2 my $fw = $self->{FW};
81
82 2 my $search = Farly::Object->new();
83 2 my $search_result = Farly::Object::List->new();
84
85 2 $search->set( "OBJECT", $ip );
86 2 $fw->contained_by( $search, $search_result );
87 2 $search->delete_key("OBJECT");
88
89 2 $search->set( "SRC_IP", $ip );
90 2 $fw->contained_by( $search, $search_result );
91 2 $search->delete_key("SRC_IP");
92
93 2 $search->set( "DST_IP", $ip );
94 2 $fw->contained_by( $search, $search_result );
95
96 2 return $search_result;
97 }
98
99 2 ▶sub _collect_garbage {
Subroutine _collect_garbage: called CC=10 SCAR=26.0
100 2 my ( $self, $garbage_list ) = @_;
101
102 2 my $fw = $self->{CONFIG};
103
104 2 my $agg = Farly::Object::Aggregate->new( $self->fw );
105 2 $agg->groupby( 'ENTRY', 'ID' );
106
107 2 my $NAME = Farly::Value::String->new('NAME');
108 2 my $GROUP = Farly::Value::String->new('GROUP');
109 2 my $RULE = Farly::Value::String->new('RULE');
110 2 my $OBJECT = Farly::Value::String->new('OBJECT');
111 2 my $INTERFACE = Farly::Value::String->new('INTERFACE');
112 2 my $ROUTE = Farly::Value::String->new('ROUTE');
113
114 2 my @stack;
115 2 my $remove = Farly::Object::List->new();
116
117 2 push @stack, $garbage_list->iter();
118
119 2 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 4 2 6
120
121 4 my $object = pop @stack;
122
123 4 ▶ if ( $object->get('ENTRY')->equals($GROUP) ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($object->get('ENTRY')->equals($GROUP)) { } 1 3 4
elsif ($object->get('ENTRY')->equals($OBJECT)) { } 2 1 3
elsif ($object->get('ENTRY')->equals($RULE)) { } 1 0 1
elsif ($object->get('ENTRY')->equals($NAME)) { } 0 0 0
elsif ($object->get('ENTRY')->equals($INTERFACE)) { } 0 0 0
elsif ($object->get('ENTRY')->equals($ROUTE)) { } 0 0 0
124
125 # convert the $object to a reference object
126 1 my $ref_obj = $self->_create_reference($object);
127
128 #it's a group, check the size
129 1 my $actual = $agg->matches($ref_obj);
130
131 1 ▶ if ( !defined $actual ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $actual) 0 1 1
132 0 confess "error ", $object->dump(), " actual not found";
133 }
134
135 # if the size of the group is 1 all references to the
136 # group must be removed first
137 1 ▶ if ( $actual->size == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual->size == 1) { } 0 1 1
138
139 # if the group can be removed no members of that group
140 # should be in $remove, i.e. the group has already been
141 # emptied out so take all other group member objects out
142 # of remove
143 0 $remove = $self->_remove_copy( $remove, $ref_obj );
144
145 0 $object->set( 'REMOVE', Farly::Value::String->new('GROUP') );
146 0 $remove->add($object);
147
148 # each referring object must be checked to see if it can be removed
149 # all references to 'object' will be in @remove after 'object'
150
151 0 my @result = $self->_reference_search($ref_obj);
152 0 push @stack, @result;
153 }
154 else {
155
156 # group size > 1
157
158 # create a new ::Set, minus the group member $object to be removed
159 1 my $new_set = $self->_remove_copy( $actual, $object );
160
161 # update the index to reflect that $object is removed
162 # because more objects could be removed from the group later on
163 1 $agg->update( $ref_obj, $new_set );
164
165 1 $object->set( 'REMOVE', Farly::Value::String->new('OBJECT') );
166 1 $remove->add($object);
167 }
168
169 }
170 elsif ( $object->get('ENTRY')->equals($OBJECT) ) {
171
172 # set the object to be removed
173 2 $object->set( 'REMOVE', Farly::Value::String->new('OBJECT') );
174 2 $remove->add($object);
175
176 # reformat the object into a reference object
177 2 my $ref_obj = $self->_create_reference($object);
178
179 # find everything that references the removed object
180 2 my @result = $self->_reference_search($ref_obj);
181 2 push @stack, @result;
182
183 }
184 elsif ( $object->get('ENTRY')->equals($RULE) ) {
185
186 # rules which refer to the Address directly can be removed
187 # immediately
188 1 $object->set( 'REMOVE', Farly::Value::String->new('RULE') );
189 1 $remove->add($object);
190
191 }
192 elsif ( $object->get('ENTRY')->equals($NAME) ) {
193 0 next;
194 }
195 elsif ( $object->get('ENTRY')->equals($INTERFACE) ) {
196 0 next;
197 }
198 elsif ( $object->get('ENTRY')->equals($ROUTE) ) {
199 0 next;
200 }
201 else {
202 0 warn "\nunknown object:\n", $object->dump();
203 0 next;
204 }
205 }
206
207 2 return $remove;
208 }
209
210 # convert an object into a reference object
211 3 ▶sub _create_reference {
Subroutine _create_reference: called CC=1 SCAR=0.0
212 3 my ( $self, $object ) = @_;
213
214 3 my $ref = Farly::Object::Ref->new();
215 3 $ref->set( 'ENTRY', $object->get('ENTRY') );
216 3 $ref->set( 'ID', $object->get('ID') );
217
218 3 return $ref;
219 }
220
221 # find every object which refers to $search
222 2 ▶sub _reference_search {
Subroutine _reference_search: called CC=4 SCAR=13.9
223 2 my ( $self, $search ) = @_;
224
225 2 my @search_result;
226
227 2 foreach my $object ( $self->fw->iter ) {
228 128 foreach my $property ( $object->get_keys ) {
229 769 ▶ if ( $object->get($property)->equals($search) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->get($property)->equals($search)) 1 768 769
230 1 push @search_result, $object;
231 }
232 }
233 }
234
235 2 return @search_result;
236 }
237
238 # Copies the objects in $set into a new ::Set, except for the objects
239 # that match $remove, which are not copied.
240 1 ▶sub _remove_copy {
Subroutine _remove_copy: called CC=3 SCAR=11.0
241 1 my ( $self, $set, $remove ) = @_;
242
243 1 my $r = Farly::Object::List->new();
244
245 1 foreach my $object ( $set->iter ) {
246 4 ▶ if ( !$object->matches($remove) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object->matches($remove)) 3 1 4
247 3 $r->add($object);
248 }
249 }
250
251 1 return $r;
252 }
253
254 # reverse the order of the remove list
255 # objects to remove must be processed last in first out order
256 # because they where pushed on the @remove array
257 2 ▶sub _add_reversed_list {
Subroutine _add_reversed_list: called CC=3 SCAR=11.0
258 2 my ( $self, $remove ) = @_;
259
260 2 ▶ for ( my $i = $remove->size() - 1 ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 4 2 6
261 4 ▶ $remove->[$i]->delete_key('LINE') if $remove->[$i]->has_defined('LINE');
Branch: 1 branch
BranchTrueFalseTotal
if $remove->[$i]->has_defined("LINE") 1 3 4
262 4 $self->result->add( $remove->[$i] );
263 }
264 }
265
266 1 1;
267 __END__
« Farly/Object.pm ↑ Summary Farly/Transport/Object.pm »