-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest.py
89 lines (78 loc) · 3.18 KB
/
rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from ryu.topology.api import get_switch
from ryu.app.wsgi import ControllerBase, route
from webob import Response
import json
import utils
class RestController(ControllerBase):
def __init__(self, req, link, data, **config):
super(RestController, self).__init__(req, link, data, **config)
self.controller = data["controller"]
# route to return all the links in the network and their associated cost.
# In format <nodei-nodej>: <cost>
@route("links", "/links", methods=["GET"])
def get_links(self, req, **kwargs):
links = self.controller.link_data
links = {str(key): value for key, value in links.items()}
links = json.dumps(links)
print(links)
return Response(json_body=links, content_type="application/json")
# route to ask from the user to select a host from where paths to all other hosts need to be computed.
# Paths will be displayed as following:
# Hi-Hj :Hi->Si->Sj->….->Hj
@route("paths", "/paths/{host}", methods=["GET"])
def get_paths(self, req, **kwargs):
try:
host = kwargs["host"]
_, paths = utils.dijkstra(
host, 0, self.controller.adj, self.controller.link_data
)
return Response(json_body=paths, content_type="application/json")
except Exception as e:
print(e)
return Response(
json_body={"error": str(e)}, content_type="application/json", status=400
)
@route("add_connection", "/add", methods=["POST"])
def add_connection(self, req, **kwargs):
id_spec = req.json.get("id_spec")
src = req.json.get("src")
dst = req.json.get("dst")
req_bw = req.json.get("bw")
print(id_spec, src, dst, req_bw)
if id_spec == "ip":
src = self.controller.get_host_by_ip(src)
dst = self.controller.get_host_by_ip(dst)
elif id_spec == "mac":
src = self.controller.get_host_by_mac(src)
dst = self.controller.get_host_by_mac(dst)
# Updated path
_, paths = utils.dijkstra(
src, req_bw, self.controller.adj, self.controller.link_data
)
path = paths[dst]
rules = self.controller.add_path_rules(path, req_bw)
# Original path
_, orig_paths = utils.dijkstra(
src, req_bw, self.controller.adj, self.controller.orig_link_data
)
orig_path = orig_paths[dst]
orig_rules = self.controller.add_path_rules(
orig_path, req_bw, new_connection=True
)
data = {
"rules": rules,
"path": path,
"orig_rules": orig_rules,
"orig_path": orig_path,
}
return Response(json_body=data, content_type="application/json")
# Only for veiwing purposes
@route("flows", "/flows/{dpid}", methods=["GET"])
def list_flows(self, req, **kwargs):
dpid = int(kwargs["dpid"])
dp = get_switch(self.controller.topology_api_app, dpid)[0].dp
self.controller.send_flow_request(dp)
self.controller.lock.wait()
return Response(
json_body=self.controller.flows, content_type="application/json"
)