Enbo Wang | 818f76a | 2020-03-04 00:42:31 +0800 | [diff] [blame^] | 1 | # ============LICENSE_START======================================================= |
| 2 | # ONAP - SO |
| 3 | # ================================================================================ |
| 4 | # Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | # ================================================================================ |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END========================================================= |
| 18 | |
| 19 | import os |
| 20 | import logging |
| 21 | import json |
| 22 | import shutil |
| 23 | |
| 24 | import conf |
| 25 | import ems_util |
| 26 | |
| 27 | |
| 28 | OPERATION_NAME = "swFallback" |
| 29 | logging.basicConfig(level=logging.INFO, format=conf.LOGGER_FORMAT, filename=ems_util.get_log_file(OPERATION_NAME)) |
| 30 | logger = logging.getLogger(OPERATION_NAME) |
| 31 | |
| 32 | |
| 33 | def fallback(ne_info_list): |
| 34 | logger.info("NE info list: %s" % ne_info_list) |
| 35 | |
| 36 | ne_list = [] |
| 37 | num_failure = 0 |
| 38 | |
| 39 | for ne_info in ne_info_list: |
| 40 | if ne_info.get("status") == conf.STATUS_DOWNLOADING: |
| 41 | ne_info["status"] = conf.STATUS_ACTIVATED |
| 42 | ems_util.update_ne_info(ne_info) |
| 43 | |
| 44 | ne_entry = { |
| 45 | "nEIdentification": ne_info["nEIdentification"], |
| 46 | "swFallbackStatus": "fallbackSuccessful" |
| 47 | } |
| 48 | ne_list.append(ne_entry) |
| 49 | continue |
| 50 | |
| 51 | sw_install_dir_in_ne = ems_util.get_install_dir(ne_info['omIP']) |
| 52 | |
| 53 | if ne_info.get("status") == conf.STATUS_INSTALLING: |
| 54 | old_sw_version = ne_info.get("currentSwVersion", "") |
| 55 | current_sw_version = ne_info.get("targetSwVersion", "") |
| 56 | else: |
| 57 | old_sw_version = ne_info.get("oldSwVersion", "") |
| 58 | current_sw_version = ne_info.get("currentSwVersion", "") |
| 59 | |
| 60 | old_sw_dir = os.path.join(sw_install_dir_in_ne, old_sw_version) |
| 61 | |
| 62 | if not old_sw_version or not os.path.isdir(old_sw_dir): |
| 63 | ne_entry = { |
| 64 | "nEIdentification": ne_info["nEIdentification"], |
| 65 | "swFallbackStatus": "fallbackUnsuccessful" |
| 66 | } |
| 67 | logger.error("oldSwVersion (%s) or oldSwDirectory (%s) is none" % (old_sw_version, old_sw_dir)) |
| 68 | ne_list.append(ne_entry) |
| 69 | |
| 70 | num_failure += 1 |
| 71 | continue |
| 72 | |
| 73 | current_sw_dir = os.path.join(sw_install_dir_in_ne, current_sw_version) |
| 74 | |
| 75 | if current_sw_version and os.path.isdir(current_sw_dir) and current_sw_dir != old_sw_dir: |
| 76 | shutil.rmtree(current_sw_dir, ignore_errors=True) |
| 77 | |
| 78 | old_cwd = os.getcwd() |
| 79 | os.chdir(sw_install_dir_in_ne) |
| 80 | if os.path.islink(conf.CURRENT_VERSION_DIR): |
| 81 | os.remove(conf.CURRENT_VERSION_DIR) |
| 82 | os.symlink(old_sw_version, conf.CURRENT_VERSION_DIR) |
| 83 | os.chdir(old_cwd) |
| 84 | |
| 85 | installed_sw_db = os.path.join(old_sw_dir, conf.INSTALLED_SW_FILE) |
| 86 | if os.path.isfile(installed_sw_db): |
| 87 | with open(installed_sw_db) as f_installed_sw: |
| 88 | installed_sw_table = json.load(f_installed_sw) |
| 89 | if not installed_sw_table: |
| 90 | installed_sw_table = {} |
| 91 | else: |
| 92 | installed_sw_table = {} |
| 93 | |
| 94 | ne_info["installedSw"] = installed_sw_table |
| 95 | if "oldSwVersion" in ne_info: |
| 96 | ne_info["currentSwVersion"] = ne_info["oldSwVersion"] |
| 97 | del ne_info["oldSwVersion"] |
| 98 | |
| 99 | if "targetSwVersion" in ne_info: |
| 100 | del ne_info["targetSwVersion"] |
| 101 | |
| 102 | if "downloadedSwLocation" in ne_info: |
| 103 | if os.path.isdir(ne_info["downloadedSwLocation"]): |
| 104 | shutil.rmtree(ne_info["downloadedSwLocation"], ignore_errors=True) |
| 105 | del ne_info["downloadedSwLocation"] |
| 106 | |
| 107 | ne_info["status"] = conf.STATUS_ACTIVATED |
| 108 | ems_util.update_ne_info(ne_info) |
| 109 | |
| 110 | ne_entry = { |
| 111 | "nEIdentification": ne_info["nEIdentification"], |
| 112 | "swFallbackStatus": "fallbackSuccessful" |
| 113 | } |
| 114 | ne_list.append(ne_entry) |
| 115 | |
| 116 | if num_failure == 0: |
| 117 | result = conf.RESULT_SUCCESS |
| 118 | elif num_failure == len(ne_info_list): |
| 119 | result = conf.RESULT_FAILURE |
| 120 | else: |
| 121 | result = conf.RESULT_PARTLY |
| 122 | logger.info("Fallback SW result: %s" % result) |
| 123 | |
| 124 | ret_value = { |
| 125 | "nEList": ne_list, |
| 126 | "result": result |
| 127 | } |
| 128 | |
| 129 | return ret_value |