[SDC] rebase 1710 code
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importHeatTypes.py
1 import pycurl
2 import sys, getopt
3 from StringIO import StringIO
4 import json
5 import copy
6 from importCommon import *
7 from importNormativeTypes import *
8 import importCommon
9
10 ################################################################################################################################################
11 #                                                                                                                                                                                                                                                                                      #        
12 # Import all users from a given file                                                                                                                                                                                                               #
13 #                                                                                                                                                                                                                                                                                          #            
14 # activation :                                                                                                                                                                                                                                                             #
15 #       python importUsers.py [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-f <input file> | --ifile=<input file> ]     #
16 #                                                                                                                                                                                                                                                                                          #                    
17 # shortest activation (be host = localhost, be port = 8080):                                                                                                                                                               #
18 #               python importUsers.py [-f <input file> | --ifile=<input file> ]                                                                                                                                    #
19 #                                                                                                                                                                                                                                                                                      #        
20 ################################################################################################################################################
21
22 def importHeatTypes(beHost, bePort, adminUser, fileDir, updateversion):
23         
24         heatTypes = [ "globalNetwork",
25                                   "globalPort",
26                                   "globalCompute",
27                                   "volume",
28                                   "cinderVolume",
29                                   "contrailVirtualNetwork",
30                                   "neutronNet",
31                                   "neutronPort",
32                                   "novaServer",
33                                   "extVl",
34                                   "internalVl",
35                                   "extCp",
36                                   "vl",
37                                   "eline",
38                                   "abstractSubstitute",
39                                   "Generic_VFC", 
40                                   "Generic_VF",
41                                   "Generic_PNF",
42                                   "Generic_Service",
43                                   "contrailNetworkRules",
44                                   "contrailPort",
45                                   "contrailV2NetworkRules",
46                                   "contrailV2VirtualNetwork",
47                                   "securityRules",
48                                   "contrailAbstractSubstitute",
49                                   "contrailCompute",
50                                   "contrailV2VirtualMachineInterface",
51                                   "subInterface",
52                                   "contrailV2VLANSubInterface",
53                                   "multiFlavorVFC",
54                                   "vnfConfiguration"
55                                   ]
56                 
57         responseCodes = [200, 201]
58                 
59         if(updateversion == 'false'):
60                 responseCodes = [200, 201, 409]
61                 
62         results = []
63         for heatType in heatTypes:
64                 result = createNormativeType(beHost, bePort, adminUser, fileDir, heatType, updateversion)
65                 results.append(result)
66                 if ( result[1] == None or result[1] not in responseCodes) :
67                         print "Failed creating heat type " + heatType + ". " + str(result[1])                           
68         return results  
69
70
71 def main(argv):
72         print 'Number of arguments:', len(sys.argv), 'arguments.'
73
74         beHost = 'localhost' 
75         bePort = '8080'
76         adminUser = 'jh0003'
77         updateversion = 'true'
78         
79         try:
80                 opts, args = getopt.getopt(argv,"i:p:u:v:h:",["ip=","port=","user=","updateversion="])
81         except getopt.GetoptError:
82                 usage()
83                 errorAndExit(2, 'Invalid input')
84                  
85         for opt, arg in opts:
86         #print opt, arg
87                 if opt == '-h':
88                         usage()                        
89                         sys.exit(3)
90                 elif opt in ("-i", "--ip"):
91                         beHost = arg
92                 elif opt in ("-p", "--port"):
93                         bePort = arg
94                 elif opt in ("-u", "--user"):
95                         adminUser = arg
96                 elif opt in ("-v", "--updateversion"):
97                         if (arg.lower() == "false" or arg.lower() == "no"):
98                                 updateversion = 'false'
99
100         print 'be host =',beHost,', be port =', bePort,', user =', adminUser
101         
102         if ( beHost == None ):
103                 usage()
104                 sys.exit(3)
105
106         results = importHeatTypes(beHost, bePort, adminUser, "../../../import/tosca/heat-types/", updateversion)
107
108         print "-----------------------------"
109         for result in results:
110                 print "{0:20} | {1:6}".format(result[0], result[1])
111         print "-----------------------------"
112         
113         responseCodes = [200, 201]
114         
115         if(updateversion == 'false'):
116                 responseCodes = [200, 201, 409]
117         
118         failedNormatives = filter(lambda x: x[1] == None or x[1] not in responseCodes, results)
119         if (len(failedNormatives) > 0):
120                 errorAndExit(1, None)
121         else:
122                 errorAndExit(0, None)
123
124
125 if __name__ == "__main__":
126         main(sys.argv[1:])
127
128