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