kill controller and clean journal command utilities
[integration.git] / test / csit / libraries / UtilLibrary.py
1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
5
6
7 import requests
8 from SSHLibrary import SSHLibrary
9
10 #
11 #Helps in making GET REST calls
12 #
13
14 def get(url, userId, password):
15
16     headers = {}
17     headers['Accept']= 'application/xml'
18
19     # Send the GET request
20     req = requests.get(url, params=None, headers=headers)
21
22     # Read the response
23     return req
24
25 #
26 #Helps in making POST REST calls without outputs
27 #
28 def nonprintpost(url, userId, password,data):
29
30     headers = {}
31     headers['Content-Type'] = 'application/json'
32     #headers['Accept']= 'application/xml'
33
34     resp = requests.post(url,data.encode(),headers=headers)
35
36
37     return resp
38
39 #
40 #Helps in making POST REST calls
41 #
42 def post(url, userId, password,data):
43     print("post request with url "+url)
44     print("post request with data "+data)
45     headers = {}
46     headers['Content-Type'] = 'application/json'
47     #headers['Accept']= 'application/xml'
48
49     resp = requests.post(url,data.encode(),headers=headers)
50
51     #print (resp.raise_for_status())
52     print (resp.headers)
53
54     return resp
55
56 #
57 #Helps in making DELET REST calls
58 #
59 def delete(url,userId,password):
60     print("delete all resources belonging to url"+url)
61     resp=requests.delete(url)
62
63
64 # use username and password of controller server for ssh and need
65 # karaf distribution location like /root/Documents/dist
66 #
67 def execute_ssh_command(ip, username, password, command):
68     print "executing ssh command"
69     lib = SSHLibrary()
70     lib.open_connection(ip)
71     lib.login(username=username,password=password)
72     print "login done"
73     lib.execute_command(command)
74     print "command executed : " + command
75     lib.close_connection()
76
77 def startcontroller(ip,username,password,karafhome):
78     execute_ssh_command(ip, username, password, karafhome+"/bin/start")
79
80 def stopcontroller(ip,username,password,karafhome):
81     execute_ssh_command(ip, username, password, karafhome+"/bin/stop")
82
83 def clean_journal(ip, username, password, karafHome):
84     execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/journal")
85
86 def kill_controller(ip, username, password, karafHome):
87     execute_ssh_command(ip, username, password, "ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh")
88
89 #
90 # main invoked
91 if __name__ != "__main__":
92     _cache = robot.utils.ConnectionCache('No sessions created')
93     # here create one session for each HTTP functions
94     _cache.register(requests.session(), alias='CLUSTERING_GET')
95     _cache.register(requests.session(),alias='CLUSTERING_POST')
96     _cache.register(requests.session(),alias='CLUSTERING_DELETE')