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