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")
55 resp = session.post(url, data.encode('utf-8'), headers=headers, auth=(userId, password))
60 def post(url, userId, password, data):
61 """Helps in making POST REST calls"""
63 "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
73 print("post request with url " + url)
74 print("post request with data " + data)
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))
81 # print(resp.raise_for_status())
83 if resp.status_code >= 500:
89 def delete(url, userId='admin', password='admin'):
90 """Helps in making DELET REST calls"""
92 "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
95 print("delete all resources belonging to url" + url)
96 session = _cache.switch("CLUSTERING_DELETE")
97 resp = session.delete(url, auth=(userId, password)) # noqa
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
105 raise AssertionError('the variable passed was type NoneType')
109 def execute_ssh_command(ip, username, password, command):
110 """Execute SSH Command
112 use username and password of controller server for ssh and need
113 karaf distribution location like /root/Documents/dist
115 print("executing ssh command")
117 lib.open_connection(ip)
118 lib.login(username=username, password=password)
120 cmd_response = lib.execute_command(command)
121 print("command executed : " + command)
122 lib.close_connection()
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"
130 print("Waiting for controller " + ip + " up.")
131 # Try 30*10s=5 minutes for the controller to be up.
134 print("attempt %s to url %s" % (str(i), url))
135 resp = get(url, "admin", "admin")
136 print("attempt %s response is %s" % (str(i), str(resp)))
138 if ('clustering-it-provider' in resp.text):
139 print("Wait for controller " + ip + " succeeded")
141 except Exception as e:
145 print("Wait for controller " + ip + " failed")
149 def startAllControllers(username, password, karafhome, port, *ips):
150 # Start all controllers
152 execute_ssh_command(ip, username, password, karafhome + "/bin/start")
154 # Wait for all of them to be up
156 rc = wait_for_controller_up(ip, port)
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)
167 def stopcontroller(ip, username, password, karafhome):
168 executeStopController(ip, username, password, karafhome)
170 wait_for_controller_stopped(ip, username, password, karafhome)
173 def executeStopController(ip, username, password, karafhome):
174 execute_ssh_command(ip, username, password, karafhome + "/bin/stop")
177 def stopAllControllers(username, password, karafhome, *ips):
179 executeStopController(ip, username, password, karafhome)
182 wait_for_controller_stopped(ip, username, password, karafhome)
185 def wait_for_controller_stopped(ip, username, password, karafHome):
187 lib.open_connection(ip)
188 lib.login(username=username, password=password)
190 # Wait 1 minute for the controller to stop gracefully
194 stdout = lib.execute_command("ps -axf | grep karaf | grep -v grep | wc -l")
195 processCnt = stdout[0].strip('\n')
196 print("processCnt: " + processCnt)
197 if processCnt == '0':
202 lib.close_connection()
205 print("Killing controller")
206 kill_controller(ip, username, password, karafHome)
209 def clean_journal(ip, username, password, karafHome):
210 execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/journal")
213 def kill_controller(ip, username, password, karafHome):
214 execute_ssh_command(ip, username, password,
215 "ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh")
218 def isolate_controller(controllers, username, password, isolated):
219 """ Isolate one controller from the others in the cluster
221 :param controllers: A list of ip addresses or host names as strings.
222 :param username: Username for the controller to be isolated.
223 :param password: Password for the controller to be isolated.
224 :param isolated: Number (starting at one) of the controller to be isolated.
225 :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
227 isolated_controller = controllers[isolated - 1]
228 for controller in controllers:
229 if controller != isolated_controller:
230 base_str = 'sudo iptables -I OUTPUT -p all --source '
231 cmd_str = base_str + isolated_controller + ' --destination ' + controller + ' -j DROP'
232 execute_ssh_command(isolated_controller, username, password, cmd_str)
233 cmd_str = base_str + controller + ' --destination ' + isolated_controller + ' -j DROP'
234 execute_ssh_command(isolated_controller, username, password, cmd_str)
235 ip_tables = execute_ssh_command(isolated_controller, username, password, 'sudo iptables -L')
238 for controller in controllers:
239 controller_regex_string = "[\s\S]*" + isolated_controller + " *" + controller + "[\s\S]*"
240 controller_regex = re.compile(controller_regex_string)
241 if not controller_regex.match(ip_tables):
242 iso_result = ip_tables
243 controller_regex_string = "[\s\S]*" + controller + " *" + isolated_controller + "[\s\S]*"
244 controller_regex = re.compile(controller_regex_string)
245 if not controller_regex.match(ip_tables):
246 iso_result = ip_tables
250 def rejoin_controller(controllers, username, password, isolated):
251 """ Return an isolated controller to the cluster.
253 :param controllers: A list of ip addresses or host names as strings.
254 :param username: Username for the isolated controller.
255 :param password: Password for the isolated controller.
256 :param isolated: Number (starting at one) of the isolated controller isolated.
257 :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
259 isolated_controller = controllers[isolated - 1]
260 for controller in controllers:
261 if controller != isolated_controller:
262 base_str = 'sudo iptables -D OUTPUT -p all --source '
263 cmd_str = base_str + isolated_controller + ' --destination ' + controller + ' -j DROP'
264 execute_ssh_command(isolated_controller, username, password, cmd_str)
265 cmd_str = base_str + controller + ' --destination ' + isolated_controller + ' -j DROP'
266 execute_ssh_command(isolated_controller, username, password, cmd_str)
267 ip_tables = execute_ssh_command(isolated_controller, username, password, 'sudo iptables -L')
270 for controller in controllers:
271 controller_regex_string = "[\s\S]*" + isolated_controller + " *" + controller + "[\s\S]*"
272 controller_regex = re.compile(controller_regex_string)
273 if controller_regex.match(ip_tables):
274 iso_result = ip_tables
275 controller_regex_string = "[\s\S]*" + controller + " *" + isolated_controller + "[\s\S]*"
276 controller_regex = re.compile(controller_regex_string)
277 if controller_regex.match(ip_tables):
278 iso_result = ip_tables
282 def flush_iptables(controllers, username, password):
283 """Removes all entries from IPTables on all controllers.
285 :param controllers: A list of ip address or host names as strings.
286 :param username: Username for all controllers.
287 :param password: Password for all controllers.
288 :return: If successful, returns "pass", otherwise returns "fail".
290 flush_result = 'pass'
291 for controller in controllers:
292 print('Flushing ', controller)
293 cmd_str = 'sudo iptables -v -F'
294 cmd_result = execute_ssh_command(controller, username, password, cmd_str)
296 success_string = "Flushing chain `INPUT'" + "\n"
297 success_string += "Flushing chain `FORWARD'" + "\n"
298 success_string += "Flushing chain `OUTPUT'"
299 if not cmd_result == success_string:
300 flush_result = "Failed to flush IPTables. Check Log."
307 def build_elastic_search_JSON_request(query_String):
310 'sort': [{'TimeStamp': {'order': 'desc'}}],
311 'query': {'query_string': {'query': query_String}}}
312 return json.dumps(data)
315 def create_query_string_search(data_category, metric_name, node_id, rk_node_id):
316 query = 'TSDRDataCategory:'
317 query += data_category
318 query += ' AND MetricName:'
320 query += ' AND NodeID:\"'
322 query += '\" AND RecordKeys.KeyValue:\"'
324 query += '\" AND RecordKeys.KeyName:Node AND RecordKeys.KeyValue:0 AND RecordKeys.KeyName:Table'
328 def create_query_string_count(data_category):
329 query = 'TSDRDataCategory:'
330 query += data_category
334 def extract_metric_value_search(response):
335 return str(response['hits']['hits'][0]['_source']['MetricValue'])
338 def extract_metric_value_count(response):
339 return int(response['hits']['total'])
344 if __name__ != "__main__":
345 _cache = robot.utils.ConnectionCache('No sessions created')
346 # here create one session for each HTTP functions
347 _cache.register(requests.session(), alias='CLUSTERING_GET')
348 _cache.register(requests.session(), alias='CLUSTERING_POST')
349 _cache.register(requests.session(), alias='CLUSTERING_DELETE')