maptoslice.in 5.04 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh B Stoller's avatar
Leigh B Stoller committed
3
# Copyright (c) 2008-2014 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
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;

#
# Map a urn to a slice.
# 
sub usage()
{
Leigh B Stoller's avatar
Leigh B Stoller committed
39
    print "Usage: maptoslice <urn> | <ip> | <mac> | vlan\n";
40 41 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
    exit(1);
}
sub fatal($);
my $optlist = "w";
my $fromweb = 0;

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use Interface;
use Node;
use GeniDB;
use GeniSlice;
use GeniUtil;
use GeniCM;
use GeniHRN;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"w"})) {
    $fromweb = 1;
}
usage()
    if (@ARGV != 1);

my $token = $ARGV[0];
my ($type, $id);

# Connect to the proper DB.
DBSetDefault(DBConnect(GENICM_DBNAME()));

Leigh B Stoller's avatar
Leigh B Stoller committed
92 93 94 95 96
if ($token =~ /^\d+$/ && $token <= 4096) {
    $type = "vlan";
    $id   = $token;
}
elsif ($token =~ /^[\w]+$/) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    $type = "mac";
    $id   = $token;
}
elsif ($token =~ /^[\d\.]+$/) {
    $type = "ip";
    $id   = $token;
}
elsif (GeniHRN::IsValid($token)) {
    (undef, $type, $id) = GeniHRN::Parse($token);
}
my $query_result;

if ($type eq "user") {
    $query_result =
	GeniDB::DBQueryFatal("select uuid from geni_slices ".
Leigh B Stoller's avatar
Leigh B Stoller committed
112
			     "where creator_urn='$token'");
113 114 115 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
}
elsif ($type eq "sliver") {
    $query_result =
	GeniDB::DBQueryFatal("(select slice_uuid from geni_slivers ".
			     " where idx='$id') ".
			     "UNION ".
			     "(select slice_uuid from geni_aggregates ".
			     " where idx='$id')");
}
elsif ($type eq "node") {
    $query_result =
	GeniDB::DBQueryFatal("select slice_uuid from geni_slivers ".
			     "where resource_id='$id'");
}
elsif ($type eq "slice") {
    my $slice = GeniSlice->Lookup($token);
    if (defined($slice)) {
	if ($fromweb) {
	    print $slice->uuid() . "\n";
	}
	else {
	    print $slice->urn() . "\n";
	}
	exit(0);
    }
}
elsif ($type eq "ip" || $type eq "mac") {
    my $interface = ($type eq "ip" ?
		     Interface->LookupByIP($id) : Interface->LookupByMAC($id));
    if (!defined($interface)) {
	fatal("No such interface for $id");
    }
    my $node = Node->Lookup($interface->node_id());
    if (!defined($node)) {
	fatal("No such node for $interface");
    }
    my $nodeid = $node->node_id();
    $query_result =
	GeniDB::DBQueryFatal("select slice_uuid from geni_slivers ".
			     "where resource_id='$nodeid'");
}
Leigh B Stoller's avatar
Leigh B Stoller committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
elsif ($type eq "vlan") {
    $query_result =
	emdb::DBQueryFatal("(select s.slice_uuid ".
			   "  from reserved_vlantags as v ".
			   " left join experiment_stats as s on ".
			   "      s.exptidx=v.exptidx ".
			   " where v.tag='$id' and s.slice_uuid is not null)".
			   " union ".
			   "(select s.slice_uuid ".
			   "  from reserved_vlantags as v ".
			   " left join lan_member_attributes as a on ".
			   "      a.lanid=v.lanid and ".
			   "      a.attrkey='portlan_exptidx' ".
			   " left join experiments as e on ".
			   "      e.idx=a.attrvalue ".
			   " left join experiment_stats as s on ".
			   "      s.exptidx=a.attrvalue ".
			   " where v.tag='$id' and ".
			   "       e.idx is not null and ".
			   "       s.slice_uuid is not null)");
}
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
else {
    fatal("Do not know how to map token to a slice!");
}
if (!defined($query_result) || !$query_result->numrows) {
    print STDERR "No slices matching the criteria.\n";
    # For the web interface.
    exit(1);
}
while (my ($token) = $query_result->fetchrow_array()) {
    my $slice = GeniSlice->Lookup($token);
    if (!defined($slice)) {
	print STDERR "Cannot map $token to slice.\n";
	next;
    }
    if ($fromweb) {
	print $slice->uuid() . "\n";
    }
    else {
	print $slice->urn() . "\n";
    }
}

exit(0);

sub fatal($) {
    my ($msg) = @_;

    print STDERR "$msg\n";
    exit(-1);
}