664c5cc64a7c7cbc16706953920b5ddd5350de1d
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importOnapTypes.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 importOnapTypes(beHost, bePort, adminUser, fileDir, updateversion):
23         
24         onapTypes = [ "extImageFile",
25                       "extLocalStorage",
26                       "extZteCP",
27                       "extZteVDU",
28                       "extZteVL",
29                       "vduCompute",
30                                   "vduCpd",
31                                   "vduVirtualStorage",
32                                   "vnfVirtualLinkDesc",
33                                   "NSD",
34                                   "VDU"
35                                   ]
36                 
37         responseCodes = [200, 201]
38                 
39         if(updateversion == 'false'):
40                 responseCodes = [200, 201, 409]
41                 
42         results = []
43         for onapType in onapTypes:
44                 result = createNormativeType(beHost, bePort, adminUser, fileDir, onapType, updateversion)
45                 results.append(result)
46                 if ( result[1] == None or result[1] not in responseCodes) :
47                         print "Failed creating heat type " + onapType + ". " + str(result[1])                           
48         return results  
49
50
51 def main(argv):
52         print 'Number of arguments:', len(sys.argv), 'arguments.'
53
54         beHost = 'localhost' 
55         bePort = '8080'
56         adminUser = 'jh0003'
57         updateversion = 'true'
58         
59         try:
60                 opts, args = getopt.getopt(argv,"i:p:u:v:h:",["ip=","port=","user=","updateversion="])
61         except getopt.GetoptError:
62                 usage()
63                 errorAndExit(2, 'Invalid input')
64                  
65         for opt, arg in opts:
66         #print opt, arg
67                 if opt == '-h':
68                         usage()                        
69                         sys.exit(3)
70                 elif opt in ("-i", "--ip"):
71                         beHost = arg
72                 elif opt in ("-p", "--port"):
73                         bePort = arg
74                 elif opt in ("-u", "--user"):
75                         adminUser = arg
76                 elif opt in ("-v", "--updateversion"):
77                         if (arg.lower() == "false" or arg.lower() == "no"):
78                                 updateversion = 'false'
79
80         print 'be host =',beHost,', be port =', bePort,', user =', adminUser
81         
82         if ( beHost == None ):
83                 usage()
84                 sys.exit(3)
85
86         results = importOnapTypes(beHost, bePort, adminUser, "../../../import/tosca/onap-types/", updateversion)
87
88         print "-----------------------------"
89         for result in results:
90                 print "{0:20} | {1:6}".format(result[0], result[1])
91         print "-----------------------------"
92         
93         responseCodes = [200, 201]
94         
95         if(updateversion == 'false'):
96                 responseCodes = [200, 201, 409]
97         
98         failedNormatives = filter(lambda x: x[1] == None or x[1] not in responseCodes, results)
99         if (len(failedNormatives) > 0):
100                 errorAndExit(1, None)
101         else:
102                 errorAndExit(0, None)
103
104
105 if __name__ == "__main__":
106         main(sys.argv[1:])
107
108