Radek Krejci | cbbb197 | 2017-09-21 13:25:19 +0200 | [diff] [blame] | 1 | """ |
| 2 | NETCONF connections |
| 3 | File: connections.py |
| 4 | Author: Radek Krejci <rkrejci@cesnet.cz> |
| 5 | """ |
| 6 | |
Radek Krejci | 67c922d | 2017-09-21 13:56:41 +0200 | [diff] [blame] | 7 | import json |
| 8 | import os |
| 9 | |
Radek Krejci | cbbb197 | 2017-09-21 13:25:19 +0200 | [diff] [blame] | 10 | from liberouterapi import auth |
| 11 | from flask import request |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 12 | import libyang as ly |
Radek Krejci | cbbb197 | 2017-09-21 13:25:19 +0200 | [diff] [blame] | 13 | import netconf2 as nc |
Radek Krejci | cbbb197 | 2017-09-21 13:25:19 +0200 | [diff] [blame] | 14 | |
| 15 | from .inventory import INVENTORY |
| 16 | from .devices import devices_get |
| 17 | from .error import NetopeerException |
| 18 | |
| 19 | sessions = {} |
| 20 | |
| 21 | @auth.required() |
| 22 | def connect(): |
| 23 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 24 | user = session['user'] |
| 25 | path = os.path.join(INVENTORY, user.username) |
| 26 | |
Radek Krejci | a6c8b41 | 2017-10-17 16:59:38 +0200 | [diff] [blame] | 27 | 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 Krejci | cbbb197 | 2017-09-21 13:25:19 +0200 | [diff] [blame] | 37 | 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() |
| 59 | def 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 Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 81 | def 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 Krejci | cbbb197 | 2017-09-21 13:25:19 +0200 | [diff] [blame] | 109 | def 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() |
| 128 | def 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})) |