communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[freeside.git] / FS / FS / addr_block.pm
index b671723..0fe2476 100755 (executable)
@@ -5,7 +5,9 @@ use vars qw( @ISA );
 use FS::Record qw( qsearchs qsearch dbh );
 use FS::router;
 use FS::svc_broadband;
+use FS::Conf;
 use NetAddr::IP;
+use Carp qw( carp );
 
 @ISA = qw( FS::Record );
 
@@ -46,6 +48,10 @@ block is assigned.
 
 =item ip_netmask - the netmask of the block, expressed as an integer.
 
+=item manual_flag - prohibit automatic ip assignment from this block when true. 
+
+=item agentnum - optional agent number (see L<FS::agent>)
+
 =back
 
 =head1 METHODS
@@ -83,6 +89,28 @@ sub delete {
 Replaces OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
+At present it's not possible to reallocate a block to a different router 
+except by deallocating it first, which requires that none of its addresses 
+be assigned.  This is probably as it should be.
+
+sub replace_check {
+  my ( $new, $old ) = ( shift, shift );
+
+  unless($new->routernum == $old->routernum) {
+    my @svc = $self->svc_broadband;
+    if (@svc) {
+      return 'Block has assigned addresses: '.
+             join ', ', map {$_->ip_addr} @svc;
+    }
+
+    return 'Block is already allocated'
+      if($new->routernum && $old->routernum);
+
+  }
+
+  '';
+}
+
 =item check
 
 Checks all fields to make sure this is a valid record.  If there is an error,
@@ -98,6 +126,8 @@ sub check {
     $self->ut_number('routernum')
     || $self->ut_ip('ip_gateway')
     || $self->ut_number('ip_netmask')
+    || $self->ut_enum('manual_flag', [ '', 'Y' ])
+    || $self->ut_agentnum_acl('agentnum', 'Broadband global configuration')
   ;
   return $error if $error;
 
@@ -121,7 +151,7 @@ sub check {
     }
   }
 
-  '';
+  $self->SUPER::check;
 }
 
 
@@ -157,24 +187,43 @@ Returns a NetAddr::IP object for this block's address and netmask.
 
 sub NetAddr {
   my $self = shift;
+  new NetAddr::IP ($self->ip_gateway, $self->ip_netmask);
+}
 
-  return new NetAddr::IP ($self->ip_gateway, $self->ip_netmask);
+=item cidr
+
+Returns a CIDR string for this block's address and netmask, i.e. 10.4.20.0/24
+
+=cut
+
+sub cidr {
+  my $self = shift;
+  $self->NetAddr->cidr;
 }
 
 =item next_free_addr
 
 Returns a NetAddr::IP object corresponding to the first unassigned address 
 in the block (other than the network, broadcast, or gateway address).  If 
-there are no free addresses, returns false.
+there are no free addresses, returns false.  There are never free addresses
+when manual_flag is true.
 
 =cut
 
 sub next_free_addr {
   my $self = shift;
 
-  my @used = map { $_->NetAddr->addr } 
-      ($self, 
-       qsearch('svc_broadband', { blocknum => $self->blocknum }) );
+  return '' if $self->manual_flag;
+
+  my $conf = new FS::Conf;
+  my @excludeaddr = $conf->config('exclude_ip_addr');
+  
+my @used =
+( (map { $_->NetAddr->addr }
+    ($self,
+     qsearch('svc_broadband', { blocknum => $self->blocknum }))
+  ), @excludeaddr
+);
 
   my @free = $self->NetAddr->hostenum;
   while (my $ip = shift @free) {
@@ -185,7 +234,7 @@ sub next_free_addr {
 
 }
 
-=item allocate
+=item allocate -- deprecated
 
 Allocates this address block to a router.  Takes an FS::router object 
 as an argument.
@@ -198,25 +247,18 @@ be assigned.  This is probably as it should be.
 
 sub allocate {
   my ($self, $router) = @_;
-
-  return 'Block is already allocated'
-    if($self->router);
+  carp "deallocate deprecated -- use replace";
 
   return 'Block must be allocated to a router'
     unless(ref $router eq 'FS::router');
 
-  my @svc = $self->svc_broadband;
-  if (@svc) {
-    return 'Block has assigned addresses: '. join ', ', map {$_->ip_addr} @svc;
-  }
-
   my $new = new FS::addr_block {$self->hash};
   $new->routernum($router->routernum);
   return $new->replace($self);
 
 }
 
-=item deallocate
+=item deallocate -- deprecated
 
 Deallocates the block (i.e. sets the routernum to 0).  If any addresses in the 
 block are assigned to services, it fails.
@@ -224,13 +266,9 @@ block are assigned to services, it fails.
 =cut
 
 sub deallocate {
+  carp "deallocate deprecated -- use replace";
   my $self = shift;
 
-  my @svc = $self->svc_broadband;
-  if (@svc) {
-    return 'Block has assigned addresses: '. join ', ', map {$_->ip_addr} @svc;
-  }
-
   my $new = new FS::addr_block {$self->hash};
   $new->routernum(0);
   return $new->replace($self);
@@ -311,6 +349,29 @@ sub split_block {
 
 To be implemented.
 
+=item agent
+
+Returns the agent (see L<FS::agent>) for this address block, if one exists.
+
+=cut
+
+sub agent {
+  qsearchs('agent', { 'agentnum' => shift->agentnum } );
+}
+
+=item label
+
+Returns text including the router name, gateway ip, and netmask for this
+block.
+
+=cut
+
+sub label {
+  my $self = shift;
+  my $router = $self->router;
+  ($router ? $router->routername : '(unallocated)'). ':'. $self->NetAddr;
+}
+
 =back
 
 =head1 BUGS
@@ -318,5 +379,7 @@ To be implemented.
 Minimum block size should be a config option.  It's hardcoded at /30 right
 now because that's the smallest block that makes any sense at all.
 
+=cut
+
 1;