1 package FS::svc_broadband;
4 use vars qw(@ISA $conf);
5 use FS::Record qw( qsearchs qsearch dbh );
9 use FS::part_svc_router;
12 @ISA = qw( FS::svc_Common );
14 $FS::UID::callback{'FS::svc_broadband'} = sub {
20 FS::svc_broadband - Object methods for svc_broadband records
24 use FS::svc_broadband;
26 $record = new FS::svc_broadband \%hash;
27 $record = new FS::svc_broadband { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
37 $error = $record->suspend;
39 $error = $record->unsuspend;
41 $error = $record->cancel;
45 An FS::svc_broadband object represents a 'broadband' Internet connection, such
46 as a DSL, cable modem, or fixed wireless link. These services are assumed to
47 have the following properties:
49 FS::svc_broadband inherits from FS::svc_Common. The following fields are
54 =item svcnum - primary key
56 =item blocknum - see FS::addr_block
59 speed_up - maximum upload speed, in bits per second. If set to zero, upload
60 speed will be unlimited. Exports that do traffic shaping should handle this
61 correctly, and not blindly set the upload speed to zero and kill the customer's
65 speed_down - maximum download speed, as above
67 =item ip_addr - the customer's IP address. If the customer needs more than one
68 IP address, set this to the address of the customer's router. As a result, the
69 customer's router will have the same address for both its internal and external
70 interfaces thus saving address space. This has been found to work on most NAT
81 Creates a new svc_broadband. To add the record to the database, see
84 Note that this stores the hash reference, not a distinct copy of the hash it
85 points to. You can ask the object for a copy with the I<hash> method.
91 'name' => 'Broadband',
92 'name_plural' => 'Broadband services',
93 'longname_plural' => 'Fixed (username-less) broadband services',
94 'display_weight' => 50,
95 'cancel_weight' => 70,
97 'description' => 'Descriptive label for this particular device.',
98 'speed_down' => 'Maximum download speed for this service in Kbps. 0 denotes unlimited.',
99 'speed_up' => 'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',
100 'ip_addr' => 'IP address. Leave blank for automatic assignment.',
101 'blocknum' => { 'label' => 'Address block',
103 'select_table' => 'addr_block',
104 'select_key' => 'blocknum',
105 'select_label' => 'cidr',
106 'disable_inventory' => 1,
112 sub table { 'svc_broadband'; }
114 =item search_sql STRING
116 Class method which returns an SQL fragment to search for the given string.
121 my( $class, $string ) = @_;
122 if ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
123 $class->search_sql_field('ip_addr', $string );
124 }elsif ( $string =~ /^([a-fA-F0-9]{12})$/ ) {
125 $class->search_sql_field('mac_addr', uc($string));
126 }elsif ( $string =~ /^(([a-fA-F0-9]{1,2}:){5}([a-fA-F0-9]{1,2}))$/ ) {
127 $class->search_sql_field('mac_addr', uc("$2$3$4$5$6$7") );
135 Returns the IP address.
144 =item insert [ , OPTION => VALUE ... ]
146 Adds this record to the database. If there is an error, returns the error,
147 otherwise returns false.
149 The additional fields pkgnum and svcpart (see FS::cust_svc) should be
150 defined. An FS::cust_svc record will be created and inserted.
152 Currently available options are: I<depend_jobnum>
154 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
155 jobnums), all provisioning jobs will have a dependancy on the supplied
156 jobnum(s) (they will not run until the specific job(s) complete(s)).
160 # Standard FS::svc_Common::insert
164 Delete this record from the database.
168 # Standard FS::svc_Common::delete
170 =item replace OLD_RECORD
172 Replaces the OLD_RECORD with this one in the database. If there is an error,
173 returns the error, otherwise returns false.
177 # Standard FS::svc_Common::replace
181 Called by the suspend method of FS::cust_pkg (see FS::cust_pkg).
185 Called by the unsuspend method of FS::cust_pkg (see FS::cust_pkg).
189 Called by the cancel method of FS::cust_pkg (see FS::cust_pkg).
193 Checks all fields to make sure this is a valid broadband service. If there is
194 an error, returns the error, otherwise returns false. Called by the insert
201 my $x = $self->setfixed;
203 return $x unless ref($x);
206 $self->ut_numbern('svcnum')
207 || $self->ut_numbern('blocknum')
208 || $self->ut_textn('description')
209 || $self->ut_number('speed_up')
210 || $self->ut_number('speed_down')
211 || $self->ut_ipn('ip_addr')
212 || $self->ut_hexn('mac_addr')
213 || $self->ut_hexn('auth_key')
214 || $self->ut_coordn('latitude', -90, 90)
215 || $self->ut_coordn('longitude', -180, 180)
216 || $self->ut_sfloatn('altitude')
217 || $self->ut_textn('vlan_profile')
219 return $error if $error;
221 #redundant, but better error message
222 return "MAC already in use"
223 if scalar( qsearch( 'svc_broadband', { 'mac_addr', $self->mac_addr } ) );
225 if($self->speed_up < 0) { return 'speed_up must be positive'; }
226 if($self->speed_down < 0) { return 'speed_down must be positive'; }
228 my $cust_svc = $self->svcnum
229 ? qsearchs('cust_svc', { 'svcnum' => $self->svcnum } )
233 $cust_pkg = $cust_svc->cust_pkg;
235 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );
236 return "Invalid pkgnum" unless $cust_pkg;
239 if ($self->blocknum) {
240 $error = $self->ut_foreign_key('blocknum', 'addr_block', 'blocknum');
241 return $error if $error;
244 if ($cust_pkg && $self->blocknum) {
245 my $addr_agentnum = $self->addr_block->agentnum;
246 if ($addr_agentnum && $addr_agentnum != $cust_pkg->cust_main->agentnum) {
247 return "Address block does not service this customer";
251 if (not($self->ip_addr) or $self->ip_addr eq '0.0.0.0') {
252 return "Must supply either address or block"
253 unless $self->blocknum;
254 my $next_addr = $self->addr_block->next_free_addr;
256 $self->ip_addr($next_addr->addr);
258 return "No free addresses in addr_block (blocknum: ".$self->blocknum.")";
262 if (not($self->blocknum)) {
263 return "Must supply either address or block"
264 unless ($self->ip_addr and $self->ip_addr ne '0.0.0.0');
265 my @block = grep { $_->NetAddr->contains($self->NetAddr) }
266 map { $_->addr_block }
267 $self->allowed_routers;
268 if (scalar(@block)) {
269 $self->blocknum($block[0]->blocknum);
271 return "Address not with available block.";
275 # This should catch errors in the ip_addr. If it doesn't,
276 # they'll almost certainly not map into the block anyway.
277 my $self_addr = $self->NetAddr; #netmask is /32
278 return ('Cannot parse address: ' . $self->ip_addr) unless $self_addr;
280 my $block_addr = $self->addr_block->NetAddr;
281 unless ($block_addr->contains($self_addr)) {
282 return 'blocknum '.$self->blocknum.' does not contain address '.$self->ip_addr;
285 my $router = $self->addr_block->router
286 or return 'Cannot assign address from unallocated block:'.$self->addr_block->blocknum;
287 if(grep { $_->routernum == $router->routernum} $self->allowed_routers) {
290 return 'Router '.$router->routernum.' cannot provide svcpart '.$self->svcpart;
298 Returns a NetAddr::IP object containing the IP address of this service. The netmask
305 new NetAddr::IP ($self->ip_addr);
310 Returns the FS::addr_block record (i.e. the address block) for this broadband service.
316 qsearchs('addr_block', { blocknum => $self->blocknum });
321 =item allowed_routers
323 Returns a list of allowed FS::router objects.
327 sub allowed_routers {
329 map { $_->router } qsearch('part_svc_router', { svcpart => $self->svcpart });
334 The business with sb_field has been 'fixed', in a manner of speaking.
336 allowed_routers isn't agent virtualized because part_svc isn't agent
341 FS::svc_Common, FS::Record, FS::addr_block,
342 FS::part_svc, schema.html from the base documentation.