1package Netdot::Model::Vlan; 2 3use base 'Netdot::Model'; 4use warnings; 5use strict; 6 7my $logger = Netdot->log->get_logger('Netdot::Model::Device'); 8 9=head1 NAME 10 11Netdot::Model::Vlan 12 13=head1 SYNOPSIS 14 15Netdot VLAN Class 16 17=head1 CLASS METHODS 18=cut 19 20###################################################################################### 21 22=head2 insert - insert vlan objects 23 24 We override the base method to: 25 - Automatically assign Vlan to a VLAN group if it applies 26 - Validate input 27 28 Arguments: 29 hash ref with field/value pairs 30 Returns: 31 New Vlan object 32 Examples: 33 $nevlan = Vlan->insert({vid=>'100', description=>'Vlan 100'}); 34 35=cut 36 37sub insert{ 38 my ($class, $argv) = @_; 39 $class->isa_class_method('insert'); 40 41 $class->throw_user("Missing required arguments: vlan id") 42 unless (exists $argv->{vid}); 43 $class->_validate_vid($argv->{vid}); 44 45 $argv->{vlangroup} = $class->_find_group($argv->{vid}) || undef; 46 47 my $new = $class->SUPER::insert($argv); 48 return $new; 49} 50 51###################################################################################### 52 53=head2 search 54 55 We override the base method to: 56 - Validate input 57 58 Arguments: 59 array 60 Returns: 61 See Class::DBI::search() 62 Examples: 63 my @vlans = Vlan->search(vid=>$number); 64 65=cut 66 67sub search { 68 my ($class, @args) = @_; 69 $class->isa_class_method('search'); 70 71 # Class::DBI::search() might include an extra 'options' hash ref 72 # at the end. In that case, we want to extract the 73 # field/value hash first. 74 my @nargs = @args; 75 my $opts = @nargs % 2 ? pop @nargs : {}; 76 my %args = @nargs; 77 78 return $class->SUPER::search(@args); 79} 80 81=head1 INSTANCE METHODS 82=cut 83 84###################################################################################### 85 86=head2 update - update VLAN objects 87 88 We override the base method to: 89 - Validate VID 90 - Automatically assign Vlan to a VLAN group if needed 91 92 Arguments: 93 hash ref with field/value pairs 94 Returns: 95 Vlan object 96 Examples: 97 98=cut 99 100sub update{ 101 my ($self, $argv) = @_; 102 $self->isa_object_method('update'); 103 104 $self->_validate_vid($argv->{vid}) if exists $argv->{vid}; 105 106 if ( exists $argv->{vid} && $argv->{vid} != $self->vid ){ 107 # reassign group 108 $argv->{vlangroup} = $self->_find_group($argv->{vid}); 109 } 110 return $self->SUPER::update($argv); 111} 112######################################################################################### 113# 114# Private methods 115# 116######################################################################################### 117# 118# Arguments: vlan id 119# Returns: VlanGroup object if found or undef if not found 120# 121sub _find_group{ 122 my ($self, $vid) = @_; 123 $self->throw_fatal("Missing required arguments: vid") 124 unless defined $vid; 125 126 foreach my $group ( VlanGroup->retrieve_all() ){ 127 if ( $vid >= $group->start_vid && $vid <= $group->end_vid ){ 128 return $group; 129 } 130 } 131 return; 132} 133 134 135######################################################################################### 136# Make sure VLAN ID is within valid range (configureable) 137# Arguments: vlan id 138# Returns: 1 if successful 139# 140sub _validate_vid { 141 my ($self, $vid) = @_; 142 $self->throw_fatal("Missing required parameter: VLAN ID") 143 unless $vid; 144 my ($min, $max) = @{Netdot->config->get('VALID_VLAN_ID_RANGE')}; 145 $self->throw_user("VLAN id '$vid' is invalid. Valid range is $min - $max") 146 unless ( ($vid =~ /^\d+$/) && ($vid >= $min) && ($vid <= $max) ); 147 1; 148} 149 150=head1 AUTHOR 151 152Carlos Vicente, C<< <cvicente at ns.uoregon.edu> >> 153 154=head1 COPYRIGHT & LICENSE 155 156Copyright 2012 University of Oregon, all rights reserved. 157 158This program is free software; you can redistribute it and/or modify 159it under the terms of the GNU General Public License as published by 160the Free Software Foundation; either version 2 of the License, or 161(at your option) any later version. 162 163This program is distributed in the hope that it will be useful, but 164WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY 165or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 166License for more details. 167 168You should have received a copy of the GNU General Public License 169along with this program; if not, write to the Free Software Foundation, 170Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 171 172=cut 173 1741; 175