Decouple TXT Report file writing and formatting logic (6/6)
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importNormativeNoHeatAndNormativeTypes.py
1 import os
2 import time
3
4 import importCommon
5 from importCommon import *
6 from importDataTypes import importDataTypes
7 from importGroupTypes import importGroupTypes
8 from importNormativeCapabilities import importNormativeCapabilities
9 from importNormativeInterfaceLifecycleTypes import importNormativeInterfaceLifecycleType
10 from importPolicyTypes import importPolicyTypes
11
12
13 #####################################################################################################################################################################################################################################################
14 #                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               #
15 # Import all users from a given file                                                                                                                                                                                                                                                                                                                                                                                                                            #
16 #                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       #
17 # activation :                                                                                                                                                                                                                                                                                                                                                                                                                                                                          #
18 #       python importNormativeNoHeatAndNormativeTypes.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ] [-d <true|false> | --debug=<true|false>]      #
19 #                                                                        [-v <true|false> | --updateversion=<true|false>]                                                                                                                                                                                                                                                                                                                               #
20 #                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       #
21 # shortest activation (be host = localhost, be port = 8080, user = jh0003):                                                                                                                                                                                                                                                                                                                                             #
22 #               python importNormativeNoHeatAndNormativeTypes.py                                                                                                                                                                                                                                                                                                                                                                                #
23 #                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               #
24 #####################################################################################################################################################################################################################################################
25
26
27 def usage():
28     print sys.argv[0], \
29         '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ] [-d <true|false> | --debug=<true|false>] [-v <true|false> | --updateversion=<true|false>]'
30
31
32 def handleResults(results, update_version):
33     print_frame_line()
34     for result in results:
35         print_name_and_return_code(result[0], result[1])
36     print_frame_line()
37
38     response_codes = [200, 201]
39
40     if update_version == 'false':
41         response_codes = [200, 201, 409]
42
43     failed_results = filter(lambda x: x[1] is None or x[1] not in response_codes, results)
44     if len(list(failed_results)) > 0:
45         error_and_exit(1, None)
46
47
48 def main(argv):
49     print 'Number of arguments:', len(sys.argv), 'arguments.'
50
51     be_host = 'localhost'
52     be_port = '8080'
53     admin_user = 'jh0003'
54     debug_f = None
55     update_version = 'true'
56     importCommon.debugFlag = False
57     scheme = 'http'
58
59     try:
60         opts, args = getopt.getopt(argv, "i:p:u:d:v:h:s:",
61                                    ["ip=", "port=", "user=", "debug=", "updateversion=", "scheme="])
62     except getopt.GetoptError:
63         usage()
64         error_and_exit(2, 'Invalid input')
65
66     for opt, arg in opts:
67         # print opt, arg
68         if opt == '-h':
69             usage()
70             sys.exit(3)
71         elif opt in ("-i", "--ip"):
72             be_host = arg
73         elif opt in ("-p", "--port"):
74             be_port = arg
75         elif opt in ("-u", "--user"):
76             admin_user = arg
77         elif opt in ("-s", "--scheme"):
78             scheme = arg
79         elif opt in ("-d", "--debug"):
80             print arg
81             debug_f = bool(arg.lower() == "true" or arg.lower() == "yes")
82         elif opt in ("-v", "--updateversion"):
83             print arg
84             if arg.lower() == "false" or arg.lower() == "no":
85                 update_version = 'false'
86
87     print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user, ', debug =', debug_f, ', updateversion =', update_version
88
89     if debug_f is not None:
90         print 'set debug mode to ' + str(debug_f)
91         importCommon.debugFlag = debug_f
92
93     if be_host is None:
94         usage()
95         sys.exit(3)
96
97     print sys.argv[0]
98     path_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
99     debug("path dir =" + path_dir)
100
101     base_file_location = path_dir + "/../../../import/tosca/"
102
103     file_location = base_file_location + "data-types/"
104     importDataTypes(scheme, be_host, be_port, admin_user, False, file_location)
105
106     print 'sleep until data type cache is updated'
107     time.sleep(70)
108
109     file_location = base_file_location + "capability-types/"
110     importNormativeCapabilities(scheme, be_host, be_port, admin_user, False, file_location)
111
112     file_location = base_file_location + "interface-lifecycle-types/"
113     importNormativeInterfaceLifecycleType(scheme, be_host, be_port, admin_user, False, file_location)
114
115     # fileLocation = baseFileLocation + "categories/"
116     # importCategories(beHost, bePort, adminUser, False, fileLocation)
117
118     # fileLocation = baseFileLocation + "normative-types/"
119     # results = importNormativeTypes(beHost, bePort, adminUser, fileLocation, updateversion)
120     # handleResults(results, updateversion)
121
122     # fileLocation = baseFileLocation + "heat-types/"
123     # resultsHeat = importHeatTypes(beHost, bePort, adminUser, fileLocation, updateversion)
124     # handleResults(resultsHeat, updateversion)
125
126     file_location = base_file_location + "group-types/"
127     importGroupTypes(scheme, be_host, be_port, admin_user, False, file_location)
128
129     file_location = base_file_location + "policy-types/"
130     importPolicyTypes(scheme, be_host, be_port, admin_user, False, file_location)
131
132     error_and_exit(0, None)
133
134
135 if __name__ == "__main__":
136     main(sys.argv[1:])