removed another bunch of references to geni
[sfa.git] / sfa / managers / aggregate_manager_pl.py
1 ### $Id: slices.py 15842 2009-11-22 09:56:13Z anil $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/plc/slices.py $
3
4 import datetime
5 import time
6 import traceback
7 import sys
8
9 from types import StringTypes
10 from sfa.util.namespace import *
11 from sfa.util.rspec import *
12 from sfa.util.specdict import *
13 from sfa.util.faults import *
14 from sfa.util.record import SfaRecord
15 from sfa.util.policy import Policy
16 from sfa.util.record import *
17 from sfa.util.sfaticket import SfaTicket
18 from sfa.server.registry import Registries
19 from sfa.util.debug import log
20 from sfa.plc.slices import Slices
21 import sfa.plc.peers as peers
22
23 def delete_slice(api, hrn):
24     slicename = hrn_to_pl_slicename(hrn)
25     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
26     if not slices:
27         return 1
28     slice = slices[0]
29
30     # determine if this is a peer slice
31     peer = peers.get_peer(api, hrn)
32     if peer:
33         api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
34     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
35     if peer:
36         api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
37     return 1
38
39 def create_slice(api, hrn, rspec):
40     # XX just import the legacy module and excute that until
41     # we transition the code to this module
42     from sfa.plc.slices import Slices
43     slices = Slices(api)
44     slices.create_slice_aggregate(hrn, rspec)
45
46 def get_ticket(api, slice_hrn, rspec, origin_hrn=None):
47     # the the slice record
48     registries = Registries(api)
49     registry = registries[api.hrn]
50     credential = api.getCredential()
51     records = registry.resolve(credential, slice_hrn)
52     
53     # make sure we get a local slice record
54     record = None  
55     for tmp_record in records:
56         if tmp_record['type'] == 'slice' and \
57            not tmp_record['peer_authority']:
58             record = SliceRecord(dict=tmp_record)
59     if not record:
60         raise RecordNotFound(slice_hrn)
61
62     # get sliver info
63     slivers = Slices(api).get_slivers(slice_hrn)
64     if not slivers:
65         raise SliverDoesNotExist(slice_hrn)
66     
67     # get initscripts
68     initscripts = None
69     data = {
70         'timestamp': int(time.time()),
71         'initscripts': initscripts,
72         'slivers': slivers
73     }
74
75     # create the ticket
76     object_gid = record.get_gid_object()
77     new_ticket = SfaTicket(subject = object_gid.get_subject())
78     new_ticket.set_gid_caller(api.auth.client_gid)
79     new_ticket.set_gid_object(object_gid)
80     new_ticket.set_issuer(key=api.key, subject=api.hrn)
81     new_ticket.set_pubkey(object_gid.get_pubkey())
82     new_ticket.set_attributes(data)
83     new_ticket.set_rspec(rspec)
84     #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
85     new_ticket.encode()
86     new_ticket.sign()
87     
88     return new_ticket.save_to_string(save_parents=True)
89
90 def start_slice(api, hrn):
91     slicename = hrn_to_pl_slicename(hrn)
92     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
93     if not slices:
94         raise RecordNotFound(hrn)
95     slice_id = slices[0]
96     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
97     attribute_id = attreibutes[0]['slice_attribute_id']
98     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
99
100     return 1
101  
102 def stop_slice(api, hrn):
103     slicename = hrn_to_pl_slicename(hrn)
104     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
105     if not slices:
106         raise RecordNotFound(hrn)
107     slice_id = slices[0]['slice_id']
108     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
109     attribute_id = attributes[0]['slice_attribute_id']
110     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
111     return 1
112
113 def reset_slice(api, hrn):
114     # XX not implemented at this interface
115     return 1
116
117 def get_slices(api):
118     # XX just import the legacy module and excute that until
119     # we transition the code to this module
120     from sfa.plc.slices import Slices
121     slices = Slices(api)
122     slices.refresh()
123     return slices['hrn']
124      
125  
126 def get_rspec(api, hrn=None, origin_hrn=None):
127     from sfa.plc.nodes import Nodes
128     nodes = Nodes(api, origin_hrn=origin_hrn)
129     if hrn:
130         rspec = nodes.get_rspec(hrn)
131     else:
132         nodes.refresh()
133         rspec = nodes['rspec'] 
134
135     return rspec
136
137 """
138 Returns the request context required by sfatables. At some point, this mechanism should be changed
139 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
140 return the basic information needed in a dict.
141 """
142 def fetch_context(slice_hrn, user_hrn, contexts):
143     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
144     return base_context
145
146 def main():
147     r = RSpec()
148     r.parseFile(sys.argv[1])
149     rspec = r.toDict()
150     create_slice(None,'plc.princeton.tmacktestslice',rspec)
151
152 if __name__ == "__main__":
153     main()