summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/nas_wrapper.pm
blob: 2499ba3eedf58bed0dead7e31d73326f14372a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package FS::part_export::nas_wrapper;

=head1 FS::part_export::nas_wrapper

This is a meta-export that triggers other exports for FS::svc_broadband objects
based on a set of configurable conditions.  These conditions are defined by the
following FS::router virtual fields:

=over 4

=item nas_conf - Per-router meta-export configuration.  See L</"nas_conf Syntax">.

=back

=head2 nas_conf Syntax

export_name|routernum[,routernum]|[field,condition[,field,condition]][||...]

=over 4

=item export_name - Name or exportnum of the export to be executed.  In order to specify export options you must use the exportnum form.  (ex. 'router' for FS::part_export::router).

=item routernum - FS::router routernum corresponding to the desired FS::router for which this export will be run.

=item field - FS::svc_broadband field (real or virtual).  The following condition (regex) will be matched against the value of this field.

=item condition - A regular expression to be match against the value of the previously listed FS::svc_broadband field.

=back

If multiple routernum's are specified, then the export will be triggered for each router listed.  If multiple field/condition pairs are present, then the results of the matches will be and'd.  Note that if a false match is found, the rest of the matches may not be checked.

You can specify multiple export/router/condition sets by concatenating them with '||'.

=cut

use strict;
use vars qw(@ISA %info $me $DEBUG);

use FS::Record qw(qsearchs);
use FS::part_export;

use Tie::IxHash;
use Data::Dumper qw(Dumper);

@ISA = qw(FS::part_export);
$me = '[' . __PACKAGE__ . ']';
$DEBUG = 0;

%info = (
  'svc'     => 'svc_broadband',
  'desc'    => 'A meta-export that triggers other svc_broadband exports.',
  'options' => {},
  'notes'   => '',
);


sub rebless { shift; }

sub _export_insert {
  my($self) = shift;
  $self->_export_command('insert', @_);
}

sub _export_delete {
  my($self) = shift;
  $self->_export_command('delete', @_);
}

sub _export_suspend {
  my($self) = shift;
  $self->_export_command('suspend', @_);
}

sub _export_unsuspend {
  my($self) = shift;
  $self->_export_command('unsuspend', @_);
}

sub _export_replace {
  my($self) = shift;
  $self->_export_command('replace', @_);
}

sub _export_command {
  my ( $self, $action, $svc_broadband) = (shift, shift, shift);

  my ($new, $old);
  if ($action eq 'replace') {
    $new = $svc_broadband;
    $old = shift;
  }

  my $router = $svc_broadband->addr_block->router;

  return '' unless grep(/^nas_conf$/, $router->fields);
  my $nas_conf = $router->nas_conf;

  my $child_exports = &_parse_nas_conf($nas_conf);

  my $error = '';

  my $queue_child_exports = {};

  # Similar to FS::svc_Common::replace, calling insert, delete, and replace
  # exports where necessary depending on which conditions match.
  if ($action eq 'replace') {

    my @new_child_exports = ();
    my @old_child_exports = ();

    # Find all the matching "new" child exports.
    foreach my $child_export (@$child_exports) {
      my $match = &_test_child_export_conditions(
        $child_export->{'conditions'},
        $new,
      );

      if ($match) {
	push @new_child_exports, $child_export;
      }
    }

    # Find all the matching "old" child exports.
    foreach my $child_export (@$child_exports) {
      my $match = &_test_child_export_conditions(
        $child_export->{'conditions'},
        $old,
      );

      if ($match) {
	push @old_child_exports, $child_export;
      }
    }

    # Insert exports for new.
    push @{$queue_child_exports->{'insert'}}, (
      map { 
	my $new_child_export = $_;
	if (! grep { $new_child_export eq $_ } @old_child_exports) {
	  $new_child_export->{'args'} = [ $new ];
	  $new_child_export;
	} else {
	  ();
	}
      } @new_child_exports
    );

    # Replace exports for new and old.
    push @{$queue_child_exports->{'replace'}}, (
      map { 
	my $new_child_export = $_;
	if (grep { $new_child_export eq $_ } @old_child_exports) {
	  $new_child_export->{'args'} = [ $new, $old ];
	  $new_child_export;
	} else {
	  ();
	}
      } @new_child_exports
    );

    # Delete exports for old.
    push @{$queue_child_exports->{'delete'}}, (
      grep { 
	my $old_child_export = $_;
	if (! grep { $old_child_export eq $_ } @new_child_exports) {
	  $old_child_export->{'args'} = [ $old ];
	  $old_child_export;
	} else {
	  ();
	}
      } @old_child_exports
    );

  } else {

    foreach my $child_export (@$child_exports) {
      my $match = &_test_child_export_conditions(
        $child_export->{'conditions'},
        $svc_broadband,
      );

      if ($match) {
	$child_export->{'args'} = [ $svc_broadband ];
        push @{$queue_child_exports->{$action}}, $child_export;
      }
    }

  }

  warn "[debug]$me Dispatching child exports... "
    . &Dumper($queue_child_exports) if $DEBUG;

  # Actually call the child exports now, with their preset action and arguments.
  foreach my $_action (keys(%$queue_child_exports)) {

    foreach my $_child_export (@{$queue_child_exports->{$_action}}) {
      $error = &_dispatch_child_export(
        $_child_export,
        $_action,
        @{$_child_export->{'args'}},
        @_,
      );

      # Bail if there's an error queueing one of the exports.
      # This will all get rolled-back.
      return $error if $error;
    }

  }

  return '';

}


