Add Aluminium releases to comparestream lib
[integration/test.git] / csit / libraries / UtilLibrary.py
1 import requests
2 from SSHLibrary import SSHLibrary
3
4 import robot
5 import time
6 import re
7 import json
8 import warnings
9
10
11 __author__ = "Basheeruddin Ahmed"
12 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
13 __license__ = "New-style BSD"
14 __email__ = "syedbahm@cisco.com"
15
16
17 global _cache
18
19
20 def get(url, userId='admin', password='admin'):
21     """Helps in making GET REST calls"""
22     warnings.warn(
23         "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
24         DeprecationWarning
25     )
26     headers = {}
27     headers['Accept'] = 'application/xml'
28
29     # Send the GET request
30     session = _cache.switch("CLUSTERING_GET")
31     resp = session.get(url, headers=headers, auth=(userId, password))
32     # resp = session.get(url,headers=headers,auth={userId,password})
33     # Read the response
34     return resp
35
36
37 def nonprintpost(url, userId, password, data):
38     """Helps in making POST REST calls without outputs"""
39     warnings.warn(
40         "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
41         DeprecationWarning
42     )
43
44     if userId is None:
45         userId = 'admin'
46
47     if password is None:
48         password = 'admin'
49
50     headers = {}
51     headers['Content-Type'] = 'application/json'
52     # headers['Accept']= 'application/xml'
53
54     session = _cache.switch("CLUSTERING_POST")
55     resp = session.post(url, data.encode('utf-8'), headers=headers, auth=(userId, password))
56
57     return resp
58
59
60 def post(url, userId, password, data):
61     """Helps in making POST REST calls"""
62     warnings.warn(
63         "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
64         DeprecationWarning
65     )
66
67     if userId is None:
68         userId = 'admin'
69
70     if password is None:
71         password = 'admin'
72
73     print("post request with url " + url)
74     print("post request with data " + data)
75     headers = {}
76     headers['Content-Type'] = 'application/json'
77     # headers['Accept'] = 'application/xml'
78     session = _cache.switch("CLUSTERING_POST")
79     resp = session.post(url, data.encode('utf-8'), headers=headers, auth=(userId, password))
80
81     # print(resp.raise_for_status())
82     print(resp.headers)
83     if resp.status_code >= 500:
84         print(resp.text)
85
86     return resp
87
88
89 def delete(url, userId='admin', password='admin'):
90     """Helps in making DELET REST calls"""
91     warnings.warn(
92         "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
93         DeprecationWarning
94     )
95     print("delete all resources belonging to url" + url)
96     session = _cache.switch("CLUSTERING_DELETE")
97     resp = session.delete(url, auth=(userId, password))  # noqa
98
99
100 def Should_Not_Be_Type_None(var):
101     '''Keyword to check if the given variable is of type NoneType.  If the
102         variable type does match  raise an assertion so the keyword will fail
103     '''
104     if var is None:
105         raise AssertionError('the variable passed was type NoneType')
106     return 'PASS'
107
108
109 def execute_ssh_command(ip, username, password, command):
110     """Execute SSH Command
111
112     use username and password of controller server for ssh and need
113     karaf distribution location like /root/Documents/dist
114     """
115     print "executing ssh command"
116     lib = SSHLibrary()
117     lib.open_connection(ip)
118     lib.login(username=username, password=password)
119     print "login done"
120     cmd_response = lib.execute_command(command)
121     print "command executed : " + command
122     lib.close_connection()
123     return cmd_response
124
125
126 def wait_for_controller_up(ip, port="8181"):
127     url = "http://" + ip + ":" + str(port) + \
128           "/restconf/config/opendaylight-inventory:nodes/node/controller-config/yang-ext:mount/config:modules"
129
130     print "Waiting for controller " + ip + " up."
131     # Try 30*10s=5 minutes for the controller to be up.
132     for i in xrange(30):
133         try:
134             print "attempt " + str(i) + " to url " + url
135             resp = get(url, "admin", "admin")
136             print "attempt " + str(i) + " response is " + str(resp)
137             print resp.text
138             if ('clustering-it-provider' in resp.text):
139                 print "Wait for controller " + ip + " succeeded"
140                 return True
141         except Exception as e:
142             print e
143         time.sleep(10)
144
145     print "Wait for controller " + ip + " failed"
146     return False
147
148
149 def startAllControllers(username, password, karafhome, port, *ips):
150     # Start all controllers
151     for ip in ips:
152         execute_ssh_command(ip, username, password, karafhome + "/bin/start")
153
154     # Wait for all of them to be up
155     for ip in ips:
156         rc = wait_for_controller_up(ip, port)
157         if rc is False:
158             return False
159     return True
160
161
162 def startcontroller(ip, username, password, karafhome, port):
163     execute_ssh_command(ip, username, password, karafhome + "/bin/start")
164     return wait_for_controller_up(ip, port)
165
166
167 def stopcontroller(ip, username, password, karafhome):
168     executeStopController(ip, username, password, karafhome)
169
170     wait_for_controller_stopped(ip, username, password, karafhome)
171
172
173 def executeStopController(ip, username, password, karafhome):
174     execute_ssh_command(ip, username, password, karafhome + "/bin/stop")
175
176
177 def stopAllControllers(username, password, karafhome, *ips):
178     for ip in ips:
179         executeStopController(ip, username, password, karafhome)
180
181     for ip in ips:
182         wait_for_controller_stopped(ip, username, password, karafhome)
183
184
185 def wait_for_controller_stopped(ip, username, password, karafHome):
186     lib = SSHLibrary()
187     lib.open_connection(ip)
188     lib.login(username=username, password=password)
189
190     # Wait 1 minute for the controller to stop gracefully
191     tries = 20
192     i = 1
193     while i <= tries:
194         stdout = lib.execute_command("ps -axf | grep karaf | grep -v grep | wc -l")
195         # print "stdout: "+stdout
196         processCnt = stdout[0].strip('\n')
197         print("processCnt: " + processCnt)
198         if processCnt == '0':
199             break
200         i = i + 1
201         time.sleep(3)
202
203     lib.close_connection()
204
205     if i > tries:
206         print "Killing controller"
207         kill_controller(ip, username, password, karafHome)
208
209
210 def clean_journal(ip, username, password, karafHome):
211     execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/journal")
212
213
214 def kill_controller(ip, username, password, karafHome):
215     execute_ssh_command(ip, username, password,
216                         "ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh")
217
218
219 def isolate_controller(controllers, username, password, isolated):
220     """ Isolate one controller from the others in the cluster
221
222     :param controllers: A list of ip addresses or host names as strings.
223     :param username: Username for the controller to be isolated.
224     :param password: Password for the controller to be isolated.
225     :param isolated: Number (starting at one) of the controller to be isolated.
226     :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
227     """
228     isolated_controller = controllers[isolated - 1]
229     for controller in controllers:
230         if controller != isolated_controller:
231             base_str = 'sudo iptables -I OUTPUT -p all --source '
232             cmd_str = base_str + isolated_controller + ' --destination ' + controller + ' -j DROP'
233             execute_ssh_command(isolated_controller, username, password, cmd_str)
234             cmd_str = base_str + controller + ' --destination ' + isolated_controller + ' -j DROP'
235             execute_ssh_command(isolated_controller, username, password, cmd_str)
236     ip_tables = execute_ssh_command(isolated_controller, username, password, 'sudo iptables -L')
237     print ip_tables
238     iso_result = 'pass'
239     for controller in controllers:
240         controller_regex_string = "[\s\S]*" + isolated_controller + " *" + controller + "[\s\S]*"
241         controller_regex = re.compile(controller_regex_string)
242         if not controller_regex.match(ip_tables):
243             iso_result = ip_tables
244         controller_regex_string = "[\s\S]*" + controller + " *" + isolated_controller + "[\s\S]*"
245         controller_regex = re.compile(controller_regex_string)
246         if not controller_regex.match(ip_tables):
247             iso_result = ip_tables
248     return iso_result
249
250
251 def rejoin_controller(controllers, username, password, isolated):
252     """ Return an isolated controller to the cluster.
253
254     :param controllers: A list of ip addresses or host names as strings.
255     :param username: Username for the isolated controller.
256     :param password: Password for the isolated controller.
257     :param isolated: Number (starting at one) of the isolated controller isolated.
258     :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
259     """
260     isolated_controller = controllers[isolated - 1]
261     for controller in controllers:
262         if controller != isolated_controller:
263             base_str = 'sudo iptables -D OUTPUT -p all --source '
264             cmd_str = base_str + isolated_controller + ' --destination ' + controller + ' -j DROP'
265             execute_ssh_command(isolated_controller, username, password, cmd_str)
266             cmd_str = base_str + controller + ' --destination ' + isolated_controller + ' -j DROP'
267             execute_ssh_command(isolated_controller, username, password, cmd_str)
268     ip_tables = execute_ssh_command(isolated_controller, username, password, 'sudo iptables -L')
269     print ip_tables
270     iso_result = 'pass'
271     for controller in controllers:
272         controller_regex_string = "[\s\S]*" + isolated_controller + " *" + controller + "[\s\S]*"
273         controller_regex = re.compile(controller_regex_string)
274         if controller_regex.match(ip_tables):
275             iso_result = ip_tables
276         controller_regex_string = "[\s\S]*" + controller + " *" + isolated_controller + "[\s\S]*"
277         controller_regex = re.compile(controller_regex_string)
278         if controller_regex.match(ip_tables):
279             iso_result = ip_tables
280     return iso_result
281
282
283 def flush_iptables(controllers, username, password):
284     """Removes all entries from IPTables on all controllers.
285
286     :param controllers: A list of ip address or host names as strings.
287     :param username: Username for all controllers.
288     :param password: Password for all controllers.
289     :return: If successful, returns "pass", otherwise returns "fail".
290     """
291     flush_result = 'pass'
292     for controller in controllers:
293         print 'Flushing ' + controller
294         cmd_str = 'sudo iptables -v -F'
295         cmd_result = execute_ssh_command(controller, username, password, cmd_str)
296         print cmd_result
297         success_string = "Flushing chain `INPUT'" + "\n"
298         success_string += "Flushing chain `FORWARD'" + "\n"
299         success_string += "Flushing chain `OUTPUT'"
300         if not cmd_result == success_string:
301             flush_result = "Failed to flush IPTables. Check Log."
302         print "."
303         print "."
304         print "."
305     return flush_result
306
307
308 def build_elastic_search_JSON_request(query_String):
309     data = {'from': '0',
310             'size': '1',
311             'sort': [{'TimeStamp': {'order': 'desc'}}],
312             'query': {'query_string': {'query': query_String}}}
313     return json.dumps(data)
314
315
316 def create_query_string_search(data_category, metric_name, node_id, rk_node_id):
317     query = 'TSDRDataCategory:'
318     query += data_category
319     query += ' AND MetricName:'
320     query += metric_name
321     query += ' AND NodeID:\"'
322     query += node_id
323     query += '\" AND RecordKeys.KeyValue:\"'
324     query += rk_node_id
325     query += '\" AND RecordKeys.KeyName:Node AND RecordKeys.KeyValue:0 AND RecordKeys.KeyName:Table'
326     return query
327
328
329 def create_query_string_count(data_category):
330     query = 'TSDRDataCategory:'
331     query += data_category
332     return query
333
334
335 def extract_metric_value_search(response):
336     return str(response['hits']['hits'][0]['_source']['MetricValue'])
337
338
339 def extract_metric_value_count(response):
340     return int(response['hits']['total'])
341
342
343 #
344 # main invoked
345 if __name__ != "__main__":
346     _cache = robot.utils.ConnectionCache('No sessions created')
347     # here create one session for each HTTP functions
348     _cache.register(requests.session(), alias='CLUSTERING_GET')
349     _cache.register(requests.session(), alias='CLUSTERING_POST')
350     _cache.register(requests.session(), alias='CLUSTERING_DELETE')