Sync Integ to Master
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / importONAPNormativeAll.py
1 import pycurl
2 import sys, getopt, os
3 from StringIO import StringIO
4 import json
5 import copy
6 import time
7 #from importNormativeElements import createNormativeElement
8 from importNormativeElements import * 
9 from importNormativeTypes import importNormativeTypes
10 from importHeatTypes import importHeatTypes
11 from importOnapTypes import importOnapTypes
12 from importNormativeCapabilities import importNormativeCapabilities
13 from importCategoryTypes import importCategories
14 from importNormativeInterfaceLifecycleTypes import importNormativeInterfaceLifecycleType
15 from importDataTypes import importDataTypes
16 from importGroupTypes import importGroupTypes
17 from importPolicyTypes import importPolicyTypes
18 from importCommon import *
19 import importCommon
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], '[-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>]'
36
37 def handleResults(results, updateversion):
38         printFrameLine()
39         for result in results:
40                 printNameAndReturnCode(result[0], result[1])
41         printFrameLine()
42         
43         responseCodes = [200, 201]
44         
45         if(updateversion == 'false'):
46                 responseCodes = [200, 201, 409]
47         
48         failedResults = filter(lambda x: x[1] == None or x[1] not in responseCodes, results)
49         if (len(failedResults) > 0):
50                 errorAndExit(1, None)
51
52 def main(argv):
53         print 'Number of arguments:', len(sys.argv), 'arguments.'
54
55         beHost = 'localhost' 
56         bePort = '8080'
57         adminUser = 'jh0003'
58         debugf = None
59         updateversion = 'true'
60         importCommon.debugFlag = False 
61         scheme = 'http'
62
63         try:
64                 opts, args = getopt.getopt(argv,"i:p:u:d:v:h",["ip=","port=","user=","debug=","updateversion="])
65         except getopt.GetoptError:
66                 usage()
67                 errorAndExit(2, 'Invalid input')
68         
69         for opt, arg in opts:
70         #print opt, arg
71                 if opt == '-h':
72                         usage()                        
73                         sys.exit(3)
74                 elif opt in ("-i", "--ip"):
75                         beHost = arg
76                 elif opt in ("-p", "--port"):
77                         bePort = arg
78                 elif opt in ("-u", "--user"):
79                         adminUser = arg
80                 elif opt in ("-s", "--scheme"):
81                         scheme = arg
82                 elif opt in ("-d", "--debug"):
83                         print arg
84                         debugf = bool(arg.lower() == "true" or arg.lower() == "yes")
85                 elif opt in ("-v", "--updateversion"):
86                         print arg
87                         if (arg.lower() == "false" or arg.lower() == "no"):
88                                 updateversion = 'false'                 
89
90         print 'scheme =',scheme,',be host =',beHost,', be port =', bePort,', user =', adminUser, ', debug =', debugf, ', updateversion =', updateversion
91
92         if (debugf != None):
93                 print 'set debug mode to ' + str(debugf)
94                 importCommon.debugFlag = debugf
95         
96         if ( beHost == None ):
97                 usage()
98                 sys.exit(3)
99
100         print sys.argv[0]
101         pathdir = os.path.dirname(os.path.realpath(sys.argv[0]))      
102         debug("path dir =" + pathdir)
103
104         baseFileLocation = pathdir + "/../../../import/tosca/"
105
106         fileLocation = baseFileLocation + "data-types/"
107         importDataTypes(scheme, beHost, bePort, adminUser, False, fileLocation)
108
109         print 'sleep until data type cache is updated'
110         time.sleep( 70 )
111         
112         fileLocation = baseFileLocation + "capability-types/"
113         importNormativeCapabilities(scheme, beHost, bePort, adminUser, False, fileLocation)
114
115         fileLocation = baseFileLocation + "interface-lifecycle-types/"
116         importNormativeInterfaceLifecycleType(scheme, beHost, bePort, adminUser, False, fileLocation)   
117
118         fileLocation = baseFileLocation + "categories/"
119         importCategories(scheme, beHost, bePort, adminUser, False, fileLocation)
120         
121         fileLocation = baseFileLocation + "normative-types/"
122         results = importNormativeTypes(scheme, beHost, bePort, adminUser, fileLocation, updateversion)
123         handleResults(results, updateversion)
124
125         fileLocation = baseFileLocation + "heat-types/"
126         resultsHeat = importHeatTypes(scheme, beHost, bePort, adminUser, fileLocation, updateversion)
127         handleResults(resultsHeat, updateversion)
128         
129         fileLocation = baseFileLocation + "onap-types/"
130         resultsHeat = importOnapTypes(scheme, beHost, bePort, adminUser, fileLocation, updateversion)
131         handleResults(resultsHeat, updateversion)
132   
133         fileLocation = baseFileLocation + "group-types/"
134         importGroupTypes(scheme, beHost, bePort, adminUser, False, fileLocation)
135         
136         fileLocation = baseFileLocation + "policy-types/"
137         importPolicyTypes(scheme, beHost, bePort, adminUser, False, fileLocation)
138
139         errorAndExit(0, None)   
140
141 if __name__ == "__main__":
142         main(sys.argv[1:])
143