fix netsapiens device provisioning? or at least better debugging, RT#5226
[freeside.git] / FS / FS / part_export / netsapiens.pm
1 package FS::part_export::netsapiens;
2
3 use vars qw(@ISA $me %info);
4 use URI;
5 use MIME::Base64;
6 use Tie::IxHash;
7 use FS::part_export;
8
9 @ISA = qw(FS::part_export);
10 $me = '[FS::part_export::netsapiens]';
11
12 tie my %options, 'Tie::IxHash',
13   'login'           => { label=>'NetSapiens tac2 User API username' },
14   'password'        => { label=>'NetSapiens tac2 User API password' },
15   'url'             => { label=>'NetSapiens tac2 User URL' },
16   'device_login'    => { label=>'NetSapiens tac2 Device API username' },
17   'device_password' => { label=>'NetSapiens tac2 Device API password' },
18   'device_url'      => { label=>'NetSapiens tac2 Device URL' },
19   'domain'          => { label=>'NetSapiens Domain' },
20   'debug'           => { label=>'Enable debugging', type=>'checkbox' },
21 ;
22
23 %info = (
24   'svc'      => 'svc_phone',
25   'desc'     => 'Provision phone numbers to NetSapiens',
26   'options'  => \%options,
27   'notes'    => <<'END'
28 Requires installation of
29 <a href="http://search.cpan.org/dist/REST-Client">REST::Client</a>
30 from CPAN.
31 END
32 );
33
34 sub rebless { shift; }
35
36 sub ns_command {
37   my $self = shift;
38   $self->_ns_command('', @_);
39 }
40
41 sub ns_device_command { 
42   my $self = shift;
43   $self->_ns_command('device_', @_);
44 }
45
46 sub _ns_command {
47   my( $self, $prefix, $method, $command ) = splice(@_,0,4);
48
49   eval 'use REST::Client';
50   die $@ if $@;
51
52   my $ns = new REST::Client 'host'=>$self->option($prefix.'url');
53
54   my @args = ( $command );
55
56   if ( $method eq 'PUT' ) {
57     my $content = $ns->buildQuery( { @_ } );
58     $content =~ s/^\?//;
59     push @args, $content;
60   } elsif ( $method eq 'GET' ) {
61     $args[0] .= $ns->buildQuery( { @_ } );
62   }
63
64   warn "$me $method ". $self->option($prefix.'url').
65        " $command ". join(', ', @_). "\n"
66     if $self->option('debug');
67
68   my $auth = encode_base64( $self->option($prefix.'login'). ':'.
69                             $self->option($prefix.'password')    );
70   push @args, { 'Authorization' => "Basic $auth" };
71
72   $ns->$method( @args );
73   $ns;
74 }
75
76 sub ns_subscriber {
77   my($self, $svc_phone) = (shift, shift);
78
79   my $domain = $self->option('domain');
80   my $phonenum = $svc_phone->phonenum;
81
82   "/domains_config/$domain/subscriber_config/$phonenum";
83 }
84
85 sub ns_registrar {
86   my($self, $svc_phone) = (shift, shift);
87
88   my $domain = $self->option('domain');
89   my $countrycode = $svc_phone->countrycode;
90   my $phonenum    = $svc_phone->phonenum;
91
92   $self->ns_subscriber($svc_phone).
93     '/registrar_config/'. $self->ns_devicename($svc_phone);
94 }
95
96 sub ns_devicename {
97   my( $self, $svc_phone ) = (shift, shift);
98
99   my $domain = $self->option('domain');
100   my $countrycode = $svc_phone->countrycode;
101   my $phonenum    = $svc_phone->phonenum;
102
103   "sip:$countrycode$phonenum@$domain";
104 }
105
106 sub ns_dialplan {
107   my($self, $svc_phone) = (shift, shift);
108
109   my $countrycode = $svc_phone->countrycode;
110   my $phonenum    = $svc_phone->phonenum;
111
112   "/dialplans/DID+Table/dialplan_config/sip:$countrycode$phonenum@*"
113 }
114
115 sub ns_device {
116   my($self, $svc_phone, $phone_device ) = (shift, shift, shift);
117
118   #my $countrycode = $svc_phone->countrycode;
119   #my $phonenum    = $svc_phone->phonenum;
120
121   "/phones_config/". $phone_device->mac_addr;
122 }
123
124 sub ns_create_or_update {
125   my($self, $svc_phone, $dial_policy) = (shift, shift, shift);
126
127   my $domain = $self->option('domain');
128   my $countrycode = $svc_phone->countrycode;
129   my $phonenum    = $svc_phone->phonenum;
130
131   my( $firstname, $lastname );
132   if ( $svc_phone->phone_name =~ /^\s*(\S+)\s+(\S.*\S)\s*$/ ) {
133     $firstname = $1;
134     $lastname  = $2;
135   } else {
136     #deal w/unaudited netsapiens services?
137     my $cust_main = $svc_phone->cust_svc->cust_pkg->cust_main;
138     $firstname = $cust_main->get('first');
139     $lastname  = $cust_main->get('last');
140   }
141
142   # Piece 1 (already done) - User creation
143
144   my $ns = $self->ns_command( 'PUT', $self->ns_subscriber($svc_phone), 
145     'subscriber_login' => $phonenum.'@'.$domain,
146     'firstname'        => $firstname,
147     'lastname'         => $lastname,
148     'subscriber_pin'   => $svc_phone->pin,
149     'dial_plan'        => 'Default', #config?
150     'dial_policy'      => $dial_policy,
151   );
152
153   if ( $ns->responseCode !~ /^2/ ) {
154      return $ns->responseCode. ' '.
155             join(', ', $self->ns_parse_response( $ns->responseContent ) );
156   }
157
158   #Piece 2 - sip device creation 
159
160   my $ns2 = $self->ns_command( 'PUT', $self->ns_registrar($svc_phone),
161   );
162
163   if ( $ns2->responseCode !~ /^2/ ) {
164      return $ns2->responseCode. ' '.
165             join(', ', $self->ns_parse_response( $ns2->responseContent ) );
166   }
167
168   #Piece 3 - DID mapping to user
169
170   my $ns3 = $self->ns_command( 'PUT', $self->ns_dialplan($svc_phone),
171     'to_user' => $countrycode.$phonenum,
172     'to_host' => $domain,
173   );
174
175   if ( $ns3->responseCode !~ /^2/ ) {
176      return $ns3->responseCode. ' '.
177             join(', ', $self->ns_parse_response( $ns3->responseContent ) );
178   }
179
180   '';
181 }
182
183 sub ns_delete {
184   my($self, $svc_phone) = (shift, shift);
185
186   my $ns = $self->ns_command( 'DELETE', $self->ns_subscriber($svc_phone) );
187
188   #delete other things?
189
190   if ( $ns->responseCode !~ /^2/ ) {
191      return $ns->responseCode. ' '.
192             join(', ', $self->ns_parse_response( $ns->responseContent ) );
193   }
194
195   '';
196
197 }
198
199 sub ns_parse_response {
200   my( $self, $content ) = ( shift, shift );
201
202   #try to screen-scrape something useful
203   tie my %hash, Tie::IxHash;
204   while ( $content =~ s/^.*?<p>\s*<b>(.+?)<\/b>\s*(.+?)\s*<\/p>//is ) {
205     ( $hash{$1} = $2 ) =~ s/^\s*<(\w+)>(.+?)<\/\1>/$2/is;
206   }
207
208   %hash;
209 }
210
211 sub _export_insert {
212   my($self, $svc_phone) = (shift, shift);
213   $self->ns_create_or_update($svc_phone, 'Permit All');
214 }
215
216 sub _export_replace {
217   my( $self, $new, $old ) = (shift, shift, shift);
218   return "can't change phonenum with NetSapiens (unprovision and reprovision?)"
219     if $old->phonenum ne $new->phonenum;
220   $self->_export_insert($new);
221 }
222
223 sub _export_delete {
224   my( $self, $svc_phone ) = (shift, shift);
225
226   $self->ns_delete($svc_phone);
227 }
228
229 sub _export_suspend {
230   my( $self, $svc_phone ) = (shift, shift);
231   $self->ns_create_or_update($svc_phone, 'Deny');
232 }
233
234 sub _export_unsuspend {
235   my( $self, $svc_phone ) = (shift, shift);
236   #$self->ns_create_or_update($svc_phone, 'Permit All');
237   $self->_export_insert($svc_phone);
238 }
239
240 sub export_device_insert {
241   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
242
243   #my $domain = $self->option('domain');
244   my $countrycode = $svc_phone->countrycode;
245   my $phonenum    = $svc_phone->phonenum;
246
247   my $device = $self->ns_devicename($svc_phone);
248
249   my $ns = $self->ns_device_command(
250     'PUT', $self->ns_device($svc_phone, $phone_device),
251       'line1_enable' => 'yes',
252       'device1'      => $self->ns_devicename($svc_phone),
253       'line1_ext'    => $countrycode.$phonenum,
254 ,
255       #'line2_enable' => 'yes',
256       #'device2'      =>
257       #'line2_ext'    =>
258
259       #'notes' => 
260       'server'       => 'SiPbx',
261       'domain'       => $self->option('domain'),
262
263       'brand'        => $phone_device->part_device->devicename,
264       
265   );
266
267   if ( $ns->responseCode !~ /^2/ ) {
268      return $ns->responseCode. ' '.
269             join(', ', $self->ns_parse_response( $ns->responseContent ) );
270   }
271
272   '';
273
274 }
275
276 sub export_device_delete {
277   my( $self, $svc_phone, $phone_device ) = (shift, shift, shift);
278
279   my $ns = $self->ns_device_command(
280     'DELETE', $self->ns_device($svc_phone, $phone_device),
281   );
282
283   if ( $ns->responseCode !~ /^2/ ) {
284      return $ns->responseCode. ' '.
285             join(', ', $self->ns_parse_response( $ns->responseContent ) );
286   }
287
288   '';
289
290 }
291
292
293 sub export_device_replace {
294   my( $self, $svc_phone, $new_phone_device, $old_phone_device ) =
295     (shift, shift, shift, shift);
296
297   #?
298   $self->export_device_insert( $svc_phone, $new_phone_device );
299
300 }
301
302 sub export_links {
303   my($self, $svc_phone, $arrayref) = (shift, shift, shift);
304   #push @$arrayref, qq!<A HREF="http://example.com/~!. $svc_phone->username.
305   #                 qq!">!. $svc_phone->username. qq!</A>!;
306   '';
307 }
308
309 1;