OpenSIPS provisioning improvements, RT10993
[freeside.git] / FS / FS / part_export / phone_sqlopensips.pm
1 package FS::part_export::phone_sqlopensips;
2
3 use vars qw(@ISA @EXPORT_OK %info %options);
4 use Exporter;
5 use Tie::IxHash;
6 use FS::Record qw( dbh qsearch qsearchs );
7 use FS::part_export;
8 use FS::svc_phone;
9 use FS::export_svc;
10 use LWP::UserAgent;
11
12 @ISA = qw(FS::part_export);
13
14 tie %options, 'Tie::IxHash',
15   'datasrc'  => { label=>'DBI data source ' },
16   'username' => { label=>'Database username' },
17   'password' => { label=>'Database password' },
18   'xmlrpc_url' => { label=>'XMLRPC URL' },
19 ;
20
21 %info = (
22   'svc'      => 'svc_phone',
23   'desc'     => 'Export DIDs to OpenSIPs dr_rules table',
24   'options'  => \%options,
25   'notes'    => 'Export DIDs to OpenSIPs dr_rules table',
26 );
27
28 sub rebless { shift; }
29
30 sub _export_insert {
31   my($self, $svc_x) = (shift, shift);
32
33   my $conf = new FS::Conf;
34   my $agentnum = $svc_x->cust_svc->cust_pkg->cust_main->agentnum || 0;
35   my $gwlist = $conf->config('opensips_gwlist',$agentnum) 
36                 || $svc_x->phone_name;
37   my $description = $conf->config('opensips_description',$agentnum)
38                 || $svc_x->gwlist;
39   my $route = $conf->config('opensips_route',$agentnum) || $svc_x->route;
40
41   my $dbh = $self->opensips_connect;
42   my $sth = $dbh->prepare("insert into dr_rules ".
43             "( groupid, prefix, timerec, routeid, gwlist, description ) ".
44             " values ( ?, ?, ?, ?, ?, ? )") or die $dbh->errstr;
45   $sth->execute('0',$svc_x->phonenum,'',$route,$gwlist,
46                 $description) or die $sth->errstr;
47   $dbh->disconnect;
48   $self->dr_reload; # XXX: if this fails, do we delete what we just inserted?
49 }
50
51 sub opensips_connect {
52     my $self = shift;
53     DBI->connect($self->option('datasrc'),$self->option('username'),
54                         $self->option('password')) or die $DBI::errstr;
55 }
56
57 sub _export_replace {
58     # disabled the below for now as we went with a agent-virtualized config for the params
59 return '';
60
61
62   my( $self, $new, $old ) = (shift, shift, shift);
63     my @update = ();
64     my @paramvalues = ();
65
66     if($old->route ne $new->route){
67         push @update, 'routeid = ?';
68         push @paramvalues, $new->route;
69     }
70
71     if($old->phone_name ne $new->phone_name) {
72         push @update, 'description = ?';
73         push @paramvalues, $new->phone_name;
74     }
75
76     if($old->gwlist ne $new->gwlist) {
77         push @update, 'gwlist = ?';
78         push @paramvalues, $new->gwlist;
79     }
80
81     if(scalar(@update)) {
82       my $update_str = join(' and ',@update);
83       my $dbh = $self->opensips_connect;
84       my $sth = $dbh->prepare("update dr_rules set $update_str " . 
85             " where prefix = ? ") or die $dbh->errstr;
86       push @paramvalues, $old->phonenum;
87       $sth->execute(@paramvalues) or die $sth->errstr;
88       $dbh->disconnect;
89       return $self->dr_reload;
90     }
91   '';
92 }
93
94 sub _export_suspend {
95   my( $self, $svc_phone ) = (shift, shift);
96   '';
97 }
98
99 sub _export_unsuspend {
100   my( $self, $svc_phone ) = (shift, shift);
101   '';
102 }
103
104 sub _export_delete {
105   my( $self, $svc_x ) = (shift, shift);
106   my $dbh = $self->opensips_connect;
107   my $sth = $dbh->prepare("delete from dr_rules where prefix = ?")
108     or die $dbh->errstr;
109   $sth->execute($svc_x->phonenum) or die $sth->errstr;
110   $dbh->disconnect;
111   $self->dr_reload; # XXX: if this fails, do we re-insert what we just deleted?
112 }
113
114 sub dr_reload {
115     my $self = shift;
116     my $reqxml = "<?xml version=\"1.0\"?>
117 <methodCall>
118   <methodName>dr_reload</methodName>
119 </methodCall>";
120     my $ua = LWP::UserAgent->new;
121     my $resp = $ua->post(   $self->option('xmlrpc_url'),
122                             Content_Type => 'text/xml', 
123                             Content => $reqxml );
124     return "invalid HTTP response from OpenSIPS: " . $resp->status_line
125         unless $resp->is_success;
126     '';
127 }
128