-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
299 lines (246 loc) · 10.8 KB
/
server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Creates an HTTP server with basic websocket communication.
"""
import argparse
from datetime import datetime
import json
import os
import traceback
import webbrowser
import tornado.web
import tornado.websocket
import tornado.escape
import tornado.ioloop
import tornado.locks
from tornado.web import url
import methods
import logging
from distutils.dir_util import copy_tree
from distutils.dir_util import remove_tree
from distutils.dir_util import mkpath
import time
# global variables...
# epnmipaddr = args.epnm_ipaddr
# epnmuser = args.epnm_user
# epnmpassword = args.epnm_pass
epnmipaddr = "10.201.1.246"
baseURL = "https://" + epnmipaddr + "/restconf"
epnmuser = "root"
epnmpassword = "Public123"
open_websockets = []
global_region = 1
class IndexHandler(tornado.web.RequestHandler):
async def get(self):
self.render("templates/index.html", port=args.port, epnm_ip=epnmipaddr, epnm_user=epnmuser,
epnm_pass=epnmpassword, region=global_region)
class AjaxHandler(tornado.web.RequestHandler):
async def post(self):
global global_region
global epnmipaddr
global baseURL
global epnmuser
global epnmpassword
request_body = self.request.body.decode("utf-8")
# request = tornado.escape.recursive_unicode(self.request.arguments)
logging.info("Received AJAX request..")
logging.info(request_body)
request = json.loads(request_body)
try:
action = request['action']
except Exception as err:
logging.warning("Invalid AJAX request")
logging.warning(err)
response = {'status': 'failed', 'error': err}
logging.info(response)
self.write(json.dumps(response))
return
if action == 'collect':
methods.collection(self, request, global_region, baseURL, epnmuser, epnmpassword)
elif action == 'assign-srrg':
methods.assign_srrg(self, request, global_region, baseURL, epnmuser, epnmpassword)
elif action == 'unassign-srrg':
methods.unassign_srrg(self, request, global_region, baseURL, epnmuser, epnmpassword)
elif action == 'delete-srrg':
result = methods.delete_srrg(request, global_region, baseURL, epnmuser, epnmpassword)
self.write(json.dumps(result))
elif action == 'get-all-srrgs':
all_srrgs = methods.getallsrrgs()
self.write(json.dumps(all_srrgs))
elif action == 'get-l1nodes':
l1nodes = methods.getl1nodes()
self.write(json.dumps(l1nodes))
elif action == 'get-l1links':
l1links = methods.getl1links()
self.write(json.dumps(l1links))
elif action == 'get-topolinks':
node_name = request['l1node']
psline = request['psline']
topolinks = methods.gettopolinks_psline(node_name, psline)
self.write(json.dumps(topolinks))
elif action == 'get-topolinks-line-card':
node_name = request['mplsnode']
topolinks = methods.gettopolinks_mpls_node(node_name)
self.write(json.dumps(topolinks))
elif action == 'update-epnm':
time.sleep(2)
epnmipaddr = request['epnm-ip']
baseURL = "https://" + epnmipaddr + "/restconf"
epnmuser = request['epnm-user']
epnmpassword = request['epnm-pass']
region = request['region']
region_int = int(region)
global_region = region_int
response = {'action': 'update-epnm', 'status': 'completed'}
logging.info(response)
self.write(json.dumps(response))
else:
logging.warning("Received request for unknown operation!")
response = {'status': 'unknown', 'error': "unknown request"}
logging.info(response)
self.write(json.dumps(response))
def send_message_open_ws(self, message):
for ws in open_websockets:
ws.send_message(message)
class SRLGHandler(tornado.web.RequestHandler):
def get(self, srlg_num):
srlg = methods.getsrlg(srlg_num)
self.render("templates/srlg_template.html", port=args.port, srlg_num=srlg_num, srlg_data=srlg)
class ROADMNodesHandler(tornado.web.RequestHandler):
def get(self):
l1nodes = methods.getl1nodes()
pools = methods.get_srrg_pools(1)
# if len(pools) == 0:
# pools = ['No Node SRLG Pools Defined']
self.render("templates/roadm_nodes_template.html", port=args.port, l1nodes_data=l1nodes, pools=pools)
class ROADMLinksHandler(tornado.web.RequestHandler):
def get(self):
# full_url = self.request.full_url()
# uri = self.request.uri
# base_full_url = self.request.protocol + "://" + self.request.host
l1links = methods.getl1links()
conduit_pools = methods.get_srrg_pools(0)
degree_pools = methods.get_srrg_pools(2)
self.render("templates/roadm_links_template.html", port=args.port, degree_pools=degree_pools,
conduit_pools=conduit_pools, l1links_data=l1links)
class MPLSNodesHandler(tornado.web.RequestHandler):
def get(self):
mpls_nodes = methods.getmplsnodes()
self.render("templates/mpls_nodes_template.html", port=args.port, mpls_nodes_data=mpls_nodes)
class AllSRLGHandler(tornado.web.RequestHandler):
def get(self):
all_srrg_data = methods.getallsrrgs()
self.render("templates/all_srlg_template.html", port=args.port, all_srrg_data=all_srrg_data)
class AddDropTopoLinksHandler(tornado.web.RequestHandler):
def get(self):
l1node = self.get_argument('l1node')
psline = self.get_argument('psline')
topo_links = methods.gettopolinks_psline(l1node, psline)
add_drop_pools = methods.get_srrg_pools(3)
self.render("templates/topo_links_template_add_drop.html", port=args.port, topo_links_data=topo_links,
add_drop_pools=add_drop_pools, l1node=l1node)
class LineCardTopoLinksHandler(tornado.web.RequestHandler):
def get(self):
thequery = self.request.query
mplsnode = thequery.split('=')[1]
topo_links = methods.gettopolinks_mpls_node(mplsnode)
card_pools = methods.get_srrg_pools(6)
self.render("templates/topo_links_template_line_card.html", port=args.port, topo_links_data=topo_links,
card_pools=card_pools)
class WebSocket(tornado.websocket.WebSocketHandler):
def open(self):
logging.info("WebSocket opened")
open_websockets.append(self)
def send_message(self, message):
self.write_message(message)
def on_message(self, message):
"""Evaluates the function pointed to by json-rpc."""
json_rpc = json.loads(message)
logging.info("Websocket received message: " + json.dumps(json_rpc))
try:
result = getattr(methods,
json_rpc["method"])(**json_rpc["params"])
error = None
except:
# Errors are handled by enabling the `error` flag and returning a
# stack trace. The client can do with it what it will.
result = traceback.format_exc()
error = 1
json_rpc_response = json.dumps({"result": result, "error": error,
"id": json_rpc["id"]},
separators=(",", ":"))
logging.info("Websocket replied with message: " + json_rpc_response)
self.write_message(json_rpc_response)
def on_close(self):
open_websockets.remove(self)
logging.info("WebSocket closed!")
def main():
# Set up logging
try:
os.remove('collection.log')
except Exception as err:
logging.info("No log file to delete...")
logFormatter = logging.Formatter('%(levelname)s: %(message)s')
rootLogger = logging.getLogger()
rootLogger.level = logging.INFO
fileHandler = logging.FileHandler(filename='collection.log')
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
logging.info("Starting webserver...")
current_time = str(datetime.now().strftime('%Y-%m-%d-%H%M-%S'))
logging.info("Current time is: " + current_time)
settings = {
# "static_path": os.path.join(os.path.dirname(__file__), "static"),
"static_path": os.path.normpath(os.path.dirname(__file__)),
# "cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
# "login_url": "/login",
# "xsrf_cookies": True,
}
handlers = [url(r"/", IndexHandler, name="home"),
url(r"/websocket", WebSocket),
url(r'/static/(.*)',
tornado.web.StaticFileHandler,
dict(path=settings['static_path'])),
# {'path': os.path.normpath(os.path.dirname(__file__))}),
url(r'/srlg/([0-9]+)', SRLGHandler),
url(r'/srlg/static/(.*)',
tornado.web.StaticFileHandler,
dict(path=settings['static_path'])),
url(r'/roadmlinks', ROADMLinksHandler, name="roadm_links"),
url(r'/roadmnodes', ROADMNodesHandler, name="roadm_nodes"),
url(r'/mplsnodes', MPLSNodesHandler, name="mpls_nodes"),
url(r'/allsrlg', AllSRLGHandler, name="all_srlg"),
url(r'/topolinks-ad/?', AddDropTopoLinksHandler, name="ad-topo_links"),
url(r'/topolinks-ad/static/(.*)',
tornado.web.StaticFileHandler,
dict(path=settings['static_path'])),
url(r'/topolinks-lc/?', LineCardTopoLinksHandler, name="lc-topo_links"),
url(r'/topolinks-lc/static/(.*)',
tornado.web.StaticFileHandler,
dict(path=settings['static_path'])),
url(r'/ajax', AjaxHandler, name="ajax")
]
application = tornado.web.Application(handlers)
application.listen(args.port)
# webbrowser.open("http://localhost:%d/" % args.port, new=2)
# tornado.ioloop.IOLoop.instance().start()
tornado.ioloop.IOLoop.current().start()
def clean_files():
# Delete all output files
logging.info("Cleaning files from last collection...")
try:
remove_tree('jsonfiles')
remove_tree('jsongets')
except Exception as err:
logging.info("No files to cleanup...")
# Recreate output directories
mkpath('jsonfiles')
mkpath('jsongets')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Starts a webserver for stuff.")
parser.add_argument("--port", type=int, default=8000, help="The port on which "
"to serve the website.")
args = parser.parse_args()
main()