5 from time import mktime
8 from django.shortcuts import render_to_response
9 from django.http import HttpResponse
11 from sfa.trust.certificate import Keypair, Certificate
12 from sfa.client.sfaserverproxy import SfaServerProxy
13 from sfa.client.return_value import ReturnValue
14 from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn
16 from manifold.core.query import Query
17 from manifold.models import db
18 from manifold.models.platform import Platform
19 from manifold.models.user import User
21 from unfold.loginrequired import LoginRequiredView
23 from myslice.settings import logger
25 from rest.json_encoder import MyEncoder
27 def dispatch(request, method):
28 Config = ConfigParser.ConfigParser()
29 Config.read(os.getcwd() + "/myslice/monitor.ini")
31 #logger.debug(request.session['user']['email'])
32 user_email = request.session['user']['email']
44 if request.method == 'POST':
45 req_items = request.POST
46 elif request.method == 'GET':
47 req_items = request.GET
49 for el in req_items.items():
50 if el[0].startswith('rspec'):
52 elif el[0].startswith('platform'):
53 platforms += req_items.getlist('platform[]')
54 #elif el[0].startswith('options'):
55 # options += req_items.getlist('options[]')
56 elif el[0].startswith('hrn'):
58 elif el[0].startswith('urn'):
60 elif el[0].startswith('type'):
62 elif el[0].startswith('recursive'):
67 elif el[0].startswith('display'):
70 if method not in ['GetVersion','ListResources']:
75 urn = hrn_to_urn(hrn, object_type)
78 raise Exception, "Provide urn OR hrn + type as parameters of method %s" % method
81 platforms = get_platforms()
82 #platforms.append('myslice')
83 #results = {'method':method,'platforms':platforms,'rspec':rspec,'options':options}
90 api_options['geni_rspec_version'] = {'type': 'GENI', 'version': '3'}
91 api_options['list_leases'] = 'all'
93 from manifoldapi.manifoldapi import execute_admin_query
95 platform = get_platform_config(pf)
96 logger.debug("platform={}".format(platform))
97 if 'sm' in platform and len(platform['sm']) > 0:
100 server_url = platform['sm']
101 if 'rm' in platform and len(platform['rm']) > 0:
104 server_url = platform['rm']
105 if 'registry' in platform and len(platform['registry']) > 0:
106 logger.debug('registry')
108 server_url = platform['registry']
110 if not Config.has_option('monitor', 'cert') :
111 return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
113 cert = os.path.abspath(Config.get('monitor', 'cert'))
114 if not os.path.isfile(cert) :
115 return HttpResponse(json.dumps({'error' : '-1'}), content_type="application/json")
117 if not Config.has_option('monitor', 'pkey') :
118 return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
120 pkey = os.path.abspath(Config.get('monitor', 'pkey'))
121 if not os.path.isfile(pkey) :
122 return HttpResponse(json.dumps({'error' : '-2'}), content_type="application/json")
124 server = SfaServerProxy(server_url, pkey, cert)
127 # Get user config from Manifold
128 user_config = get_user_config(user_email, pf)
129 if 'delegated_user_credential' in user_config:
130 user_cred = user_config['delegated_user_credential']
135 if 'delegated_%s_credentials'%object_type in user_config:
136 for obj_name, cred in user_config['delegated_%s_credentials'%object_type].items():
141 if method == "GetVersion":
142 result = server.GetVersion()
146 if method == "ListResources":
147 result = server.ListResources([user_cred], api_options)
148 dict_result = xmltodict.parse(result['value'])
149 result['json'] = json.dumps(dict_result)
150 if isinstance(dict_result['rspec']['node'], list):
151 columns.extend(dict_result['rspec']['node'][0].keys())
153 columns.extend(dict_result['rspec']['node'].keys())
155 elif method == "Describe":
156 version = server.GetVersion()
157 logger.debug(version['geni_api'])
159 if version['geni_api'] == 2:
160 # ListResources(slice_hrn)
161 api_options['geni_slice_urn'] = urn
162 result = server.ListResources([object_cred], api_options)
163 dict_result = xmltodict.parse(result['value'])
164 # else GetVersion = v3
166 result = server.Describe([urn] ,[object_cred], api_options)
167 dict_result = xmltodict.parse(result['value']['geni_rspec'])
169 result['json'] = json.dumps(dict_result)
170 if isinstance(dict_result['rspec']['node'], list):
171 columns.extend(dict_result['rspec']['node'][0].keys())
173 columns.extend(dict_result['rspec']['node'].keys())
175 elif method == 'Renew':
176 # Renew till 1 month from now
177 d = datetime.datetime.utcnow() + datetime.timedelta(365/12)
178 date = d.isoformat("T") + "Z"
179 result = server.Renew([urn] ,[object_cred], date, api_options)
180 elif method == 'Delete':
181 result = server.Delete([urn] ,[object_cred], api_options)
182 elif method == 'Allocate':
184 # CreateSliver(slice_hrn)
185 # else GetVersion = v3
186 api_options['call_id'] = unique_call_id()
187 # List of users comes from the Registry
188 api_options['sfa_users'] = sfa_users
189 api_options['geni_users'] = geni_users
190 result = server.Allocate([urn] ,[object_cred], rspec, api_options)
191 elif method == 'Provision':
193 # Nothing it is not supported by v2 AMs
194 api_options['call_id'] = unique_call_id()
195 # List of users comes from the Registry
196 api_options['sfa_users'] = sfa_users
197 api_options['geni_users'] = geni_users
198 result = server.Provision([urn] ,[object_cred], api_options)
199 elif method == 'Status':
200 result = server.Status([urn] ,[object_cred], api_options)
201 elif method == 'PerformOperationalAction':
203 # Nothing it is not supported by v2 AMs
204 result = server.PerformOperationalAction([urn] ,[object_cred], action, api_options)
205 elif method == 'Shutdown':
206 result = server.Shutdown(urn ,[object_cred], api_options)
208 #return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported by AM'}), content_type="application/json")
209 logger.debug('method %s not handled by AM' % method)
214 record_dict = {'urn': urn, 'hrn': hrn, 'type': object_type}
217 options['recursive'] = recursive
218 result = server.List(hrn, user_cred, options)
220 result = filter_records(object_type, result)
221 elif method == "Resolve":
223 # details can be True or False
224 options['details']=True
225 result = server.Resolve(hrn, user_cred, options)
227 result = filter_records(object_type, result)
228 elif method == "Register":
229 # record_dict must be crafted
230 # auth_cred must be selected in the list of auth_creds from user's account
231 result = server.Register(record_dict, auth_cred)
232 elif method == "Update":
233 # record_dict must be crafted
234 # object_cred must be selected in the list of creds for the object type
235 # from user's account
236 result = server.Update(record_dict, object_cred)
237 elif method == "Remove":
239 # auth_cred must be selected in the list of auth_creds from user's account
240 # object_type is required
241 result = server.Remove(hrn, auth_cred, object_type)
243 #return HttpResponse(json.dumps({'error' : '-3','msg':'method not supported by Registry'}), content_type="application/json")
244 logger.debug('method %s not handled by Registry' % method)
249 if isinstance(dict_result['rspec']['node'], list):
250 data = data + dict_result['rspec']['node']
252 data.append(dict_result['rspec']['node'])
255 logger.error(traceback.format_exc())
257 results[pf] = {'error':'-3', 'error_msg': str(e)}
258 if display == 'table':
259 return render_to_response('table-default.html', {'data' : data, 'fields' : columns, 'id' : '@component_id', 'options' : None})
261 results['columns'] = columns
262 return HttpResponse(json.dumps(results, cls=MyEncoder), content_type="application/json")
264 def get_user_account(user_email, platform_name):
266 Returns the user configuration for a given platform.
267 This function does not resolve references.
269 user = db.query(User).filter(User.email == user_email).one()
270 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
271 accounts = [a for a in user.accounts if a.platform == platform]
273 raise Exception, "this account does not exist"
275 if accounts[0].auth_type == 'reference':
276 pf = json.loads(accounts[0].config)['reference_platform']
277 return get_user_account(user_email, pf)
281 def get_user_config(user_email, platform_name):
282 account = get_user_account(user_email, platform_name)
283 return json.loads(account.config) if account.config else {}
287 platforms = db.query(Platform).filter(Platform.gateway_type == 'sfa', Platform.disabled == 0).all()
289 ret.append(p.platform)
293 def get_platform_config(platform_name):
294 platform = db.query(Platform).filter(Platform.platform == platform_name).one()
295 return json.loads(platform.config) if platform.config else {}
297 def filter_records(type, records):
298 filtered_records = []
299 for record in records:
300 if (record['type'] == type) or (type == "all"):
301 filtered_records.append(record)
302 return filtered_records