Add an EMS simulator 52/86052/2
authorEnbo Wang <wangenbo@huawei.com>
Tue, 23 Apr 2019 13:42:21 +0000 (13:42 +0000)
committerEnbo Wang <wangenbo@huawei.com>
Tue, 23 Apr 2019 14:17:45 +0000 (14:17 +0000)
Change-Id: I4fedf9a812e19033e7f9a1bff55eae264bc5122f
Issue-ID: INT-1041
Signed-off-by: Enbo Wang <wangenbo@huawei.com>
41 files changed:
test/mocks/emssimulator/install.sh [new file with mode: 0755]
test/mocks/emssimulator/requirements.txt [new file with mode: 0644]
test/mocks/emssimulator/swm/__init__.py [new file with mode: 0644]
test/mocks/emssimulator/swm/activateNESw [new file with mode: 0755]
test/mocks/emssimulator/swm/conf.py [new file with mode: 0644]
test/mocks/emssimulator/swm/downloadNESw [new file with mode: 0755]
test/mocks/emssimulator/swm/ems_db/ne_info_table.json [new file with mode: 0644]
test/mocks/emssimulator/swm/ems_util.py [new file with mode: 0644]
test/mocks/emssimulator/swm/installNESw [new file with mode: 0755]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/current [new symlink]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/installed_sw.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1/app1 [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2/app2 [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/current [new symlink]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/installed_sw.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1/app1 [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2/app2 [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/installed_sw.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1/app1 [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2/app2 [new file with mode: 0644]
test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/swFallback [new file with mode: 0755]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v1/app1 [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v1/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v2/app1 [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v2/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v3/app1 [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v3/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v1/app2 [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v1/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v2/app2 [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v2/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v3/app2 [new file with mode: 0644]
test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v3/manifest.json [new file with mode: 0644]
test/mocks/emssimulator/swm/upgrade-post-check [new file with mode: 0755]
test/mocks/emssimulator/swm/upgrade-pre-check [new file with mode: 0755]

diff --git a/test/mocks/emssimulator/install.sh b/test/mocks/emssimulator/install.sh
new file mode 100755 (executable)
index 0000000..e3069aa
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+readonly old_pwd=$PWD
+cd swm/sw_server_simulator
+
+for i in `ls -1`; do
+       if [ -d $i ]; then
+               cd $i
+               echo $i
+               zip -r ${i}.zip *
+               mv ${i}.zip ..
+               cd $OLDPWD
+       fi
+done
+
+cd $old_pwd
+
diff --git a/test/mocks/emssimulator/requirements.txt b/test/mocks/emssimulator/requirements.txt
new file mode 100644 (file)
index 0000000..feca32d
--- /dev/null
@@ -0,0 +1 @@
+jsonpath
diff --git a/test/mocks/emssimulator/swm/__init__.py b/test/mocks/emssimulator/swm/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/test/mocks/emssimulator/swm/activateNESw b/test/mocks/emssimulator/swm/activateNESw
new file mode 100755 (executable)
index 0000000..67d233e
--- /dev/null
@@ -0,0 +1,135 @@
+#!/usr/bin/python
+
+import sys
+import argparse
+import json
+import os
+import shutil
+import random
+import time
+
+import conf
+import ems_util
+
+
+def do_activate_sw(sw_version_to_be_activated, ne_info):
+    """
+    return err, reason
+    """
+
+    installed_sw = ne_info.get("installedSw", {})
+    if sw_version_to_be_activated in installed_sw:
+        target_sw_version = installed_sw[sw_version_to_be_activated]["version"]
+    else:
+        target_sw_version = sw_version_to_be_activated
+
+    sw_install_dir_in_ne = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_INSTALL_DIR
+    target_sw_dir = sw_install_dir_in_ne + '/' + target_sw_version
+    if not os.path.isdir(target_sw_dir):
+        return True, "SW to be activated does not install"
+
+    if "targetSwVersion" in ne_info:
+        if ne_info["targetSwVersion"] != target_sw_version:
+            return True, "Conflicted targetVersion with to be activated %s" % target_sw_version
+        del ne_info["targetSwVersion"]
+
+    old_sw_version = ne_info.get("oldSwVersion", "")
+
+    if target_sw_version != ne_info["currentSwVersion"]:
+        ne_info["oldSwVersion"] = ne_info["currentSwVersion"]
+        ne_info["currentSwVersion"] = target_sw_version
+        ne_info["status"] = conf.STATUS_ACTIVATING
+        ems_util.update_ne_info(ne_info)
+
+        if target_sw_version != old_sw_version:
+            old_sw_dir = sw_install_dir_in_ne + '/' + old_sw_version
+            if old_sw_version and os.path.isdir(old_sw_dir):
+                shutil.rmtree(old_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(target_sw_version, conf.CURRENT_VERSION_DIR)
+    os.chdir(old_cwd)
+
+    if "downloadedSwLocation" in ne_info:
+        if os.path.isdir(ne_info["downloadedSwLocation"]):
+            shutil.rmtree(ne_info["downloadedSwLocation"], ignore_errors=True)
+        del ne_info["downloadedSwLocation"]
+
+    return False, None
+
+
+def generate_notification(activate_process_id, activate_status, sw_version, failure_reason):
+    notification = {
+        "objectClass": "EMSClass",
+        "objectInstance": "EMSInstance",
+        "notificationId": random.randint(1, conf.MAX_INT),
+        "eventTime": time.asctime(),
+        "systemDN": "emssimulator",
+        "notificationType": "notifyActivateNESwStatusChanged",
+        "activateProcessId": activate_process_id,
+        "activateOperationStatus": activate_status,
+        "swVersion": sw_version
+    }
+
+    if failure_reason:
+        notification["failureReason"] = failure_reason
+
+    return notification
+
+
+def activate_ne_sw(sw_version_to_be_activated, ne_id):
+    ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)
+
+    activate_process_id = random.randint(1, conf.MAX_INT)
+    result = conf.REQ_SUCCESS
+    ret_value = {
+        "activateProcessId": activate_process_id,
+        "result": result
+    }
+
+    if not ne_info:
+        ret_value["result"] = conf.REQ_FAILURE
+        ret_value["reason"] = "Can not find NE %s" % ne_id
+        return ret_value
+
+    err, reason = do_activate_sw(sw_version_to_be_activated, ne_info)
+
+    if not err:
+        ne_info["status"] = conf.STATUS_ACTIVATED
+        ems_util.update_ne_info(ne_info)
+        activate_status = "NE_SWACTIVATION_SUCCESSFUL"
+    else:
+        ret_value["result"] = conf.REQ_FAILURE
+        ret_value["reason"] = reason
+
+        activate_status = "NE_SWACTIVATION_FAILED"
+
+    notification = generate_notification(activate_process_id, activate_status, sw_version_to_be_activated, reason)
+    ems_util.send_notification(notification, activate_process_id)
+
+    # for automated software management, there is no listOfStepNumbersAndDurations
+    return ret_value
+
+
+def main():
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument("--swVersionToBeActivated", help="The NE software version to be activated", required=True)
+    parser.add_argument("--neIdentifier", help="The NE where the software can be activated", required=True)
+
+    args = parser.parse_args()
+
+    ret_value = activate_ne_sw(args.swVersionToBeActivated, args.neIdentifier)
+    print json.dumps(ret_value)
+
+    if ret_value["result"] == conf.REQ_SUCCESS:
+        sys.exit(conf.RET_CODE_SUCCESS)
+    else:
+        sys.exit(conf.RET_CODE_FAILURE)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/test/mocks/emssimulator/swm/conf.py b/test/mocks/emssimulator/swm/conf.py
new file mode 100644 (file)
index 0000000..d7ba5b4
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+
+import sys
+
+SWM_DIR = sys.path[0]
+
+NE_INFO_TABLE = SWM_DIR + "/ems_db/ne_info_table.json"
+SW_SERVER_SIMULATOR = SWM_DIR + "/sw_server_simulator"
+PNF_SIMULATORS_DIR = SWM_DIR + "/pnf_simulators"
+PNF_SW_DOWNLOAD_DIR = "/opt/download"
+PNF_SW_INSTALL_DIR = "/opt/install"
+PNF_SW_FALLBACK_DIR = "/opt/fallback"
+MANIFEST_FILE = "manifest.json"
+INSTALLED_SW = "installed_sw.json"
+CURRENT_VERSION_DIR = "current"
+NOTIFICATION_DIR = "/tmp"
+
+MAX_INT = (2**32) - 1
+
+STATUS_DOWNLOADING = "Downloading"
+STATUS_INSTALLING = "Installing"
+STATUS_ACTIVATING = "Activating"
+STATUS_ACTIVATED = "Activated"
+
+STATUS_PRECHECKED = "PreChecked"
+STATUS_POSTCHECKED = "PostChecked"
+
+REQ_SUCCESS = "requestAccepted"
+REQ_FAILURE = "requestFailed"
+
+RET_CODE_SUCCESS = 0
+RET_CODE_FAILURE = 1
+
+RESULT_SUCCESS = "Success"
+RESULT_FAILURE = "Failure"
+RESULT_PARTLY = "Partly successful"
+
diff --git a/test/mocks/emssimulator/swm/downloadNESw b/test/mocks/emssimulator/swm/downloadNESw
new file mode 100755 (executable)
index 0000000..06a8d6b
--- /dev/null
@@ -0,0 +1,154 @@
+#!/usr/bin/python
+
+import sys
+import argparse
+import json
+import os
+import shutil
+import random
+import time
+import tempfile
+
+import conf
+import ems_util
+
+
+def do_download_sw(sw_info, sw_download_dir):
+    """
+    return err, reason, file_location_in_nes
+    """
+
+    sw_location = sw_info['swLocation']
+
+    # Use copy file from SW_SERVER_SIMULATOR to simulate download file
+    sw_file_name = sw_location.split('/')[-1]
+
+    file_location_in_server = conf.SW_SERVER_SIMULATOR + '/' + sw_file_name
+    file_location_in_ne = sw_download_dir + '/' + sw_file_name
+
+    try:
+        shutil.copy(file_location_in_server, sw_download_dir)
+    except IOError as e:
+        return True, "Download %s to %s error: %s" % (sw_file_name, sw_download_dir, str(e)), file_location_in_ne
+
+    return False, None, file_location_in_ne
+
+
+def generate_notification(download_process_id, download_status, downloaded_ne_sw_info, failed_sw_info):
+    notification = {
+        "objectClass": "EMSClass",
+        "objectInstance": "EMSInstance",
+        "notificationId": random.randint(1, conf.MAX_INT),
+        "eventTime": time.asctime(),
+        "systemDN": "emssimulator",
+        "notificationType": "notifyDownloadNESwStatusChanged",
+        "downloadProcessId": download_process_id,
+        "downloadOperationStatus": download_status
+    }
+
+    if downloaded_ne_sw_info:
+        notification["downloadedNESwInfo"] = downloaded_ne_sw_info
+
+    if failed_sw_info:
+        notification["failedSwInfo"] = failed_sw_info
+
+    return notification
+
+
+def download_ne_sw(sw_to_be_downloaded, ne_id):
+    ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)
+
+    download_process_id = random.randint(1, conf.MAX_INT)
+    result = conf.REQ_SUCCESS
+    ret_value = {
+        "downloadProcessId": download_process_id,
+        "result": result
+    }
+
+    if not ne_info:
+        ret_value["result"] = conf.REQ_FAILURE
+        ret_value["reason"] = "Can not find NE %s" % ne_id
+        return ret_value
+
+    ne_info["status"] = conf.STATUS_DOWNLOADING
+    ems_util.update_ne_info(ne_info)
+
+    num_sw_to_be_downloaded = len(sw_to_be_downloaded)
+
+    downloaded_ne_sw_info = []
+    failed_sw_info = []
+
+    sw_download_parent_dir = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_DOWNLOAD_DIR
+    sw_download_dir = ne_info.get("downloadedSwLocation", "")
+    try:
+        if not os.path.isdir(sw_download_parent_dir):
+            os.makedirs(sw_download_parent_dir)
+
+        if sw_download_dir and not os.path.isdir(sw_download_dir):
+            os.makedirs(sw_download_dir)
+    except OSError as e:
+        ret_value["result"] = conf.REQ_FAILURE
+        ret_value["reason"] = str(e)
+        return ret_value
+
+    if not sw_download_dir:
+        sw_download_dir = tempfile.mkdtemp(dir=sw_download_parent_dir)
+
+    for sw_info in sw_to_be_downloaded:
+        err, reason, file_location = do_download_sw(sw_info, sw_download_dir)
+        if not err:
+            downloaded_ne_sw_info.append(file_location)
+        else:
+            result = conf.REQ_FAILURE
+            failed_sw_entry = {
+                "failedSw": file_location,
+                "failureReason": reason
+            }
+            failed_sw_info.append(failed_sw_entry)
+
+    num_downloaded_ne_sw = len(downloaded_ne_sw_info)
+
+    if num_downloaded_ne_sw == num_sw_to_be_downloaded:
+        download_status = "NE_SWDOWNLOAD_SUCCESSFUL"
+    elif num_downloaded_ne_sw == 0:
+        download_status = "NE_SWDOWNLOAD_FAILED"
+    else:
+        download_status = "NE_SWDOWNLOAD_PARTIALLY_SUCCESSFUL"
+
+    notification = generate_notification(download_process_id, download_status, downloaded_ne_sw_info, failed_sw_info)
+    ems_util.send_notification(notification, download_process_id)
+
+    if result == conf.REQ_SUCCESS:
+        ne_info["downloadedSwLocation"] = sw_download_dir
+        ems_util.update_ne_info(ne_info)
+    else:
+        shutil.rmtree(sw_download_dir, ignore_errors=True)
+
+        ret_value["result"] = result
+        ret_value["reason"] = json.dumps(failed_sw_info)
+
+    # for automated software management, there is no listOfStepNumbersAndDurations
+    return ret_value
+
+
+def main():
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument("--swToBeDownloaded", help="The NE software to be downloaded", required=True)
+    parser.add_argument("--neIdentifier", help="The NE where the software can be downloaded", required=True)
+
+    args = parser.parse_args()
+
+    sw_to_be_downloaded = json.loads(args.swToBeDownloaded)
+
+    ret_value = download_ne_sw(sw_to_be_downloaded, args.neIdentifier)
+    print json.dumps(ret_value)
+
+    if ret_value["result"] == conf.REQ_SUCCESS:
+        sys.exit(conf.RET_CODE_SUCCESS)
+    else:
+        sys.exit(conf.RET_CODE_FAILURE)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/test/mocks/emssimulator/swm/ems_db/ne_info_table.json b/test/mocks/emssimulator/swm/ems_db/ne_info_table.json
new file mode 100644 (file)
index 0000000..90aaeb2
--- /dev/null
@@ -0,0 +1,41 @@
+[
+  {
+    "status": "Activated", 
+    "nEIdentification": "5gDU0001", 
+    "updateTime": "Tue Apr 23 13:08:43 2019", 
+    "omIP": "192.168.1.1", 
+    "currentSwVersion": "v1", 
+    "installedSw": {
+      "ran_du_pkg2-v1": {
+        "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2", 
+        "version": "v1", 
+        "name": "ran_du_pkg2"
+      }, 
+      "ran_du_pkg1-v1": {
+        "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1", 
+        "version": "v1", 
+        "name": "ran_du_pkg1"
+      }
+    }
+  }, 
+  {
+    "status": "Activated", 
+    "nEIdentification": "5gDU0002", 
+    "updateTime": "Tue Apr 23 11:06:36 2019", 
+    "omIP": "192.168.1.2", 
+    "oldSwVersion": "v1", 
+    "currentSwVersion": "v2", 
+    "installedSw": {
+      "ran_du_pkg1-v2": {
+        "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1", 
+        "version": "v2", 
+        "name": "ran_du_pkg1"
+      }, 
+      "ran_du_pkg2-v2": {
+        "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2", 
+        "version": "v2", 
+        "name": "ran_du_pkg2"
+      }
+    }
+  }
+]
diff --git a/test/mocks/emssimulator/swm/ems_util.py b/test/mocks/emssimulator/swm/ems_util.py
new file mode 100644 (file)
index 0000000..6d0d310
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+
+import time
+import json
+import jsonpath
+
+import conf
+
+
+def get_ne_info_list_from_db(ne_filter):
+    with open(conf.NE_INFO_TABLE) as f_ne_info:
+        ne_info_table = json.load(f_ne_info)
+
+    if ne_info_table:
+        ne_info_list = jsonpath.jsonpath(ne_info_table, ne_filter)
+        return ne_info_list if ne_info_list else []
+    else:
+        return []
+
+
+def get_ne_info_from_db_by_id(ne_id):
+    ne_filter = '$..[?(@.nEIdentification == "%s")]' % ne_id
+
+    ne_info_list = get_ne_info_list_from_db(ne_filter)
+
+    return ne_info_list[0] if ne_info_list else None
+
+
+def update_ne_info(ne_info):
+    with open(conf.NE_INFO_TABLE) as f_ne_info:
+        ne_info_table = json.load(f_ne_info)
+
+    ne_info["updateTime"] = time.asctime()
+
+    if ne_info_table:
+        for i in range(0, len(ne_info_table)):
+            if ne_info_table[i]["nEIdentification"] == ne_info["nEIdentification"]:
+                ne_info_table[i] = ne_info
+                break
+        else:
+            ne_info_table.append(ne_info)
+    else:
+        ne_info_table = [ne_info]
+
+    with open(conf.NE_INFO_TABLE, 'w') as f_ne_info:
+        json.dump(ne_info_table, f_ne_info, indent=2)
+
+
+def send_notification(notification, process_id):
+    notification_file = conf.NOTIFICATION_DIR + '/%s-%d' % (notification['notificationType'], process_id)
+
+    with open(notification_file, 'w') as f_notification:
+        f_notification.write(json.dumps(notification))
diff --git a/test/mocks/emssimulator/swm/installNESw b/test/mocks/emssimulator/swm/installNESw
new file mode 100755 (executable)
index 0000000..84e2fb9
--- /dev/null
@@ -0,0 +1,206 @@
+#!/usr/bin/python
+
+import sys
+import argparse
+import json
+import os
+import shutil
+import random
+import time
+import tempfile
+import zipfile
+
+import conf
+import ems_util
+
+
+def do_install_sw(sw_to_be_installed, ne_info):
+    """
+    return err, reason, installed_ne_sw
+    """
+
+    sw_install_dir_in_ne = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_INSTALL_DIR
+
+    if sw_to_be_installed.startswith('/'):
+        file_location = sw_to_be_installed
+    else:
+        sw_download_dir_in_ne = ne_info.get("downloadedSwLocation", "")
+        file_location = sw_download_dir_in_ne + '/' + sw_to_be_installed
+
+    if not os.access(file_location, os.R_OK):
+        return True, "Missing to be installed SW file %s" % file_location, None
+
+    try:
+        if not os.path.isdir(sw_install_dir_in_ne):
+            os.makedirs(sw_install_dir_in_ne)
+    except OSError as e:
+        return True, str(e), None
+
+    temp_dir = tempfile.mkdtemp(dir=sw_install_dir_in_ne)
+    if file_location.endswith(".zip"):
+        with zipfile.ZipFile(file_location) as sw_zip:
+            sw_zip.extractall(temp_dir)
+    else:
+        return True, "Only support zip file", None
+
+    manifest_location = temp_dir + '/' + conf.MANIFEST_FILE
+    if os.access(manifest_location, os.R_OK):
+        with open(manifest_location) as f_manifest:
+            manifest = json.load(f_manifest)
+    else:
+        shutil.rmtree(temp_dir, ignore_errors=True)
+        return True, "Missing manifest file in %s" % file_location, None
+
+    try:
+        target_sw_name = manifest["name"]
+        target_sw_version = manifest["version"]
+    except KeyError as e:
+        shutil.rmtree(temp_dir, ignore_errors=True)
+        return True, "Missing key %s in %s of %s" % (str(e), conf.MANIFEST_FILE, file_location), None
+
+    if "targetSwVersion" in ne_info and ne_info["targetSwVersion"] != target_sw_version:
+        shutil.rmtree(temp_dir, ignore_errors=True)
+        return True, "Conflicted targetVersion for %s" % file_location, None
+
+    ne_info["targetSwVersion"] = target_sw_version
+    ems_util.update_ne_info(ne_info)
+
+    target_sw_parent_dir = sw_install_dir_in_ne + '/' + target_sw_version
+    try:
+        if not os.path.isdir(target_sw_parent_dir):
+            os.makedirs(target_sw_parent_dir)
+    except OSError as e:
+        shutil.rmtree(temp_dir, ignore_errors=True)
+        return True, str(e), None
+
+    target_sw_dir = target_sw_parent_dir + '/' + target_sw_name
+    if os.path.isdir(target_sw_dir):
+        shutil.rmtree(target_sw_dir, ignore_errors=True)
+
+    try:
+        shutil.move(temp_dir, target_sw_dir)
+    except shutil.Error as e:
+        shutil.rmtree(temp_dir, ignore_errors=True)
+        return True, str(e), None
+
+    installed_ne_sw = target_sw_name + '-' + target_sw_version
+
+    installed_sw_db = target_sw_parent_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 = {}
+
+    target_sw_info = {
+        "name": target_sw_name,
+        "version": target_sw_version,
+        "installedLocation": target_sw_dir
+    }
+    installed_sw_table[installed_ne_sw] = target_sw_info
+
+    with open(installed_sw_db, 'w') as f_installed_sw:
+        json.dump(installed_sw_table, f_installed_sw, indent=2)
+
+    ne_info["installedSw"] = installed_sw_table
+
+    return False, None, installed_ne_sw
+
+
+def generate_notification(install_process_id, install_status, installed_ne_sw_info, failed_sw_info):
+    notification = {
+        "objectClass": "EMSClass",
+        "objectInstance": "EMSInstance",
+        "notificationId": random.randint(1, conf.MAX_INT),
+        "eventTime": time.asctime(),
+        "systemDN": "emssimulator",
+        "notificationType": "notifyInstallNESwStatusChanged",
+        "installProcessId": install_process_id,
+        "installOperationStatus": install_status
+    }
+
+    if installed_ne_sw_info:
+        notification["installedNESwInfo"] = installed_ne_sw_info
+
+    if failed_sw_info:
+        notification["failedSwInfo"] = failed_sw_info
+
+    return notification
+
+
+def install_ne_sw(sw_to_be_installed, ne_id):
+    ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)
+
+    install_process_id = random.randint(1, conf.MAX_INT)
+    result = conf.REQ_SUCCESS
+    ret_value = {
+        "installProcessId": install_process_id,
+        "result": result
+    }
+
+    if not ne_info:
+        ret_value["result"] = conf.REQ_FAILURE
+        ret_value["reason"] = "Can not find NE %s" % ne_id
+        return ret_value
+
+    ne_info["status"] = conf.STATUS_INSTALLING
+    ems_util.update_ne_info(ne_info)
+
+    installed_ne_sw_info = []
+    failed_sw_info = []
+
+    err, reason, installed_ne_sw = do_install_sw(sw_to_be_installed, ne_info)
+
+    if not err:
+        installed_ne_sw_info.append(installed_ne_sw)
+    else:
+        result = conf.REQ_FAILURE
+        failed_sw_entry = {
+            "failedSw": installed_ne_sw,
+            "failureReason": reason
+        }
+        failed_sw_info.append(failed_sw_entry)
+
+    num_installed_ne_sw = len(installed_ne_sw_info)
+
+    if num_installed_ne_sw == 1:
+        install_status = "NE_SWINSTALLATION_SUCCESSFUL"
+    elif num_installed_ne_sw == 0:
+        install_status = "NE_SWINSTALLATION_FAILED"
+    else:
+        install_status = "NE_SWINSTALLATION_PARTIALLY_SUCCESSFUL"
+
+    notification = generate_notification(install_process_id, install_status, installed_ne_sw_info, failed_sw_info)
+    ems_util.send_notification(notification, install_process_id)
+
+    if result == conf.REQ_SUCCESS:
+        ems_util.update_ne_info(ne_info)
+    else:
+        ret_value["result"] = result
+        ret_value["reason"] = json.dumps(failed_sw_info)
+
+    # for automated software management, there is no listOfStepNumbersAndDurations
+    return ret_value
+
+
+def main():
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument("--swToBeInstalled", help="The NE software to be installed", required=True)
+    parser.add_argument("--neIdentifier", help="The NE where the software can be installed", required=True)
+
+    args = parser.parse_args()
+
+    ret_value = install_ne_sw(args.swToBeInstalled, args.neIdentifier)
+    print json.dumps(ret_value)
+
+    if ret_value["result"] == conf.REQ_SUCCESS:
+        sys.exit(conf.RET_CODE_SUCCESS)
+    else:
+        sys.exit(conf.RET_CODE_FAILURE)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/current b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/current
new file mode 120000 (symlink)
index 0000000..28c218c
--- /dev/null
@@ -0,0 +1 @@
+v1
\ No newline at end of file
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/installed_sw.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/installed_sw.json
new file mode 100644 (file)
index 0000000..9c19215
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "ran_du_pkg2-v1": {
+    "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2", 
+    "version": "v1", 
+    "name": "ran_du_pkg2"
+  }, 
+  "ran_du_pkg1-v1": {
+    "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1", 
+    "version": "v1", 
+    "name": "ran_du_pkg1"
+  }
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1/app1 b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1/app1
new file mode 100644 (file)
index 0000000..9df42f7
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app1"
+
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1/manifest.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg1/manifest.json
new file mode 100644 (file)
index 0000000..dd19981
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg1",
+  "version": "v1"
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2/app2 b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2/app2
new file mode 100644 (file)
index 0000000..e74b365
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app2"
+
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2/manifest.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.1/opt/install/v1/ran_du_pkg2/manifest.json
new file mode 100644 (file)
index 0000000..9bc88f5
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg2",
+  "version": "v1"
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/current b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/current
new file mode 120000 (symlink)
index 0000000..8494ac2
--- /dev/null
@@ -0,0 +1 @@
+v2
\ No newline at end of file
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/installed_sw.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/installed_sw.json
new file mode 100644 (file)
index 0000000..31cddf9
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "ran_du_pkg2-v1": {
+    "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2", 
+    "version": "v1", 
+    "name": "ran_du_pkg2"
+  }, 
+  "ran_du_pkg1-v1": {
+    "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1", 
+    "version": "v1", 
+    "name": "ran_du_pkg1"
+  }
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1/app1 b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1/app1
new file mode 100644 (file)
index 0000000..9df42f7
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app1"
+
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1/manifest.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg1/manifest.json
new file mode 100644 (file)
index 0000000..dd19981
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg1",
+  "version": "v1"
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2/app2 b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2/app2
new file mode 100644 (file)
index 0000000..e74b365
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app2"
+
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2/manifest.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v1/ran_du_pkg2/manifest.json
new file mode 100644 (file)
index 0000000..9bc88f5
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg2",
+  "version": "v1"
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/installed_sw.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/installed_sw.json
new file mode 100644 (file)
index 0000000..e55c7f5
--- /dev/null
@@ -0,0 +1,12 @@
+{
+  "ran_du_pkg1-v2": {
+    "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1", 
+    "version": "v2", 
+    "name": "ran_du_pkg1"
+  }, 
+  "ran_du_pkg2-v2": {
+    "installedLocation": "/home/ubuntu/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2", 
+    "version": "v2", 
+    "name": "ran_du_pkg2"
+  }
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1/app1 b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1/app1
new file mode 100644 (file)
index 0000000..9df42f7
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app1"
+
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1/manifest.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg1/manifest.json
new file mode 100644 (file)
index 0000000..a3e7d05
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg1",
+  "version": "v2"
+}
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2/app2 b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2/app2
new file mode 100644 (file)
index 0000000..e74b365
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app2"
+
diff --git a/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2/manifest.json b/test/mocks/emssimulator/swm/pnf_simulators/192.168.1.2/opt/install/v2/ran_du_pkg2/manifest.json
new file mode 100644 (file)
index 0000000..fe9ffe0
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg2",
+  "version": "v2"
+}
diff --git a/test/mocks/emssimulator/swm/swFallback b/test/mocks/emssimulator/swm/swFallback
new file mode 100755 (executable)
index 0000000..9d6608c
--- /dev/null
@@ -0,0 +1,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()
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v1/app1 b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v1/app1
new file mode 100644 (file)
index 0000000..9df42f7
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app1"
+
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v1/manifest.json b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v1/manifest.json
new file mode 100644 (file)
index 0000000..dd19981
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg1",
+  "version": "v1"
+}
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v2/app1 b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v2/app1
new file mode 100644 (file)
index 0000000..9df42f7
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app1"
+
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v2/manifest.json b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v2/manifest.json
new file mode 100644 (file)
index 0000000..a3e7d05
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg1",
+  "version": "v2"
+}
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v3/app1 b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v3/app1
new file mode 100644 (file)
index 0000000..9df42f7
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app1"
+
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v3/manifest.json b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg1-v3/manifest.json
new file mode 100644 (file)
index 0000000..37ec87c
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg1",
+  "version": "v3"
+}
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v1/app2 b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v1/app2
new file mode 100644 (file)
index 0000000..e74b365
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app2"
+
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v1/manifest.json b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v1/manifest.json
new file mode 100644 (file)
index 0000000..9bc88f5
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg2",
+  "version": "v1"
+}
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v2/app2 b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v2/app2
new file mode 100644 (file)
index 0000000..e74b365
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app2"
+
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v2/manifest.json b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v2/manifest.json
new file mode 100644 (file)
index 0000000..fe9ffe0
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg2",
+  "version": "v2"
+}
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v3/app2 b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v3/app2
new file mode 100644 (file)
index 0000000..e74b365
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo "app2"
+
diff --git a/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v3/manifest.json b/test/mocks/emssimulator/swm/sw_server_simulator/ran_du_pkg2-v3/manifest.json
new file mode 100644 (file)
index 0000000..f0af8bc
--- /dev/null
@@ -0,0 +1,4 @@
+{
+  "name": "ran_du_pkg2",
+  "version": "v3"
+}
diff --git a/test/mocks/emssimulator/swm/upgrade-post-check b/test/mocks/emssimulator/swm/upgrade-post-check
new file mode 100755 (executable)
index 0000000..799afbc
--- /dev/null
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+
+import sys
+import json
+
+import conf
+import ems_util
+
+
+def upgrade_postcheck(pnf_id, old_sw_version, target_sw_version, rule_name, tmp_file=None):
+    ne_info = ems_util.get_ne_info_from_db_by_id(pnf_id)
+
+    if not ne_info:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Can not find NE %s" % pnf_id
+        }
+        return ret_value
+
+    old_sw_version_in_db = ne_info.get("oldSwVersion", "")
+    current_sw_version_in_db = ne_info.get("currentSwVersion", "")
+
+    if old_sw_version != old_sw_version_in_db:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Old SW version %s in PNF is not matched with oldSwVersion %s" %
+                      (old_sw_version_in_db, old_sw_version)
+        }
+        return ret_value
+
+    if target_sw_version != current_sw_version_in_db:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Current SW version %s in PNF is not matched with targetSwVersion %s" %
+                      (current_sw_version_in_db, target_sw_version)
+        }
+        return ret_value
+
+    ne_info["checkStatus"] = conf.STATUS_POSTCHECKED
+    ems_util.update_ne_info(ne_info)
+
+    ret_value = {
+         "result": conf.RESULT_SUCCESS
+    }
+
+    return ret_value
+
+
+def main():
+    #  {{pnfId}} {{oldSwVersion}} {{targetSwVersion}} {{ruleName}} /tmp/tmp-{{Id}}
+
+    if len(sys.argv) < 5:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Missing parameters"
+        }
+        print json.dumps(ret_value)
+        sys.exit(conf.RET_CODE_FAILURE)
+
+    if len(sys.argv) >= 5:
+        pnf_id = sys.argv[1]
+        old_sw_version = sys.argv[2]
+        target_sw_version = sys.argv[3]
+        rule_name = sys.argv[4]
+        tmp_file = None
+
+    if len(sys.argv) >= 6:
+        tmp_file = sys.argv[5]
+
+    ret_value = upgrade_postcheck(pnf_id, old_sw_version, target_sw_version, rule_name, tmp_file)
+    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()
diff --git a/test/mocks/emssimulator/swm/upgrade-pre-check b/test/mocks/emssimulator/swm/upgrade-pre-check
new file mode 100755 (executable)
index 0000000..6bf867f
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/python
+
+import sys
+import json
+
+import conf
+import ems_util
+
+
+def upgrade_precheck(pnf_id, old_sw_version, target_sw_version, rule_name, tmp_file=None):
+    ne_info = ems_util.get_ne_info_from_db_by_id(pnf_id)
+
+    if not ne_info:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Can not find NE %s" % pnf_id
+        }
+        return ret_value
+
+    current_sw_version_in_db = ne_info.get("currentSwVersion", "")
+
+    if old_sw_version != current_sw_version_in_db:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Current SW version %s in PNF is not matched with oldSwVersion %s" %
+                      (current_sw_version_in_db, old_sw_version)
+        }
+        return ret_value
+
+    ne_info["checkStatus"] = conf.STATUS_PRECHECKED
+    ems_util.update_ne_info(ne_info)
+
+    ret_value = {
+         "result": conf.RESULT_SUCCESS
+    }
+
+    return ret_value
+
+
+def main():
+    #  {{pnfId}} {{oldSwVersion}} {{targetSwVersion}} {{ruleName}} /tmp/tmp-{{Id}}
+
+    if len(sys.argv) < 5:
+        ret_value = {
+            "result": conf.RESULT_FAILURE,
+            "reason": "Missing parameters"
+        }
+        print json.dumps(ret_value)
+        sys.exit(conf.RET_CODE_FAILURE)
+
+    if len(sys.argv) >= 5:
+        pnf_id = sys.argv[1]
+        old_sw_version = sys.argv[2]
+        target_sw_version = sys.argv[3]
+        rule_name = sys.argv[4]
+        tmp_file = None
+
+    if len(sys.argv) >= 6:
+        tmp_file = sys.argv[5]
+
+    ret_value = upgrade_precheck(pnf_id, old_sw_version, target_sw_version, rule_name, tmp_file)
+    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()