1package Netdot::Model::RRNAPTR;
2
3use base 'Netdot::Model';
4use warnings;
5use strict;
6
7my $logger = Netdot->log->get_logger('Netdot::Model::DNS');
8
9my $MAX_ORDER      = 2**16 - 1;
10my $MAX_PREFERENCE = $MAX_ORDER;
11
12=head1 Netdot::Model::RRNAPTR - DNS NAPTR record Class
13
14=head1 CLASS METHODS
15=cut
16
17############################################################################
18
19=head2 insert - Insert new RRNAPTR object
20
21    We override the base method to:
22    - Validate fields
23    - Check for conflicts with other record types
24
25  Arguments:
26    See schema
27  Returns:
28    RRNAPTR object
29  Example:
30    my $record = RRNAPTR->insert(\%args)
31
32=cut
33
34sub insert {
35    my($class, $argv) = @_;
36    $class->isa_class_method('insert');
37
38    $class->throw_fatal('Missing required arguments: rr')
39	unless ( $argv->{rr} );
40
41    foreach my $field ( qw/order_field preference flags services regexpr replacement/ ){
42	$class->throw_user("Missing required argument: $field")
43	    unless (defined $argv->{$field});
44    }
45
46    $class->throw_user("Invalid order value: ".$argv->{order_field})
47	if ( $argv->{order_field} < 0 || $argv->{order_field} > $MAX_ORDER );
48
49    $class->throw_user("Invalid preference value: ".$argv->{preference})
50	if ( $argv->{preference} < 0 || $argv->{preference} > $MAX_PREFERENCE );
51
52    $class->throw_user("Invalid services string: ".$argv->{services})
53	if ( !($argv->{services} =~ /^e2u\+/i) );
54
55    my $rr = (ref $argv->{rr})? $argv->{rr} : RR->retrieve($argv->{rr});
56    $class->throw_fatal("Invalid rr argument") unless $rr;
57
58    # TTL needs to be set and converted into integer
59    $argv->{ttl} = (defined($argv->{ttl}) && length($argv->{ttl}))? $argv->{ttl} : $rr->zone->default_ttl;
60    $argv->{ttl} = $class->ttl_from_text($argv->{ttl});
61
62    # Avoid the "CNAME and other records" error condition
63    if ( $rr->cnames ){
64	$class->throw_user("Cannot add any other record to an alias");
65    }
66    if ( $rr->ptr_records ){
67	$class->throw_user("Cannot add any other record when PTR records exist");
68    }
69
70    return $class->SUPER::insert($argv);
71
72}
73
74=head1 INSTANCE METHODS
75=cut
76
77############################################################################
78
79=head2 update
80
81    We override the base method to:
82     - Validate TTL and other values
83
84  Arguments:
85    Hash with field/value pairs
86  Returns:
87    Number of rows updated or -1
88  Example:
89    $record->update(\%args)
90
91=cut
92
93sub update {
94    my($self, $argv) = @_;
95    $self->isa_object_method('update');
96
97    if ( defined $argv->{ttl} && length($argv->{ttl}) ){
98	$argv->{ttl} = $self->ttl_from_text($argv->{ttl});
99    }else{
100	delete $argv->{ttl};
101    }
102    if ( defined $argv->{order_field} ){
103	$self->throw_user("Invalid order value: ".$argv->{order_field})
104	    if ( $argv->{order_field} < 0 || $argv->{order_field} > $MAX_ORDER );
105    }
106    if ( defined $argv->{preference} ){
107	$self->throw_user("Invalid preference value: ".$argv->{preference})
108	    if ( $argv->{preference} < 0 || $argv->{preference} > $MAX_PREFERENCE );
109    }
110    if ( defined $argv->{service} ){
111	$self->throw_user("Invalid service string: ".$argv->{service})
112	    if ( !($argv->{service} =~ /^e2u\+/i) );
113    }
114
115    return $self->SUPER::update($argv);
116}
117
118##################################################################
119
120=head2 as_text
121
122    Returns the text representation of this record
123
124  Arguments:
125    None
126  Returns:
127    string
128  Examples:
129    print $rr->as_text();
130
131=cut
132
133sub as_text {
134    my $self = shift;
135    $self->isa_object_method('as_text');
136
137    return $self->_net_dns->string();
138}
139
140
141##################################################################
142# Private methods
143##################################################################
144
145##################################################################
146sub _net_dns {
147    my $self = shift;
148
149    my $ndo = Net::DNS::RR->new(
150	name        => $self->rr->get_label,
151	ttl         => $self->ttl,
152	class       => 'IN',
153	type        => 'NAPTR',
154	order       => $self->order_field,
155	preference  => $self->preference,
156	flags       => $self->flags,
157	service     => $self->services,
158	regexp      => $self->regexpr,
159	replacement => $self->replacement,
160	);
161
162    return $ndo;
163}
164
165=head1 AUTHOR
166
167Carlos Vicente, C<< <cvicente at ns.uoregon.edu> >>
168
169=head1 COPYRIGHT & LICENSE
170
171Copyright 2012 University of Oregon, all rights reserved.
172
173This program is free software; you can redistribute it and/or modify
174it under the terms of the GNU General Public License as published by
175the Free Software Foundation; either version 2 of the License, or
176(at your option) any later version.
177
178This program is distributed in the hope that it will be useful, but
179WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
180or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
181License for more details.
182
183You should have received a copy of the GNU General Public License
184along with this program; if not, write to the Free Software Foundation,
185Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
186
187=cut
188
189#Be sure to return 1
1901;
191
192