1 package FS::part_export::saisei;
4 use vars qw( @ISA %info );
5 use base qw( FS::part_export );
6 use Date::Format 'time2str';
18 FS::part_export::saisei
22 Saisei integration for Freeside
26 This export offers basic svc_broadband provisioning for Saisei.
28 This is a customer integration with Saisei. This will set up a rate plan and tie
29 the rate plan to a host and the access point via the Saisei API when the broadband service is provisioned.
30 It will also untie the host from the rate plan, setting it to the default rate plan via the API upon unprovisioning of the broadband service.
32 This will create and modify the rate plans at Saisei as soon as the broadband service attached to this export is created or modified.
33 This will also create and modify an access point at Saisei as soon as the tower is created or modified.
35 To use this export, follow the below instructions:
37 Create a new service definition and set the table to svc_broadband. The service name will become the Saisei rate plan name.
38 Set the upload and download speed for the service. This is required to be able to export the service to Saisei.
39 Attach this Saisei export to this service.
41 Create a tower and add a sector to that tower. The sector name will be the name of the access point,
42 Make sure you have set the up and down rate limit for the tower and the sector. This is required to be able to export the access point.
43 The tower and sector will be set up as access points at Saisei upon the creation of the tower or sector. They will be modified at Saisei when modified in freeside.
44 Each sector will be attached to its tower access point using the Saisei uplink field.
46 Create a package for the above created service, and order this package for a customer.
48 Provision the service, making sure to enter the IP address associated with this service and select the tower and sector for it's access point.
49 This provisioned service will then be exported as a host to Saisei.
51 Unprovisioning this service will set the host entry at Saisei to the default rate plan with the user and access point set to <none>.
53 After this export is set up and attached to a service, you can export the already provisioned services by clicking the link Export provisioned services attached to this export.
54 Clicking on this link will export all services attached to this export not currently exported to Saisei.
56 This module also provides generic methods for working through the L</Saisei API>.
60 tie my %scripts, 'Tie::IxHash',
61 'export_provisioned_services' => { component => '/elements/popup_link.html',
62 label => 'Export provisioned services',
63 description => 'will export provisioned services of part service with Saisei export attached.',
64 html_label => '<b>Export provisioned services attached to this export.</b>',
68 tie my %options, 'Tie::IxHash',
69 'port' => { label => 'Port',
71 'username' => { label => 'Saisei API User Name',
73 'password' => { label => 'Saisei API Password',
75 'debug' => { type => 'checkbox',
76 label => 'Enable debug warnings' },
80 'svc' => 'svc_broadband',
81 'desc' => 'Export broadband service/account to Saisei',
82 'options' => \%options,
83 'scripts' => \%scripts,
85 This is a customer integration with Saisei. This will set up a rate plan and tie
86 the rate plan to a host and the access point via the Saisei API when the broadband service is provisioned.
87 It will also untie the host from the rate plan, setting it to the default rate plan via the API upon unprovisioning of the broadband service.
89 This will create and modify the rate plans at Saisei as soon as the broadband service attached to this export is created or modified.
90 This will also create and modify an access point at Saisei as soon as the tower is created or modified.
92 To use this export, follow the below instructions:
96 Create a new service definition and set the table to svc_broadband. The service name will become the Saisei rate plan name.
97 Set the upload speed, download speed, and tower to be required for the service. This is required to be able to export the service to Saisei.
98 Attach this Saisei export to this service.
102 Create a tower and add a sector to that tower. The sector name will be the name of the access point,
103 Make sure you have set the up and down rate limit for the tower and the sector. This is required to be able to export the access point.
104 The tower and sector will be set up as access points at Saisei upon the creation of the tower or sector. They will be modified at Saisei when modified in freeside.
105 Each sector will be attached to its tower access point using the Saisei uplink field.
109 Create a package for the above created service, and order this package for a customer.
113 Provision the service, making sure to enter the IP address associated with this service, the upload and download speed are correct, and select the tower and sector for it's access point.
114 This provisioned service will then be exported as a host to Saisei.
116 Unprovisioning this service will set the host entry at Saisei to the default rate plan with the user and access point set to <i>none</i>.
120 After this export is set up and attached to a service, you can export the already provisioned services by clicking the link <b>Export provisioned services attached to this export</b>.
121 Clicking on this link will export all services attached to this export not currently exported to Saisei.
125 <A HREF="http://www.freeside.biz/mediawiki/index.php/Saisei_provisioning_export" target="_new">Documentation</a>
130 my ($self, $svc_broadband) = @_;
132 my $rateplan_name = $self->get_rateplan_name($svc_broadband);
134 # check for existing rate plan
135 my $existing_rateplan;
136 $existing_rateplan = $self->api_get_rateplan($rateplan_name) unless $self->{'__saisei_error'};
138 # if no existing rate plan create one and modify it.
139 $self->api_create_rateplan($svc_broadband, $rateplan_name) unless $existing_rateplan;
140 $self->api_modify_rateplan($svc_broadband, $rateplan_name) unless ($self->{'__saisei_error'} || $existing_rateplan);
141 return $self->api_error if $self->{'__saisei_error'};
143 # set rateplan to existing one or newly created one.
144 my $rateplan = $existing_rateplan ? $existing_rateplan : $self->api_get_rateplan($rateplan_name);
146 my $username = $svc_broadband->{Hash}->{svcnum};
147 my $description = $svc_broadband->{Hash}->{description};
150 $self->{'__saisei_error'} = 'no username - can not export';
151 return $self->api_error;
154 # check for existing user.
156 $existing_user = $self->api_get_user($username) unless $self->{'__saisei_error'};
158 # if no existing user create one.
159 $self->api_create_user($username, $description) unless $existing_user;
160 return $self->api_error if $self->{'__saisei_error'};
162 # set user to existing one or newly created one.
163 my $user = $existing_user ? $existing_user : $self->api_get_user($username);
166 my $tower_sector = FS::Record::qsearchs({
167 'table' => 'tower_sector',
168 'select' => 'tower.towername,
169 tower.up_rate_limit as tower_upratelimit,
170 tower.down_rate_limit as tower_downratelimit,
171 tower_sector.sectorname,
172 tower_sector.up_rate_limit as sector_upratelimit,
173 tower_sector.down_rate_limit as sector_downratelimit ',
174 'addl_from' => 'LEFT JOIN tower USING ( towernum )',
176 'sectornum' => $svc_broadband->{Hash}->{sectornum},
180 my $tower_name = $tower_sector->{Hash}->{towername};
181 $tower_name =~ s/\s/_/g;
184 'tower_name' => $tower_name,
185 'tower_uprate_limit' => $tower_sector->{Hash}->{tower_upratelimit},
186 'tower_downrate_limit' => $tower_sector->{Hash}->{tower_downratelimit},
189 my $tower_ap = process_tower($self, $tower_opt);
190 return $self->api_error if $self->{'__saisei_error'};
192 my $sector_name = $tower_sector->{Hash}->{sectorname};
193 $sector_name =~ s/\s/_/g;
196 'tower_name' => $tower_name,
197 'sector_name' => $sector_name,
198 'sector_uprate_limit' => $tower_sector->{Hash}->{sector_upratelimit},
199 'sector_downrate_limit' => $tower_sector->{Hash}->{sector_downratelimit},
200 'rateplan' => $rateplan_name,
202 my $accesspoint = process_sector($self, $sector_opt);
203 return $self->api_error if $self->{'__saisei_error'};
205 ## get custnum and pkgpart from cust_pkg for virtual access point
206 my $cust_pkg = FS::Record::qsearchs({
207 'table' => 'cust_pkg',
208 'hashref' => { 'pkgnum' => $svc_broadband->{Hash}->{pkgnum}, },
210 my $virtual_ap_name = $cust_pkg->{Hash}->{custnum}.'_'.$cust_pkg->{Hash}->{pkgpart}.'_'.$svc_broadband->{Hash}->{speed_down}.'_'.$svc_broadband->{Hash}->{speed_up};
212 my $virtual_ap_opt = {
213 'virtual_name' => $virtual_ap_name,
214 'sector_name' => $sector_name,
215 'virtual_uprate_limit' => $svc_broadband->{Hash}->{speed_up},
216 'virtual_downrate_limit' => $svc_broadband->{Hash}->{speed_down},
218 my $virtual_ap = process_virtual_ap($self, $virtual_ap_opt);
219 return $self->api_error if $self->{'__saisei_error'};
221 ## tie host to user add sector name as access point.
222 $self->api_add_host_to_user(
223 $user->{collection}->[0]->{name},
224 $rateplan->{collection}->[0]->{name},
225 $svc_broadband->{Hash}->{ip_addr},
226 $virtual_ap->{collection}->[0]->{name},
227 ) unless $self->{'__saisei_error'};
230 return $self->api_error;
234 sub _export_replace {
235 my ($self, $svc_broadband) = @_;
236 my $error = $self->_export_insert($svc_broadband);
241 my ($self, $svc_broadband) = @_;
243 my $rateplan_name = $self->get_rateplan_name($svc_broadband);
245 my $username = $svc_broadband->{Hash}->{svcnum};
247 ## untie host to user
248 $self->api_delete_host_to_user($username, $rateplan_name, $svc_broadband->{Hash}->{ip_addr}) unless $self->{'__saisei_error'};
253 sub _export_suspend {
254 my ($self, $svc_broadband) = @_;
258 sub _export_unsuspend {
259 my ($self, $svc_broadband) = @_;
264 my ($self, $svc_part) = @_;
267 if ($svc_part->{Hash}->{svc_broadband__speed_down} eq "down" || $svc_part->{Hash}->{svc_broadband__speed_up} eq "up") {
268 for my $type (qw( down up )) {
269 my $speed_type = "broadband_".$type."stream";
270 foreach my $pkg_svc (FS::Record::qsearch({
271 'table' => 'pkg_svc',
272 'select' => 'pkg_svc.*, part_pkg_fcc_option.fccoptionname, part_pkg_fcc_option.optionvalue',
273 'addl_from' => ' LEFT JOIN part_pkg_fcc_option USING (pkgpart) ',
274 'extra_sql' => " WHERE pkg_svc.svcpart = ".$svc_part->{Hash}->{svcpart}." AND pkg_svc.quantity > 0 AND part_pkg_fcc_option.fccoptionname = '".$speed_type."'",
275 })) { $fcc_477_speeds->{
276 $pkg_svc->{Hash}->{pkgpart}}->{$speed_type} = $pkg_svc->{Hash}->{optionvalue} * 1000 unless !$pkg_svc->{Hash}->{optionvalue}; }
280 $fcc_477_speeds->{1}->{broadband_downstream} = $svc_part->{Hash}->{"svc_broadband__speed_down"};
281 $fcc_477_speeds->{1}->{broadband_upstream} = $svc_part->{Hash}->{"svc_broadband__speed_up"};
284 foreach my $key (keys %$fcc_477_speeds) {
286 $svc_part->{Hash}->{speed_down} = $fcc_477_speeds->{$key}->{broadband_downstream};
287 $svc_part->{Hash}->{speed_up} = $fcc_477_speeds->{$key}->{broadband_upstream};
288 $svc_part->{Hash}->{svc_broadband__speed_down} = $fcc_477_speeds->{$key}->{broadband_downstream};
289 $svc_part->{Hash}->{svc_broadband__speed_up} = $fcc_477_speeds->{$key}->{broadband_upstream};
291 my $temp_svc = $svc_part->{Hash};
292 my $svc_broadband = {};
293 map { if ($_ =~ /^svc_broadband__(.*)$/) { $svc_broadband->{Hash}->{$1} = $temp_svc->{$_}; } } keys %$temp_svc;
295 my $rateplan_name = $self->get_rateplan_name($svc_broadband, $svc_part->{Hash}->{svc});
297 # check for existing rate plan
298 my $existing_rateplan;
299 $existing_rateplan = $self->api_get_rateplan($rateplan_name) unless $self->{'__saisei_error'};
301 # Modify the existing rate plan with new service data.
302 $self->api_modify_existing_rateplan($svc_broadband, $rateplan_name) unless ($self->{'__saisei_error'} || !$existing_rateplan);
304 # if no existing rate plan create one and modify it.
305 $self->api_create_rateplan($svc_broadband, $rateplan_name) unless $existing_rateplan;
306 $self->api_modify_rateplan($svc_part, $rateplan_name) unless ($self->{'__saisei_error'} || $existing_rateplan);
310 return $self->api_error;
314 sub export_tower_sector {
315 my ($self, $tower) = @_;
317 #modify tower or create it.
318 my $tower_name = $tower->{Hash}->{towername};
319 $tower_name =~ s/\s/_/g;
321 'tower_name' => $tower_name,
322 'tower_uprate_limit' => $tower->{Hash}->{up_rate_limit},
323 'tower_downrate_limit' => $tower->{Hash}->{down_rate_limit},
324 'modify_existing' => '1', # modify an existing access point with this info
327 my $tower_access_point = process_tower($self, $tower_opt);
328 return $tower_access_point if $tower_access_point->{error};
330 #get list of all access points
332 'table' => 'tower_sector',
334 'hashref' => { 'towernum' => $tower->{Hash}->{towernum}, },
337 #for each one modify or create it.
338 foreach my $tower_sector ( FS::Record::qsearch($hash_opt) ) {
339 my $sector_name = $tower_sector->{Hash}->{sectorname};
340 $sector_name =~ s/\s/_/g;
342 'tower_name' => $tower_name,
343 'sector_name' => $sector_name,
344 'sector_uprate_limit' => $tower_sector->{Hash}->{up_rate_limit},
345 'sector_downrate_limit' => $tower_sector->{Hash}->{down_rate_limit},
346 'modify_existing' => '1', # modify an existing access point with this info
348 my $sector_access_point = process_sector($self, $sector_opt) unless ($sector_name eq "_default");
349 return $sector_access_point if $sector_access_point->{error};
352 return { error => $self->api_error, };
355 ## creates the rateplan name
356 sub get_rateplan_name {
357 my ($self, $svc_broadband, $svc_name) = @_;
359 my $service_part = FS::Record::qsearchs( 'part_svc', { 'svcpart' => $svc_broadband->{Hash}->{svcpart} } ) unless $svc_name;
360 my $service_name = $svc_name ? $svc_name : $service_part->{Hash}->{svc};
362 my $rateplan_name = $service_name . " " . $svc_broadband->{Hash}->{speed_down} . "-" . $svc_broadband->{Hash}->{speed_up};
363 $rateplan_name =~ s/\s/_/g; $rateplan_name =~ s/[^A-Za-z0-9\-_]//g;
365 return $rateplan_name;
370 These methods allow access to the Saisei API using the credentials
371 set in the export options.
377 Accepts I<$method>, I<$path>, I<$params> hashref and optional.
378 Places an api call to the specified path and method with the specified params.
379 Returns the decoded json object returned by the api call.
380 Returns empty on failure; retrieve error messages using L</api_error>.
385 my ($self,$method,$path,$params) = @_;
387 $self->{'__saisei_error'} = '';
388 my $auth_info = $self->option('username') . ':' . $self->option('password');
391 warn "Calling $method on http://"
392 .$self->{Hash}->{machine}.':'.$self->option('port')
393 ."/rest/top/configurations/running/$path\n" if $self->option('debug');
395 my $data = encode_json($params) if keys %{ $params };
397 my $client = REST::Client->new();
398 $client->addHeader("Authorization", "Basic ".encode_base64($auth_info));
399 $client->setHost('http://'.$self->{Hash}->{machine}.':'.$self->option('port'));
400 $client->$method('/rest/top/configurations/running'.$path, $data, { "Content-type" => 'application/json'});
402 warn "Saisei Response Code is ".$client->responseCode()."\n" if $self->option('debug');
406 if ($client->responseCode() eq '200' || $client->responseCode() eq '201') {
407 eval { $result = decode_json($client->responseContent()) };
409 $self->{'__saisei_error'} = "There was an error decoding the JSON data from Saisei. Bad JSON data logged in error log if debug option was set.";
410 warn "Saisei RC 201 Response Content is not json\n".$client->responseContent()."\n" if $self->option('debug');
414 elsif ($client->responseCode() eq '404') {
415 eval { $result = decode_json($client->responseContent()) };
417 $self->{'__saisei_error'} = "There was an error decoding the JSON data from Saisei. Bad JSON data logged in error log if debug option was set.";
418 warn "Saisei RC 404 Response Content is not json\n".$client->responseContent()."\n" if $self->option('debug');
421 ## check if message is for empty hash.
422 my($does_not_exist) = $result->{message} =~ /'(.*)' does not exist$/;
423 $self->{'__saisei_error'} = "Saisei Error: ".$result->{message} unless $does_not_exist;
424 warn "Saisei Response Content is\n".$client->responseContent."\n" if ($self->option('debug') && !$does_not_exist);
427 elsif ($client->responseCode() eq '500') {
428 $self->{'__saisei_error'} = "Could not connect to the host (".$self->{Hash}->{machine}.':'.$self->option('port').") during $method , we received the responce code: " . $client->responseCode();
429 warn "Saisei Response Content is\n".$client->responseContent."\n" if $self->option('debug');
433 $self->{'__saisei_error'} = "Received Bad response from server during $method , we received responce code: " . $client->responseCode();
434 warn "Saisei Response Content is\n".$client->responseContent."\n" if $self->option('debug');
444 Returns the error string set by L</Saisei API> methods,
445 or a blank string if most recent call produced no errors.
451 return $self->{'__saisei_error'} || '';
454 =head2 api_get_policies
456 Gets a list of global policies.
460 sub api_get_policies {
463 my $get_policies = $self->api_call("GET", '/policies/?token=1&order=name&start=0&limit=20&select=name%2Cpercent_rate%2Cassured%2C');
464 return if $self->api_error;
465 $self->{'__saisei_error'} = "Did not receive any global policies from Saisei."
466 unless $get_policies;
468 return $get_policies->{collection};
471 =head2 api_get_rateplan
473 Gets rateplan info for specific rateplan.
477 sub api_get_rateplan {
479 my $rateplan = shift;
481 my $get_rateplan = $self->api_call("GET", "/rate_plans/$rateplan");
482 return if $self->api_error;
484 return $get_rateplan;
489 Gets user info for specific user.
497 my $get_user = $self->api_call("GET", "/users/$user");
498 return if $self->api_error;
503 =head2 api_get_accesspoint
505 Gets user info for specific access point.
509 sub api_get_accesspoint {
511 my $accesspoint = shift;
513 my $get_accesspoint = $self->api_call("GET", "/access_points/$accesspoint");
514 return if $self->api_error;
516 return $get_accesspoint;
521 Gets user info for specific host.
529 my $get_host = $self->api_call("GET", "/hosts/$ip");
531 return { message => $self->api_error, } if $self->api_error;
536 =head2 api_create_rateplan
542 sub api_create_rateplan {
543 my ($self, $svc, $rateplan) = @_;
545 $self->{'__saisei_error'} = "There is no download speed set for the service $rateplan with host (".$svc->{Hash}->{ip_addr}."). All services that are to be exported to Saisei need to have a download speed set for them." if !$svc->{Hash}->{speed_down};
546 $self->{'__saisei_error'} = "There is no upload speed set for the service $rateplan with host (".$svc->{Hash}->{ip_addr}."). All services that are to be exported to Saisei need to have a upload speed set for them." if !$svc->{Hash}->{speed_up};
548 my $new_rateplan = $self->api_call(
550 "/rate_plans/$rateplan",
552 'downstream_rate' => $svc->{Hash}->{speed_down},
553 'upstream_rate' => $svc->{Hash}->{speed_up},
555 ) unless $self->{'__saisei_error'};
557 $self->{'__saisei_error'} = "Saisei could not create the rate plan $rateplan."
558 unless ($new_rateplan || $self->{'__saisei_error'});
560 return $new_rateplan;
564 =head2 api_modify_rateplan
566 Modify a new rateplan.
570 sub api_modify_rateplan {
571 my ($self,$svc,$rateplan_name) = @_;
574 my $policies = $self->api_get_policies();
576 foreach my $policy (@$policies) {
577 my $policyname = $policy->{name};
578 my $rate_multiplier = '';
579 if ($policy->{background}) { $rate_multiplier = ".01"; }
580 my $modified_rateplan = $self->api_call(
582 "/rate_plans/$rateplan_name/partitions/$policyname",
584 'restricted' => $policy->{assured}, # policy_assured_flag
585 'rate_multiplier' => $rate_multiplier, # policy_background 0.1
586 'rate' => $policy->{percent_rate}, # policy_percent_rate
590 $self->{'__saisei_error'} = "Saisei could not modify the rate plan $rateplan_name after it was created."
591 unless ($modified_rateplan || $self->{'__saisei_error'}); # should never happen
599 =head2 api_modify_existing_rateplan
601 Modify a existing rateplan.
605 sub api_modify_existing_rateplan {
606 my ($self,$svc,$rateplan_name) = @_;
608 my $modified_rateplan = $self->api_call(
610 "/rate_plans/$rateplan_name",
612 'downstream_rate' => $svc->{Hash}->{speed_down},
613 'upstream_rate' => $svc->{Hash}->{speed_up},
617 $self->{'__saisei_error'} = "Saisei could not modify the rate plan $rateplan_name."
618 unless ($modified_rateplan || $self->{'__saisei_error'}); # should never happen
624 =head2 api_create_user
630 sub api_create_user {
631 my ($self,$user, $description) = @_;
633 my $new_user = $self->api_call(
637 'description' => $description,
641 $self->{'__saisei_error'} = "Saisei could not create the user $user"
642 unless ($new_user || $self->{'__saisei_error'}); # should never happen
648 =head2 api_create_accesspoint
650 Creates a access point.
654 sub api_create_accesspoint {
655 my ($self,$accesspoint, $upratelimit, $downratelimit) = @_;
657 my $new_accesspoint = $self->api_call(
659 "/access_points/$accesspoint",
661 'downstream_rate_limit' => $downratelimit,
662 'upstream_rate_limit' => $upratelimit,
666 $self->{'__saisei_error'} = "Saisei could not create the access point $accesspoint"
667 unless ($new_accesspoint || $self->{'__saisei_error'}); # should never happen
672 =head2 api_modify_accesspoint
674 Modify a new access point.
678 sub api_modify_accesspoint {
679 my ($self, $accesspoint, $uplink) = @_;
681 my $modified_accesspoint = $self->api_call(
683 "/access_points/$accesspoint",
685 'uplink' => $uplink, # name of attached access point
689 $self->{'__saisei_error'} = "Saisei could not modify the access point $accesspoint after it was created."
690 unless ($modified_accesspoint || $self->{'__saisei_error'}); # should never happen
696 =head2 api_modify_existing_accesspoint
698 Modify a existing accesspoint.
702 sub api_modify_existing_accesspoint {
703 my ($self, $accesspoint, $uplink, $upratelimit, $downratelimit) = @_;
705 my $modified_accesspoint = $self->api_call(
707 "/access_points/$accesspoint",
709 'downstream_rate_limit' => $downratelimit,
710 'upstream_rate_limit' => $upratelimit,
711 # 'uplink' => $uplink, # name of attached access point
715 $self->{'__saisei_error'} = "Saisei could not modify the access point $accesspoint."
716 unless ($modified_accesspoint || $self->{'__saisei_error'}); # should never happen
722 =head2 api_add_host_to_user
724 ties host to user, rateplan and default access point.
728 sub api_add_host_to_user {
729 my ($self,$user, $rateplan, $ip, $accesspoint) = @_;
731 my $new_host = $self->api_call(
736 'rate_plan' => $rateplan,
737 'access_point' => $accesspoint,
741 $self->{'__saisei_error'} = "Saisei could not create the host $ip"
742 unless ($new_host || $self->{'__saisei_error'}); # should never happen
748 =head2 api_delete_host_to_user
750 unties host from user and rateplan.
751 this will set the host entry at Saisei to the default rate plan with the user and access point set to <none>.
755 sub api_delete_host_to_user {
756 my ($self,$user, $rateplan, $ip) = @_;
758 my $default_rate_plan = $self->api_call("GET", '?token=1&select=default_rate_plan');
759 return if $self->api_error;
760 $self->{'__saisei_error'} = "Can not delete the host as Saisei did not return a default rate plan. Please make sure Saisei has a default rateplan setup."
761 unless $default_rate_plan;
763 my $default_rateplan_name = $default_rate_plan->{collection}->[0]->{default_rate_plan}->{link}->{name};
765 my $delete_host = $self->api_call(
770 'access_point' => '<none>',
771 'rate_plan' => $default_rateplan_name,
775 $self->{'__saisei_error'} = "Saisei could not delete the host $ip"
776 unless ($delete_host || $self->{'__saisei_error'}); # should never happen
783 my ($self, $opt) = @_;
785 if (!$opt->{tower_uprate_limit} || !$opt->{tower_downrate_limit}) {
786 $self->{'__saisei_error'} = "Could not export tower ".$opt->{tower_name}." because there was no up or down rates attached to the tower. Saisei requires a up and down rate be attached to each tower.";
787 return { error => $self->api_error, };
790 my $existing_tower_ap;
791 my $tower_name = $opt->{tower_name};
793 #check if tower has been set up as an access point.
794 $existing_tower_ap = $self->api_get_accesspoint($tower_name) unless $self->{'__saisei_error'};
796 # modify the existing accesspoint if changing tower .
797 $self->api_modify_existing_accesspoint (
799 '', # tower does not have a uplink on sectors.
800 $opt->{tower_uprate_limit},
801 $opt->{tower_downrate_limit},
802 ) if $existing_tower_ap->{collection} && $opt->{modify_existing};
804 #if tower does not exist as an access point create it.
805 $self->api_create_accesspoint(
807 $opt->{tower_uprate_limit},
808 $opt->{tower_downrate_limit},
809 ) unless $existing_tower_ap->{collection};
811 my $accesspoint = $self->api_get_accesspoint($tower_name);
813 return { error => $self->api_error, } if $self->api_error;
818 my ($self, $opt) = @_;
820 if (!$opt->{sector_name} || $opt->{sector_name} eq '_default') {
821 $self->{'__saisei_error'} = "No sector attached to Tower (".$opt->{tower_name}.") for service ".$opt->{'rateplan'}.". Saisei requires a tower sector to be attached to each service that is exported to Saisei.";
822 return { error => $self->api_error, };
825 if (!$opt->{sector_uprate_limit} || !$opt->{sector_downrate_limit}) {
826 $self->{'__saisei_error'} = "Could not export sector ".$opt->{tower_name}." because there was no up or down rates attached to the sector. Saisei requires a up and down rate be attached to each sector.";
827 return { error => $self->api_error, };
830 my $existing_sector_ap;
831 my $sector_name = $opt->{sector_name};
833 #check if sector has been set up as an access point.
834 $existing_sector_ap = $self->api_get_accesspoint($sector_name);
836 # modify the existing accesspoint if changing sector .
837 $self->api_modify_existing_accesspoint (
840 $opt->{sector_uprate_limit},
841 $opt->{sector_downrate_limit},
842 ) if $existing_sector_ap && $opt->{modify_existing};
844 #if sector does not exist as an access point create it.
845 $self->api_create_accesspoint(
847 $opt->{sector_uprate_limit},
848 $opt->{sector_downrate_limit},
849 ) unless $existing_sector_ap;
851 # Attach newly created sector to it's tower.
852 $self->api_modify_accesspoint($sector_name, $opt->{tower_name}) unless ($self->{'__saisei_error'} || $existing_sector_ap);
854 # set access point to existing one or newly created one.
855 my $accesspoint = $existing_sector_ap ? $existing_sector_ap : $self->api_get_accesspoint($sector_name);
857 return { error => $self->api_error, } if $self->api_error;
861 =head2 require_tower_and_sector
863 sets whether the service export requires a sector with it's tower.
867 sub require_tower_and_sector {
871 sub required_fields {
872 my @fields = ('svc_broadband__ip_addr_required', 'svc_broadband__speed_up_required', 'svc_broadband__speed_down_required', 'svc_broadband__sectornum_required');
876 sub process_virtual_ap {
877 my ($self, $opt) = @_;
879 my $existing_virtual_ap;
880 my $virtual_name = $opt->{virtual_name};
882 #check if virtual_ap has been set up as an access point.
883 $existing_virtual_ap = $self->api_get_accesspoint($virtual_name);
885 # modify the existing virtual accesspoint if changing it. this should never happen
886 $self->api_modify_existing_accesspoint (
889 $opt->{virtual_uprate_limit},
890 $opt->{virtual_downrate_limit},
891 ) if $existing_virtual_ap && $opt->{modify_existing};
893 #if virtual ap does not exist as an access point create it.
894 $self->api_create_accesspoint(
896 $opt->{virtual_uprate_limit},
897 $opt->{virtual_downrate_limit},
898 ) unless $existing_virtual_ap;
901 if ($existing_virtual_ap && (ref $existing_virtual_ap->{collection}->[0]->{uplink} eq "HASH") && ($existing_virtual_ap->{collection}->[0]->{uplink}->{link}->{name} ne $opt->{sector_name})) {
905 # Attach newly created virtual ap to tower sector ap or if sector has changed.
906 $self->api_modify_accesspoint($virtual_name, $opt->{sector_name}) unless ($self->{'__saisei_error'} || ($existing_virtual_ap && !$update_sector));
908 # set access point to existing one or newly created one.
909 my $accesspoint = $existing_virtual_ap ? $existing_virtual_ap : $self->api_get_accesspoint($virtual_name);
914 sub export_provisioned_services {
916 my $param = thaw(decode_base64(shift));
918 my $part_export = FS::Record::qsearchs('part_export', { 'exportnum' => $param->{export_provisioned_services_exportnum}, } )
919 or die "You are trying to use an unknown exportnum $param->{export_provisioned_services_exportnum}. This export does not exist.\n";
922 my @svcparts = FS::Record::qsearch({
923 'table' => 'export_svc',
924 'addl_from' => 'LEFT JOIN part_svc USING ( svcpart ) ',
925 'hashref' => { 'exportnum' => $param->{export_provisioned_services_exportnum}, },
927 my $part_count = scalar @svcparts;
929 my $parts = join "', '", map { $_->{Hash}->{svcpart} } @svcparts;
931 my @svcs = FS::Record::qsearch({
932 'table' => 'cust_svc',
933 'addl_from' => 'LEFT JOIN svc_broadband USING ( svcnum ) ',
934 'extra_sql' => " WHERE svcpart in ('".$parts."')",
937 my $svc_count = scalar @svcs;
940 for (my $c=1; $c <=100; $c=$c+1) { $status{int($svc_count * ($c/100))} = $c; }
943 foreach my $svc (@svcs) {
944 if ($status{$process_count}) { my $s = $status{$process_count}; $job->update_statustext($s); }
945 ## check if service exists as host if not export it.
946 my $host = api_get_host($part_export, $svc->{Hash}->{ip_addr});
947 die ("Please double check your credentials as ".$host->{message}."\n") if $host->{message};
948 warn "Exporting service ".$svc->{Hash}->{ip_addr}."\n" if ($part_export->option('debug'));
949 my $export_error = _export_insert($part_export,$svc) unless $host->{collection};
951 warn "Error exporting service ".$svc->{Hash}->{ip_addr}."\n" if ($part_export->option('debug'));
952 die ("$export_error\n");