Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 1 | """ |
| 2 | Manipulation with the YANG schemas. |
| 3 | File: schemas.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 | import errno |
| 10 | import time |
| 11 | |
| 12 | from liberouterapi import auth |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 13 | from flask import request |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 14 | import yang |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 15 | |
Radek Krejci | 2b9bbc2 | 2017-09-21 13:20:48 +0200 | [diff] [blame] | 16 | from .inventory import INVENTORY, inventory_check |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 17 | from .error import NetopeerException |
| 18 | |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 19 | __SCHEMAS_EMPTY = '{"schemas":{"timestamp":0,"schema":[]}}' |
| 20 | |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 21 | |
| 22 | def __schema_parse(path, format = yang.LYS_IN_UNKNOWN): |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 23 | try: |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 24 | ctx = yang.Context(os.path.dirname(path)) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 25 | except Exception as e: |
| 26 | raise NetopeerException(str(e)) |
| 27 | |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 28 | try: |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 29 | module = ctx.parse_path(path, yang.LYS_IN_YANG if format == yang.LYS_IN_UNKNOWN else format) |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 30 | except Exception as e: |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 31 | if format != yang.LYS_IN_UNKOWN: |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 32 | raise NetopeerException(str(e)) |
| 33 | try: |
| 34 | module = ctx.parse_path(path, ly_LYS_IN_YIN) |
| 35 | except Exception as e: |
| 36 | raise NetopeerException(str(e)) |
| 37 | |
| 38 | return module |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 39 | |
| 40 | |
| 41 | def __schemas_init(): |
| 42 | schemas = json.loads(__SCHEMAS_EMPTY) |
| 43 | try: |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 44 | ctx = yang.Context() |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 45 | except Exception as e: |
| 46 | raise NetopeerException(str(e)) |
| 47 | |
| 48 | # initialize the list with libyang's internal modules |
| 49 | modules = ctx.get_module_iter() |
| 50 | for module in modules: |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 51 | schemas['schemas']['schema'].append({'key':module.name() + '@' + module.rev().date(), 'name':module.name(), 'revision':module.rev().date()}) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 52 | return schemas |
| 53 | |
| 54 | |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 55 | def __schemas_inv_load(path): |
| 56 | schemainv_path = os.path.join(path, 'schemas.json') |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 57 | try: |
| 58 | with open(schemainv_path, 'r') as schemas_file: |
| 59 | schemas = json.load(schemas_file) |
| 60 | except OSError as e: |
| 61 | if e.errno == errno.ENOENT: |
| 62 | schemas = __schemas_init() |
| 63 | else: |
| 64 | raise NetopeerException('Unable to use user\'s schemas inventory ' + schemainv_path + ' (' + str(e) + ').') |
| 65 | except ValueError: |
| 66 | schemas = __schemas_init() |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 67 | |
| 68 | return schemas |
| 69 | |
| 70 | def __schemas_inv_save(path, schemas): |
| 71 | schemainv_path = os.path.join(path, 'schemas.json') |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 72 | |
| 73 | # update the timestamp |
| 74 | schemas['schemas']['timestamp'] = time.time() |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 75 | |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 76 | #store the list |
| 77 | try: |
| 78 | with open(schemainv_path, 'w') as schema_file: |
| 79 | json.dump(schemas, schema_file) |
| 80 | except Exception: |
| 81 | pass |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 82 | |
| 83 | return schemas |
| 84 | |
| 85 | def __schemas_update(path): |
| 86 | # get schemas database |
| 87 | schemas = __schemas_inv_load(path) |
| 88 | |
| 89 | # get the previous timestamp |
| 90 | timestamp = schemas['schemas']['timestamp'] |
| 91 | |
| 92 | # check the current content of the storage |
| 93 | for file in os.listdir(path): |
| 94 | if file[-5:] == '.yang': |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 95 | format = yang.LYS_IN_YANG |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 96 | elif file[-4:] == '.yin': |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 97 | format = yang.LYS_IN_YIN |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 98 | else: |
| 99 | continue |
| 100 | |
| 101 | schemapath = os.path.join(path, file); |
| 102 | if os.path.getmtime(schemapath) > timestamp: |
| 103 | # update the list |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 104 | try: |
| 105 | module = __schema_parse(schemapath, format) |
| 106 | if module.rev_size(): |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 107 | schemas['schemas']['schema'].append({'key':module.name() + '@' + module.rev().date(), |
| 108 | 'name':module.name(), |
| 109 | 'revision':module.rev().date(), |
| 110 | 'file':os.path.basename(schemapath)}) |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 111 | else: |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 112 | schemas['schemas']['schema'].append({'key':module.name() + '@', |
| 113 | 'name':module.name(), |
| 114 | 'file':os.path.basename(schemapath)}) |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 115 | except Exception as e: |
| 116 | continue |
| 117 | |
| 118 | #store the list |
| 119 | __schemas_inv_save(path, schemas) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 120 | |
| 121 | # return the up-to-date list |
| 122 | return schemas['schemas']['schema'] |
| 123 | |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 124 | @auth.required() |
| 125 | def schemas_list(): |
| 126 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 127 | user = session['user'] |
Radek Krejci | 2b9bbc2 | 2017-09-21 13:20:48 +0200 | [diff] [blame] | 128 | path = os.path.join(INVENTORY, user.username) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 129 | |
Radek Krejci | b2feaac | 2017-09-21 13:16:54 +0200 | [diff] [blame] | 130 | inventory_check(path) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 131 | schemas = __schemas_update(path) |
| 132 | |
| 133 | return(json.dumps(schemas)) |
| 134 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 135 | |
| 136 | @auth.required() |
| 137 | def schema_get(): |
| 138 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 139 | user = session['user'] |
| 140 | req = request.args.to_dict() |
| 141 | path = os.path.join(INVENTORY, user.username) |
| 142 | |
| 143 | if not 'key' in req: |
| 144 | return(json.dumps({'success': False, 'error-msg': 'Missing schema key.'})) |
| 145 | key = req['key'] |
| 146 | |
| 147 | schemas = __schemas_inv_load(path) |
| 148 | for i in range(len(schemas['schemas']['schema'])): |
| 149 | schema = schemas['schemas']['schema'][i] |
| 150 | if schema['key'] == key: |
| 151 | data = "" |
| 152 | if 'file' in schema: |
| 153 | with open(os.path.join(path, schema['file']), 'r') as schema_file: |
| 154 | data = schema_file.read() |
| 155 | else: |
| 156 | ctx = yang.Context() |
| 157 | data = ctx.get_module(schema['name']).print_mem(yang.LYS_OUT_YANG, 0) |
| 158 | return(json.dumps({'success': True, 'data': data})) |
| 159 | |
| 160 | return(json.dumps({'success': False, 'error-msg':'Schema ' + key + ' not found.'})) |
| 161 | |
| 162 | |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 163 | @auth.required() |
| 164 | def schemas_add(): |
| 165 | if 'schema' not in request.files: |
| 166 | raise NetopeerException('Missing schema file in upload request.') |
| 167 | |
| 168 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 169 | user = session['user'] |
| 170 | file = request.files['schema'] |
| 171 | |
| 172 | # store the file |
Radek Krejci | 2b9bbc2 | 2017-09-21 13:20:48 +0200 | [diff] [blame] | 173 | path = os.path.join(INVENTORY, user.username, file.filename) |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 174 | file.save(path) |
| 175 | |
| 176 | # parse file |
| 177 | try: |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 178 | if file.filename[-5:] == '.yang': |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 179 | format = yang.LYS_IN_YANG |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 180 | elif file.filename[-4:] == '.yin': |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 181 | format = yang.LYS_IN_YIN |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 182 | else: |
Radek Krejci | 0f793fe | 2018-02-14 08:33:45 +0100 | [diff] [blame] | 183 | format = yang.LYS_IN_UNKNOWN |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 184 | module = __schema_parse(path, format) |
| 185 | # TODO: normalize file name to allow removing without remembering schema path |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 186 | except Exception: |
| 187 | os.remove(path) |
| 188 | return(json.dumps({'success': False})) |
| 189 | |
| 190 | return(json.dumps({'success': True})) |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 191 | |
| 192 | @auth.required() |
| 193 | def schemas_rm(): |
| 194 | session = auth.lookup(request.headers.get('Authorization', None)) |
| 195 | user = session['user'] |
Radek Krejci | 2b9bbc2 | 2017-09-21 13:20:48 +0200 | [diff] [blame] | 196 | path = os.path.join(INVENTORY, user.username) |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 197 | |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 198 | key = request.get_json() |
| 199 | if not key: |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 200 | raise NetopeerException('Invalid schema remove request.') |
| 201 | |
| 202 | schemas = __schemas_inv_load(path) |
| 203 | for i in range(len(schemas['schemas']['schema'])): |
| 204 | schema = schemas['schemas']['schema'][i] |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 205 | if schema['key'] == key: |
| 206 | schemas['schemas']['schema'].pop(i) |
| 207 | break; |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 208 | else: |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 209 | schema = None; |
Radek Krejci | 3cb753f | 2017-09-08 16:14:29 +0200 | [diff] [blame] | 210 | |
| 211 | if not schema: |
| 212 | # schema not in inventory |
| 213 | return (json.dumps({'success': False})) |
| 214 | |
| 215 | # update the inventory database |
| 216 | __schemas_inv_save(path, schemas) |
| 217 | |
| 218 | # remove the schema file |
| 219 | if 'revision' in schema: |
| 220 | path = os.path.join(path, schema['name'] + '@' + schema['revision'] + '.yang') |
| 221 | else: |
| 222 | path = os.path.join(path, schema['name'] + '.yang') |
| 223 | os.remove(path) |
| 224 | |
| 225 | # TODO: resolve dependencies |
| 226 | |
| 227 | return(json.dumps({'success': True})) |