blob: 06a8d6b374da29aae6e53e4c644a709cdd4ad592 [file] [log] [blame]
Enbo Wang6ab8b622019-04-23 13:42:21 +00001#!/usr/bin/python
2
3import sys
4import argparse
5import json
6import os
7import shutil
8import random
9import time
10import tempfile
11
12import conf
13import ems_util
14
15
16def do_download_sw(sw_info, sw_download_dir):
17 """
18 return err, reason, file_location_in_nes
19 """
20
21 sw_location = sw_info['swLocation']
22
23 # Use copy file from SW_SERVER_SIMULATOR to simulate download file
24 sw_file_name = sw_location.split('/')[-1]
25
26 file_location_in_server = conf.SW_SERVER_SIMULATOR + '/' + sw_file_name
27 file_location_in_ne = sw_download_dir + '/' + sw_file_name
28
29 try:
30 shutil.copy(file_location_in_server, sw_download_dir)
31 except IOError as e:
32 return True, "Download %s to %s error: %s" % (sw_file_name, sw_download_dir, str(e)), file_location_in_ne
33
34 return False, None, file_location_in_ne
35
36
37def generate_notification(download_process_id, download_status, downloaded_ne_sw_info, failed_sw_info):
38 notification = {
39 "objectClass": "EMSClass",
40 "objectInstance": "EMSInstance",
41 "notificationId": random.randint(1, conf.MAX_INT),
42 "eventTime": time.asctime(),
43 "systemDN": "emssimulator",
44 "notificationType": "notifyDownloadNESwStatusChanged",
45 "downloadProcessId": download_process_id,
46 "downloadOperationStatus": download_status
47 }
48
49 if downloaded_ne_sw_info:
50 notification["downloadedNESwInfo"] = downloaded_ne_sw_info
51
52 if failed_sw_info:
53 notification["failedSwInfo"] = failed_sw_info
54
55 return notification
56
57
58def download_ne_sw(sw_to_be_downloaded, ne_id):
59 ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)
60
61 download_process_id = random.randint(1, conf.MAX_INT)
62 result = conf.REQ_SUCCESS
63 ret_value = {
64 "downloadProcessId": download_process_id,
65 "result": result
66 }
67
68 if not ne_info:
69 ret_value["result"] = conf.REQ_FAILURE
70 ret_value["reason"] = "Can not find NE %s" % ne_id
71 return ret_value
72
73 ne_info["status"] = conf.STATUS_DOWNLOADING
74 ems_util.update_ne_info(ne_info)
75
76 num_sw_to_be_downloaded = len(sw_to_be_downloaded)
77
78 downloaded_ne_sw_info = []
79 failed_sw_info = []
80
81 sw_download_parent_dir = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_DOWNLOAD_DIR
82 sw_download_dir = ne_info.get("downloadedSwLocation", "")
83 try:
84 if not os.path.isdir(sw_download_parent_dir):
85 os.makedirs(sw_download_parent_dir)
86
87 if sw_download_dir and not os.path.isdir(sw_download_dir):
88 os.makedirs(sw_download_dir)
89 except OSError as e:
90 ret_value["result"] = conf.REQ_FAILURE
91 ret_value["reason"] = str(e)
92 return ret_value
93
94 if not sw_download_dir:
95 sw_download_dir = tempfile.mkdtemp(dir=sw_download_parent_dir)
96
97 for sw_info in sw_to_be_downloaded:
98 err, reason, file_location = do_download_sw(sw_info, sw_download_dir)
99 if not err:
100 downloaded_ne_sw_info.append(file_location)
101 else:
102 result = conf.REQ_FAILURE
103 failed_sw_entry = {
104 "failedSw": file_location,
105 "failureReason": reason
106 }
107 failed_sw_info.append(failed_sw_entry)
108
109 num_downloaded_ne_sw = len(downloaded_ne_sw_info)
110
111 if num_downloaded_ne_sw == num_sw_to_be_downloaded:
112 download_status = "NE_SWDOWNLOAD_SUCCESSFUL"
113 elif num_downloaded_ne_sw == 0:
114 download_status = "NE_SWDOWNLOAD_FAILED"
115 else:
116 download_status = "NE_SWDOWNLOAD_PARTIALLY_SUCCESSFUL"
117
118 notification = generate_notification(download_process_id, download_status, downloaded_ne_sw_info, failed_sw_info)
119 ems_util.send_notification(notification, download_process_id)
120
121 if result == conf.REQ_SUCCESS:
122 ne_info["downloadedSwLocation"] = sw_download_dir
123 ems_util.update_ne_info(ne_info)
124 else:
125 shutil.rmtree(sw_download_dir, ignore_errors=True)
126
127 ret_value["result"] = result
128 ret_value["reason"] = json.dumps(failed_sw_info)
129
130 # for automated software management, there is no listOfStepNumbersAndDurations
131 return ret_value
132
133
134def main():
135 parser = argparse.ArgumentParser()
136
137 parser.add_argument("--swToBeDownloaded", help="The NE software to be downloaded", required=True)
138 parser.add_argument("--neIdentifier", help="The NE where the software can be downloaded", required=True)
139
140 args = parser.parse_args()
141
142 sw_to_be_downloaded = json.loads(args.swToBeDownloaded)
143
144 ret_value = download_ne_sw(sw_to_be_downloaded, args.neIdentifier)
145 print json.dumps(ret_value)
146
147 if ret_value["result"] == conf.REQ_SUCCESS:
148 sys.exit(conf.RET_CODE_SUCCESS)
149 else:
150 sys.exit(conf.RET_CODE_FAILURE)
151
152
153if __name__ == '__main__':
154 main()