globalPOPs provisioning
[freeside.git] / FS / FS / part_export / globalpops_voip.pm
1 package FS::part_export::globalpops_voip;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::Record qw(qsearch dbh);
6 use FS::part_export;
7 use FS::phone_avail;
8
9 @ISA = qw(FS::part_export);
10
11 tie my %options, 'Tie::IxHash',
12   'login'         => { label=>'GlobalPOPs Media Services API login' },
13   'password'      => { label=>'GlobalPOPs Media Services API password' },
14   'endpointgroup' => { label=>'GlobalPOPs endpoint group number' },
15 ;
16
17 %info = (
18   'svc'     => 'svc_phone',
19   'desc'    => 'Provision phone numbers to GlobalPOPs VoIP',
20   'options' => \%options,
21   'notes'   => <<'END'
22 Requires installation of
23 <a href="http://search.cpan.org/dist/Net-GlobalPOPs-MediaServicesAPI">Net::GlobalPOPs::MediaServicesAPI</a>
24 from CPAN.
25 END
26 );
27
28 sub rebless { shift; }
29
30 sub get_dids {
31   my $self = shift;
32   my %opt = ref($_[0]) ? %{$_[0]} : @_;
33
34   my %search = ();
35   #  'orderby' => 'npa', #but it doesn't seem to work :/
36
37   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
38     %getdids = ( 'npa'   => $opt{'areacode'},
39                  'nxx'   => $opt{'exchange'},
40                );
41   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
42     %getdids = ( 'npa'   => $opt{'areacode'} );
43   } elsif ( $opt{'state'} ) {
44
45     my @avail = qsearch({
46       'table'    => 'phone_avail',
47       'hashref'  => { 'exportnum'   => $self->exportnum,
48                       'countrycode' => '1', #don't hardcode me when gp goes int'l
49                       'state'       => $opt{'state'},
50                     },
51       'order_by' => 'ORDER BY npa',
52     });
53
54     return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
55
56     #otherwise, search for em
57     %getdids = ( 'state' => $opt{'state'} );
58
59   }
60
61   my $dids = $self->gp_command('getDIDs', %getdids);
62
63   #use Data::Dumper;
64   #warn Dumper($dids);
65
66   my $search = $dids->{'search'};
67
68   if ( $search->{'statuscode'} == 302200 ) {
69     return [];
70   } elsif ( $search->{'statuscode'} != 100 ) {
71     die "Error running globalpop getDIDs: ".
72         $search->{'statuscode'}. ': '. $search->{'status'}; #die??
73   }
74
75   my @return = ();
76
77   #my $latas = $search->{state}{lata};
78   my %latas;
79   if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
80     %latas = map $search->{state}{lata}{$_},
81                  qw(name rate_center);
82   } else {
83     %latas = %{ $search->{state}{lata} };
84   } 
85
86   foreach my $lata ( keys %latas ) {
87
88     #warn "LATA $lata";
89     
90     #my $l = $latas{$lata};
91     #$l = $l->{rate_center} if exists $l->{rate_center};
92     
93     my $lata_dids = $self->gp_command('getDIDs', %getdids, 'lata'=>$lata);
94     my $lata_search = $lata_dids->{'search'};
95     unless ( $lata_search->{'statuscode'} == 100 ) {
96       die "Error running globalpop getDIDs: ". $lata_search->{'status'}; #die??
97     }
98    
99     my $l = $lata_search->{state}{lata}{'rate_center'};
100
101     #use Data::Dumper;
102     #warn Dumper($l);
103
104     my %rate_center;
105     if ( grep $l->{$_}, qw(name friendlyname) ) {
106       %rate_center = map $l->{$_},
107                          qw(name friendlyname);
108     } else {
109       %rate_center = %$l;
110     } 
111
112     foreach my $rate_center ( keys %rate_center ) {
113       
114       #warn "rate center $rate_center";
115
116       my $rc = $rate_center{$rate_center}; 
117       $rc = $rc->{friendlyname} if exists $rc->{friendlyname};
118
119       my @r = ();
120       if ( exists($rc->{npa}) ) {
121         @r = ($rc);
122       } else {
123         @r = map { { 'name'=>$_, %{ $rc->{$_} } }; } keys %$rc
124       }
125
126       foreach my $r (@r) {
127
128         my @npa = ();
129         if ( exists($r->{npa}{name}) ) {
130           @npa = ($r->{npa})
131         } else {
132           @npa = map { { 'name'=>$_, %{ $r->{npa}{$_} } } } keys %{ $r->{npa} };
133         }
134
135         foreach my $npa (@npa) {
136
137           if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
138
139             #warn Dumper($npa);
140
141             my $tn = $npa->{nxx}{tn} || $npa->{nxx}{$opt{'exchange'}}{tn};
142
143             my @tn = ref($tn) ? @$tn : ($tn);
144             #push @return, @tn;
145             push @return, map {
146                                 if ( /^\s*(\d{3})(\d{3})(\d{4})\s*$/ ) {
147                                   "$1-$2-$3";
148                                 } else {
149                                   $_;
150                                 }
151                               }
152                               @tn;
153
154           } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
155
156             if ( $npa->{nxx}{name} ) {
157               @nxx = ( $npa->{nxx}{name} );
158             } else {
159               @nxx = keys %{ $npa->{nxx} };
160             }
161
162             push @return, map { $r->{name}. ' ('. $npa->{name}. "-$_-XXXX)"; }
163                               @nxx;
164
165           } elsif ( $opt{'state'} ) { #and not other things, then return areacode
166             #my $ac = $npa->{name};
167             #use Data::Dumper;
168             #warn Dumper($r) unless length($ac) == 3;
169
170             push @return, $npa->{name}
171               unless grep { $_ eq $npa->{name} } @return;
172
173           } else {
174             warn "WARNING: returning nothing for get_dids without known options"; #?
175           }
176
177         } #foreach my $npa
178
179       } #foreach my $r
180
181     } #foreach my $rate_center
182
183   } #foreach my $lata
184
185   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
186     @return = sort { $a cmp $b } @return; #string comparison actually dwiw
187   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
188     @return = sort { lc($a) cmp lc($b) } @return;
189   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
190
191     #populate cache
192
193     local $SIG{HUP} = 'IGNORE';
194     local $SIG{INT} = 'IGNORE';
195     local $SIG{QUIT} = 'IGNORE';
196     local $SIG{TERM} = 'IGNORE';
197     local $SIG{TSTP} = 'IGNORE';
198     local $SIG{PIPE} = 'IGNORE';
199
200     my $oldAutoCommit = $FS::UID::AutoCommit;
201     local $FS::UID::AutoCommit = 0;
202     my $dbh = dbh;
203
204     my $errmsg = 'WARNING: error populating phone availability cache: ';
205     my $error = '';
206     foreach my $return (@return) {
207       my $phone_avail = new FS::phone_avail {
208         'exportnum'   => $self->exportnum,
209         'countrycode' => '1', #don't hardcode me when gp goes int'l
210         'state'       => $opt{'state'},
211         'npa'         => $return,
212       };
213       $error = $phone_avail->insert();
214       if ( $error ) {
215         warn $errmsg.$error;
216         last;
217       }
218     }
219
220     if ( $error ) {
221       $dbh->rollback if $oldAutoCommit;
222     } else {
223       $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
224     }
225
226     #end populate cache
227
228     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
229     @return = sort { $a <=> $b } @return;
230   } else {
231     warn "WARNING: returning nothing for get_dids without known options"; #?
232   }
233
234   \@return;
235
236 }
237
238 sub gp_command {
239   my( $self, $command, @args ) = @_;
240
241   eval "use Net::GlobalPOPs::MediaServicesAPI;";
242   die $@ if $@;
243
244   my $gp = Net::GlobalPOPs::MediaServicesAPI->new(
245     'login'    => $self->option('login'),
246     'password' => $self->option('password'),
247     #'debug'    => $debug,
248   );
249
250   $gp->$command(@args);
251 }
252
253
254 sub _export_insert {
255   my( $self, $svc_phone ) = (shift, shift);
256   #we want to provision and catch errors now, not queue
257
258   my $r = $self->gp_command('reserveDID',
259     'did'           => $svc_phone->phonenum,
260     'minutes'       => 1,
261     'endpointgroup' => $self->option('endpointgroup'),
262   );
263
264   my $rdid = $r->{did};
265
266   if ( $rdid->{'statuscode'} != 100 ) {
267     return "Error running globalpop reserveDID: ".
268            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
269   }
270
271   my $a = $self->gp_command('assignDID',
272     'did'           => $svc_phone->phonenum,
273     'endpointgroup' => $self->option('endpointgroup'),
274     #'rewrite'
275     #'cnam'
276   );
277
278   my $adid = $a->{did};
279
280   if ( $adid->{'statuscode'} != 100 ) {
281     return "Error running globalpop assignDID: ".
282            $adid->{'statuscode'}. ': '. $adid->{'status'};
283   }
284
285   '';
286 }
287
288 sub _export_replace {
289   my( $self, $new, $old ) = (shift, shift, shift);
290
291   #hmm, what's to change?
292   '';
293 }
294
295 sub _export_delete {
296   my( $self, $svc_phone ) = (shift, shift);
297
298   #probably okay to queue the deletion...?
299   #but hell, let's do it inline anyway, who wants phone numbers hanging around
300
301   my $r = $self->gp_command('releaseDID',
302     'did'           => $svc_phone->phonenum,
303   );
304
305   my $rdid = $r->{did};
306
307   if ( $rdid->{'statuscode'} != 100 ) {
308     return "Error running globalpop releaseDID: ".
309            $rdid->{'statuscode'}. ': '. $rdid->{'status'};
310   }
311
312   '';
313 }
314
315 sub _export_suspend {
316   my( $self, $svc_phone ) = (shift, shift);
317   #nop for now
318   '';
319 }
320
321 sub _export_unsuspend {
322   my( $self, $svc_phone ) = (shift, shift);
323   #nop for now
324   '';
325 }
326
327 #hmm, might forgo queueing entirely for most things, data is too much of a pita
328
329 sub globalpops_voip_queue {
330   my( $self, $svcnum, $method ) = (shift, shift, shift);
331   my $queue = new FS::queue {
332     'svcnum' => $svcnum,
333     'job'    => 'FS::part_export::globalpops_voip::globalpops_voip_command',
334   };
335   $queue->insert(
336     $self->option('login'),
337     $self->option('password'),
338     $method,
339     @_,
340   );
341 }
342
343 sub globalpops_voip_command {
344   my($login, $password, $method, @args) = @_;
345
346   eval "use Net::GlobalPOPs::MediaServicesAPI;";
347   die $@ if $@;
348
349   my $gp = new Net::GlobalPOPs
350                  'login'    => $login,
351                  'password' => $password,
352                  #'debug'    => 1,
353                ;
354
355   my $return = $gp->$method( @args );
356
357   #$return->{'status'} 
358   #$return->{'statuscode'} 
359
360   die $return->{'status'} if $return->{'statuscode'};
361
362 }
363
364 1;
365