summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/prizm.pm
blob: 2d4d8584c3091c0f6f89c05391fb2662b30e4632 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package FS::part_export::prizm;

use vars qw(@ISA %info %options $DEBUG);
use Tie::IxHash;
use FS::Record qw(fields dbh);
use FS::part_export;

@ISA = qw(FS::part_export);
$DEBUG = 1;

tie %options, 'Tie::IxHash',
  'url'      => { label => 'Northbound url', default=>'https://localhost:8443/prizm/nbi' },
  'user'     => { label => 'Northbound username', default=>'nbi' },
  'password' => { label => 'Password', default => '' },
  'ems'      => { label => 'Full EMS', type => 'checkbox' },
  'always_bam' => { label => 'Always activate/suspend authentication', type => 'checkbox' },
  'element_name_length' => { label => 'Size of siteName (best left blank)' },
;

my $notes = <<'EOT';
Real-time export of <b>svc_broadband</b>, <b>cust_pkg</b>, and <b>cust_main</b>
record data to Motorola
<a href="http://motorola.canopywireless.com/products/prizm/">Canopy Prizm
software</a> via the Northbound interface.<br><br>

Freeside will attempt to create an element in an existing network with the
values provided in svc_broadband.  Of particular interest are
<ul>
  <li> mac address - used to identify the element
  <li> vlan profile - an exact match for a vlan profiles defined in prizm
  <li> ip address - defines the management ip address of the prizm element
  <li> latitude - GPS latitude
  <li> longitude - GPS longitude
  <li> altitude - GPS altitude
</ul>

In addition freeside attempts to set the service plan name in prizm to the
name of the package in which the service resides.

The service is associated with a customer in prizm as well, and freeside
will create the customer should none already exist with import id matching
the freeside customer number.  The following fields are set.

<ul>
  <li> importId - the freeside customer number
  <li> customerType - freeside
  <li> customerName - the name associated with the freeside shipping address
  <li> address1 - the shipping address
  <li> address2
  <li> city
  <li> state
  <li> zipCode
  <li> country
  <li> workPhone - the daytime phone number
  <li> homePhone - the night phone number
  <li> freesideId - the freeside customer number
</ul>

  Additionally set on the element are
<ul>
  <li> Site Name - The shipping name followed by the service broadband description field
  <li> Site Location - the shipping address
  <li> Site Contact - the daytime and night phone numbers
</ul>

Freeside provisions, suspends, and unsuspends elements BAM only unless the
'Full EMS' checkbox is checked.<br><br>

When freeside provisions an element the siteName is copied internally by
prizm in such a manner that it is possible for the value to exceed the size
of the column used in the prizm database.  Therefore freeside truncates
by default this value to 50 characters.  It is thought that this
column is the account_name column of the element_user_account table.  It
may be possible to lift this limit by modifying the prizm database and
setting a new appropriate value on this export.  This is untested and
possibly harmful.

EOT

%info = (
  'svc'      => 'svc_broadband',
  'desc'     => 'Real-time export to Northbound Interface',
  'options'  => \%options,
  'nodomain' => 'Y',
  'notes'    => $notes,
);

sub prizm_command {
  my ($self,$namespace,$method) = (shift,shift,shift);

  eval "use Net::Prizm 0.04 qw(CustomerInfo PrizmElement);";
  die $@ if $@;

  my $prizm = new Net::Prizm (
    namespace => $namespace,
    url => $self->option('url'),
    user => $self->option('user'),
    password => $self->option('password'),
  );
  
  $prizm->$method(@_);
}

sub queued_prizm_command {  # subroutine
  my( $url, $user, $password, $namespace, $method, @args ) = @_;

  eval "use Net::Prizm 0.04 qw(CustomerInfo PrizmElement);";
  die $@ if $@;

  my $prizm = new Net::Prizm (
    namespace => $namespace,
    url => $url,
    user => $user,
    password => $password,
  );
  
  $err_or_som = $prizm->$method( @args);

  die $err_or_som
    unless ref($err_or_som);

  '';

}

