summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/soma.pm
blob: c73d9f978587155a2bb3be37efb618256376340a (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
package FS::part_export::soma;

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 => 'Soma OSS-API url', default=>'https://localhost:8088/ossapi/services' },
  'data_app_id' => { label => 'SOMA Data Application Id', default => '' },
;

my $notes = <<'EOT';
Real-time export of <b>svc_external</b> and <b>svc_broadband</b> record data
to SOMA Networks <a href="http://www.somanetworks.com">platform</a> via the
OSS-API.<br><br>

Freeside will attempt to create/delete a cpe for the ESN provided in
svc_external.  If a data application id is provided then freeside will
use the values provided in svc_broadband to manage the attributes and 
features of that cpe.

EOT

%info = (
  'svc'      => [ qw ( svc_broadband svc_external ) ],
  'desc'     => 'Real-time export to SOMA platform',
  'options'  => \%options,
  'nodomain' => 'Y',
  'notes'    => $notes,
);

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

  warn "_export_insert called for service ". $svc->svcnum
    if $DEBUG;

  my %args = ( url => $self->option('url'), method => '_queueable_insert' );

  $args{esn} = $self->esn($svc) or return 'No ESN found!';

  my $svcdb = $svc->cust_svc->part_svc->svcdb;
  $args{svcdb} = $svcdb;
  if ( $svcdb eq 'svc_external' ) {
    #do nothing
  } elsif ( $svcdb eq 'svc_broadband' ){
    $args{data_app_id} = $self->option('data_app_id')
  } else {
    return "Don't know how to provision $svcdb";
  }

  warn "dispatching statuschange" if $DEBUG;

  eval { statuschange(%args) };
  return $@ if $@;

  '';
}

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

  my %args = ( url => $self->option('url'), method => '_queueable_delete' );

  $args{esn} = $self->esn($svc) or return 'No ESN found!';

  my $svcdb = $svc->cust_svc->part_svc->svcdb;
  $args{svcdb} = $svcdb;
  if ( $svcdb eq 'svc_external' ) {
    #do nothing
  } elsif ( $svcdb eq 'svc_broadband' ){
    $args{data_app_id} = $self->option('data_app_id')
  } else {
    return "Don't know how to provision $svcdb";
  }

  eval { statuschange(%args) };
  return $@ if $@;

  '';
}

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

  my %args = ( url => $self->option('url'), method => '_queueable_replace' );

  $args{esn}     = $self->esn($old) or return 'No old ESN found!';
  $args{new_esn} = $self->esn($new) or return 'No new ESN found!';

  my $svcdb = $old->cust_svc->part_svc->svcdb;
  $args{svcdb} = $svcdb;
  if ( $svcdb eq 'svc_external' ) {
    #do nothing
  } elsif ( $svcdb eq 'svc_broadband' ){
    $args{data_app_id} = $self->option('data_app_id')
  } else {
    return "Don't know how to provision $svcdb";
  }

  eval { statuschange(%args) };
  return $@ if $@;

  '';
}

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

  $self->queue_statuschange('_queueable_suspend', $svc);
}

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

  $self->queue_statuschange('_queueable_unsuspend', $svc);
}

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

  my %args = ( url => $self->option('url'), method => $method );

  my $svcdb = $svc->cust_svc->part_svc->svcdb;
  $args{svcdb} = $svcdb;
  if ( $svcdb eq 'svc_external' ) {
    #do absolutely nothing
    return '';
  } elsif ( $svcdb eq 'svc_broadband' ){
    $args{data_app_id} = $self->option('data_app_id')
  } else {
    return "Don't know how to provision $svcdb";
  }

  $args{esn} = $self->esn($svc);

  my $queue = new FS::queue {
    'svcnum' => $svc->svcnum,
    'job'    => 'FS::part_export::soma::statuschange',
  };
  my $error = $queue->insert( %args );

  return $error if $error;

  '';

}

sub statuschange {  # subroutine
  my( %options ) = @_;

  warn "statuschange called with options ". 
       join (', ', map { "$_ => $options{$_}" } keys(%options))
    if $DEBUG;

  my $method = $options{method};

  eval "use Net::Soma 0.01 qw(ApplicationDef ApplicationInstance
                              AttributeDef AttributeInstance);";
  die $@ if $@;

  my %soma_objects = ();
  foreach my $service ( qw ( CPECollection CPEAccess AppCatalog Applications ) )
  {
    $soma_objects{$service} = new Net::Soma ( namespace => $service."Service",
                                              url       => $options{'url'},
                                              die_on_fault => 1,
                                            );
  }
  
  my $cpeid = eval {$soma_objects{CPECollection}->getCPEByESN( $options{esn} )};
  warn "failed to find CPE with ESN $options{esn}"
    if ($DEBUG && !$cpeid);

  if ( $method eq '_queueable_insert' && $options{svcdb} eq 'svc_external' ) {
    if ( !$cpeid ) {
      # only type 1 is used at this time
      $cpeid = $soma_objects{CPECollection}->createCPE( $options{esn}, 1 );
    } else {
      $soma_objects{CPECollection}->releaseCPE( $cpeid );
      die "Soma element for $options{esn} already exists";
    }
  }

  die "Can't find soma element for $options{esn}"
    unless $cpeid;

  warn "dispatching $method from statuschange" if $DEBUG;
  &{$method}( \%soma_objects, $cpeid, %options );

}

