blob: 9d6608c23b6bae8049a69488da82be90c1d32b23 [file] [log] [blame]
Enbo Wang6ab8b622019-04-23 13:42:21 +00001#!/usr/bin/python
2
3import argparse
4import json
5import sys
6import os
7import shutil
8
9import conf
10import ems_util
11
12
13def sw_fallback(ne_info_list):
14 ne_list = []
15 num_failure = 0
16
17 for ne_info in ne_info_list:
18 if ne_info.get("status") == conf.STATUS_DOWNLOADING:
19 ne_info["status"] = conf.STATUS_ACTIVATED
20 ems_util.update_ne_info(ne_info)
21
22 ne_entry = {
23 "nEIdentification": ne_info["nEIdentification"],
24 "swFallbackStatus": "fallbackSuccessful"
25 }
26 ne_list.append(ne_entry)
27 continue
28
29 sw_install_dir_in_ne = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_INSTALL_DIR
30
31 if ne_info.get("status") == conf.STATUS_INSTALLING:
32 old_sw_version = ne_info.get("currentSwVersion", "")
33 current_sw_version = ne_info.get("targetSwVersion", "")
34 else:
35 old_sw_version = ne_info.get("oldSwVersion", "")
36 current_sw_version = ne_info.get("currentSwVersion", "")
37
38 old_sw_dir = sw_install_dir_in_ne + '/' + old_sw_version
39
40 if not old_sw_version or not os.path.isdir(old_sw_dir):
41 ne_entry = {
42 "nEIdentification": ne_info["nEIdentification"],
43 "swFallbackStatus": "fallbackUnsuccessful"
44 }
45 ne_list.append(ne_entry)
46
47 num_failure += 1
48 continue
49
50 current_sw_dir = sw_install_dir_in_ne + '/' + current_sw_version
51
52 if current_sw_version and os.path.isdir(current_sw_dir) and current_sw_dir != old_sw_dir:
53 shutil.rmtree(current_sw_dir, ignore_errors=True)
54
55 old_cwd = os.getcwd()
56 os.chdir(sw_install_dir_in_ne)
57 if os.path.islink(conf.CURRENT_VERSION_DIR):
58 os.remove(conf.CURRENT_VERSION_DIR)
59 os.symlink(old_sw_version, conf.CURRENT_VERSION_DIR)
60 os.chdir(old_cwd)
61
62 installed_sw_db = old_sw_dir + '/' + conf.INSTALLED_SW
63 if os.path.isfile(installed_sw_db):
64 with open(installed_sw_db) as f_installed_sw:
65 installed_sw_table = json.load(f_installed_sw)
66 if not installed_sw_table:
67 installed_sw_table = {}
68 else:
69 installed_sw_table = {}
70
71 ne_info["installedSw"] = installed_sw_table
72 if "oldSwVersion" in ne_info:
73 ne_info["currentSwVersion"] = ne_info["oldSwVersion"]
74 del ne_info["oldSwVersion"]
75
76 if "targetSwVersion" in ne_info:
77 del ne_info["targetSwVersion"]
78
79 if "downloadedSwLocation" in ne_info:
80 if os.path.isdir(ne_info["downloadedSwLocation"]):
81 shutil.rmtree(ne_info["downloadedSwLocation"], ignore_errors=True)
82 del ne_info["downloadedSwLocation"]
83
84 ne_info["status"] = conf.STATUS_ACTIVATED
85 ems_util.update_ne_info(ne_info)
86
87 ne_entry = {
88 "nEIdentification": ne_info["nEIdentification"],
89 "swFallbackStatus": "fallbackSuccessful"
90 }
91 ne_list.append(ne_entry)
92
93 if num_failure == 0:
94 result = conf.RESULT_SUCCESS
95 elif num_failure == len(ne_info_list):
96 result = conf.RESULT_FAILURE
97 else:
98 result = conf.RESULT_PARTLY
99
100 ret_value = {
101 "nEList": ne_list,
102 "result": result
103 }
104
105 return ret_value
106
107
108def main():
109 parser = argparse.ArgumentParser()
110
111 parser.add_argument("--filter", help="To describe properties of the NEs to be selected", required=True)
112
113 args = parser.parse_args()
114
115 ne_info_list = ems_util.get_ne_info_list_from_db(args.filter)
116
117 ret_value = sw_fallback(ne_info_list)
118 print json.dumps(ret_value)
119
120 if ret_value["result"] == conf.RESULT_SUCCESS:
121 sys.exit(conf.RET_CODE_SUCCESS)
122 else:
123 sys.exit(conf.RET_CODE_FAILURE)
124
125
126if __name__ == '__main__':
127 main()