sub _export_insert {
  my( $self, $svc ) = ( shift, shift );

  my $cust_main = $svc->cust_svc->cust_pkg->cust_main;

  my $err_or_som = $self->prizm_command('CustomerIfService', 'getCustomers',
                                        ['import_id'],
                                        [$cust_main->custnum],
                                        ['='],
                                       );
  return $err_or_som
    unless ref($err_or_som);

  my $pre = '';
  if ( defined $cust_main->dbdef_table->column('ship_last') ) {
    $pre = $cust_main->ship_last ? 'ship_' : '';
  }
  my $name = $pre ? $cust_main->ship_name : $cust_main->name;
  my $location = join(" ", map { my $method = "$pre$_"; $cust_main->$method }
                           qw (address1 address2 city state zip)
                     );
  my $contact = join(" ", map { my $method = "$pre$_"; $cust_main->$method }
                          qw (daytime night)
                     );

  my $pcustomer;
  if ($err_or_som->result->[0]) {
    $pcustomer = $err_or_som->result->[0]->customerId;
  }else{
    my $chashref = $cust_main->hashref;
    my $customerinfo = {
      importId         => $cust_main->custnum,
      customerName     => $name,
      customerType     => 'freeside',
      address1         => $chashref->{"${pre}address1"},
      address2         => $chashref->{"${pre}address2"},
      city             => $chashref->{"${pre}city"},
      state            => $chashref->{"${pre}state"},
      zipCode          => $chashref->{"${pre}zip"},
      workPhone        => $chashref->{"${pre}daytime"},
      homePhone        => $chashref->{"${pre}night"},
      email            => @{[$cust_main->invoicing_list_emailonly]}[0],
      extraFieldNames  => [ 'country', 'freesideId',
                          ],
      extraFieldValues => [ $chashref->{"${pre}country"}, $cust_main->custnum,
                          ],
    };

    $err_or_som = $self->prizm_command('CustomerIfService', 'addCustomer',
                                       $customerinfo);
    return $err_or_som
      unless ref($err_or_som);

    $pcustomer = $err_or_som->result;
  }
  warn "multiple prizm customers found for $cust_main->custnum"
    if scalar(@$pcustomer) > 1;

#  #kinda big question/expensive
#  $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
#                                     ['Network Default Gateway Address'],
#                                     [$svc->addr_block->ip_gateway],
#                                     ['='],
#                   );
#  return $err_or_som
#    unless ref($err_or_som);
#
#  return "No elements in network" unless exists $err_or_som->result->[0];

  my $networkid = 0;
#  for (my $i = 0; $i < $err_or_som->result->[0]->attributeNames; $i++) {
#    if ($err_or_som->result->[0]->attributeNames->[$i] eq "Network.ID"){
#      $networkid = $err_or_som->result->[0]->attributeValues->[$i];
#      last;
#    }
#  }

  my $element_name_length = 50;
  $element_name_length = $1
    if $self->option('element_name_length') =~ /^\s*(\d+)\s*$/;
  $err_or_som = $self->prizm_command('NetworkIfService', 'addProvisionedElement',
                                      $networkid,
                                      $svc->mac_addr,
                                      substr($name . " " . $svc->description,
                                             0, $element_name_length),
                                      $location,
                                      $contact,
                                      sprintf("%032X", $svc->authkey),
                                      $svc->cust_svc->cust_pkg->part_pkg->pkg,
                                      $svc->vlan_profile,
                                      ($self->option('ems') ? 1 : 0 ),
                                     );
  return $err_or_som
    unless ref($err_or_som);

  my (@names) = ('Management IP',
                 'GPS Latitude',
                 'GPS Longitude',
                 'GPS Altitude',
                 'Site Name',
                 'Site Location',
                 'Site Contact',
                 );
  my (@values) = ($svc->ip_addr,
                  $svc->latitude,
                  $svc->longitude,
                  $svc->altitude,
                  $name . " " . $svc->description,
                  $location,
                  $contact,
                  );
  $element = $err_or_som->result->elementId;
  $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfig',
                                     [ $element ],
                                     \@names,
                                     \@values,
                                     0,
                                     1,
                                    );
  return $err_or_som
    unless ref($err_or_som);

  $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
                                     [ $element ],
                                     $svc->vlan_profile,
                                     0,
                                     1,
                                    );
  return $err_or_som
    unless ref($err_or_som);

  $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
                                     [ $element ],
                                     $svc->cust_svc->cust_pkg->part_pkg->pkg,
                                     0,
                                     1,
                                    );
  return $err_or_som
    unless ref($err_or_som);

  $err_or_som = $self->prizm_command('NetworkIfService',
                                     'activateNetworkElements',
                                     [ $element ],
                                     1,
                                     ( $self->option('ems') ? 1 : 0 ),
                                    );

  return $err_or_som
    unless ref($err_or_som);

  $err_or_som = $self->prizm_command('CustomerIfService',
                                     'addElementToCustomer',
                                     0,
                                     $cust_main->custnum,
                                     0,
                                     $svc->mac_addr,
                                    );

  return $err_or_som
    unless ref($err_or_som);

  '';
}

