Correcting Get Value from JSON
[integration/test.git] / csit / libraries / UtilLibrary.py
index 05ef69ab374ada992473714c97b64244f737328d..c342386f0aa85407410cc7d12333d15156928a58 100644 (file)
@@ -1,22 +1,28 @@
-__author__ = "Basheeruddin Ahmed"
-__copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
-__license__ = "New-style BSD"
-__email__ = "syedbahm@cisco.com"
-
-
 import requests
 from SSHLibrary import SSHLibrary
 
 import robot
 import time
 import re
+import json
+import warnings
+
+
+__author__ = "Basheeruddin Ahmed"
+__copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
+__license__ = "New-style BSD"
+__email__ = "syedbahm@cisco.com"
+
 
 global _cache
 
 
 def get(url, userId='admin', password='admin'):
     """Helps in making GET REST calls"""
-
+    warnings.warn(
+        "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
+        DeprecationWarning
+    )
     headers = {}
     headers['Accept'] = 'application/xml'
 
@@ -30,6 +36,10 @@ def get(url, userId='admin', password='admin'):
 
 def nonprintpost(url, userId, password, data):
     """Helps in making POST REST calls without outputs"""
+    warnings.warn(
+        "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
+        DeprecationWarning
+    )
 
     if userId is None:
         userId = 'admin'
@@ -49,6 +59,10 @@ def nonprintpost(url, userId, password, data):
 
 def post(url, userId, password, data):
     """Helps in making POST REST calls"""
+    warnings.warn(
+        "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
+        DeprecationWarning
+    )
 
     if userId is None:
         userId = 'admin'
@@ -56,16 +70,16 @@ def post(url, userId, password, data):
     if password is None:
         password = 'admin'
 
-    print("post request with url "+url)
-    print("post request with data "+data)
+    print("post request with url " + url)
+    print("post request with data " + data)
     headers = {}
     headers['Content-Type'] = 'application/json'
-    # headers['Accept']= 'application/xml'
+    # headers['Accept'] = 'application/xml'
     session = _cache.switch("CLUSTERING_POST")
     resp = session.post(url, data.encode('utf-8'), headers=headers, auth=(userId, password))
 
-    # print (resp.raise_for_status())
-    print (resp.headers)
+    # print(resp.raise_for_status())
+    print(resp.headers)
     if resp.status_code >= 500:
         print(resp.text)
 
@@ -74,7 +88,11 @@ def post(url, userId, password, data):
 
 def delete(url, userId='admin', password='admin'):
     """Helps in making DELET REST calls"""
-    print("delete all resources belonging to url"+url)
+    warnings.warn(
+        "Use the Robot RequestsLibrary rather than this. See DatastoreCRUD.robot for examples",
+        DeprecationWarning
+    )
+    print("delete all resources belonging to url" + url)
     session = _cache.switch("CLUSTERING_DELETE")
     resp = session.delete(url, auth=(userId, password))  # noqa
 
@@ -94,13 +112,13 @@ def execute_ssh_command(ip, username, password, command):
     use username and password of controller server for ssh and need
     karaf distribution location like /root/Documents/dist
     """
-    print "executing ssh command"
+    print("executing ssh command")
     lib = SSHLibrary()
     lib.open_connection(ip)
     lib.login(username=username, password=password)
-    print "login done"
+    print("login done")
     cmd_response = lib.execute_command(command)
-    print "command executed : " + command
+    print("command executed : " + command)
     lib.close_connection()
     return cmd_response
 
@@ -109,29 +127,29 @@ def wait_for_controller_up(ip, port="8181"):
     url = "http://" + ip + ":" + str(port) + \
           "/restconf/config/opendaylight-inventory:nodes/node/controller-config/yang-ext:mount/config:modules"
 
-    print "Waiting for controller " + ip + " up."
+    print("Waiting for controller " + ip + " up.")
     # Try 30*10s=5 minutes for the controller to be up.
     for i in xrange(30):
         try:
-            print "attempt " + str(i) + " to url " + url
+            print("attempt %s to url %s" % (str(i), url))
             resp = get(url, "admin", "admin")
-            print "attempt " + str(i) + " response is " + str(resp)
-            print resp.text
+            print("attempt %s response is %s" % (str(i), str(resp)))
+            print(resp.text)
             if ('clustering-it-provider' in resp.text):
-                print "Wait for controller " + ip + " succeeded"
+                print("Wait for controller " + ip + " succeeded")
                 return True
         except Exception as e:
-            print e
+            print(e)
         time.sleep(10)
 
-    print "Wait for controller " + ip + " failed"
+    print("Wait for controller " + ip + " failed")
     return False
 
 
 def startAllControllers(username, password, karafhome, port, *ips):
     # Start all controllers
     for ip in ips:
-        execute_ssh_command(ip, username, password, karafhome+"/bin/start")
+        execute_ssh_command(ip, username, password, karafhome + "/bin/start")
 
     # Wait for all of them to be up
     for ip in ips:
@@ -153,7 +171,7 @@ def stopcontroller(ip, username, password, karafhome):
 
 
 def executeStopController(ip, username, password, karafhome):
-    execute_ssh_command(ip, username, password, karafhome+"/bin/stop")
+    execute_ssh_command(ip, username, password, karafhome + "/bin/stop")
 
 
 def stopAllControllers(username, password, karafhome, *ips):
@@ -174,7 +192,6 @@ def wait_for_controller_stopped(ip, username, password, karafHome):
     i = 1
     while i <= tries:
         stdout = lib.execute_command("ps -axf | grep karaf | grep -v grep | wc -l")
-        # print "stdout: "+stdout
         processCnt = stdout[0].strip('\n')
         print("processCnt: " + processCnt)
         if processCnt == '0':
@@ -185,12 +202,12 @@ def wait_for_controller_stopped(ip, username, password, karafHome):
     lib.close_connection()
 
     if i > tries:
-        print "Killing controller"
+        print("Killing controller")
         kill_controller(ip, username, password, karafHome)
 
 
 def clean_journal(ip, username, password, karafHome):
-    execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/journal")
+    execute_ssh_command(ip, username, password, "rm -rf " + karafHome + "/*journal")
 
 
 def kill_controller(ip, username, password, karafHome):
@@ -207,7 +224,7 @@ def isolate_controller(controllers, username, password, isolated):
     :param isolated: Number (starting at one) of the controller to be isolated.
     :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
     """
