BlockstoreType.pm 5.86 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2012,2013,2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
package BlockstoreType;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

use libdb;
use libtestbed;
use English;
use Data::Dumper;
use overload ('""' => 'Stringify');

# Cache of instances to avoid regenerating them.
my %bstypes	= ();
41
BEGIN { use emutil; emutil::AddCache(\%bstypes); }
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
my $debug	= 0;

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

#
# Lookup a (physical) storage object type and create a class instance to 
# return.
#
sub Lookup($$)
{
    my ($class, $type) = @_;

    # Look in cache first
    return $bstypes{$type}
        if (exists($bstypes{$type}));

    my $self         = {};
    $self->{"TYPE"}  = $type;
    $self->{"ATTRS"} = undef;

    bless($self, $class);

    # Load attributes for type from DB.  No attrs means type doesn't exist.
    $self->LoadAttributes();
    if (!$self->{"ATTRS"}) {
	return undef;
    }

    # Add to cache.
    $bstypes{$type} = $self;
    return $self;
}

#
# Force a reload of the data.
#
sub LookupSync($$)
{
    my ($class, $type) = @_;

    # delete from cache
    delete($bstypes{$type})
        if (exists($bstypes{$type}));

    return Lookup($class, $type);
}

#
# Return a list of all types.
#
sub AllTypes($)
{
    my ($class)  = @_;
    my @alltypes = ();
    
    my $query_result =
	DBQueryWarn("select distinct type from blockstore_type_attributes");
    
Kirk Webb's avatar
Kirk Webb committed
108
    return ()
109 110 111 112 113 114
	if (!$query_result || !$query_result->numrows);

    while (my ($type) = $query_result->fetchrow_array()) {
	my $typeinfo = Lookup($class, $type);

	# Something went wrong?
Kirk Webb's avatar
Kirk Webb committed
115
	return ()
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	    if (!defined($typeinfo));
	
	push(@alltypes, $typeinfo);
    }
    return @alltypes;
}

sub AllClasses($)
{
    my ($class) = @_;
    my @allclasses = ();

    my @alltypes = $class->AllTypes();

    foreach my $bst (@alltypes) {
	my $cl = $bst->class();
	if ($cl) {
	    push(@allclasses, $cl)
	}
    }
    return @allclasses;
}

sub AllProtocols($)
{
    my ($class) = @_;
    my @allprotos = ();

    my @alltypes = $class->AllTypes();

    foreach my $bst (@alltypes) {
	my $proto = $bst->protocol();
	if ($proto) {
	    push(@allprotos, $proto)
	}
    }
    return @allprotos;
}

#
# Load attributes if not already loaded.
#
sub LoadAttributes($)
{
    my ($self) = @_;

    return -1
	if (!ref($self));

    return 0
	if (defined($self->{"ATTRS"}));

    #
    # Get the attribute array.
    #
    my $type = $self->type();
    
    my $query_result =
174
	DBQueryWarn("select attrkey,attrvalue,attrtype,isfeature ".
175 176 177 178
		    "  from blockstore_type_attributes ".
		    "where type='$type'");

    $self->{"ATTRS"} = {};
179
    while (my ($key,$val,$type,$isfeature) = $query_result->fetchrow_array()) {
180 181
	$self->{"ATTRS"}->{$key} = { "key"   => $key,
				     "value" => $val,
182 183
				     "type"  => $type ,
				     "isfeature" => $isfeature };
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    }
    return 0;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $type  = $self->type();
    my $class = $self->class();

    return "[BlockstoreType: $type/$class]";
}

#
# Look for an attribute.
#
204
sub GetAttribute($$;$$$)
205
{
206
    my ($self, $attrkey, $pattrvalue, $pattrtype, $pattrfeature) = @_;
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    
    goto bad
	if (!ref($self));

    $self->LoadAttributes() == 0
	or goto bad;

    if (!exists($self->{"ATTRS"}->{$attrkey})) {
	return undef
	    if (!defined($pattrvalue));
	$$pattrvalue = undef;
	return 0;
    }

    my $ref = $self->{"ATTRS"}->{$attrkey};

    # Return value instead if a $pattrvalue not provided. 
    return $ref->{'value'}
225
        if (!defined($pattrvalue));
226 227 228 229
    
    $$pattrvalue = $ref->{'value'};
    $$pattrtype  = $ref->{'type'}
        if (defined($pattrtype));
230 231
    $$pattrfeature = $ref->{'isfeature'}
        if (defined($pattrfeature));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

    return 0;
    
  bad:
    return undef
	if (!defined($pattrvalue));
    $$pattrvalue = undef;
    return -1;
}

#
# Grab all attributes.
#
sub GetAttributes($)
{
    my ($self) = @_;
    
    return undef
	if (!ref($self));

    $self->LoadAttributes() == 0
	or return undef;

    return $self->{"ATTRS"};
}

# Shortcuts for typical attributes.
259
sub type($)            {return $_[0]->{'TYPE'}; }
260 261 262 263 264 265
sub class($;$)         {return GetAttribute($_[0], "class", $_[1]); }
sub protocol($;$)      {return GetAttribute($_[0], "protocol", $_[1]); }

#
# Set the value of an attribute
#
266
sub SetAttribute($$$;$$)
267
{
268
    my ($self, $attrkey, $attrvalue, $attrtype, $attrfeature) = @_;
269 270 271 272 273 274 275 276 277
    
    goto bad
	if (!ref($self));

    $self->LoadAttributes() == 0
	or return -1;

    $attrtype = "string"
	if (!defined($attrtype));
278
    $attrfeature = (defined($attrfeature) && $attrfeature) ? 1 : 0;
279 280 281
    my $safe_attrvalue = DBQuoteSpecial($attrvalue);
    my $type = $self->type();

282
    DBQueryWarn("replace into blockstore_type_attributes set ".
283
		"  type='$type', attrkey='$attrkey', ".
284 285
		"  attrtype='$attrtype', attrvalue=$safe_attrvalue, ".
		"  isfeature='$attrfeature'")
286 287 288 289 290 291 292 293 294
	or return -1;

    $self->{"ATTRS"}->{$attrkey} = $attrvalue;
    return 0;
}

# _Always_ make sure that this 1 is at the end of the file...

1;