sub _export_delete {
  my( $self, $svc ) = ( shift, shift );

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  my $cust_pkg = $svc->cust_svc->cust_pkg;

  my $depend = [];

  if ($cust_pkg) {
    my $queue = new FS::queue {
      'svcnum' => $svc->svcnum,
      'job'    => 'FS::part_export::prizm::queued_prizm_command',
    };
    my $error = $queue->insert(
      ( map { $self->option($_) }
            qw( url user password ) ),
      'CustomerIfService',
      'removeElementFromCustomer',
      0,
      $cust_pkg->custnum,
      0,
      $svc->mac_addr,
    );

    if ($error) {
      $dbh->rollback if $oldAutoCommit;
      return $error;
    }

    push @$depend, $queue->jobnum;
  }

  my $err_or_queue =
    $self->queue_statuschange('deleteElement', $depend, $svc, 1);

  unless (ref($err_or_queue)) {
    $dbh->rollback if $oldAutoCommit;
    return $err_or_queue;
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;

  '';
}

sub _export_replace {
  my( $self, $new, $old ) = ( shift, shift, shift );

  my $err_or_som = $self->prizm_command('NetworkIfService', 'getPrizmElements',
                                        [ 'MAC Address' ],
                                        [ $old->mac_addr ],
                                        [ '=' ],
                                       );
  return $err_or_som
    unless ref($err_or_som);

  return "Can't find prizm element for " . $old->mac_addr
    unless $err_or_som->result->[0];

  my %freeside2prizm = (  mac_addr     => 'MAC Address',
                          ip_addr      => 'Management IP',
                          latitude     => 'GPS Latitude',
                          longitude    => 'GPS Longitude',
                          altitude     => 'GPS Altitude',
                          authkey      => 'Authentication Key',
                       );
  
  my (@values);
  my (@names) = map { push @values, $new->$_; $freeside2prizm{$_} }
    grep { $old->$_ ne $new->$_ }
      grep { exists($freeside2prizm{$_}) }
        fields( 'svc_broadband' );

  if ($old->description ne $new->description) {
    my $cust_main = $old->cust_svc->cust_pkg->cust_main;
    my $name = defined($cust_main->dbdef_table->column('ship_last'))
             ? $cust_main->ship_name
             : $cust_main->name;
    push @values, $name . " " . $new->description;
    push @names, "Site Name";
  }

  my $element = $err_or_som->result->[0]->elementId;

  $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfig',
                                        [ $element ],
                                        \@names,
                                        \@values,
                                        0,
                                        1,
                                       );
  return $err_or_som
    unless ref($err_or_som);

  $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
                                     [ $element ],
                                     $new->vlan_profile,
                                     0,
                                     1,
                                    )
    if $old->vlan_profile ne $new->vlan_profile;

  return $err_or_som
    unless ref($err_or_som);

  $err_or_som = $self->prizm_command('NetworkIfService', 'setElementConfigSet',
                                     [ $element ],
                                     $new->cust_svc->cust_pkg->part_pkg->pkg,
                                     0,
                                     1,
                                    );
  return $err_or_som
    unless ref($err_or_som);

  '';

}