sub _queueable_insert {
  my( $soma_objects, $cpeid, %options ) = @_;

  warn "_queueable_insert called for $cpeid with options ". 
       join (', ', map { "$_ => $options{$_}" } keys(%options))
    if $DEBUG;

  my $appid = $options{data_app_id};
  if ($appid) {
    my $application =
      $soma_objects->{AppCatalog}
                   ->getDefaultApplicationInstance($appid, $cpeid);

    my $attribute =
      $soma_objects->{AppCatalog}
                   ->getDefaultApplicationAttributeInstance(2, 1, $cpeid);
    $attribute->value('G');

    my $i = 0;
    foreach my $instance (@{$application->attributes}) {
      unless ($instance->definitionId == $attribute->definitionId) {
        $i++; next;
      }
      $application->attributes->[$i] = $attribute;
      last;
    }

    $soma_objects->{Applications}->subscribeApp( $cpeid, $application );
  }

  $soma_objects->{CPECollection}->releaseCPE( $cpeid );

  '';
}

sub _queueable_delete {
  my( $soma_objects, $cpeid, %options ) = @_;

  my $appid = $options{data_app_id};
  my $norelease;

  if ($appid) {
    my $applications =
      $soma_objects->{Applications}->getSubscribedApplications( $cpeid );

    my $instance_id;
    foreach $application (@$applications) {
      next unless $application->definitionId == $appid;
      $instance_id = $application->instanceId;
    }

    $soma_objects->{Applications}->unsubscribeApp( $cpeid, $instance_id );

  } else {

    $soma_objects->{CPECollection}->deleteCPE($cpeid);
    $norelease = 1;

  }

  $soma_objects->{CPECollection}->releaseCPE( $cpeid ) unless $norelease;

  '';
}

sub _queueable_replace {
  my( $soma_objects, $cpeid, %options ) = @_;

  my $appid = $options{data_app_id} || '';

  if (exists($options{data_app_id})) {
    my $applications =
      $soma_objects->{Applications}->getSubscribedApplications( $cpeid );

    my $instance_id;
    foreach $application (@$applications) {
      next unless $application->internalName eq 'dataApplication';
      if ($application->definitionId != $options{data_app_id}) {
        $instance_id = $application->instanceId;
        $soma_objects->{Applications}->unsubscribeApp( $cpeid, $instance_id );
      }
    }

    if ($appid && !$instance_id ) {
      my $application =
        $soma_objects->{AppCatalog}
                     ->getDefaultApplicationInstance($appid, $cpeid);

      $soma_objects->{Applications}->subscribeApp( $cpeid, $application );
    }

  } else {

    $soma_objects->{CPEAccess}->switchCPE($cpeid, $options{new_esn})
      unless( $options{new_esn} eq $options{esn});

  }

  $soma_objects->{CPECollection}->releaseCPE( $cpeid );

  '';
}

sub _queueable_suspend {
  my( $soma_objects, $cpeid, %options ) = @_;

  my $appid = $options{data_app_id};

  if ($appid) {
    my $applications =
      $soma_objects->{Applications}->getSubscribedApplications( $cpeid );

    my $instance_id;
    foreach $application (@$applications) {
      next unless $application->definitionId == $appid;

      $instance_id = $application->instanceId;
      my $app_def =
        $soma_objects->{AppCatalog}->getApplicationDef($appid, $cpeid);
      my @attr_def = grep { $_->internalName eq 'status' }
                          @{$app_def->attributes};

      foreach my $attribute ( @{$application->attributes} ) {
        next unless $attribute->definitionId == $attr_def[0]->definitionId;
        $attribute->{value} = 'S';  

        $soma_objects->{Applications}->setAppAttribute( $cpeid,
                                                        $instance_id,
                                                        $attribute
                                                      );
      }
      
    }

  } else {

    #do nothing

  }

  $soma_objects->{CPECollection}->releaseCPE( $cpeid );

  '';
}

sub _queueable_unsuspend {
  my( $soma_objects, $cpeid, %options ) = @_;

  my $appid = $options{data_app_id};

  if ($appid) {
    my $applications =
      $soma_objects->{Applications}->getSubscribedApplications( $cpeid );

    my $instance_id;
    foreach $application (@$applications) {
      next unless $application->definitionId == $appid;

      $instance_id = $application->instanceId;
      my $app_def =
        $soma_objects->{AppCatalog}->getApplicationDef($appid, $cpeid);
      my @attr_def = grep { $_->internalName eq 'status' }
                     @{$app_def->attributes};

      foreach my $attribute ( @{$application->attributes} ) {
        next unless $attribute->definitionId == $attr_def[0]->definitionId;
        $attribute->{value} = 'E';  

        $soma_objects->{Applications}->setAppAttribute( $cpeid,
                                                        $instance_id,
                                                        $attribute
                                                      );
      }
      
    }

  } else {

    #do nothing

  }

  $soma_objects->{CPECollection}->releaseCPE( $cpeid );

  '';
}

sub esn {
  my ( $self, $svc ) = @_;
  my $svcdb = $svc->cust_svc->part_svc->svcdb;

  if ($svcdb eq 'svc_external') {
    my $esn = $svc->title;
    $esn =~ /^\s*([\da-fA-F]{1,16})\s*$/ && ($esn = $1);
    return sprintf( '%016s', $esn );
  }
  
  my $cust_pkg = $svc->cust_svc->cust_pkg;
  return '' unless $cust_pkg;

  my @cust_svc = grep { $_->part_svc->svcdb eq 'svc_external' &&
                        scalar( $_->part_svc->part_export('soma') )
                      }
                 $cust_pkg->cust_svc;
  return '' unless scalar(@cust_svc);
  warn "part_export::soma found multiple ESNs for cust_svc ". $svc->svcnum
    if scalar( @cust_svc ) > 1;

  my $esn = $cust_svc[0]->svc_x->title;
  $esn =~ /^\s*([\da-fA-F]{1,16})\s*$/ && ($esn = $1);
  
  sprintf( '%016s', $esn );
}


1;