fix odl patches
[ccsdk/distribution.git] / ansible-server / src / main / ansible-server / UsersRestServer.py
index 9da6fb9..d4566b8 100755 (executable)
@@ -19,7 +19,6 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
-* ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */
 '''
@@ -65,7 +64,7 @@ def sys_call (cmd):
 
 def callback (Id, Result, Output, Log, returncode):
     
-    print "***> in RestServer.callback"
+    print("***> in RestServer.callback")
 
     if Id in TestRecord:
         time_now = datetime.datetime.utcnow()
@@ -117,17 +116,17 @@ def callback (Id, Result, Output, Log, returncode):
                     if key in TestRecord[Id]['Output']['Output']:
                         data["Results"][key]["Output"] = TestRecord[Id]['Output']['Output'][key]
 
-            print "     Posting to", TestRecord[Id]['CallBack']
+            print("     Posting to", TestRecord[Id]['CallBack'])
             
             s = requests.Session()
             r = s.post(TestRecord[Id]['CallBack'], data = json.dumps(data),
                        headers = {'content-type': 'application/json'})
-            print  "     Response", r.status_code, r.text
+            print("     Response", r.status_code, r.text)
 
 def RunAnsible_Playbook (callback, Id, Inventory, Playbook, NodeList, TestRecord,
                          Path, ArchiveFlag):
 
-    print "***> in RestServer.RunAnsible_Playbook"
+    print("***> in RestServer.RunAnsible_Playbook")
 
     # Run test in playbook for given target
     Result = ''
@@ -158,8 +157,8 @@ def RunAnsible_Playbook (callback, Id, Inventory, Playbook, NodeList, TestRecord
             f = open(Path + "/" + file, "r")
             resultsData =  f.read()  # Not to pass vnf instance name
             OutputP = json.loads(resultsData)
-            Output['Output'] =  OutputP 
-           cherrypy.log("Output = " + str(Output['Output']))
+            Output['Output'] =  OutputP
+            cherrypy.log("Output = " + str(Output['Output']))
             #Output['Output'][key] = f.read() # To pass vnf instance name
             f.close()
 
@@ -342,7 +341,7 @@ class TestManager (object):
                 if True:
 
                     if not input_json['Id'] in TestRecord:
-                       # check if Id exists in previous run dirctory
+                        # check if Id exists in previous run dirctory
                         # if so retun error
                         s_cmd = 'ls ' + ansible_temp + '/*_' + input_json['Id']
                         #if subprocess.check_output([s_cmd, ]):
@@ -360,7 +359,7 @@ class TestManager (object):
                         #  cherrypy.log("EnvParameter object: " + i)
                         #  cherrypy.log("  EnvParameter Value: " + EnvParameters[ i ])
 
-                       # Now get things out of EnvParameters
+                        # Now get things out of EnvParameters
                         VNF_instance   = None
                         VNF_instance = EnvParameters.get('vnf_instance')
 
@@ -393,42 +392,42 @@ class TestManager (object):
                         
                         # Verify VNF_instance was passed in EnvParameters
                         if VNF_instance != None:
-                         cherrypy.log( "Request Decode: VnfInstance      " + VNF_instance)
+                          cherrypy.log( "Request Decode: VnfInstance      " + VNF_instance)
                         else:
-                         cherrypy.log( "StatusCode: 107, StatusMessage: VNF_instance NOT PROVIDED" )
+                          cherrypy.log( "StatusCode: 107, StatusMessage: VNF_instance NOT PROVIDED" )
                           return {"StatusCode": 107,
                                     "StatusMessage": "VNF_instance NOT PROVIDED"}
 
                         if inventory_names != None:
-                         cherrypy.log( "Request Decode: Inventory Names  " + inventory_names)
+                          cherrypy.log( "Request Decode: Inventory Names  " + inventory_names)
                         else:
-                         cherrypy.log( "Request Decode: Inventory Names  " + "Not provided")
-
-                       cherrypy.log( "Request Decode: PlaybookName     " + PlaybookName)
-                       PlayBookFunction = PlaybookName.rsplit("/",2)[1]
-                       PlayBookFile = PlayBookFunction + "/site.yml"
-                       cherrypy.log( "Request Decode: PlaybookFunction " + PlayBookFunction)
-                       cherrypy.log( "Request Decode: Playbook file    " + PlayBookFile)
-                       
+                          cherrypy.log( "Request Decode: Inventory Names  " + "Not provided")
+
+                        cherrypy.log( "Request Decode: PlaybookName     " + PlaybookName)
+                        PlayBookFunction = PlaybookName.rsplit("/",2)[1]
+                        PlayBookFile = PlayBookFunction + "/site.yml"
+                        cherrypy.log( "Request Decode: PlaybookFunction " + PlayBookFunction)
+                        cherrypy.log( "Request Decode: Playbook file    " + PlayBookFile)
+
                         BaseDir = ansible_path + "/" + PlaybookName.rsplit("/",1)[0]
                         CopyDir = ansible_path + "/" + PlaybookName.rsplit("/",2)[0]
-                       cherrypy.log( "Request Decode: Basedir          " + BaseDir)
-                       cherrypy.log( "Request Decode: Copydir          " + CopyDir)
-                       
+                        cherrypy.log( "Request Decode: Basedir          " + BaseDir)
+                        cherrypy.log( "Request Decode: Copydir          " + CopyDir)
+                        
 
                         PlaybookDir = ansible_temp + "/" + \
                                       VNF_instance + "_" + str_uuid + "_" + str(Id)
 
-                       # AnsibleInv is the directory where the host file to be run exsists
+                        # AnsibleInv is the directory where the host file to be run exsists
                         AnsibleInv = ansible_path + "/" + VnfType + "/latest/ansible/inventory/" + VNF_instance
                         ArchiveFlag = False
 
-                       # Create base run directory if it doesn't exist
+                        # Create base run directory if it doesn't exist
                         if not os.path.exists(ansible_temp):
                             cherrypy.log( "Creating Base Run Directory: "  + ansible_temp)
                             os.makedirs(ansible_temp)
 
-                       if not os.path.exists( CopyDir ):
+                        if not os.path.exists( CopyDir ):
                             cherrypy.log("Playbook Not Found")
                             return {"StatusCode": 101,
                                     "StatusMessage": "PLAYBOOK NOT FOUND"}
@@ -444,7 +443,7 @@ class TestManager (object):
                         cherrypy.log( "PlaybookDir:    " + PlaybookDir)
                         cherrypy.log( "AnsibleInv:     " + AnsibleInv)
 
-                       #location of host file
+                        #location of host file
                         #HostFile = PlaybookDir + "/inventory/" + VNF_instance + "hosts"
                         #cherrypy.log("HostFile: " +  HostFile)
 
@@ -468,14 +467,14 @@ class TestManager (object):
                                 #AnsibleInvFail = False # ???
                                 HaveNodeList = True
 
-                       ###############################################################################
-                       ##### Host file processing                          ###########################
-                       ##### 1. Use file delivered with playbook           ###########################
-                       ##### 2. If HostNames + NodeList generate and use   ###########################
-                       ##### 3. If HostNames = VM or NVF copy and use.     ###########################
-                       ###############################################################################
+                        ###############################################################################
+                        ##### Host file processing                          ###########################
+                        ##### 1. Use file delivered with playbook           ###########################
+                        ##### 2. If HostNames + NodeList generate and use   ###########################
+                        ##### 3. If HostNames = VM or NVF copy and use.     ###########################
+                        ###############################################################################
 
-                       #location of host file - Default
+                        #location of host file - Default
                         HostFile = PlaybookDir + "/inventory/" + VNF_instance + "hosts"
                         cherrypy.log("HostFile: " +  HostFile)
 
@@ -546,9 +545,9 @@ class TestManager (object):
                         if 'CallBack' in input_json:
                             callback_flag = input_json['CallBack']
 
-                       # if AnsibleServer is not set to 'na'  don't send AnsibleServer in PENDING responce.
-                       if AnsibleServer != 'na':
-                               TestRecord[Id] = {'PlaybookName': PlaybookName,
+                        # if AnsibleServer is not set to 'na'  don't send AnsibleServer in PENDING responce.
+                        if AnsibleServer != 'na':
+                                TestRecord[Id] = {'PlaybookName': PlaybookName,
                                           'Version': version,
                                           'NodeList': NodeList,
                                           'HostGroupList': hostgrouplist,
@@ -568,8 +567,8 @@ class TestManager (object):
                                           'Output': {},
                                           'Path': PlaybookDir,
                                           'Mandatory': None}
-                       else:
-                               TestRecord[Id] = {'PlaybookName': PlaybookName,
+                        else:
+                                TestRecord[Id] = {'PlaybookName': PlaybookName,
                                           'Version': version,
                                           'NodeList': NodeList,
                                           'HostGroupList': hostgrouplist,
@@ -602,7 +601,7 @@ class TestManager (object):
                                 
                         
                         # Process playbook
-                       if os.path.exists( ansible_path + '/' + PlaybookName):
+                        if os.path.exists( ansible_path + '/' + PlaybookName):
                             AnsiblePlaybookFail = False
                                     
                         if AnsiblePlaybookFail:
@@ -628,9 +627,9 @@ class TestManager (object):
                             # PAP
                             f = open(playbook_path + "/vars/defaults.yml","a")
                             #for id, record in TestRecord.items():
-                            print TestRecord[Id]['LocalParameters']
+                            print(TestRecord[Id]['LocalParameters'])
                             local_parms = TestRecord[Id]['LocalParameters']
-                            for key, value in local_parms.items():
+                            for key, value in list(local_parms.items()):
                                 f.write(key +"=" + value + "\n");
                             f.close()
                             ###################################################
@@ -665,7 +664,7 @@ class TestManager (object):
                             f.close()
                             f = open(playbook_path + "/JsonRequest.txt", "w")
                             #f.write(str(input_json))
-                            print( json.dumps(input_json, indent=4, sort_keys=True))
+                            print(( json.dumps(input_json, indent=4, sort_keys=True)))
                             f.write( json.dumps(input_json, indent=4, sort_keys=True))
                             f.close()
 
@@ -714,14 +713,14 @@ class TestManager (object):
             cherrypy.log("Payload: " + str(input_data) + " Type " + input_data['Type'])
 
             if 'LogRest' in input_data['Type']:
-               sys.stdout.close()
-               sys.stdout = open("/var/log/RestServer.log", "w")
+                sys.stdout.close()
+                sys.stdout = open("/var/log/RestServer.log", "w")
 
             # Just a debug to dump any records
             if 'GetStatus' in input_data['Type']:
                 cherrypy.log( "******** Dump Records **********")
-                if TestRecord.items():
-                  for id, record in TestRecord.items():
+                if list(TestRecord.items()):
+                  for id, record in list(TestRecord.items()):
                     cherrypy.log( "    Id: " + id)
                     cherrypy.log( "Record: " + str(record))
                 else:
@@ -785,10 +784,10 @@ class TestManager (object):
                         return out_obj
 
                     elif 'GetStatus' in input_data['Type']:
-                        print " Dump Records"
+                        print(" Dump Records")
                         for id, record in TestRecord,items():
-                           print " id: " + id
-                           print "   Record:" + str(reecord)
+                           print(" id: " + id)
+                           print("   Record:" + str(reecord))
 
                     elif 'GetOutput' in input_data['Type']:
 
@@ -886,7 +885,7 @@ class TestManager (object):
                             else:
                                playbooknametxt = "NA"
 
-                           # Add code to get other items not just output.log from files
+                            # Add code to get other items not just output.log from files
                             if os.path.exists( rdir + "/log.file"):
                                cherrypy.log("Found log.file")
                                out_results = "NA:"
@@ -894,7 +893,7 @@ class TestManager (object):
                                
                                line =  f.readline()
                                while line :
-                                if "fatal" in line:
+                                 if "fatal" in line:
                                    out_results = out_results +  line
                                  elif "RECAP" in line:
                                    out_results = out_results +  line
@@ -945,7 +944,7 @@ if __name__ == '__main__':
     config_file_path = "RestServer_config"
 
     if not os.path.exists(config_file_path):
-        print '[INFO] The config file does not exist'
+        print('[INFO] The config file does not exist')
         sys.exit(0)
 
     ip = 'na'
@@ -1019,15 +1018,15 @@ if __name__ == '__main__':
         # Point to certificate files
 
         if not os.path.exists(pub):
-            print '[INFO] The public certificate does not exist'
+            print('[INFO] The public certificate does not exist')
             sys.exit(0)
 
         if not os.path.exists(priv):
-            print '[INFO] The private key does not exist'
+            print('[INFO] The private key does not exist')
             sys.exit(0)            
 
         if not os.path.exists(intermediate):
-            print '[INFO] The intermediate certificate does not exist'
+            print('[INFO] The intermediate certificate does not exist')
             sys.exit(0)
 
         
@@ -1038,7 +1037,7 @@ if __name__ == '__main__':
     if auth:
         # Read in and build user dictionary
         if not os.path.exists(users):
-          print '[INFO] The users file does not exist: ' + users
+          print('[INFO] The users file does not exist: ' + users)
           sys.exit(0)
         userpassdict = {}
         user_file = open(users, 'r')