sub _export_suspend {
  my( $self, $svc ) = ( shift, shift );
  my $depend = [];
  my $ems = $self->option('ems') ? 1 : 0;
  my $err_or_queue = '';

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  $err_or_queue = 
     $self->queue_statuschange('suspendNetworkElements', [], $svc, 1, $ems);
  unless (ref($err_or_queue)) {
    $dbh->rollback if $oldAutoCommit;
    return $err_or_queue;
  }
  push @$depend, $err_or_queue->jobnum;

  if ($ems && $self->option('always_bam')) {
    $err_or_queue =
      $self->queue_statuschange('suspendNetworkElements', $depend, $svc, 1, 0);
    unless (ref($err_or_queue)) {
      $dbh->rollback if $oldAutoCommit;
      return $err_or_queue;
    }
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;

  '';
}

sub _export_unsuspend {
  my( $self, $svc ) = ( shift, shift );
  my $depend = [];
  my $ems = $self->option('ems') ? 1 : 0;
  my $err_or_queue = '';

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  if ($ems && $self->option('always_bam')) {
    $err_or_queue =
      $self->queue_statuschange('activateNetworkElements', [], $svc, 1, 0);
    unless (ref($err_or_queue)) {
      $dbh->rollback if $oldAutoCommit;
      return $err_or_queue;
    }
    push @$depend, $err_or_queue->jobnum;
  }

  $err_or_queue =
    $self->queue_statuschange('activateNetworkElements', $depend, $svc, 1, $ems);
  unless (ref($err_or_queue)) {
    $dbh->rollback if $oldAutoCommit;
    return $err_or_queue;
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;

  '';
}

sub export_links {
  my( $self, $svc, $arrayref ) = ( shift, shift, shift );

  push @$arrayref, '<A HREF="http://'. $svc->ip_addr. '">SM</A>';

  '';
}

sub queue_statuschange {
  my( $self, $method, $jobs, $svc, @args ) = @_;

  # already in a transaction and can't die here

  my $queue = new FS::queue {
    'svcnum' => $svc->svcnum,
    'job'    => 'FS::part_export::prizm::statuschange',
  };
  my $error = $queue->insert(
    ( map { $self->option($_) }
          qw( url user password ) ),
    $method,
    $svc->mac_addr,
    @args,
  );

  unless ($error) {                   # successful insertion
    foreach my $job ( @$jobs ) {
      $error ||= $queue->depend_insert($job);
    }
  }

  $error or $queue;
}

sub statuschange {  # subroutine
  my( $url, $user, $password, $method, $mac_addr, @args) = @_;

  eval "use Net::Prizm 0.04 qw(CustomerInfo PrizmElement);";
  die $@ if $@;

  my $prizm = new Net::Prizm (
    namespace => 'NetworkIfService',
    url => $url,
    user => $user,
    password => $password,
  );
  
  my $err_or_som = $prizm->getPrizmElements( [ 'MAC Address' ],
                                             [ $mac_addr ],
                                             [ '=' ],
                                           );
  die $err_or_som
    unless ref($err_or_som);

  die "Can't find prizm element for " . $mac_addr
    unless $err_or_som->result->[0];

  my $arg1;
  # yuck!
  if ($method =~ /suspendNetworkElements/ || $method =~ /activateNetworkElements/) {
    $arg1 = [ $err_or_som->result->[0]->elementId ];
  }else{
    $arg1 = $err_or_som->result->[0]->elementId;
  }
  $err_or_som = $prizm->$method( $arg1, @args );

  die $err_or_som
    unless ref($err_or_som);

  '';

}


1;