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