| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| tierno | 9202102 | 2018-09-12 16:29:23 +0200 | [diff] [blame] | 4 | # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U. |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 5 | # This file is part of openmano |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | ''' |
| tierno | 66aa037 | 2016-07-06 17:31:12 +0200 | [diff] [blame] | 25 | utils is a module that implements functions that are used by all openmano modules, |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 26 | dealing with aspects such as reading/writing files, formatting inputs/outputs for quick translation |
| 27 | from dictionaries to appropriate database dictionaries, etc. |
| 28 | ''' |
| 29 | __author__="Alfonso Tierno, Gerardo Garcia" |
| 30 | __date__ ="$08-sep-2014 12:21:22$" |
| 31 | |
| 32 | import datetime |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 33 | import warnings |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 34 | from jsonschema import validate as js_v, exceptions as js_e |
| 35 | #from bs4 import BeautifulSoup |
| 36 | |
| 37 | def read_file(file_to_read): |
| 38 | """Reads a file specified by 'file_to_read' and returns (True,<its content as a string>) in case of success or (False, <error message>) in case of failure""" |
| 39 | try: |
| 40 | f = open(file_to_read, 'r') |
| 41 | read_data = f.read() |
| 42 | f.close() |
| venkatamahesh | 6ecca18 | 2017-01-27 23:04:40 +0530 | [diff] [blame] | 43 | except Exception as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 44 | return (False, str(e)) |
| 45 | |
| 46 | return (True, read_data) |
| 47 | |
| 48 | def write_file(file_to_write, text): |
| 49 | """Write a file specified by 'file_to_write' and returns (True,NOne) in case of success or (False, <error message>) in case of failure""" |
| 50 | try: |
| 51 | f = open(file_to_write, 'w') |
| 52 | f.write(text) |
| 53 | f.close() |
| venkatamahesh | 6ecca18 | 2017-01-27 23:04:40 +0530 | [diff] [blame] | 54 | except Exception as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 55 | return (False, str(e)) |
| 56 | |
| 57 | return (True, None) |
| 58 | |
| 59 | def format_in(http_response, schema): |
| 60 | try: |
| 61 | client_data = http_response.json() |
| 62 | js_v(client_data, schema) |
| 63 | #print "Input data: ", str(client_data) |
| 64 | return True, client_data |
| venkatamahesh | 6ecca18 | 2017-01-27 23:04:40 +0530 | [diff] [blame] | 65 | except js_e.ValidationError as exc: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 66 | print "validate_in error, jsonschema exception ", exc.message, "at", exc.path |
| 67 | return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path) |
| 68 | |
| 69 | def remove_extra_items(data, schema): |
| 70 | deleted=[] |
| 71 | if type(data) is tuple or type(data) is list: |
| 72 | for d in data: |
| 73 | a= remove_extra_items(d, schema['items']) |
| 74 | if a is not None: deleted.append(a) |
| 75 | elif type(data) is dict: |
| 76 | #TODO deal with patternProperties |
| 77 | if 'properties' not in schema: |
| 78 | return None |
| 79 | for k in data.keys(): |
| 80 | if k not in schema['properties'].keys(): |
| 81 | del data[k] |
| 82 | deleted.append(k) |
| 83 | else: |
| 84 | a = remove_extra_items(data[k], schema['properties'][k]) |
| 85 | if a is not None: deleted.append({k:a}) |
| 86 | if len(deleted) == 0: return None |
| 87 | elif len(deleted) == 1: return deleted[0] |
| 88 | else: return deleted |
| 89 | |
| 90 | #def format_html2text(http_content): |
| 91 | # soup=BeautifulSoup(http_content) |
| 92 | # text = soup.p.get_text() + " " + soup.pre.get_text() |
| 93 | # return text |
| 94 | |
| 95 | |
| 96 | def convert_bandwidth(data, reverse=False): |
| 97 | '''Check the field bandwidth recursivelly and when found, it removes units and convert to number |
| 98 | It assumes that bandwidth is well formed |
| 99 | Attributes: |
| 100 | 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid |
| 101 | 'reverse': by default convert form str to int (Mbps), if True it convert from number to units |
| 102 | Return: |
| 103 | None |
| 104 | ''' |
| 105 | if type(data) is dict: |
| 106 | for k in data.keys(): |
| 107 | if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: |
| 108 | convert_bandwidth(data[k], reverse) |
| 109 | if "bandwidth" in data: |
| 110 | try: |
| 111 | value=str(data["bandwidth"]) |
| 112 | if not reverse: |
| 113 | pos = value.find("bps") |
| 114 | if pos>0: |
| 115 | if value[pos-1]=="G": data["bandwidth"] = int(data["bandwidth"][:pos-1]) * 1000 |
| 116 | elif value[pos-1]=="k": data["bandwidth"]= int(data["bandwidth"][:pos-1]) / 1000 |
| 117 | else: data["bandwidth"]= int(data["bandwidth"][:pos-1]) |
| 118 | else: |
| 119 | value = int(data["bandwidth"]) |
| 120 | if value % 1000 == 0: data["bandwidth"]=str(value/1000) + " Gbps" |
| 121 | else: data["bandwidth"]=str(value) + " Mbps" |
| 122 | except: |
| 123 | print "convert_bandwidth exception for type", type(data["bandwidth"]), " data", data["bandwidth"] |
| 124 | return |
| 125 | if type(data) is tuple or type(data) is list: |
| 126 | for k in data: |
| 127 | if type(k) is dict or type(k) is tuple or type(k) is list: |
| 128 | convert_bandwidth(k, reverse) |
| 129 | |
| 130 | |
| 131 | |
| 132 | def convert_datetime2str(var): |
| 133 | '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s' |
| 134 | It enters recursively in the dict var finding this kind of variables |
| 135 | ''' |
| 136 | if type(var) is dict: |
| 137 | for k,v in var.items(): |
| 138 | if type(v) is datetime.datetime: |
| 139 | var[k]= v.strftime('%Y-%m-%dT%H:%M:%S') |
| 140 | elif type(v) is dict or type(v) is list or type(v) is tuple: |
| 141 | convert_datetime2str(v) |
| 142 | if len(var) == 0: return True |
| 143 | elif type(var) is list or type(var) is tuple: |
| 144 | for v in var: |
| 145 | convert_datetime2str(v) |
| 146 | |
| 147 | def convert_str2boolean(data, items): |
| 148 | '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean |
| 149 | Done recursively |
| 150 | Attributes: |
| 151 | 'data': dictionary variable to be checked. None or empty is considered valid |
| 152 | 'items': tuple of keys to convert |
| 153 | Return: |
| 154 | None |
| 155 | ''' |
| 156 | if type(data) is dict: |
| 157 | for k in data.keys(): |
| 158 | if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: |
| 159 | convert_str2boolean(data[k], items) |
| 160 | if k in items: |
| 161 | if type(data[k]) is str: |
| 162 | if data[k]=="false" or data[k]=="False": data[k]=False |
| 163 | elif data[k]=="true" or data[k]=="True": data[k]=True |
| 164 | if type(data) is tuple or type(data) is list: |
| 165 | for k in data: |
| 166 | if type(k) is dict or type(k) is tuple or type(k) is list: |
| 167 | convert_str2boolean(k, items) |
| 168 | |
| 169 | def check_valid_uuid(uuid): |
| 170 | id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"} |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 171 | id_schema2 = {"type" : "string", "pattern": "^[a-fA-F0-9]{32}$"} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 172 | try: |
| 173 | js_v(uuid, id_schema) |
| 174 | return True |
| 175 | except js_e.ValidationError: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 176 | try: |
| 177 | js_v(uuid, id_schema2) |
| 178 | return True |
| 179 | except js_e.ValidationError: |
| 180 | return False |
| 181 | return False |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 182 | |
| 183 | |
| 184 | def expand_brackets(text): |
| 185 | """ |
| 186 | Change a text with TEXT[ABC..] into a list with [TEXTA, TEXTB, TEXC, ... |
| 187 | if no bracket is used it just return the a list with the single text |
| 188 | It uses recursivity to allow several [] in the text |
| 189 | :param text: |
| 190 | :return: |
| 191 | """ |
| 192 | start = text.find("[") |
| 193 | end = text.find("]") |
| 194 | if start < 0 or end < 0: |
| 195 | return [text] |
| 196 | text_list = [] |
| 197 | for char in text[start+1:end]: |
| 198 | text_list += expand_brackets(text[:start] + char + text[end+1:]) |
| 199 | return text_list |
| tierno | b8569aa | 2018-08-24 11:34:54 +0200 | [diff] [blame] | 200 | |
| 201 | def deprecated(message): |
| 202 | def deprecated_decorator(func): |
| 203 | def deprecated_func(*args, **kwargs): |
| 204 | warnings.warn("{} is a deprecated function. {}".format(func.__name__, message), |
| 205 | category=DeprecationWarning, |
| 206 | stacklevel=2) |
| 207 | warnings.simplefilter('default', DeprecationWarning) |
| 208 | return func(*args, **kwargs) |
| 209 | return deprecated_func |
| 210 | return deprecated_decorator |