blob: e852b1643ac8cb3d7055ce0466742ee7640deab9 [file] [log] [blame]
Scott Baker0b48b4b2014-06-19 15:24:58 -07001import datetime
Scott Bakerbf1610d2014-06-18 18:22:03 -07002import os
3import sys
Scott Baker0b48b4b2014-06-19 15:24:58 -07004import time
Scott Bakerbf1610d2014-06-18 18:22:03 -07005import json
6from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
7from django.views.generic import TemplateView, View
8from core.models import *
9from django.forms.models import model_to_dict
10
Scott Baker0b48b4b2014-06-19 15:24:58 -070011def ensure_serializable(d):
12 d2={}
13 for (k,v) in d.items():
14 # datetime is not json serializable
15 if isinstance(v, datetime.datetime):
16 d2[k] = time.mktime(v.timetuple())
Scott Bakerbf5f2542014-06-19 16:59:33 -070017 elif v.__class__.__name__ == "Geoposition":
18 pass
Scott Baker0b48b4b2014-06-19 15:24:58 -070019 else:
20 d2[k] = v
21 return d2
22
Scott Bakerbf5f2542014-06-19 16:59:33 -070023def sliver_to_dict(sliver):
24 d = model_to_dict(sliver)
25 d["slice_id"] = sliver.slice.id
26 d["node_id"] = sliver.node.id
27 return d
28
29def slice_to_dict(slice):
30 d = model_to_dict(slice)
31 d["slivers"] = [sliver_to_dict(x) for x in slice.slivers]
32 return d
33
34def node_to_dict(node):
35 d = model_to_dict(node)
36 d["slivers"] = []
37
38
39class OpenCloudData:
40 def __init__(self, user):
41 self.loadAll()
42
43 def loadAll(self):
44 self.allNodes = list(Node.objects.all())
45 self.allSlices = list(Slice.objects.all())
46 self.allSlivers = list(Sliver.objects.all())
47 self.allSites = list(Site.objects.all())
48
49 self.site_id = {}
50 for site in self.allSites:
51 d = model_to_dict(site)
52 d["node_ids"] = []
53 d["slice_ids"] = []
54 self.site_id[site.id] = ensure_serializable(d)
55
56 self.node_id = {}
57 for node in self.allNodes:
58 d = model_to_dict(node)
59 d["sliver_ids"] = []
60 self.node_id[node.id] = ensure_serializable(d)
61 self.site_id[node.site_id]["node_ids"].append(node.id)
62
63 self.slice_id = {}
64 for slice in self.allSlices:
65 d = model_to_dict(slice)
66 d["sliver_ids"] = []
67 self.slice_id[slice.id] = ensure_serializable(d)
68 self.site_id[slice.site_id]["slice_ids"].append(site.id)
69
70 print self.slice_id.keys()
71
72 self.sliver_id = {}
73 for sliver in self.allSlivers:
74 self.sliver_id[sliver.id] = model_to_dict(sliver)
75
76 self.slice_id[sliver.slice_id]["sliver_ids"].append(sliver.id)
77 self.node_id[sliver.node_id]["sliver_ids"].append(sliver.id)
78
79 def get_opencloud_data(self):
80 return {"slices": self.slice_id.values(),
81 "slivers": self.sliver_id.values(),
82 "nodes": self.node_id.values(),
83 "sites": self.site_id.values()}
84
Scott Bakerbf1610d2014-06-18 18:22:03 -070085class ShellDataView(View):
86 url = r'^shelldata/'
87
88 def get(self, request, **kwargs):
Scott Bakerbf5f2542014-06-19 16:59:33 -070089 result = OpenCloudData(request.user).get_opencloud_data()
Scott Bakerbf1610d2014-06-18 18:22:03 -070090
91 return HttpResponse(json.dumps(result), mimetype='application/json')