import repository from arizona
[raven.git] / apps / geni / mprepo_geni_libs / sfa / util / sfaticket.py
1 #
2 # implements SFA tickets
3 #
4
5 ### $Id: sfaticket.py 14306 2009-07-06 20:38:44Z thierry $
6 ### $URL: https://svn.planet-lab.org/svn/geniwrapper/trunk/sfa/util/sfaticket.py $
7
8 import xmlrpclib
9
10 from sfa.trust.certificate import Certificate
11 from sfa.trust.rights import *
12 from sfa.trust.gid import *
13
14 # Ticket is tuple:
15 #   (gidCaller, gidObject, attributes, rspec, delegate)
16 #
17 #    gidCaller = GID of the caller performing the operation
18 #    gidObject = GID of the slice
19 #    attributes = slice attributes (keys, vref, instantiation, etc)
20 #    rspec = resources
21
22 class SfaTicket(Certificate):
23     gidCaller = None
24     gidObject = None
25     attributes = {}
26     rspec = {}
27     delegate = False
28
29     def __init__(self, create=False, subject=None, string=None, filename=None):
30         Certificate.__init__(self, create, subject, string, filename)
31
32     def set_gid_caller(self, gid):
33         self.gidCaller = gid
34
35     def get_gid_caller(self):
36         if not self.gidCaller:
37             self.decode()
38         return self.gidCaller
39
40     def set_gid_object(self, gid):
41         self.gidObject = gid
42
43     def get_gid_object(self):
44         if not self.gidObject:
45             self.decode()
46         return self.gidObject
47
48     def set_attributes(self, gid):
49         self.attributes = gid
50
51     def get_attributes(self):
52         if not self.attributes:
53             self.decode()
54         return self.attributes
55
56     def set_rspec(self, gid):
57         self.rspec = gid
58
59     def get_rspec(self):
60         if not self.rspec:
61             self.decode()
62         return self.rspec
63
64     def set_delegate(self, delegate):
65         self.delegate = delegate
66
67     def get_delegate(self):
68         if not self.delegate:
69             self.decode()
70         return self.delegate
71
72     def encode(self):
73         dict = {"gidCaller": None,
74                 "gidObject": None,
75                 "attributes": self.attributes,
76                 "rspec": self.rspec,
77                 "delegate": self.delegate}
78         if self.gidCaller:
79             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
80         if self.gidObject:
81             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
82         str = xmlrpclib.dumps((dict,), allow_none=True)
83         self.set_data(str)
84
85     def decode(self):
86         data = self.get_data()
87         if data:
88             dict = xmlrpclib.loads(self.get_data())[0][0]
89         else:
90             dict = {}
91
92         self.attributes = dict.get("attributes", {})
93         self.rspec = dict.get("rspec", {})
94         self.delegate = dict.get("delegate", False)
95
96         gidCallerStr = dict.get("gidCaller", None)
97         if gidCallerStr:
98             self.gidCaller = GID(string=gidCallerStr)
99         else:
100             self.gidCaller = None
101
102         gidObjectStr = dict.get("gidObject", None)
103         if gidObjectStr:
104             self.gidObject = GID(string=gidObjectStr)
105         else:
106             self.gidObject = None
107
108     def dump(self, dump_parents=False):
109         print "TICKET", self.get_subject()
110
111         print "  gidCaller:"
112         gidCaller = self.get_gid_caller()
113         if gidCaller:
114             gidCaller.dump(8, dump_parents)
115
116         print "  gidObject:"
117         gidObject = self.get_gid_object()
118         if gidObject:
119             gidObject.dump(8, dump_parents)
120
121         print "  attributes:"
122         for attrname in self.get_attributes().keys():
123             print "        ", attrname, self.get_attributes()[attrname]
124
125         print "       rspec:"
126         for attrname in self.get_rspec().keys():
127             print "        ", attrname, self.get_rspec()[attrname]
128
129         if self.parent and dump_parents:
130            print "PARENT",
131            self.parent.dump(dump_parents)