| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 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 |
| 33 | from jsonschema import validate as js_v, exceptions as js_e |
| 34 | #from bs4 import BeautifulSoup |
| 35 | |
| 36 | def read_file(file_to_read): |
| 37 | """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""" |
| 38 | try: |
| 39 | f = open(file_to_read, 'r') |
| 40 | read_data = f.read() |
| 41 | f.close() |
| venkatamahesh | 6ecca18 | 2017-01-27 23:04:40 +0530 | [diff] [blame] | 42 | except Exception as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 43 | return (False, str(e)) |
| 44 | |
| 45 | return (True, read_data) |
| 46 | |
| 47 | def write_file(file_to_write, text): |
| 48 | """Write a file specified by 'file_to_write' and returns (True,NOne) in case of success or (False, <error message>) in case of failure""" |
| 49 | try: |
| 50 | f = open(file_to_write, 'w') |
| 51 | f.write(text) |
| 52 | f.close() |
| venkatamahesh | 6ecca18 | 2017-01-27 23:04:40 +0530 | [diff] [blame] | 53 | except Exception as e: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 54 | return (False, str(e)) |
| 55 | |
| 56 | return (True, None) |
| 57 | |
| 58 | def format_in(http_response, schema): |
| 59 | try: |
| 60 | client_data = http_response.json() |
| 61 | js_v(client_data, schema) |
| 62 | #print "Input data: ", str(client_data) |
| 63 | return True, client_data |
| venkatamahesh | 6ecca18 | 2017-01-27 23:04:40 +0530 | [diff] [blame] | 64 | except js_e.ValidationError as exc: |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 65 | print "validate_in error, jsonschema exception ", exc.message, "at", exc.path |
| 66 | return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path) |
| 67 | |
| 68 | def remove_extra_items(data, schema): |
| 69 | deleted=[] |
| 70 | if type(data) is tuple or type(data) is list: |
| 71 | for d in data: |
| 72 | a= remove_extra_items(d, schema['items']) |
| 73 | if a is not None: deleted.append(a) |
| 74 | elif type(data) is dict: |
| 75 | #TODO deal with patternProperties |
| 76 | if 'properties' not in schema: |
| 77 | return None |
| 78 | for k in data.keys(): |
| 79 | if k not in schema['properties'].keys(): |
| 80 | del data[k] |
| 81 | deleted.append(k) |
| 82 | else: |
| 83 | a = remove_extra_items(data[k], schema['properties'][k]) |
| 84 | if a is not None: deleted.append({k:a}) |
| 85 | if len(deleted) == 0: return None |
| 86 | elif len(deleted) == 1: return deleted[0] |
| 87 | else: return deleted |
| 88 | |
| 89 | #def format_html2text(http_content): |
| 90 | # soup=BeautifulSoup(http_content) |
| 91 | # text = soup.p.get_text() + " " + soup.pre.get_text() |
| 92 | # return text |
| 93 | |
| 94 | |
| 95 | def convert_bandwidth(data, reverse=False): |
| 96 | '''Check the field bandwidth recursivelly and when found, it removes units and convert to number |
| 97 | It assumes that bandwidth is well formed |
| 98 | Attributes: |
| 99 | 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid |
| 100 | 'reverse': by default convert form str to int (Mbps), if True it convert from number to units |
| 101 | Return: |
| 102 | None |
| 103 | ''' |
| 104 | if type(data) is dict: |
| 105 | for k in data.keys(): |
| 106 | if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: |
| 107 | convert_bandwidth(data[k], reverse) |
| 108 | if "bandwidth" in data: |
| 109 | try: |
| 110 | value=str(data["bandwidth"]) |
| 111 | if not reverse: |
| 112 | pos = value.find("bps") |
| 113 | if pos>0: |
| 114 | if value[pos-1]=="G": data["bandwidth"] = int(data["bandwidth"][:pos-1]) * 1000 |
| 115 | elif value[pos-1]=="k": data["bandwidth"]= int(data["bandwidth"][:pos-1]) / 1000 |
| 116 | else: data["bandwidth"]= int(data["bandwidth"][:pos-1]) |
| 117 | else: |
| 118 | value = int(data["bandwidth"]) |
| 119 | if value % 1000 == 0: data["bandwidth"]=str(value/1000) + " Gbps" |
| 120 | else: data["bandwidth"]=str(value) + " Mbps" |
| 121 | except: |
| 122 | print "convert_bandwidth exception for type", type(data["bandwidth"]), " data", data["bandwidth"] |
| 123 | return |
| 124 | if type(data) is tuple or type(data) is list: |
| 125 | for k in data: |
| 126 | if type(k) is dict or type(k) is tuple or type(k) is list: |
| 127 | convert_bandwidth(k, reverse) |
| 128 | |
| 129 | |
| 130 | |
| 131 | def convert_datetime2str(var): |
| 132 | '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s' |
| 133 | It enters recursively in the dict var finding this kind of variables |
| 134 | ''' |
| 135 | if type(var) is dict: |
| 136 | for k,v in var.items(): |
| 137 | if type(v) is datetime.datetime: |
| 138 | var[k]= v.strftime('%Y-%m-%dT%H:%M:%S') |
| 139 | elif type(v) is dict or type(v) is list or type(v) is tuple: |
| 140 | convert_datetime2str(v) |
| 141 | if len(var) == 0: return True |
| 142 | elif type(var) is list or type(var) is tuple: |
| 143 | for v in var: |
| 144 | convert_datetime2str(v) |
| 145 | |
| 146 | def convert_str2boolean(data, items): |
| 147 | '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean |
| 148 | Done recursively |
| 149 | Attributes: |
| 150 | 'data': dictionary variable to be checked. None or empty is considered valid |
| 151 | 'items': tuple of keys to convert |
| 152 | Return: |
| 153 | None |
| 154 | ''' |
| 155 | if type(data) is dict: |
| 156 | for k in data.keys(): |
| 157 | if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: |
| 158 | convert_str2boolean(data[k], items) |
| 159 | if k in items: |
| 160 | if type(data[k]) is str: |
| 161 | if data[k]=="false" or data[k]=="False": data[k]=False |
| 162 | elif data[k]=="true" or data[k]=="True": data[k]=True |
| 163 | if type(data) is tuple or type(data) is list: |
| 164 | for k in data: |
| 165 | if type(k) is dict or type(k) is tuple or type(k) is list: |
| 166 | convert_str2boolean(k, items) |
| 167 | |
| 168 | def check_valid_uuid(uuid): |
| 169 | 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] | 170 | id_schema2 = {"type" : "string", "pattern": "^[a-fA-F0-9]{32}$"} |
| tierno | 7edb675 | 2016-03-21 17:37:52 +0100 | [diff] [blame] | 171 | try: |
| 172 | js_v(uuid, id_schema) |
| 173 | return True |
| 174 | except js_e.ValidationError: |
| tierno | ae4a8d1 | 2016-07-08 12:30:39 +0200 | [diff] [blame] | 175 | try: |
| 176 | js_v(uuid, id_schema2) |
| 177 | return True |
| 178 | except js_e.ValidationError: |
| 179 | return False |
| 180 | return False |
| tierno | 7f426e9 | 2018-06-28 15:21:32 +0200 | [diff] [blame] | 181 | |
| 182 | |
| 183 | def expand_brackets(text): |
| 184 | """ |
| 185 | Change a text with TEXT[ABC..] into a list with [TEXTA, TEXTB, TEXC, ... |
| 186 | if no bracket is used it just return the a list with the single text |
| 187 | It uses recursivity to allow several [] in the text |
| 188 | :param text: |
| 189 | :return: |
| 190 | """ |
| 191 | start = text.find("[") |
| 192 | end = text.find("]") |
| 193 | if start < 0 or end < 0: |
| 194 | return [text] |
| 195 | text_list = [] |
| 196 | for char in text[start+1:end]: |
| 197 | text_list += expand_brackets(text[:start] + char + text[end+1:]) |
| 198 | return text_list |