Decouple TXT Report file writing and formatting logic (6/6)
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importHeatTypes.py
1 from importNormativeTypes import *
2
3
4 #####################################################################################################################################################################################
5 #                                                                                                                                                                                                                                                                                                                                                               #
6 # Import heat types                                                                                                                                                                                                                                                                                                                                     #
7 #                                                                                                                                                                                                                                                                                                                                                                       #
8 # activation :                                                                                                                                                                                                                                                                                                                                          #
9 #       python importHeatTypes.py [-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]       #
10 #                                                                                                                                                                                                                                                                                                                                                                       #
11 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                                                                                                            #
12 #               python importHeatTypes.py [-f <input file> | --ifile=<input file> ]                                                                                                                                                                                                             #
13 #                                                                                                                                                                                                                                                                                                                                                               #
14 #####################################################################################################################################################################################
15
16 def importHeatTypes(scheme, be_host, be_port, admin_user, file_dir, update_version):
17     heat_types = ["globalNetwork",
18                   "globalPort",
19                   "globalCompute",
20                   "volume",
21                   "cinderVolume",
22                   "contrailVirtualNetwork",
23                   "neutronNet",
24                   "neutronPort",
25                   "novaServer",
26                   "extVl",
27                   "internalVl",
28                   "extCp",
29                   "vl",
30                   "eline",
31                   "abstractSubstitute",
32                   "Generic_VFC",
33                   "Generic_VF",
34                   "Generic_CR",
35                   "Generic_PNF",
36                   "Generic_Service",
37                   "contrailNetworkRules",
38                   "contrailPort",
39                   "portMirroring",
40                   "serviceProxy",
41                   "contrailV2NetworkRules",
42                   "contrailV2VirtualNetwork",
43                   "securityRules",
44                   "contrailAbstractSubstitute",
45                   "contrailCompute",
46                   "contrailV2VirtualMachineInterface",
47                   "subInterface",
48                   "contrailV2VLANSubInterface",
49                   "multiFlavorVFC",
50                   "vnfConfiguration",
51                   "extCp2",
52                   "extNeutronCP",
53                   "extContrailCP",
54                   "portMirroringByPolicy",
55                   "forwardingPath",
56                   "configuration",
57                   "VRFObject",
58                   "extVirtualMachineInterfaceCP",
59                   "VLANNetworkReceptor",
60                   "VRFEntry",
61                   "subInterfaceV2",
62                   "contrailV2VLANSubInterfaceV2",
63                   "fabricConfiguration"]
64
65     response_codes = [200, 201]
66
67     if update_version == 'false':
68         response_codes = [200, 201, 409]
69
70     results = []
71     for heat_type in heat_types:
72         result = createNormativeType(scheme, be_host, be_port, admin_user, file_dir, heat_type, update_version)
73         results.append(result)
74         if result[1] is None or result[1] not in response_codes:
75             print "Failed creating heat type " + heat_type + ". " + str(result[1])
76     return results
77
78
79 def main(argv):
80     print 'Number of arguments:', len(sys.argv), 'arguments.'
81
82     be_host = 'localhost'
83     be_port = '8080'
84     admin_user = 'jh0003'
85     update_version = 'true'
86     scheme = 'http'
87
88     try:
89         opts, args = getopt.getopt(argv, "i:p:u:v:h:s:", ["ip=", "port=", "user=", "updateversion=", "scheme="])
90     except getopt.GetoptError:
91         usage()
92         error_and_exit(2, 'Invalid input')
93
94     for opt, arg in opts:
95         # print opt, arg
96         if opt == '-h':
97             usage()
98             sys.exit(3)
99         elif opt in ("-i", "--ip"):
100             be_host = arg
101         elif opt in ("-p", "--port"):
102             be_port = arg
103         elif opt in ("-u", "--user"):
104             admin_user = arg
105         elif opt in ("-s", "--scheme"):
106             scheme = arg
107         elif opt in ("-v", "--updateversion"):
108             if arg.lower() == "false" or arg.lower() == "no":
109                 update_version = 'false'
110
111     print 'scheme =', scheme, ', be host =', be_host, ', be port =', be_port, ', user =', admin_user
112
113     if be_host is None:
114         usage()
115         sys.exit(3)
116
117     results = importHeatTypes(scheme, be_host, be_port, admin_user, "../../../import/tosca/heat-types/", update_version)
118
119     print "-----------------------------"
120     for result in results:
121         print "{0:20} | {1:6}".format(result[0], result[1])
122     print "-----------------------------"
123
124     response_codes = [200, 201]
125
126     if update_version == 'false':
127         response_codes = [200, 201, 409]
128
129     failed_normatives = filter(lambda x: x[1] is None or x[1] not in response_codes, results)
130     if len(list(failed_normatives)) > 0:
131         error_and_exit(1, None)
132     else:
133         error_and_exit(0, None)
134
135
136 if __name__ == "__main__":
137     main(sys.argv[1:])