1 package FS::svc_broadband;
12 { no warnings 'redefine'; use NetAddr::IP; }
13 use FS::Record qw( qsearchs qsearch dbh );
16 use FS::part_svc_router;
19 $FS::UID::callback{'FS::svc_broadband'} = sub {
25 FS::svc_broadband - Object methods for svc_broadband records
29 use FS::svc_broadband;
31 $record = new FS::svc_broadband \%hash;
32 $record = new FS::svc_broadband { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
42 $error = $record->suspend;
44 $error = $record->unsuspend;
46 $error = $record->cancel;
50 An FS::svc_broadband object represents a 'broadband' Internet connection, such
51 as a DSL, cable modem, or fixed wireless link. These services are assumed to
52 have the following properties:
54 FS::svc_broadband inherits from FS::svc_Common. The following fields are
59 =item svcnum - primary key
61 =item blocknum - see FS::addr_block
64 speed_up - maximum upload speed, in bits per second. If set to zero, upload
65 speed will be unlimited. Exports that do traffic shaping should handle this
66 correctly, and not blindly set the upload speed to zero and kill the customer's
70 speed_down - maximum download speed, as above
72 =item ip_addr - the customer's IP address. If the customer needs more than one
73 IP address, set this to the address of the customer's router. As a result, the
74 customer's router will have the same address for both its internal and external
75 interfaces thus saving address space. This has been found to work on most NAT
88 Creates a new svc_broadband. To add the record to the database, see
91 Note that this stores the hash reference, not a distinct copy of the hash it
92 points to. You can ask the object for a copy with the I<hash> method.
98 'name' => 'Wireless broadband',
99 'name_plural' => 'Wireless broadband services',
100 'longname_plural' => 'Fixed wireless broadband services',
101 'display_weight' => 50,
102 'cancel_weight' => 70,
103 'ip_field' => 'ip_addr',
105 'svcnum' => 'Service',
106 'description' => 'Descriptive label for this particular device',
107 'speed_down' => 'Maximum download speed for this service in Kbps. 0 denotes unlimited.',
108 'speed_up' => 'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',
109 'ip_addr' => 'IP address. Leave blank for automatic assignment.',
111 { 'label' => 'Address block',
113 'select_table' => 'addr_block',
114 'select_key' => 'blocknum',
115 'select_label' => 'cidr',
116 'disable_inventory' => 1,
118 'plan_id' => 'Service Plan Id',
119 'performance_profile' => 'Peformance Profile',
120 'authkey' => 'Authentication key',
121 'mac_addr' => 'MAC address',
122 'latitude' => 'Latitude',
123 'longitude' => 'Longitude',
124 'altitude' => 'Altitude',
125 'vlan_profile' => 'VLAN profile',
126 'sectornum' => 'Tower/sector',
127 'routernum' => 'Router/block',
129 label => 'RADIUS groups',
130 type => 'select-radius_group.html',
131 #select_table => 'radius_group',
132 #select_key => 'groupnum',
133 #select_label => 'groupname',
134 disable_inventory => 1,
141 sub table { 'svc_broadband'; }
143 sub table_dupcheck_fields { ( 'ip_addr', 'mac_addr' ); }
147 Class method which returns a qsearch hash expression to search for parameters
148 specified in HASHREF.
154 =item unlinked - set to search for all unlinked services. Overrides all other options.
164 =item pkgpart - arrayref
166 =item routernum - arrayref
168 =item sectornum - arrayref
170 =item towernum - arrayref
179 my( $class, $params, $from, $where ) = @_;
181 #routernum, can be arrayref
182 for my $routernum ( $params->{'routernum'} ) {
183 # this no longer uses addr_block
184 if ( ref $routernum and grep { $_ } @$routernum ) {
185 my $in = join(',', map { /^(\d+)$/ ? $1 : () } @$routernum );
187 push @orwhere, "svc_broadband.routernum IN ($in)" if $in;
188 push @orwhere, "svc_broadband.routernum IS NULL"
189 if grep /^none$/, @$routernum;
190 push @$where, '( '.join(' OR ', @orwhere).' )';
192 elsif ( $routernum =~ /^(\d+)$/ ) {
193 push @$where, "svc_broadband.routernum = $1";
195 elsif ( $routernum eq 'none' ) {
196 push @$where, "svc_broadband.routernum IS NULL";
200 #this should probably move to svc_Tower_Mixin, or maybe we never should have
201 # done svc_acct # towers (or, as mark thought, never should have done
204 #sector and tower, as above
205 my @where_sector = $class->tower_sector_sql($params);
206 if ( @where_sector ) {
207 push @$where, @where_sector;
208 push @$from, 'LEFT JOIN tower_sector USING ( sectornum )';
212 if ( $params->{'ip_addr'} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/ ) {
213 push @$where, "ip_addr = '$1'";
218 =item search_sql STRING
220 Class method which returns an SQL fragment to search for the given string.
225 my( $class, $string ) = @_;
226 if ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
227 $class->search_sql_field('ip_addr', $string );
228 }elsif ( $string =~ /^([a-fA-F0-9]{12})$/ ) {
229 $class->search_sql_field('mac_addr', uc($string));
230 }elsif ( $string =~ /^(([a-fA-F0-9]{1,2}:){5}([a-fA-F0-9]{1,2}))$/ ) {
231 $class->search_sql_field('mac_addr', uc("$2$3$4$5$6$7") );
239 Returns the IP address.
245 my $label = 'IP:'. ($self->ip_addr || 'Unknown');
246 $label .= ', MAC:'. $self->mac_addr
248 $label .= ' ('. $self->description. ')'
249 if $self->description;
253 =item insert [ , OPTION => VALUE ... ]
255 Adds this record to the database. If there is an error, returns the error,
256 otherwise returns false.
258 The additional fields pkgnum and svcpart (see FS::cust_svc) should be
259 defined. An FS::cust_svc record will be created and inserted.
261 Currently available options are: I<depend_jobnum>
263 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
264 jobnums), all provisioning jobs will have a dependancy on the supplied
265 jobnum(s) (they will not run until the specific job(s) complete(s)).
269 # Standard FS::svc_Common::insert
273 Delete this record from the database.
277 # Standard FS::svc_Common::delete
279 =item replace OLD_RECORD
281 Replaces the OLD_RECORD with this one in the database. If there is an error,
282 returns the error, otherwise returns false.
284 # Standard FS::svc_Common::replace
288 Called by the suspend method of FS::cust_pkg (see FS::cust_pkg).
292 Called by the unsuspend method of FS::cust_pkg (see FS::cust_pkg).
296 Called by the cancel method of FS::cust_pkg (see FS::cust_pkg).
300 Checks all fields to make sure this is a valid broadband service. If there is
301 an error, returns the error, otherwise returns false. Called by the insert
308 my $x = $self->setfixed;
310 return $x unless ref($x);
313 my $mac_addr = uc($self->get('mac_addr'));
314 $mac_addr =~ s/[\W_]//g;
315 $self->set('mac_addr', $mac_addr);
318 $self->ut_numbern('svcnum')
319 || $self->ut_numbern('blocknum')
320 || $self->ut_foreign_keyn('routernum', 'router', 'routernum')
321 || $self->ut_foreign_keyn('sectornum', 'tower_sector', 'sectornum')
322 || $self->ut_textn('description')
323 || $self->ut_numbern('speed_up')
324 || $self->ut_numbern('speed_down')
325 || $self->ut_ipn('ip_addr')
326 || $self->ut_hexn('mac_addr')
327 || $self->ut_hexn('auth_key')
328 || $self->ut_coordn('latitude')
329 || $self->ut_coordn('longitude')
330 || $self->ut_sfloatn('altitude')
331 || $self->ut_textn('vlan_profile')
332 || $self->ut_textn('plan_id')
334 return $error if $error;
336 if(($self->speed_up || 0) < 0) { return 'speed_up must be positive'; }
337 if(($self->speed_down || 0) < 0) { return 'speed_down must be positive'; }
339 my $cust_svc = $self->svcnum
340 ? qsearchs('cust_svc', { 'svcnum' => $self->svcnum } )
345 $cust_pkg = $cust_svc->cust_pkg;
346 $svcpart = $cust_svc->svcpart;
348 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );
349 return "Invalid pkgnum" unless $cust_pkg;
350 $svcpart = $self->svcpart;
352 my $agentnum = $cust_pkg->cust_main->agentnum if $cust_pkg;
354 # assign IP address / router / block
355 $error = $self->svc_ip_check;
356 return $error if $error;
358 and !$conf->exists('svc_broadband-allow_null_ip_addr') ) {
359 return 'IP address is required';
362 if ( $cust_pkg && ! $self->latitude && ! $self->longitude ) {
363 my $l = $cust_pkg->cust_location_or_main;
364 if ( $l->ship_latitude && $l->ship_longitude ) {
365 $self->latitude( $l->ship_latitude );
366 $self->longitude( $l->ship_longitude );
367 } elsif ( $l->latitude && $l->longitude ) {
368 $self->latitude( $l->latitude );
369 $self->longitude( $l->longitude );
376 sub _check_duplicate {
378 # Not a reliable check because the table isn't locked, but
379 # that's why we have a unique index. This is just to give a
380 # friendlier error message.
382 @dup = $self->find_duplicates('global', 'mac_addr');
384 return "MAC address in use (svcnum ".$dup[0]->svcnum.")";
390 =item mac_addr_formatted CASE DELIMITER
392 Format the MAC address (for use by exports). If CASE starts with "l"
393 (for "lowercase"), it's returned in lowercase. DELIMITER is inserted
398 sub mac_addr_formatted {
400 my ($case, $delim) = @_;
401 my $addr = $self->mac_addr;
402 $addr = lc($addr) if $case =~ /^l/i;
403 join( $delim || '', $addr =~ /../g );
410 local($FS::svc_Common::noexport_hack) = 1;
412 # set routernum to addr_block.routernum
413 foreach my $self (qsearch('svc_broadband', {
414 blocknum => {op => '!=', value => ''},
417 my $addr_block = $self->addr_block;
418 if ( !$addr_block ) {
419 # super paranoid mode
420 warn "WARNING: svcnum ".$self->svcnum." is assigned to addr_block ".$self->blocknum.", which does not exist; skipped.\n";
423 my $ip_addr = $self->ip_addr;
424 my $routernum = $addr_block->routernum;
426 $self->set(routernum => $routernum);
427 my $error = $self->check;
428 # sanity check: don't allow this to change IP address or block
429 # (other than setting blocknum to null for a non-auto-assigned router)
430 if ( $self->ip_addr ne $ip_addr
431 or ($self->blocknum and $self->blocknum != $addr_block->blocknum)) {
432 warn "WARNING: Upgrading service ".$self->svcnum." would change its block/address; skipped.\n";
436 $error ||= $self->replace;
437 warn "WARNING: error assigning routernum $routernum to service ".$self->svcnum.
438 ":\n$error; skipped\n"
442 warn "svcnum ".$self->svcnum.
443 ": no routernum in address block ".$addr_block->cidr.", skipped\n";
447 # assign blocknums to services that should have them
448 my @all_blocks = qsearch('addr_block', { });
449 SVC: foreach my $self (
451 'select' => 'svc_broadband.*',
452 'table' => 'svc_broadband',
453 'addl_from' => 'JOIN router USING (routernum)',
455 'extra_sql' => 'WHERE svc_broadband.blocknum IS NULL '.
456 'AND router.manual_addr IS NULL',
460 next SVC if $self->ip_addr eq '';
461 my $NetAddr = $self->NetAddr;
462 # inefficient, but should only need to run once
463 foreach my $block (@all_blocks) {
464 if ($block->NetAddr->contains($NetAddr)) {
465 $self->set(blocknum => $block->blocknum);
466 my $error = $self->replace;
467 warn "WARNING: error assigning blocknum ".$block->blocknum.
468 " to service ".$self->svcnum."\n$error; skipped\n"
473 warn "WARNING: no block found containing ".$NetAddr->addr." for service ".
485 The business with sb_field has been 'fixed', in a manner of speaking.
487 allowed_routers isn't agent virtualized because part_svc isn't agent
490 Having both routernum and blocknum as foreign keys is somewhat dubious.
494 FS::svc_Common, FS::Record, FS::addr_block,
495 FS::part_svc, schema.html from the base documentation.