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