Allow several pci for of_port_mapping. Log enhancement
[osm/openvim.git] / auxiliary_functions.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
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 '''
25 Common usuful functions
26 '''
27
28 __author__="Alfonso Tierno, Pablo Montes"
29 __date__ ="$10-jul-2014 12:07:15$"
30
31
32 import yaml
33 import paramiko
34 from definitionsClass import definitionsClass
35 from definitionsClass import Units
36 import random
37 from jsonschema import validate as js_v, exceptions as js_e
38
39 def check_and_convert_units(value, value_type):
40 """TODO: Update description
41 This method receives a text with 2 fields using a blank as separator and a list of valid units. The first field must represent a number
42 and the second one units.
43 In case the second field is not one of valid_units (False, <error description>) is returned.
44 In case the second field is a valid unit the first number is converted in the following way:
45 Gbps, Mbps, kbps -> Mbps
46 GB,MB,KB,B,GiB,MiB,KiB -> B
47 GHz,MHz,KHz,Hz -> Hz
48 If conversion is done successfully (True, <converted value>) is returned"""
49 try:
50 if value_type == Units.no_units:
51 if not isinstance(value,int) and not isinstance(value,float):
52 return (False, 'When no units are used only an integer or float must be used')
53 elif value_type == Units.name:
54 if not isinstance(value,str):
55 return (False, 'For names str must be used')
56 elif value_type == Units.boolean:
57 if not isinstance(value,bool):
58 return (False, 'A boolean or Yes/No mut be used')
59 else:
60 splitted = value.split(' ')
61 if len(splitted) != 2:
62 return (False, 'Expected format: <value> <units>')
63 (value, units) = splitted
64 if ',' in value or '.' in value:
65 return (False, 'Use integers to represent numeric values')
66
67 value = int(value)
68
69 # if not isinstance(value_type, Units):
70 # return (False, 'Not valid value_type')
71
72 valid_units = definitionsClass.units[value_type]
73
74 #Convert everything to upper in order to make comparations easier
75 units = units.upper()
76 for i in range(0, len(valid_units)):
77 valid_units[i] = valid_units[i].upper()
78
79 #Check the used units are valid ones
80 if units not in valid_units:
81 return (False, 'Valid units are: '+', '.join(valid_units))
82
83 if units.startswith('GI'):
84 value = value *1024*1024*1024
85 elif units.startswith('MI'):
86 value = value *1024*1024
87 elif units.startswith('KI'):
88 value = value *1024
89 elif units.startswith('G'):
90 value = value *1000000000
91 elif units.startswith('M'):
92 value = value *1000000
93 elif units.startswith('K'):
94 value = value *1000
95 except Exception,e:
96 return (False, 'Unexpected error in auxiliary_functions.py - check_and_convert_units:\n'+str(e))
97
98 return (True, value)
99
100 def get_ssh_connection(machine, user=None, password=None):
101 """Stablishes an ssh connection to the remote server. Returns (True, paramiko_ssh) in case of success or (False, <error message>) in case of error"""
102 try:
103 s = paramiko.SSHClient()
104 s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
105 s.load_system_host_keys()
106 s.connect(machine, 22, user, password, timeout=10)
107 except Exception,e:
108 return (False, 'It was not possible to connect to '+machine+str(e))
109
110 return (True, s)
111
112 def run_in_remote_server(s,command):
113 """Runs in the remote server the specified command. Returns (True, stdout) in case of success or (False, <error message>) in case of error"""
114 try:
115 (_, stdout, stderr) = s.exec_command(command)
116 error_msg = stderr.read()
117 if len(error_msg) > 0:
118 return (False, error_msg)
119 except Exception,e:
120 return (False, str(e))
121
122 return (True, stdout)
123
124 def read_file(file_):
125 """Reads a file specified by 'file' and returns (True,<its content as a string>) in case of success or (False, <error message>) in case of failure"""
126 try:
127 f = open(file_, 'r')
128 read_data = f.read()
129 f.close()
130 except Exception,e:
131 return (False, str(e))
132
133 return (True, read_data)
134
135 def check_contains(element, keywords):
136 """Auxiliary function used to check if a yaml structure contains or not
137 an specific field. Returns a bool"""
138 for key in keywords:
139 if not key in element:
140 return False
141 return True
142
143 def check_contains_(element, keywords):
144 """Auxiliary function used to check if a yaml structure contains or not
145 an specific field. Returns a bool,missing_variables"""
146 for key in keywords:
147 if not key in element:
148 return False, key
149 return True, None
150
151 def write_file(file_, content):
152 """Generates a file specified by 'file' and fills it using 'content'"""
153 f = open(file_, 'w')
154 f.write(content)
155 f.close()
156
157 def nice_print(yaml_element):
158 """Print a yaml structure. Used mainly for debugging"""
159 print(yaml.dump(yaml_element, default_flow_style=False))
160
161 def new_random_mac():
162 mac = (0xE2, random.randint(0x00, 0xff), random.randint(0x00, 0xff), random.randint(0x00, 0xff), random.randint(0x00, 0xff), random.randint(0x00, 0xff) )
163 return ':'.join(map(lambda x: "%02X" % x, mac))
164
165 def parse_dict(var, template):
166 if type(var) is not dict: return -1, 'not a dictionary'
167 for _,tv in template.items():
168 if type(tv) is list:
169 return
170
171 def delete_nulls(var):
172 if type(var) is dict:
173 for k in var.keys():
174 if var[k] is None: del var[k]
175 elif type(var[k]) is dict or type(var[k]) is list or type(var[k]) is tuple:
176 if delete_nulls(var[k]): del var[k]
177 if len(var) == 0: return True
178 elif type(var) is list or type(var) is tuple:
179 for k in var:
180 if type(k) is dict: delete_nulls(k)
181 if len(var) == 0: return True
182 return False
183
184 def get_next_2pow(var):
185 if var==0: return 0
186 v=1
187 while v<var: v=v*2
188 return v
189
190 def check_valid_uuid(uuid):
191 id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
192 try:
193 js_v(uuid, id_schema)
194 return True
195 except js_e.ValidationError:
196 return False
197
198 def DeleteNone(var):
199 '''Removes recursively empty dictionaries or lists
200 return True if var is an empty dict or list '''
201 if type(var) is dict:
202 for k in var.keys():
203 if var[k] is None: del var[k]
204 elif type(var[k]) is dict or type(var[k]) is list or type(var[k]) is tuple:
205 if DeleteNone(var[k]): del var[k]
206 if len(var) == 0: return True
207 elif type(var) is list or type(var) is tuple:
208 for k in var:
209 if type(k) is dict: DeleteNone(k)
210 if len(var) == 0: return True
211 return False
212
213 def gen_random_mac():
214 '''generates a random mac address. Avoid multicast, broadcast, etc
215 '''
216 mac = (
217 #52,54,00,
218 #2 + 4*random.randint(0x00, 0x3f), #4 multiple, unicast local mac address
219 0x52,
220 random.randint(0x00, 0xff),
221 random.randint(0x00, 0xff),
222 random.randint(0x00, 0xff),
223 random.randint(0x00, 0xff),
224 random.randint(0x00, 0xff)
225 )
226 return ':'.join(map(lambda x: "%02x" % x, mac))
227