OpenNebula/VNet.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 274 CC 27 SCAR 46.1
CC
27
Cov
0%
get_ar
272.0
_ar
30.0
get_ar_pool
12.0
CRAP 100.8
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=3 sw=3 tw=0:
5 # vim: set expandtab:
6 #
7 # !no_doc!
8 use strict;
9 use warnings;
10  
11 package Net::OpenNebula::VNet;
12 $Net::OpenNebula::VNet::VERSION = '0.317.0';
13 use version;
14  
15 use Net::OpenNebula::RPC;
16 push our @ISA , qw(Net::OpenNebula::RPC);
17  
18 use constant ONERPC => 'vn';
19 use constant ONEPOOLKEY => 'VNET';
20 use constant NAME_FROM_TEMPLATE => 1;
21  
22 sub create {
23 my ($self, $tpl_txt, %option) = @_;
24 return $self->_allocate([ string => $tpl_txt ],
25 [ int => (exists $option{cluster} ? $option{cluster} : -1) ],
26 );
27 }
28  
29 sub used {
30 my ($self) = @_;
31 my $tl = $self->_get_info_extended('TOTAL_LEASES');
32 if ($tl->[0]) {
33 return 1;
34 }
35 };
36  
37 # New since 4.8.0
38 sub _ar {
39 my ($self, $txt, $mode) = @_;
40  
41 if ($self->{rpc}->version() < version->new('4.8.0')) {
42 $self->error("AR RPC API new since 4.8.0");
43 return;
44 }
45  
46 $mode = "add" if (! ($mode && $mode =~ m/^(add|rm|update)$/));
47  
48 my $what = [ string => $txt ];
49 if ($mode =~ m/^(rm|free)$/) {
50 if ($txt =~ m/^\d+$/) {
51 $what = [ int => $txt ];
52 } else {
53 $self->error("_ar mode $mode expects integer ID, got $txt");
54 return;
55 }
56 };
57  
58 return $self->_onerpc("${mode}_ar",
59 [ int => $self->id ],
60 $what,
61 );
62 }
63  
64 sub addar {
65 my ($self, $txt) = @_;
66 return $self->_ar($txt, "add");
67 }
68  
69 # the id is in the template as AR_ID
70 sub updatear {
71 my ($self, $txt) = @_;
72 return $self->_ar($txt, "update");
73 }
74  
75 sub rmar {
76 my ($self, $id) = @_;
77 return $self->_ar($id, "rm");
78 }
79  
80 sub freear {
81 my ($self, $id) = @_;
82 return $self->_ar($id, "free");
83 }
84  
85 # Find the AR matching opts
86 # opts are
87 # ip : check that it is in range (assuming IPv4)
88 # mac : check that it is in range
89 # size : match if range is lower or equal the size
90 # template : (try to) extract ip/mac/size from the template
91 # ip/mac/size defined via opts take precedence
92 # If more then opts is defined, all requirements have to fullfill
93 # Only one AR is returned (first one wins)
94 # (If no opts are specified, first AR is returned)
95 sub get_ar {
96 my ($self, %opts) = @_;
97 $self->verbose("get_ar: no options specified") if (! %opts);
98  
99 if ($opts{template}) {
100 if(! exists($opts{mac}) && $opts{template} =~ m/[^#]*MAC\s*=\s*("|')((?:[0-9a-f]{2}:){5}[0-9a-f]{2})\1/i) {
101 $opts{mac} = $2;
102 $self->debug(1, "get_ar: found MAC $opts{mac} in template, none in opts");
103 }
104  
105 if (! exists($opts{ip}) && $opts{template} =~ m/[^#]*IP\s*=\s*("|')((?:\d{1,3}\.){3}\d{1,3})\1/) {
106 $opts{ip} = $2;
107 $self->debug(1, "get_ar: found IP $opts{ip} in template, none in opts");
108 }
109  
110 if (! exists($opts{size}) && $opts{template} =~ m/[^#]*SIZE\s*=\s*("|')(\d+)\1/) {
111 $opts{size} = $2;
112 $self->debug(1, "get_ar: found SIZE $opts{size} in template, none in opts");
113 }
114 }
115  
116 my %ar_pool = $self->get_ar_pool();
117 return if (! %ar_pool);
118  
119 foreach my $id (sort keys %ar_pool) {
120 my $msg = "get_ar AR_ID $id:";
121 my $mac = $ar_pool{$id}->{MAC}->[0];
122 my $ip = $ar_pool{$id}->{IP}->[0];
123 my $size = $ar_pool{$id}->{SIZE}->[0];
124 if(!defined($size)) {
125 $self->debug(2, "$msg AR SIZE undefined, using 1");
126 $size = 1;
127 }
128  
129 # use inverse logic because all requirements have to match
130 my $match = 1;
131  
132 # no match if requested size is larger then set one
133 if (exists($opts{size}) && $opts{size} > $size) {
134 $self->debug(2, "$msg NO match: requested size $opts{size} larger then AR Size $size");
135 $match = 0;
136 }
137  
138 if (exists($opts{mac})) {
139 # get the range
140 if(!defined($mac)) {
141 $self->debug(2, "$msg NO match: requested mac $opts{mac}, but no MAC defined");
142 $match = 0;
143 } else {
144 # http://www.perlmonks.org/?node_id=440768
145 # Use doubles, no int (bit operators use ints)
146 my $mac_hex2num = sub {
147 my $mac_hex = shift;
148 $mac_hex =~ s/://g;
149 $mac_hex = substr(('0'x12).$mac_hex, -12);
150 my @mac_bytes = unpack("A2"x6, $mac_hex);
151 my $mac_num = 0;
152 foreach (@mac_bytes) {
153 $mac_num = $mac_num * (2**8) + hex($_);
154 }
155 return $mac_num;
156 };
157  
158 my $om = &$mac_hex2num($opts{mac});
159 my $am = &$mac_hex2num($mac);
160 # include boundaries of size?
161 if( ($om < $am) || ($om > ($am + $size -1) )) {
162 $self->debug(2, "$msg NO match: requested mac $opts{mac} falls outside the range starting with AR MAC $mac and size $size (int mac $om AR MAC $am)");
163 $match = 0;
164 }
165 }
166 }
167  
168 if (exists($opts{ip})) {
169 # get the range
170 if(!defined($ip)) {
171 $self->debug(2, "$msg NO match: requested ip $opts{ip}, but no IP defined");
172 $match = 0;
173 } else {
174 # Use doubles, no int (bit operators use ints)
175 my $ip2num = sub {
176 my $ip = shift;
177 my $num = 0;
178 foreach (split(/\./, $ip)) {
179 $num = $num * (2**8) + $_;
180 }
181 return $num;
182 };
183  
184 my $oi = &$ip2num($opts{ip});
185 my $ai = &$ip2num($ip);
186 # include boundaries of size?
187 if( ($oi < $ai) || ($oi > ($ai + $size -1) )) {
188 $self->debug(2, "$msg NO match: requested ip $opts{ip} falls outside the range starting with AR IP $ip and size $size (int ip $oi AR IP $ai)");
189 $match = 0;
190 }
191 }
192 }
193  
194 if($match) {
195 $self->debug(1, "$msg found match, returning AR.");
196 return $ar_pool{$id};
197 } else {
198 $self->debug(1, "$msg no match found, continuing.");
199 }
200 }
201 $self->verbose("get_ar: no matching ARs found. Returning undef.");
202 return;
203  
204 }
205  
206 # Using same opts as get_ar, returns the ID
207 sub get_ar_id {
208 my($self, %opts) = @_;
209  
210 my $arref = $self->get_ar(%opts);
211 if(defined($arref)) {
212 my $id = $arref->{AR_ID}->[0];
213 $self->verbose("get_ar_id: found ID $id");
214 return $id;
215 } else {
216 $self->verbose("get_ar_id: no AR found");
217 }
218 }
219  
220 # Return hash with key=AR_ID, and value the AR instance
221 sub get_ar_pool {
222 my ($self) = @_;
223  
224 if ($self->{rpc}->version() < version->new('4.8.0')) {
225 $self->error("AR RPC API new since 4.8.0");
226 return;
227 }
228  
229 my $ap = $self->_get_info_extended('AR_POOL');
230  
231 my %res;
232  
233 foreach my $arref (@{$ap->[0]->{AR}}) {
234 my $id = int($arref->{AR_ID}->[0]);
235 $res{$id} = $arref;
236 }
237  
238 if (%res) {
239 $self->verbose("AR POOL with ", scalar keys %res, " AR instances found");
240 } else {
241 $self->verbose("empty AR POOL");
242 };
243  
244 return %res;
245 }
246  
247  
248 # Removed since 4.8.0
249 sub _leases {
250 my ($self, $txt, $mode) = @_;
251  
252 if ($self->{rpc}->version() >= version->new('4.8.0')) {
253 $self->error("Leases RPC API removed since 4.8.0");
254 return;
255 }
256  
257 $mode = "add" if (! ($mode && $mode =~ m/^(add|rm)$/));
258  
259 return $self->_onerpc("${mode}leases",
260 [ int => $self->id ],
261 [ string => $txt ]
262 );
263 }
264  
265 sub addleases {
266 my ($self, $txt) = @_;
267 return $self->_leases($txt, "add");
268 }
269  
270 sub rmleases {
271 my ($self, $txt) = @_;
272 return $self->_leases($txt, "rm");
273 }
274  
275 1;