blob: 6c32a7dce2088d0027fdf5bfab1a5db57802af88 [file] [log] [blame]
Radek Krejcicbbb1972017-09-21 13:25:19 +02001"""
2NETCONF connections
3File: connections.py
4Author: Radek Krejci <rkrejci@cesnet.cz>
5"""
6
Radek Krejci67c922d2017-09-21 13:56:41 +02007import json
8import os
9
Radek Krejcicbbb1972017-09-21 13:25:19 +020010from liberouterapi import auth
11from flask import request
Radek Krejci2e578562017-10-17 11:11:13 +020012import libyang as ly
Radek Krejcicbbb1972017-09-21 13:25:19 +020013import netconf2 as nc
Radek Krejcicbbb1972017-09-21 13:25:19 +020014
15from .inventory import INVENTORY
16from .devices import devices_get
17from .error import NetopeerException
18
19sessions = {}
20
21@auth.required()
22def connect():
23 session = auth.lookup(request.headers.get('Authorization', None))
24 user = session['user']
25 path = os.path.join(INVENTORY, user.username)
26
Radek Krejcia6c8b412017-10-17 16:59:38 +020027 data = request.get_json()
28 if 'id' in data:
29 # stored device
30 device = devices_get(data['id'], user.username)
31 elif 'device' in data:
32 # one-time connect, the device is specified in request
33 device = data['device']
34 else:
35 raise NetopeerException('Invalid connection request.')
36
Radek Krejcicbbb1972017-09-21 13:25:19 +020037 if not device:
38 raise NetopeerException('Unknown device to connect to request.')
39
40 nc.setSearchpath(path)
41
42 ssh = nc.SSH(device['username'], password=device['password'])
43 try:
44 session = nc.Session(device['hostname'], device['port'], ssh)
45 except Exception as e:
46 return(json.dumps({'success': False, 'error-msg': str(e)}))
47
48 if not user.username in sessions:
49 sessions[user.username] = {}
50
51 # use key (as hostname:port:session-id) to store the created NETCONF session
52 key = session.host + ":" + str(session.port) + ":" + session.id
53 sessions[user.username][key] = session
54
55 return(json.dumps({'success': True, 'session-key': key}))
56
57
58@auth.required()
59def session_get_capabilities():
60 session = auth.lookup(request.headers.get('Authorization', None))
61 user = session['user']
62 req = request.args.to_dict()
63
64 if not 'key' in req:
65 return(json.dumps({'success': False, 'error-msg': 'Missing session key.'}))
66
67 if not user.username in sessions:
68 sessions[user.username] = {}
69
70 key = req['key']
71 if not key in sessions[user.username]:
72 return(json.dumps({'success': False, 'error-msg': 'Invalid session key.'}))
73
74 cpblts = []
75 for c in sessions[user.username][key].capabilities:
76 cpblts.append(c)
77
78 return(json.dumps({'success': True, 'capabilities': cpblts}))
79
80@auth.required()
Radek Krejci2e578562017-10-17 11:11:13 +020081def session_get():
82 session = auth.lookup(request.headers.get('Authorization', None))
83 user = session['user']
84 req = request.args.to_dict()
85
86 if not 'key' in req:
87 return(json.dumps({'success': False, 'error-msg': 'Missing session key.'}))
88
89 if not user.username in sessions:
90 sessions[user.username] = {}
91
92 key = req['key']
93 if not key in sessions[user.username]:
94 return(json.dumps({'success': False, 'error-msg': 'Invalid session key.'}))
95
96 session = sessions[user.username][key]
97 try:
98 data = session.rpcGet()
99 except nc.ReplyError as e:
100 reply = {'success': False, 'error': []}
101 for err in e.args[0]:
102 reply['error'].append(json.loads(str(err)))
103 return(json.dumps(reply))
104
105 return(json.dumps({'success': True, 'data': json.loads(data.print_mem(ly.LYD_JSON, ly.LYP_WITHSIBLINGS))}))
106
107
108@auth.required()
Radek Krejcicbbb1972017-09-21 13:25:19 +0200109def session_close():
110 session = auth.lookup(request.headers.get('Authorization', None))
111 user = session['user']
112 req = request.args.to_dict()
113
114 if not 'key' in req:
115 return(json.dumps({'success': False, 'error-msg': 'Missing session key.'}))
116
117 if not user.username in sessions:
118 sessions[user.username] = {}
119
120 key = req['key']
121 if not key in sessions[user.username]:
122 return(json.dumps({'success': False, 'error-msg': 'Invalid session key.'}))
123
124 del sessions[user.username][key]
125 return(json.dumps({'success': True}))
126
127@auth.required()
128def session_alive():
129 session = auth.lookup(request.headers.get('Authorization', None))
130 user = session['user']
131 req = request.args.to_dict()
132
133 if not 'key' in req:
134 return(json.dumps({'success': False, 'error-msg': 'Missing session key.'}))
135
136 if not user.username in sessions:
137 sessions[user.username] = {}
138
139 key = req['key']
140 if not key in sessions[user.username]:
141 return(json.dumps({'success': False, 'error-msg': 'Invalid session key.'}))
142
143 return(json.dumps({'success': True}))