2 from SSHLibrary import SSHLibrary
11 __author__ = "Basheeruddin Ahmed"
12 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
13 __license__ = "New-style BSD"
14 __email__ = "syedbahm@cisco.com"
20 def get(url, userId="admin", password="admin"):
21 """Helps in making GET REST calls"""
23 "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
27 headers["Accept"] = "application/xml"
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})
37 def nonprintpost(url, userId, password, data):
38 """Helps in making POST REST calls without outputs"""
40 "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
51 headers["Content-Type"] = "application/json"
52 # headers['Accept']= 'application/xml'
54 session = _cache.switch("CLUSTERING_POST")
56 url, data.encode("utf-8"), headers=headers, auth=(userId, password)
62 def post(url, userId, password, data):
63 """Helps in making POST REST calls"""
65 "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
75 print("post request with url " + url)
76 print("post request with data " + data)
78 headers["Content-Type"] = "application/json"
79 # headers['Accept'] = 'application/xml'
80 session = _cache.switch("CLUSTERING_POST")
82 url, data.encode("utf-8"), headers=headers, auth=(userId, password)
85 # print(resp.raise_for_status())
87 if resp.status_code >= 500:
93 def delete(url, userId="admin", password="admin"):
94 """Helps in making DELET REST calls"""
96 "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
99 print("delete all resources belonging to url" + url)
100 session = _cache.switch("CLUSTERING_DELETE")
101 resp = session.delete(url, auth=(userId, password)) # noqa
104 def Should_Not_Be_Type_None(var):
105 """Keyword to check if the given variable is of type NoneType. If the
106 variable type does match raise an assertion so the keyword will fail
109 raise AssertionError("the variable passed was type NoneType")
113 def execute_ssh_command(ip, username, password, command):
114 """Execute SSH Command
116 use username and password of controller server for ssh and need
117 karaf distribution location like /root/Documents/dist
119 print("executing ssh command")
121 lib.open_connection(ip)
122 lib.login(username=username, password=password)
124 cmd_response = lib.execute_command(command)
125 print("command executed : " + command)
126 lib.close_connection()
130 def wait_for_controller_up(ip, port="8181"):
136 + "/restconf/config/opendaylight-inventory:nodes/node/controller-config/yang-ext:mount/config:modules"
139 print("Waiting for controller " + ip + " up.")
140 # Try 30*10s=5 minutes for the controller to be up.
143 print("attempt %s to url %s" % (str(i), url))
144 resp = get(url, "admin", "admin")
145 print("attempt %s response is %s" % (str(i), str(resp)))
147 if "clustering-it-provider" in resp.text:
148 print("Wait for controller " + ip + " succeeded")
150 except Exception as e:
154 print("Wait for controller " + ip + " failed")
158 def startAllControllers(username, password, karafhome, port, *ips):
159 # Start all controllers
161 execute_ssh_command(ip, username, password, karafhome + "/bin/start")
163 # Wait for all of them to be up
165 rc = wait_for_controller_up(ip, port)
171 def startcontroller(ip, username, password, karafhome, port):
172 execute_ssh_command(ip, username, password, karafhome + "/bin/start")
173 return wait_for_controller_up(ip, port)
176 def stopcontroller(ip, username, password, karafhome):
177 executeStopController(ip, username, password, karafhome)
179 wait_for_controller_stopped(ip, username, password, karafhome)
182 def executeStopController(ip, username, password, karafhome):
183 execute_ssh_command(ip, username, password, karafhome + "/bin/stop")
186 def stopAllControllers(username, password, karafhome, *ips):
188 executeStopController(ip, username, password, karafhome)
191 wait_for_controller_stopped(ip, username, password, karafhome)
194 def wait_for_controller_stopped(ip, username, password, karafHome):
196 lib.open_connection(ip)
197 lib.login(username=username, password=password)
199 # Wait 1 minute for the controller to stop gracefully
203 stdout = lib.execute_command("ps -axf | grep karaf | grep -v grep | wc -l")
204 processCnt = stdout[0].strip("\n")
205 print("processCnt: " + processCnt)
206 if processCnt == "0":
211 lib.close_connection()
214 print("Killing controller")
215 kill_controller(ip, username, password, karafHome)
218 def clean_journal(ip, username, password, karafHome):
219 execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/*journal")
222 def kill_controller(ip, username, password, karafHome):
227 "ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh",
231 def isolate_controller(controllers, username, password, isolated):
232 """Isolate one controller from the others in the cluster
234 :param controllers: A list of ip addresses or host names as strings.
235 :param username: Username for the controller to be isolated.
236 :param password: Password for the controller to be isolated.
237 :param isolated: Number (starting at one) of the controller to be isolated.
238 :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
240 isolated_controller = controllers[isolated - 1]
241 for controller in controllers:
242 if controller != isolated_controller:
243 base_str = "sudo iptables -I OUTPUT -p all --source "
246 + isolated_controller
251 execute_ssh_command(isolated_controller, username, password, cmd_str)
256 + isolated_controller
259 execute_ssh_command(isolated_controller, username, password, cmd_str)
260 ip_tables = execute_ssh_command(
261 isolated_controller, username, password, "sudo iptables -L"
265 for controller in controllers:
266 controller_regex_string = (
267 r"[\s\S]*" + isolated_controller + " *" + controller + r"[\s\S]*"
269 controller_regex = re.compile(controller_regex_string)
270 if not controller_regex.match(ip_tables):
271 iso_result = ip_tables
272 controller_regex_string = (
273 r"[\s\S]*" + controller + " *" + isolated_controller + r"[\s\S]*"
275 controller_regex = re.compile(controller_regex_string)
276 if not controller_regex.match(ip_tables):
277 iso_result = ip_tables
281 def rejoin_controller(controllers, username, password, isolated):
282 """Return an isolated controller to the cluster.
284 :param controllers: A list of ip addresses or host names as strings.
285 :param username: Username for the isolated controller.
286 :param password: Password for the isolated controller.
287 :param isolated: Number (starting at one) of the isolated controller isolated.
288 :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
290 isolated_controller = controllers[isolated - 1]
291 for controller in controllers:
292 if controller != isolated_controller:
293 base_str = "sudo iptables -D OUTPUT -p all --source "
296 + isolated_controller
301 execute_ssh_command(isolated_controller, username, password, cmd_str)
306 + isolated_controller
309 execute_ssh_command(isolated_controller, username, password, cmd_str)
310 ip_tables = execute_ssh_command(
311 isolated_controller, username, password, "sudo iptables -L"
315 for controller in controllers:
316 controller_regex_string = (
317 r"[\s\S]*" + isolated_controller + " *" + controller + r"[\s\S]*"
319 controller_regex = re.compile(controller_regex_string)
320 if controller_regex.match(ip_tables):
321 iso_result = ip_tables
322 controller_regex_string = (
323 r"[\s\S]*" + controller + " *" + isolated_controller + r"[\s\S]*"
325 controller_regex = re.compile(controller_regex_string)
326 if controller_regex.match(ip_tables):
327 iso_result = ip_tables
331 def flush_iptables(controllers, username, password):
332 """Removes all entries from IPTables on all controllers.
334 :param controllers: A list of ip address or host names as strings.
335 :param username: Username for all controllers.
336 :param password: Password for all controllers.
337 :return: If successful, returns "pass", otherwise returns "fail".
339 flush_result = "pass"
340 for controller in controllers:
341 print("Flushing ", controller)
342 cmd_str = "sudo iptables -v -F"
343 cmd_result = execute_ssh_command(controller, username, password, cmd_str)
345 success_string = "Flushing chain `INPUT'" + "\n"
346 success_string += "Flushing chain `FORWARD'" + "\n"
347 success_string += "Flushing chain `OUTPUT'"
348 if not cmd_result == success_string:
349 flush_result = "Failed to flush IPTables. Check Log."
356 def build_elastic_search_JSON_request(query_String):
360 "sort": [{"TimeStamp": {"order": "desc"}}],
361 "query": {"query_string": {"query": query_String}},
363 return json.dumps(data)
366 def create_query_string_search(data_category, metric_name, node_id, rk_node_id):
367 query = "TSDRDataCategory:"
368 query += data_category
369 query += " AND MetricName:"
371 query += ' AND NodeID:"'
373 query += '" AND RecordKeys.KeyValue:"'
375 query += '" AND RecordKeys.KeyName:Node AND RecordKeys.KeyValue:0 AND RecordKeys.KeyName:Table'
379 def create_query_string_count(data_category):
380 query = "TSDRDataCategory:"
381 query += data_category
385 def extract_metric_value_search(response):
386 return str(response["hits"]["hits"][0]["_source"]["MetricValue"])
389 def extract_metric_value_count(response):
390 return int(response["hits"]["total"])
395 if __name__ != "__main__":
396 _cache = robot.utils.ConnectionCache("No sessions created")
397 # here create one session for each HTTP functions
398 _cache.register(requests.session(), alias="CLUSTERING_GET")
399 _cache.register(requests.session(), alias="CLUSTERING_POST")
400 _cache.register(requests.session(), alias="CLUSTERING_DELETE")