geni-am.pm.in 5.63 KB
Newer Older
1
2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2013 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
# 
# {{{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.
# 
# }}}
29
30
31
#
use strict;
use English;
32
33
use Frontier::Responder;
use Frontier::RPC2;
34
35

# Do this early so that we talk to the right DB.
36
use vars qw($GENI_DBNAME $GENI_METHODS $EMULAB_PEMFILE
37
	    $GENI_RESPONSE_CONVERTER $GENI_VERSION);
Leigh B Stoller's avatar
Leigh B Stoller committed
38
BEGIN { $GENI_DBNAME = "geni-cm"; }
39
40
41

# Configure variables
my $ETCDIR         = "@prefix@/etc";
Leigh B Stoller's avatar
Leigh B Stoller committed
42
$EMULAB_PEMFILE    = "$ETCDIR/genicm.pem";
43
44
45
46

# Testbed libraries.
use lib '@prefix@/lib';
use GeniAM;
47
use GeniResponse;
48

49
if (!defined($GENI_VERSION)) {
50
    $GENI_VERSION = "2.0";
51
52
53
54
55
}

GeniAM::SetGeniVersion($GENI_VERSION);

if ($GENI_VERSION eq "1.0" || $GENI_VERSION eq "2.0") {
56
57
58
59
60
61
62
    $GENI_METHODS = {
        "GetVersion"    => \&GeniAM::GetVersion,
        "ListResources" => \&GeniAM::ListResources,
        "CreateSliver"  => \&GeniAM::CreateSliver,
        "DeleteSliver"  => \&GeniAM::DeleteSliver,
        "SliverStatus"  => \&GeniAM::SliverStatus,
        "RenewSliver"   => \&GeniAM::RenewSliver,
63
        "Shutdown"      => \&GeniAM::Shutdown,
64
        "CreateImage"   => \&GeniAM::CreateImage,
65
        "DeleteImage"   => \&GeniAM::DeleteImage,
66
        "ListImages"    => \&GeniAM::ListImages,
67
    };
68
69
70
71
72
73
74
75
76
77
78
79
} elsif ($GENI_VERSION eq "3.0") {
    $GENI_METHODS = {
        "GetVersion"    => \&GeniAM::GetVersion,
        "ListResources" => \&GeniAM::ListResources,
	"Describe"      => \&GeniAM::Describe,
	"Allocate"      => \&GeniAM::Allocate,
	"Renew"         => \&GeniAM::Renew,
	"Provision"     => \&GeniAM::Provision,
	"Status"        => \&GeniAM::Status,
	"PerformOperationalAction" => \&GeniAM::PerformOperationalAction,
	"Delete"        => \&GeniAM::Delete,
	"Shutdown"      => \&GeniAM::Shutdown,
80
	"Update"        => \&GeniAM::Update,
Jonathon Duerig's avatar
Jonathon Duerig committed
81
        "Cancel"        => \&GeniAM::Cancel,
82
        "CreateImage"   => \&GeniAM::CreateImage,
83
        "DeleteImage"   => \&GeniAM::DeleteImage,
84
        "ListImages"    => \&GeniAM::ListImages,
85
    };
86
87
}

88
89
#----------------------------------------------------------------------
# Morph the ProtoGENI response (a hash with three keys; a
90
# GeniResponse) into a GENI AMv1 response (a single value or a fault if
91
92
# there is an error).
#
93
# $geni_response is an XML RPC response, which is a three element hash. The
94
95
# value element is the GeniResponse hash.
#----------------------------------------------------------------------
96
sub ConvertResponseV1($)
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
    my ($geni_response) = @_;

    my $decoder = Frontier::RPC2->new();
    my $response;
    
    if (GeniResponse::IsError($geni_response)) {
	# An error result gets mapped to an XML RPC fault
	$response =
	    $decoder->encode_fault(GeniResponse::code($geni_response),
				   GeniResponse::output($geni_response));
    } else {
	# A successful result means return the value
	$response =
	    $decoder->encode_response(GeniResponse::value($geni_response));
    }
    return $response;
}

116
117
118
119
120
121
122
123
124
125
126
#----------------------------------------------------------------------
# Morph the ProtoGENI response (a hash with three keys; a
# GeniResponse) into a GENI AMv2 response (same value and output, but
# more complex code format).
#
# $geni_response is an XML RPC response, which is a three element hash. The
# value element is the GeniResponse hash.
#----------------------------------------------------------------------
sub ConvertResponseV2($)
{
    my ($geni_response) = @_;
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    my $decoder = Frontier::RPC2->new();
    my $version = $geni_response->{"geni_api"};
    my $am_code = GeniResponse::code($geni_response);
    # For standard GENI errors, the geni_code is the same as our am_code
    my $geni_code = $am_code;
    if ($am_code < GeniResponse::GENIRESPONSE_SUCCESS() ||
	$am_code > GeniResponse::GENIRESPONSE_ALREADYEXISTS()) {
	# If the code is outside of the standard GENI code range, set
	# the geni_code to ERROR
	$geni_code = GeniResponse::GENIRESPONSE_ERROR();
    }
    my $blob = {
	"value" => GeniResponse::value($geni_response),
	"output" => GeniResponse::output($geni_response),
	"code" => {
	    "geni_code" => $geni_code,
	    "am_code" => $am_code,
	    "am_type" => "protogeni"
	}
    };
148
149
150
    if (exists($geni_response->{'protogeni_error_log'})) {
	$blob->{'code'}->{'protogeni_error_log'} =
	    $geni_response->{'protogeni_error_log'};
151
    }
152
153
154
155
    if (exists($geni_response->{'protogeni_error_url'})) {
	$blob->{'code'}->{'protogeni_error_url'} =
	    $geni_response->{'protogeni_error_url'};
    }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

    if (defined($version)) {
	$blob->{"geni_api"} = $version;
    }

    return $decoder->encode_response($blob);
}

if ($GENI_VERSION eq "1.0") {
    $GENI_RESPONSE_CONVERTER = \&ConvertResponseV1;
} else {
    $GENI_RESPONSE_CONVERTER = \&ConvertResponseV2;
}

1;