Sync Integ to Master
[sdc.git] / catalog-be / src / main / resources / scripts / import / tosca / upgradeNormative.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 importCategoryTypes import importCategories
8 from upgradeHeatAndNormativeTypes import upgradeTypesPerConfigFile
9 from importDataTypes import importDataTypes
10 from importPolicyTypes import importPolicyTypes
11 from importGroupTypes import importGroupTypes
12 from importNormativeCapabilities import importNormativeCapabilities
13 from importNormativeInterfaceLifecycleTypes import importNormativeInterfaceLifecycleType
14 from importOnapTypes import importOnapTypes
15
16
17 from importCommon import *
18 import importCommon
19
20 #################################################################################################################################################################################################################################
21 #                                                                                                                                                                                                                                                                                                                                                                                                                                                       #
22 # Upgrades the normative types                                                                                                                                                                                                                                                                                                                                                                                                  #
23 #                                                                                                                                                                                                                                                                                                                                                                                                                                                               #
24 # activation :                                                                                                                                                                                                                                                                                                                                                                                                                                  #
25 #       python upgradeNormative.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>]    #
26 #                                                                                                                                                                                                                                                                                                                                                                                                                                                               #
27 #                                                                                                                                                                                                                                                                                                                                                                                                                                                               #
28 # shortest activation (be host = localhost, be port = 8080, user = jh0003):                                                                                                                                                                                                                                                                                                     #
29 #               python upgradeNormative.py                                                                                                                                                                                                                                                                                                                                                                                      #
30 #                                                                                                                                                                                                                                                                                                                                                                                                                                                       #
31 #################################################################################################################################################################################################################################
32
33 def usage():
34         print sys.argv[0], '[optional -s <scheme> | --scheme=<scheme>, default http] [-i <be host> | --ip=<be host>] [-p <be port> | --port=<be port> ] [-u <user userId> | --user=<user userId> ] [-d <true|false> | --debug=<true|false>]'
35
36 def handleResults(results, updateversion):
37         printFrameLine()
38         for result in results:
39                 printNameAndReturnCode(result[0], result[1])
40         printFrameLine()
41
42         failedResults = filter(lambda x: x[1] == None or x[1] not in [200, 201, 409], results)
43         if (len(failedResults) > 0):
44                 errorAndExit(1, None)
45
46 def main(argv):
47         print 'Number of arguments:', len(sys.argv), 'arguments.'
48
49         beHost = 'localhost'
50         bePort = '8080'
51         adminUser = 'jh0003'
52         debugf = None
53         updateversion = 'true'
54         importCommon.debugFlag = False
55         scheme = 'http'
56
57         try:
58                 opts, args = getopt.getopt(argv,"i:p:u:d:h:s:",["ip=","port=","user=","debug=","scheme="])
59         except getopt.GetoptError:
60                 usage()
61                 errorAndExit(2, 'Invalid input')
62
63         for opt, arg in opts:
64         #print opt, arg
65                 if opt == '-h':
66                         usage()
67                         sys.exit(3)
68                 elif opt in ("-i", "--ip"):
69                         beHost = arg
70                 elif opt in ("-p", "--port"):
71                         bePort = arg
72                 elif opt in ("-u", "--user"):
73                         adminUser = arg
74                 elif opt in ("-s", "--scheme"):
75                         scheme = arg
76                 elif opt in ("-d", "--debug"):
77                         print arg
78                         debugf = bool(arg.lower() == "true" or arg.lower() == "yes")
79
80         print 'scheme =',scheme,', be host =',beHost,', be port =', bePort,', user =', adminUser, ', debug =', debugf
81
82         if (debugf != None):
83                 print 'set debug mode to ' + str(debugf)
84                 importCommon.debugFlag = debugf
85
86         if ( beHost == None ):
87                 usage()
88                 sys.exit(3)
89
90         print sys.argv[0]
91         pathdir = os.path.dirname(os.path.realpath(sys.argv[0]))
92         debug("path dir =" + pathdir)
93
94         baseFileLocation = pathdir + "/../../../import/tosca/"
95
96         fileLocation = baseFileLocation + "categories/"
97         importCategories(scheme, beHost, bePort, adminUser, False, fileLocation)
98
99         fileLocation = baseFileLocation + "data-types/"
100         importDataTypes(scheme, beHost, bePort, adminUser, False, fileLocation)
101
102         fileLocation = baseFileLocation + "policy-types/"
103         importPolicyTypes(scheme, beHost, bePort, adminUser, False, fileLocation)
104
105         fileLocation = baseFileLocation + "capability-types/"
106         importNormativeCapabilities(scheme, beHost, bePort, adminUser, False, fileLocation)
107
108         fileLocation = baseFileLocation + "group-types/"
109         importGroupTypes(scheme, beHost, bePort, adminUser, False, fileLocation)
110
111         fileLocation = baseFileLocation + "interface-lifecycle-types/"
112         importNormativeInterfaceLifecycleType(scheme, beHost, bePort, adminUser, False, fileLocation)
113
114         print 'sleep until data type cache is updated'
115         time.sleep( 70 )
116
117         resultsHeat = upgradeTypesPerConfigFile(scheme, beHost, bePort, adminUser, baseFileLocation, updateversion)
118         handleResults(resultsHeat, 'false')
119         
120         errorAndExit(0, None)
121
122 if __name__ == "__main__":
123         main(sys.argv[1:])