1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
8 from SSHLibrary import SSHLibrary
16 #Helps in making GET REST calls
19 def get(url, userId='admin', password='admin'):
22 headers['Accept']= 'application/xml'
24 # Send the GET request
25 session = _cache.switch("CLUSTERING_GET")
26 resp = session.get(url,headers=headers,auth=(userId,password))
27 #resp = session.get(url,headers=headers,auth={userId,password})
32 #Helps in making POST REST calls without outputs
34 def nonprintpost(url, userId, password, data):
43 headers['Content-Type'] = 'application/json'
44 #headers['Accept']= 'application/xml'
46 session = _cache.switch("CLUSTERING_POST")
47 resp = session.post(url,data.encode('utf-8'),headers=headers,auth=(userId,password))
53 #Helps in making POST REST calls
55 def post(url, userId, password, data):
63 print("post request with url "+url)
64 print("post request with data "+data)
66 headers['Content-Type'] = 'application/json'
67 #headers['Accept']= 'application/xml'
68 session = _cache.switch("CLUSTERING_POST")
69 resp = session.post(url,data.encode('utf-8'),headers=headers,auth=(userId,password))
71 #print (resp.raise_for_status())
73 if (resp.status_code >= 500):
79 #Helps in making DELET REST calls
81 def delete(url, userId='admin', password='admin'):
82 print("delete all resources belonging to url"+url)
83 session = _cache.switch("CLUSTERING_DELETE")
84 resp=session.delete(url,auth=(userId,password))
86 def Should_Not_Be_Type_None(var):
87 '''Keyword to check if the given variable is of type NoneType. If the
88 variable type does match raise an assertion so the keyword will fail
91 raise AssertionError('the variable passed was type NoneType')
94 # use username and password of controller server for ssh and need
95 # karaf distribution location like /root/Documents/dist
97 def execute_ssh_command(ip, username, password, command):
98 print "executing ssh command"
100 lib.open_connection(ip)
101 lib.login(username=username,password=password)
103 lib.execute_command(command)
104 print "command executed : " + command
105 lib.close_connection()
107 def wait_for_controller_up(ip, port="8181"):
108 url = "http://" + ip + ":" + str(port) + \
109 "/restconf/config/opendaylight-inventory:nodes/node/controller-config/yang-ext:mount/config:modules"
111 print "Waiting for controller " + ip + " up."
112 # Try 30*10s=5 minutes for the controller to be up.
115 print "attempt " + str(i) + " to url " + url
116 resp = get(url, "admin", "admin")
117 print "attempt " + str(i) + " response is " + str(resp)
119 if ('clustering-it-provider' in resp.text):
120 print "Wait for controller " + ip + " succeeded"
122 except Exception as e:
126 print "Wait for controller " + ip + " failed"
129 def startAllControllers(username, password, karafhome, port, *ips):
130 # Start all controllers
132 execute_ssh_command(ip, username, password, karafhome+"/bin/start")
134 # Wait for all of them to be up
136 rc = wait_for_controller_up(ip, port)
141 def startcontroller(ip,username,password,karafhome,port):
142 execute_ssh_command(ip, username, password, karafhome+"/bin/start")
143 return wait_for_controller_up(ip, port)
145 def stopcontroller(ip,username,password,karafhome):
146 executeStopController(ip,username,password,karafhome)
148 wait_for_controller_stopped(ip,username,password,karafhome)
150 def executeStopController(ip,username,password,karafhome):
151 execute_ssh_command(ip, username, password, karafhome+"/bin/stop")
153 def stopAllControllers(username,password,karafhome,*ips):
155 executeStopController(ip,username,password,karafhome)
158 wait_for_controller_stopped(ip, username, password, karafhome)
160 def wait_for_controller_stopped(ip, username, password, karafHome):
162 lib.open_connection(ip)
163 lib.login(username=username,password=password)
165 # Wait 1 minute for the controller to stop gracefully
169 stdout = lib.execute_command("ps -axf | grep karaf | grep -v grep | wc -l")
170 #print "stdout: "+stdout
171 processCnt = stdout[0].strip('\n')
172 print "processCnt: "+processCnt
173 if processCnt == '0':
178 lib.close_connection()
181 print "Killing controller"
182 kill_controller(ip, username, password, karafHome)
184 def clean_journal(ip, username, password, karafHome):
185 execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/journal")
187 def kill_controller(ip, username, password, karafHome):
188 execute_ssh_command(ip, username, password, "ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh")
192 if __name__ != "__main__":
193 _cache = robot.utils.ConnectionCache('No sessions created')
194 # here create one session for each HTTP functions
195 _cache.register(requests.session(), alias='CLUSTERING_GET')
196 _cache.register(requests.session(),alias='CLUSTERING_POST')
197 _cache.register(requests.session(),alias='CLUSTERING_DELETE')