aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/emssimulator/swm/swFallback
blob: 9d6608c23b6bae8049a69488da82be90c1d32b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python

import argparse
import json
import sys
import os
import shutil

import conf
import ems_util


def sw_fallback(ne_info_list):
    ne_list = []
    num_failure = 0

    for ne_info in ne_info_list:
        if ne_info.get("status") == conf.STATUS_DOWNLOADING:
            ne_info["status"] = conf.STATUS_ACTIVATED
            ems_util.update_ne_info(ne_info)

            ne_entry = {
                "nEIdentification": ne_info["nEIdentification"],
                "swFallbackStatus": "fallbackSuccessful"
            }
            ne_list.append(ne_entry)
            continue

        sw_install_dir_in_ne = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_INSTALL_DIR

        if ne_info.get("status") == conf.STATUS_INSTALLING:
            old_sw_version = ne_info.get("currentSwVersion", "")
            current_sw_version = ne_info.get("targetSwVersion", "")
        else:
            old_sw_version = ne_info.get("oldSwVersion", "")
            current_sw_version = ne_info.get("currentSwVersion", "")

        old_sw_dir = sw_install_dir_in_ne + '/' + old_sw_version

        if not old_sw_version or not os.path.isdir(old_sw_dir):
            ne_entry = {
                "nEIdentification": ne_info["nEIdentification"],
                "swFallbackStatus": "fallbackUnsuccessful"
            }
            ne_list.append(ne_entry)

            num_failure += 1
            continue

        current_sw_dir = sw_install_dir_in_ne + '/' + current_sw_version

        if current_sw_version and os.path.isdir(current_sw_dir) and current_sw_dir != old_sw_dir:
            shutil.rmtree(current_sw_dir, ignore_errors=True)

        old_cwd = os.getcwd()
        os.chdir(sw_install_dir_in_ne)
        if os.path.islink(conf.CURRENT_VERSION_DIR):
            os.remove(conf.CURRENT_VERSION_DIR)
        os.symlink(old_sw_version, conf.CURRENT_VERSION_DIR)
        os.chdir(old_cwd)

        installed_sw_db = old_sw_dir + '/' + conf.INSTALLED_SW
        if os.path.isfile(installed_sw_db):
            with open(installed_sw_db) as f_installed_sw:
                installed_sw_table = json.load(f_installed_sw)
            if not installed_sw_table:
                installed_sw_table = {}
        else:
            installed_sw_table = {}

        ne_info["installedSw"] = installed_sw_table
        if "oldSwVersion" in ne_info:
            ne_info["currentSwVersion"] = ne_info["oldSwVersion"]
            del ne_info["oldSwVersion"]

        if "targetSwVersion" in ne_info:
            del ne_info["targetSwVersion"]

        if "downloadedSwLocation" in ne_info:
            if os.path.isdir(ne_info["downloadedSwLocation"]):
                shutil.rmtree(ne_info["downloadedSwLocation"], ignore_errors=True)
            del ne_info["downloadedSwLocation"]

        ne_info["status"] = conf.STATUS_ACTIVATED
        ems_util.update_ne_info(ne_info)

        ne_entry = {
            "nEIdentification": ne_info["nEIdentification"],
            "swFallbackStatus": "fallbackSuccessful"
        }
        ne_list.append(ne_entry)

    if num_failure == 0:
        result = conf.RESULT_SUCCESS
    elif num_failure == len(ne_info_list):
        result = conf.RESULT_FAILURE
    else:
        result = conf.RESULT_PARTLY

    ret_value = {
        "nEList": ne_list,
        "result": result
    }

    return ret_value


def main():
    parser = argparse.ArgumentParser()

    parser.add_argument("--filter", help="To describe properties of the NEs to be selected", required=True)

    args = parser.parse_args()

    ne_info_list = ems_util.get_ne_info_list_from_db(args.filter)

    ret_value = sw_fallback(ne_info_list)
    print json.dumps(ret_value)

    if ret_value["result"] == conf.RESULT_SUCCESS:
        sys.exit(conf.RET_CODE_SUCCESS)
    else:
        sys.exit(conf.RET_CODE_FAILURE)


if __name__ == '__main__':
    main()