Interact/ActionSet.pm

filter: Statementstmt 20.8% 19 / 91 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 28.5% 4 / 14 Podpod 80.0% 4 / 5 totaltotal 15.5% 27 / 174 CC 28 SCAR 35.6
CC
28
Cov
13%
BUILDARGS
72.0
_forward_continuation_to_match
56.0
_pad_send_with_match
42.0
CRAP 35.1
« Interact/Transport/Wrapper/Base.pm ↑ Summary Interact/Role/Engine.pm »
1 package Net::CLI::Interact::ActionSet;
2 {
3 1 $Net::CLI::Interact::ActionSet::VERSION = '2.143070';
4 }
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooX::Types::MooseLike::Base qw(InstanceOf ArrayRef CodeRef RegexpRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::CLI::Interact::Action;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Net::CLI::Interact::Role::Iterator';
12  
13 1 has default_continuation => (
14 is => 'rw',
15 isa => InstanceOf['Net::CLI::Interact::ActionSet'],
16 predicate => 1,
17 );
18  
19 1 has current_match => (
20 is => 'rw',
21 isa => ArrayRef[RegexpRef],
22 predicate => 1,
23 coerce => quote_sub(q{ (ref qr// eq ref $_[0]) ? [$_[0]] : $_[0] }),
24 );
25  
26 sub BUILDARGS {
27 0 ▶ my ($class, @rest) = @_;
Subroutine BUILDARGS: not called CC=8 SCAR=42.8
Pod undocumented
28  
29 # accept single hash ref or naked hash
30 0 ▶ my $params = (ref {} eq ref $rest[0] ? $rest[0] : {@rest});
Branch: 1 branch
BranchTrueFalseTotal
ref {} eq ref $rest[0] ? : 0 0 0
31  
32 0 ▶ if (exists $params->{actions} and ref $params->{actions} eq ref []) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"actions"} and ref $params->{"actions"} eq ref []) 0 0 0
Condition0%0/3
exists $params->{"actions"} and ref $params->{"actions"} eq ref []
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"actions"} and ref $params->{"actions"} eq ref []
exists $params->{"actions"} ref $params->{"actions"} eq ref []
Truth table0%0/3
A and B
A= exists $params->{"actions"}B= ref $params->{"actions"} eq ref []
ABresult
0X0
100
111
33 0 foreach my $a (@{$params->{actions}}) {
34 0 ▶ if (ref $a eq 'Net::CLI::Interact::ActionSet') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "Net::CLI::Interact::ActionSet") 0 0 0
35 0 push @{$params->{_sequence}}, @{ $a->_sequence };
36 0 next;
37 }
38  
39 0 ▶ if (ref $a eq 'Net::CLI::Interact::Action') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "Net::CLI::Interact::Action") 0 0 0
40 0 push @{$params->{_sequence}}, $a;
41 0 next;
42 }
43  
44 0 ▶ if (ref $a eq ref {}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq ref {}) 0 0 0
45 0 push @{$params->{_sequence}},
46 Net::CLI::Interact::Action->new($a);
47 0 next;
48 }
49  
50 0 die "don't know what to do with a: '$a'\n";
51 }
52 0 delete $params->{actions};
53 }
54  
55 0 return $params;
56 }
57  
58 sub clone {
59 0 ▶ my $self = shift;
Subroutine clone: not called CC=4 SCAR=30.0
Pod documented
60 return Net::CLI::Interact::ActionSet->new({
61 0 ▶ actions => [ map { $_->clone } @{ $self->_sequence } ],
Branch: 3 branches
BranchTrueFalseTotal
$self->_has_callbacks ? : 0 0 0
$self->has_default_continuation ? : 0 0 0
$self->has_current_match ? : 0 0 0
62 ($self->_has_callbacks ? (_callbacks => $self->_callbacks) : ()),
63 ($self->has_default_continuation ? (default_continuation => $self->default_continuation) : ()),
64 ($self->has_current_match ? (current_match => $self->current_match) : ()),
65 });
66 }
67  
68 # store params to the set, used when send is passed via sprintf
69 sub apply_params {
70 0 ▶ my ($self, @params) = @_;
Subroutine apply_params: not called CC=2 SCAR=17.9
Pod documented
71  
72 0 $self->reset;
73 0 ▶ while ($self->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next) 0 0 0
74 0 my $next = $self->next;
75 0 $next->params([splice @params, 0, $next->num_params]);
76 }
77 }
78  
79 has _callbacks => (
80 is => 'rw',
81 isa => ArrayRef[CodeRef],
82 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 1 predicate => 1,
84 );
85  
86 sub register_callback {
87 0 ▶ my $self = shift;
Subroutine register_callback: not called CC=1 SCAR=6.9
Pod documented
88 0 $self->_callbacks([ @{$self->_callbacks}, shift ]);
89 }
90  
91 sub execute {
92 0 ▶ my $self = shift;
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
93  
94 0 $self->_pad_send_with_match;
95 0 $self->_forward_continuation_to_match;
96 0 $self->_do_exec;
97 0 $self->_marshall_responses;
98 }
99  
100 sub _do_exec {
101 0 ▶ my $self = shift;
Subroutine _do_exec: not called CC=3 SCAR=24.8
102  
103 0 $self->reset;
104 0 ▶ while ($self->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next) 0 0 0
105 0 $_->($self->next) for @{$self->_callbacks};
106 }
107 }
108  
109 # pad out the Actions with match Actions if needed between send pairs.
110 sub _pad_send_with_match {
111 0 ▶ my $self = shift;
Subroutine _pad_send_with_match: not called CC=6 SCAR=37.4
112 0 my $match = Net::CLI::Interact::Action->new({
113 type => 'match', value => $self->current_match,
114 });
115  
116 0 $self->reset;
117 0 ▶ while ($self->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next) 0 0 0
118 0 my $this = $self->next;
119 0 ▶ my $next = $self->peek or last; # careful...
Branch: 1 branch
BranchTrueFalseTotal
my $next = $self->peek or last 0 0 0
120 0 ▶ next unless $this->type eq 'send' and $next->type eq 'send';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->type eq "send" and $next->type eq "send" 0 0 0
Condition0%0/3
$this->type eq "send" and $next->type eq "send"
!ll&&!rl&&r
000
MC/DC0%0/2
$this->type eq "send" and $next->type eq "send"
$this->type eq "send" $next->type eq "send"
Truth table0%0/3
A and B
A= $this->type eq "send"B= $next->type eq "send"
ABresult
0X0
100
111
121  
122 0 $self->insert_at($self->idx + 1, $match->clone);
123 }
124  
125 # always finish on a match
126 0 ▶ if ($self->last->type ne 'match') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last->type ne "match") 0 0 0
127 0 $self->insert_at($self->count, $match->clone);
128 }
129 }
130  
131 # carry-forward a continuation beacause it's the match which really does the
132 # heavy lifting.
133 sub _forward_continuation_to_match {
134 0 ▶ my $self = shift;
Subroutine _forward_continuation_to_match: not called CC=7 SCAR=40.3
135  
136 0 $self->reset;
137 0 ▶ while ($self->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next) 0 0 0
138 0 my $this = $self->next;
139 0 ▶ my $next = $self->peek or last; # careful...
Branch: 1 branch
BranchTrueFalseTotal
my $next = $self->peek or last 0 0 0
140 0 ▶ my $cont = ($this->continuation || $self->default_continuation);
Condition0%0/3
$this->continuation || $self->default_continuation
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->continuation || $self->default_continuation
$this->continuation $self->default_continuation
Truth table0%0/3
A || B
A= $this->continuationB= $self->default_continuation
ABresult
1X1
011
000
141 0 ▶ next unless $this->type eq 'send'
Branch: 1 branch
BranchTrueFalseTotal
unless $this->type eq "send" and $next->type eq "match" and defined $cont 0 0 0
Condition0%0/6
$this->type eq "send" and $next->type eq "match" and defined $cont
!ll&&!rl&&r
000
$this->type eq "send" and $next->type eq "match"
!ll&&!rl&&r
000
MC/DC0%0/3
$this->type eq "send" and $next->type eq "match" and defined $cont
$this->type eq "send" $next->type eq "match" defined $cont
Truth table0%0/4
A and B and C
A= $this->type eq "send"B= $next->type eq "match"C= defined $cont
ABCresult
0XX0
10X0
1100
1111
142 and $next->type eq 'match'
143 and defined $cont;
144  
145 0 $next->continuation($cont);
146 }
147 }
148  
149 # marshall the responses so as to move data from match to send
150 sub _marshall_responses {
151 0 ▶ my $self = shift;
Subroutine _marshall_responses: not called CC=4 SCAR=30.0
152  
153 0 $self->reset;
154 0 ▶ while ($self->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next) 0 0 0
155 0 my $send = $self->next;
156 0 ▶ my $match = $self->peek or last; # careful...
Branch: 1 branch
BranchTrueFalseTotal
my $match = $self->peek or last 0 0 0
157 0 ▶ next unless $match->type eq 'match';
Branch: 1 branch
BranchTrueFalseTotal
unless $match->type eq "match" 0 0 0
158  
159 # remove echoed command from the beginning
160 0 my $cmd = quotemeta( sprintf $send->value, @{ $send->params } );
161 0 (my $output = $match->response_stash) =~ s/^${cmd}[\t ]*(?:\r\n|\r|\n)?//s;
162 0 $send->response($output);
163 }
164 }
165  
166 1 1;
167  
168 # ABSTRACT: Conversation of Send and Match Actions
169  
170  
171 __END__
172 =pod
173  
174 =head1 NAME
175  
176 Net::CLI::Interact::ActionSet - Conversation of Send and Match Actions
177  
178 =head1 VERSION
179  
180 version 2.143070
181  
182 =head1 DESCRIPTION
183  
184 This class is used internally by L<Net::CLI::Interact> and it's unlikely that
185 an end-user will need to make use of ActionSet objects directly. The interface
186 is documented here as a matter of record.
187  
188 An ActionSet comprises a sequence (usefully, two or more) of
189 L<Actions|Net::CLI::Interact::Action> which describe a conversation with a
190 connected network device. Actions will alternate between type C<send> and
191 C<match>, perhaps not in their original
192 L<Phrasebook|Net::CLI::Interact::Phrasebook> definition, but certainly by the
193 time they are used.
194  
195 If the first Action is of type C<send> then the ActionSet is a normal sequence
196 of "send a command" then "match a response", perhaps repeated. If the first
197 Action is of type C<match> then the ActionSet represents a C<continuation>,
198 which is the method of dealing with paged output.
199  
200 =head1 INTERFACE
201  
202 =head2 default_continuation
203  
204 An ActionSet (C<match> then C<send>) which will be available for use on all
205 commands sent from this ActionSet. An alternative to explicitly describing the
206 Continuation sequence within the Phrasebook.
207  
208 =head2 current_match
209  
210 A stash for the current Prompt (regular expression reference) which
211 L<Net::CLI::Interact> expects to see after each command. This is passed into
212 the constructor and is used when padding Match Actions into the ActionSet (see
213 C<execute>, below).
214  
215 =head2 clone
216  
217 Returns a new ActionSet which is a shallow clone of the existing one. All the
218 reference based slots will share data, but you can add (for example) a
219 C<current_match> without affecting the original ActionSet. Used when preparing
220 to execute an ActionSet which has been retrieved from the
221 L<Phrasebook|Net::CLI::Interact::Phrasebook>.
222  
223 =head2 apply_params
224  
225 Accepts a list of parameters which will be used when C<sprintf> is called on
226 each Send Action in the set. You must supply sufficient parameters as a list
227 for I<all> Send Actions in the set, and they will be popped off and stashed
228 with the Action(s) according to how many are required.
229  
230 =head2 register_callback
231  
232 Allows the L<Transport|Net::CLI::Interact::Transport> to be registered
233 such that when the ActionSet is executed, commands are sent to the registered
234 callback subroutine. May be called more than once, and on execution each of
235 the callbacks will be run, in turn and in order.
236  
237 =head2 execute
238  
239 The business end of this class, where the sequence of Actions is prepared for
240 execution and then control passed to the Transport. This process is split into
241 a number of phases:
242  
243 =over 4
244  
245 =item Pad C<send> with C<match>
246  
247 The Phrasebook allows missing out of the Match statements between Send
248 statements, when they are expected to be the same as the C<current_match>.
249 This phase inserts Match statements to restore a complete ActionSet
250 definition.
251  
252 =item Forward C<continuation> to C<match>
253  
254 In the Phrasebook a user defines a Continuation (C<match>, then C<send>)
255 following a Send statement (because it deals with the response to the sent
256 command). However they are actually used by the Match, as it's the Match which
257 captures output.
258  
259 This phase copies Continuation ActionSets from Send statements to following
260 Match statements in the ActionSet. It also performs a similar action using the
261 C<default_continuation> if one is set and there's no existing Continuation
262 configured.
263  
264 =item Callback(s)
265  
266 Here the registered callbacks are executed (i.e. data is sent to the
267 Transport).
268  
269 =item Marshall Responses
270  
271 Finally, responses which are stashed in the Match Actions are copied back to
272 the Send actions, as more logically they are responses to commands sent. The
273 ActionSet is now ready for access to retrieve the C<last_response> from the
274 device.
275  
276 =back
277  
278 =head1 COMPOSITION
279  
280 See the following for further interface details:
281  
282 =over 4
283  
284 =item *
285  
286 L<Net::CLI::Interact::Role::Iterator>
287  
288 =back
289  
290 =head1 AUTHOR
291  
292 Oliver Gorwits <oliver@cpan.org>
293  
294 =head1 COPYRIGHT AND LICENSE
295  
296 This software is copyright (c) 2014 by Oliver Gorwits.
297  
298 This is free software; you can redistribute it and/or modify it under
299 the same terms as the Perl 5 programming language system itself.
300  
301 =cut
302  
« Interact/Transport/Wrapper/Base.pm ↑ Summary Interact/Role/Engine.pm »