Enbo Wang | 6ab8b62 | 2019-04-23 13:42:21 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | import argparse |
| 5 | import json |
| 6 | import os |
| 7 | import shutil |
| 8 | import random |
| 9 | import time |
| 10 | |
| 11 | import conf |
| 12 | import ems_util |
| 13 | |
| 14 | |
| 15 | def do_activate_sw(sw_version_to_be_activated, ne_info): |
| 16 | """ |
| 17 | return err, reason |
| 18 | """ |
| 19 | |
| 20 | installed_sw = ne_info.get("installedSw", {}) |
| 21 | if sw_version_to_be_activated in installed_sw: |
| 22 | target_sw_version = installed_sw[sw_version_to_be_activated]["version"] |
| 23 | else: |
| 24 | target_sw_version = sw_version_to_be_activated |
| 25 | |
| 26 | sw_install_dir_in_ne = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_INSTALL_DIR |
| 27 | target_sw_dir = sw_install_dir_in_ne + '/' + target_sw_version |
| 28 | if not os.path.isdir(target_sw_dir): |
| 29 | return True, "SW to be activated does not install" |
| 30 | |
| 31 | if "targetSwVersion" in ne_info: |
| 32 | if ne_info["targetSwVersion"] != target_sw_version: |
| 33 | return True, "Conflicted targetVersion with to be activated %s" % target_sw_version |
| 34 | del ne_info["targetSwVersion"] |
| 35 | |
| 36 | old_sw_version = ne_info.get("oldSwVersion", "") |
| 37 | |
| 38 | if target_sw_version != ne_info["currentSwVersion"]: |
| 39 | ne_info["oldSwVersion"] = ne_info["currentSwVersion"] |
| 40 | ne_info["currentSwVersion"] = target_sw_version |
| 41 | ne_info["status"] = conf.STATUS_ACTIVATING |
| 42 | ems_util.update_ne_info(ne_info) |
| 43 | |
| 44 | if target_sw_version != old_sw_version: |
| 45 | old_sw_dir = sw_install_dir_in_ne + '/' + old_sw_version |
| 46 | if old_sw_version and os.path.isdir(old_sw_dir): |
| 47 | shutil.rmtree(old_sw_dir, ignore_errors=True) |
| 48 | |
| 49 | old_cwd = os.getcwd() |
| 50 | os.chdir(sw_install_dir_in_ne) |
| 51 | if os.path.islink(conf.CURRENT_VERSION_DIR): |
| 52 | os.remove(conf.CURRENT_VERSION_DIR) |
| 53 | os.symlink(target_sw_version, conf.CURRENT_VERSION_DIR) |
| 54 | os.chdir(old_cwd) |
| 55 | |
| 56 | if "downloadedSwLocation" in ne_info: |
| 57 | if os.path.isdir(ne_info["downloadedSwLocation"]): |
| 58 | shutil.rmtree(ne_info["downloadedSwLocation"], ignore_errors=True) |
| 59 | del ne_info["downloadedSwLocation"] |
| 60 | |
| 61 | return False, None |
| 62 | |
| 63 | |
| 64 | def generate_notification(activate_process_id, activate_status, sw_version, failure_reason): |
| 65 | notification = { |
| 66 | "objectClass": "EMSClass", |
| 67 | "objectInstance": "EMSInstance", |
| 68 | "notificationId": random.randint(1, conf.MAX_INT), |
| 69 | "eventTime": time.asctime(), |
| 70 | "systemDN": "emssimulator", |
| 71 | "notificationType": "notifyActivateNESwStatusChanged", |
| 72 | "activateProcessId": activate_process_id, |
| 73 | "activateOperationStatus": activate_status, |
| 74 | "swVersion": sw_version |
| 75 | } |
| 76 | |
| 77 | if failure_reason: |
| 78 | notification["failureReason"] = failure_reason |
| 79 | |
| 80 | return notification |
| 81 | |
| 82 | |
| 83 | def activate_ne_sw(sw_version_to_be_activated, ne_id): |
| 84 | ne_info = ems_util.get_ne_info_from_db_by_id(ne_id) |
| 85 | |
| 86 | activate_process_id = random.randint(1, conf.MAX_INT) |
| 87 | result = conf.REQ_SUCCESS |
| 88 | ret_value = { |
| 89 | "activateProcessId": activate_process_id, |
| 90 | "result": result |
| 91 | } |
| 92 | |
| 93 | if not ne_info: |
| 94 | ret_value["result"] = conf.REQ_FAILURE |
| 95 | ret_value["reason"] = "Can not find NE %s" % ne_id |
| 96 | return ret_value |
| 97 | |
| 98 | err, reason = do_activate_sw(sw_version_to_be_activated, ne_info) |
| 99 | |
| 100 | if not err: |
| 101 | ne_info["status"] = conf.STATUS_ACTIVATED |
| 102 | ems_util.update_ne_info(ne_info) |
| 103 | activate_status = "NE_SWACTIVATION_SUCCESSFUL" |
| 104 | else: |
| 105 | ret_value["result"] = conf.REQ_FAILURE |
| 106 | ret_value["reason"] = reason |
| 107 | |
| 108 | activate_status = "NE_SWACTIVATION_FAILED" |
| 109 | |
| 110 | notification = generate_notification(activate_process_id, activate_status, sw_version_to_be_activated, reason) |
| 111 | ems_util.send_notification(notification, activate_process_id) |
| 112 | |
| 113 | # for automated software management, there is no listOfStepNumbersAndDurations |
| 114 | return ret_value |
| 115 | |
| 116 | |
| 117 | def main(): |
| 118 | parser = argparse.ArgumentParser() |
| 119 | |
| 120 | parser.add_argument("--swVersionToBeActivated", help="The NE software version to be activated", required=True) |
| 121 | parser.add_argument("--neIdentifier", help="The NE where the software can be activated", required=True) |
| 122 | |
| 123 | args = parser.parse_args() |
| 124 | |
| 125 | ret_value = activate_ne_sw(args.swVersionToBeActivated, args.neIdentifier) |
| 126 | print json.dumps(ret_value) |
| 127 | |
| 128 | if ret_value["result"] == conf.REQ_SUCCESS: |
| 129 | sys.exit(conf.RET_CODE_SUCCESS) |
| 130 | else: |
| 131 | sys.exit(conf.RET_CODE_FAILURE) |
| 132 | |
| 133 | |
| 134 | if __name__ == '__main__': |
| 135 | main() |