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