ca02b8f3ee4aebc65d16df10b04cdc796ee768d7
[freeside.git] / FS / FS / part_export / prizm.pm
1 package FS::part_export::prizm;
2
3 use vars qw(@ISA %info %options $DEBUG);
4 use Tie::IxHash;
5 use FS::Record qw(fields);
6 use FS::part_export;
7
8 @ISA = qw(FS::part_export);
9 $DEBUG = 1;
10
11 tie %options, 'Tie::IxHash',
12   'url'      => { label => 'Northbound url', default=>'https://localhost:8443/prizm/nbi' },
13   'user'     => { label => 'Northbound username', default=>'nbi' },
14   'password' => { label => 'Password', default => '' },
15 ;
16
17 %info = (
18   'svc'      => 'svc_broadband',
19   'desc'     => 'Real-time export to Northbound Interface',
20   'options'  => \%options,
21   'nodomain' => 'Y',
22   'notes'    => 'These are notes.'
23 );
24
25 sub prizm_command {
26   my ($self,$namespace,$method) = (shift,shift,shift);
27
28   eval "use Net::Prizm qw(CustomerInfo PrizmElement);";
29   die $@ if $@;
30
31   my $prizm = new Net::Prizm (
32     namespace => $namespace,
33     url => $self->option('url'),
34     user => $self->option('user'),
35     password => $self->option('password'),
36   );
37   
38   $prizm->$method(@_);
39 }
40
41 sub _export_insert {
42   my( $self, $svc ) = ( shift, shift );
43
44   my $cust_main = $svc->cust_svc->cust_pkg->cust_main;
45
46   my $err_or_som = $self->prizm_command('CustomerIfService', 'getCustomers',
47                                         ['import_id'],
48                                         [$cust_main->custnum],
49                                         ['='],
50                                        );
51   return $err_or_som
52     unless ref($err_or_som);
53
54   my $pre = '';
55   if ( defined $cust_main->dbdef_table->column('ship_last') ) {
56     $pre = $cust_main->ship_last ? 'ship_' : '';
57   }
58   my $name = $pre ? $cust_main->ship_name : $cust_main->name;
59   my $location = join(" ", map { my $method = "$pre$_"; $cust_main->$method }
60                            qw (address1 address2 city state zip)
61                      );
62   my $contact = join(" ", map { my $method = "$pre$_"; $cust_main->$method }
63                           qw (daytime night)
64                      );
65
66   my $pcustomer;
67   if ($err_or_som->result->[0]) {
68     $pcustomer = $err_or_som->result->[0]->customerId;
69   }else{
70     my $chashref = $cust_main->hashref;
71     my $customerinfo = {
72       importId         => $cust_main->custnum,
73       customerName     => $name,
74       customerType     => 'freeside',
75       address1         => $chashref->{"${pre}address1"},
76       address2         => $chashref->{"${pre}address2"},
77       city             => $chashref->{"${pre}city"},
78       state            => $chashref->{"${pre}state"},
79       zipCode          => $chashref->{"${pre}zip"},
80       workPhone        => $chashref->{"${pre}daytime"},
81       homePhone        => $chashref->{"${pre}night"},
82       email            => @{[$cust_main->invoicing_list_emailonly]}[0],
83       extraFieldNames  => [ 'country', 'freesideId',
84                           ],
85       extraFieldValues => [ $chashref->{"${pre}country"}, $cust_main->custnum,
86                           ],
87     };
88
89     $err_or_som = $self->prizm_command('CustomerIfService', 'addCustomer',
90                                        $customerinfo);
91     return $err_or_som
92       unless ref($err_or_som);
93
94     $pcustomer = $err_or_som->result;
95   }
96   warn "multiple prizm customers found for $cust_main->custnum"
97     if scalar(@$pcustomer) > 1;
98
99   #kinda big question/expensive
100   $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
101                                      ['Network Default Gateway Address'],
102                                      [$svc->addr_block->ip_gateway],
103                                      ['='],
104                    );
105   return $err_or_som
106     unless ref($err_or_som);
107
108   return "No elements in network" unless exists $err_or_som->result->[0];
109
110   my $networkid = 0;
111   for (my $i = 0; $i < $err_or_som->result->[0]->attributeNames; $i++) {
112     if ($err_or_som->result->[0]->attributeNames->[$i] eq "Network.ID"){
113       $networkid = $err_or_som->result->[0]->attributeValues->[$i];
114       last;
115     }
116   }
117
118   $err_or_som = $self->prizm_command('NetworkIfService', 'addProvisionedElement',
119                                       $networkid,
120                                       $svc->mac_addr,
121                                       $name, # we fix this below (bug in prizm?)
122                                       $location,
123                                       $contact,
124                                       sprintf("%032X", $svc->authkey),
125                                       $svc->cust_svc->cust_pkg->part_pkg->pkg,
126                                       $svc->vlan_profile,
127                                       1,
128                                      );
129   return $err_or_som
130     unless ref($err_or_som);
131
132   my (@names) = ('Management IP',
133                  'GPS Latitude',
134                  'GPS Longitude',
135                  'GPS Altitude',
136                  'Site Name',
137                  'Site Location',
138                  'Site Contact',
139                  );
140   my (@values) = ($svc->ip_addr,
141                   $svc->latitude,
142                   $svc->longitude,
143                   $svc->altitude,
144                   $name . " " . $svc->description,
145                   $location,
146                   $contact,
147                   );
148   $element = $err_or_som->result->elementId;
149   $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfig',
150                                      [ $element ],
151                                      \@names,
152                                      \@values,
153                                      0,
154                                      1,
155                                     );
156   return $err_or_som
157     unless ref($err_or_som);
158
159   $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
160                                      [ $element ],
161                                      $svc->vlan_profile,
162                                      0,
163                                      1,
164                                     );
165   return $err_or_som
166     unless ref($err_or_som);
167
168   $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
169                                      [ $element ],
170                                      $svc->cust_svc->cust_pkg->part_pkg->pkg,
171                                      0,
172                                      1,
173                                     );
174   return $err_or_som
175     unless ref($err_or_som);
176
177   $err_or_som = $self->prizm_command('NetworkIfService',
178                                      'activateNetworkElements',
179                                      [ $element ],
180                                      1,
181                                      1,
182                                     );
183
184   return $err_or_som
185     unless ref($err_or_som);
186
187   $err_or_som = $self->prizm_command('CustomerIfService',
188                                      'addElementToCustomer',
189                                      0,
190                                      $cust_main->custnum,
191                                      0,
192                                      $svc->mac_addr,
193                                     );
194
195   return $err_or_som
196     unless ref($err_or_som);
197
198   '';
199 }
200
201 sub _export_delete {
202   my( $self, $svc ) = ( shift, shift );
203
204   my $custnum = $svc->cust_svc->cust_pkg->cust_main->custnum;
205
206   my $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
207                                         ['MAC Address'],
208                                         [$svc->mac_addr],
209                                         ['='],
210                                        );
211   return $err_or_som
212     unless ref($err_or_som);
213
214   return "Can't find prizm element for " . $svc->mac_addr
215     unless $err_or_som->result->[0];
216
217   $err_or_som = $self->prizm_command('NetworkIfService',
218                                      'suspendNetworkElements',
219                                      [$err_or_som->result->[0]->elementId],
220                                      1,
221                                      1,
222                                     );
223
224   return $err_or_som
225     unless ref($err_or_som);
226
227   $err_or_som = $self->prizm_command('CustomerIfService',
228                                      'removeElementFromCustomer',
229                                      0,
230                                      $custnum,
231                                      0,
232                                      $svc->mac_addr,
233                                     );
234
235   return $err_or_som
236     unless ref($err_or_som);
237
238   '';
239 }
240
241 sub _export_replace {
242   my( $self, $new, $old ) = ( shift, shift, shift );
243
244   my $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
245                                         [ 'MAC Address' ],
246                                         [ $old->mac_addr ],
247                                         [ '=' ],
248                                        );
249   return $err_or_som
250     unless ref($err_or_som);
251
252   return "Can't find prizm element for " . $old->mac_addr
253     unless $err_or_som->result->[0];
254
255   my %freeside2prizm = (  mac_addr     => 'MAC Address',
256                           ip_addr      => 'Management IP',
257                           latitude     => 'GPS Latitude',
258                           longitude    => 'GPS Longitude',
259                           altitude     => 'GPS Altitude',
260                           authkey      => 'Authentication Key',
261                        );
262   
263   my (@values);
264   my (@names) = map { push @values, $new->$_; $freeside2prizm{$_} }
265     grep { $old->$_ ne $new->$_ }
266       grep { exists($freeside2prizm{$_}) }
267         fields( 'svc_broadband' );
268
269   if ($old->description ne $new->description) {
270     my $cust_main = $old->cust_svc->cust_pkg->cust_main;
271     my $name = defined($cust_main->dbdef_table->column('ship_last'))
272              ? $cust_main->ship_name
273              : $cust_main->name;
274     push @values, $name . " " . $new->description;
275     push @names, "Site Name";
276   }
277
278   my $element = $err_or_som->result->[0]->elementId;
279
280   $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfig',
281                                         [ $element ],
282                                         \@names,
283                                         \@values,
284                                         0,
285                                         1,
286                                        );
287   return $err_or_som
288     unless ref($err_or_som);
289
290   $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
291                                      [ $element ],
292                                      $new->vlan_profile,
293                                      0,
294                                      1,
295                                     )
296     if $old->vlan_profile ne $new->vlan_profile;
297
298   return $err_or_som
299     unless ref($err_or_som);
300
301   '';
302
303 }
304
305 sub _export_suspend {
306   my( $self, $svc ) = ( shift, shift );
307
308   my $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
309                                         [ 'MAC Address' ],
310                                         [ $svc->mac_addr ],
311                                         [ '=' ],
312                                        );
313   return $err_or_som
314     unless ref($err_or_som);
315
316   return "Can't find prizm element for " . $svc->mac_addr
317     unless $err_or_som->result->[0];
318
319   $err_or_som = $self->prizm_command('NetworkIfService',
320                                      'suspendNetworkElements',
321                                      [ $err_or_som->result->[0]->elementId ],
322                                      1,
323                                      1,
324                                     );
325
326   return $err_or_som
327     unless ref($err_or_som);
328
329   '';
330
331 }
332
333 sub _export_unsuspend {
334   my( $self, $svc ) = ( shift, shift );
335
336   my $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
337                                         [ 'MAC Address' ],
338                                         [ $svc->mac_addr ],
339                                         [ '=' ],
340                                        );
341   return $err_or_som
342     unless ref($err_or_som);
343
344   return "Can't find prizm element for " . $svc->mac_addr
345     unless $err_or_som->result->[0];
346
347   $err_or_som = $self->prizm_command('NetworkIfService',
348                                      'activateNetworkElements',
349                                      [ $err_or_som->result->[0]->elementId ],
350                                      1,
351                                      1,
352                                     );
353
354   return $err_or_som
355     unless ref($err_or_som);
356
357   '';
358
359 }
360
361 1;