sub _parse_nas_conf {

  my $nas_conf = shift;
  my @child_exports = ();

  foreach my $cond_set ($nas_conf =~ m/(.*?[^\\])(?:\|\||$)/g) {

    warn "[debug]$me cond_set is '$cond_set'" if $DEBUG;

    my @args = $cond_set =~ m/(.*?[^\\])(?:\||$)/g;

    my %child_export = (
      'export' => $args[0],
      'routernum' => [ split(/,\s*/, $args[1]) ],
      'conditions' => { @args[2..$#args] },
    );

    warn "[debug]$me " . Dumper(\%child_export) if $DEBUG;

    push @child_exports, { %child_export };

  }

  return \@child_exports;

}

sub _dispatch_child_export {

  my ($child_export, $action, @args) = (shift, shift, @_);

  my $child_export_name = $child_export->{'export'};
  my @routernums = @{$child_export->{'routernum'}};

  my $error = '';

  # And the real hack begins...

  my $child_part_export;
  if ($child_export_name =~ /^(\d+)$/) {
    my $exportnum = $1;
    $child_part_export = qsearchs('part_export', { exportnum => $exportnum });
    unless ($child_part_export) {
      return "No such FS::part_export with exportnum '$exportnum'";
    }

    $child_export_name = $child_part_export->exporttype;
  } else {
    $child_part_export = new FS::part_export {
      'exporttype' => $child_export_name,
      'machine' => 'bogus',
    };
  }

  warn "[debug]$me running export '$child_export_name' for routernum(s) '"
    . join(',', @routernums) . "'" if $DEBUG;

  my $cmd_method = "_export_$action";

  foreach my $routernum (@routernums) {
    $error ||= $child_part_export->$cmd_method(
      @args,
      'routernum' => $routernum,
    );
    last if $error;
  }

  warn "[debug]$me export '$child_export_name' returned '$error'"
    if $DEBUG;

  return $error;

}

sub _test_child_export_conditions {

  my ($conditions, $svc_broadband) = (shift, shift);

  my $match = 1;
  foreach my $cond_field (keys %$conditions) {
    my $cond_regex = $conditions->{$cond_field};
    warn "[debug]$me Condition: $cond_field =~ /$cond_regex/" if $DEBUG;
    unless ($svc_broadband->get($cond_field) =~ /$cond_regex/) {
      $match = 0;
      last;
    }
  }

  return $match;

}


1;