-    isolated_controller = controllers[isolated-1]
+    isolated_controller = controllers[isolated - 1]
     for controller in controllers:
         if controller != isolated_controller:
             base_str = 'sudo iptables -I OUTPUT -p all --source '
@@ -216,7 +233,7 @@ def isolate_controller(controllers, username, password, isolated):
             cmd_str = base_str + controller + ' --destination ' + isolated_controller + ' -j DROP'
             execute_ssh_command(isolated_controller, username, password, cmd_str)
     ip_tables = execute_ssh_command(isolated_controller, username, password, 'sudo iptables -L')
-    print ip_tables
+    print(ip_tables)
     iso_result = 'pass'
     for controller in controllers:
         controller_regex_string = "[\s\S]*" + isolated_controller + " *" + controller + "[\s\S]*"
@@ -239,7 +256,7 @@ def rejoin_controller(controllers, username, password, isolated):
     :param isolated: Number (starting at one) of the isolated controller isolated.
     :return: If successful, returns "pass", otherwise returns the last failed IPTables text.
     """
-    isolated_controller = controllers[isolated-1]
+    isolated_controller = controllers[isolated - 1]
     for controller in controllers:
         if controller != isolated_controller:
             base_str = 'sudo iptables -D OUTPUT -p all --source '
@@ -248,7 +265,7 @@ def rejoin_controller(controllers, username, password, isolated):
             cmd_str = base_str + controller + ' --destination ' + isolated_controller + ' -j DROP'
             execute_ssh_command(isolated_controller, username, password, cmd_str)
     ip_tables = execute_ssh_command(isolated_controller, username, password, 'sudo iptables -L')
-    print ip_tables
+    print(ip_tables)
     iso_result = 'pass'
     for controller in controllers:
         controller_regex_string = "[\s\S]*" + isolated_controller + " *" + controller + "[\s\S]*"
@@ -272,21 +289,56 @@ def flush_iptables(controllers, username, password):
     """
     flush_result = 'pass'
     for controller in controllers:
-        print 'Flushing ' + controller
+        print('Flushing ', controller)
         cmd_str = 'sudo iptables -v -F'
         cmd_result = execute_ssh_command(controller, username, password, cmd_str)
-        print cmd_result
+        print(cmd_result)
         success_string = "Flushing chain `INPUT'" + "\n"
         success_string += "Flushing chain `FORWARD'" + "\n"
         success_string += "Flushing chain `OUTPUT'"
         if not cmd_result == success_string:
             flush_result = "Failed to flush IPTables. Check Log."
-        print "."
-        print "."
-        print "."
+        print(".")
+        print(".")
+        print(".")
     return flush_result
 
 
+def build_elastic_search_JSON_request(query_String):
+    data = {'from': '0',
+            'size': '1',
+            'sort': [{'TimeStamp': {'order': 'desc'}}],
+            'query': {'query_string': {'query': query_String}}}
+    return json.dumps(data)
+
+
+def create_query_string_search(data_category, metric_name, node_id, rk_node_id):
+    query = 'TSDRDataCategory:'
+    query += data_category
+    query += ' AND MetricName:'
+    query += metric_name
+    query += ' AND NodeID:\"'
+    query += node_id
+    query += '\" AND RecordKeys.KeyValue:\"'
+    query += rk_node_id
+    query += '\" AND RecordKeys.KeyName:Node AND RecordKeys.KeyValue:0 AND RecordKeys.KeyName:Table'
+    return query
+
+
+def create_query_string_count(data_category):
+    query = 'TSDRDataCategory:'
+    query += data_category
+    return query
+
+
+def extract_metric_value_search(response):
+    return str(response['hits']['hits'][0]['_source']['MetricValue'])
+
+
+def extract_metric_value_count(response):
+    return int(response['hits']['total'])
+
+
 #
 # main invoked
 if __name__ != "__main__":