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========================================================= |
Enbo Wang | 6ab8b62 | 2019-04-23 13:42:21 +0000 | [diff] [blame] | 18 | |
Enbo Wang | 818f76a | 2020-03-04 00:42:31 +0800 | [diff] [blame] | 19 | import os |
Enbo Wang | 6ab8b62 | 2019-04-23 13:42:21 +0000 | [diff] [blame] | 20 | import time |
| 21 | import json |
| 22 | import jsonpath |
| 23 | |
| 24 | import conf |
| 25 | |
| 26 | |
Enbo Wang | 818f76a | 2020-03-04 00:42:31 +0800 | [diff] [blame] | 27 | def get_log_file(operation): |
| 28 | return os.path.join(conf.LOGGER_FILE_DIR, "%s.txt" % operation) |
| 29 | |
| 30 | |
| 31 | def get_download_dir(om_ip): |
| 32 | return os.path.join(conf.PNF_SIMULATORS_DIR, om_ip, conf.COMMON_PATH, conf.PNF_SW_DOWNLOAD_DIR) |
| 33 | |
| 34 | |
| 35 | def get_install_dir(om_ip): |
| 36 | return os.path.join(conf.PNF_SIMULATORS_DIR, om_ip, conf.COMMON_PATH, conf.PNF_SW_INSTALL_DIR) |
| 37 | |
| 38 | |
Enbo Wang | 6ab8b62 | 2019-04-23 13:42:21 +0000 | [diff] [blame] | 39 | def get_ne_info_list_from_db(ne_filter): |
| 40 | with open(conf.NE_INFO_TABLE) as f_ne_info: |
| 41 | ne_info_table = json.load(f_ne_info) |
| 42 | |
| 43 | if ne_info_table: |
| 44 | ne_info_list = jsonpath.jsonpath(ne_info_table, ne_filter) |
| 45 | return ne_info_list if ne_info_list else [] |
| 46 | else: |
| 47 | return [] |
| 48 | |
| 49 | |
| 50 | def get_ne_info_from_db_by_id(ne_id): |
| 51 | ne_filter = '$..[?(@.nEIdentification == "%s")]' % ne_id |
| 52 | |
| 53 | ne_info_list = get_ne_info_list_from_db(ne_filter) |
| 54 | |
| 55 | return ne_info_list[0] if ne_info_list else None |
| 56 | |
| 57 | |
| 58 | def update_ne_info(ne_info): |
| 59 | with open(conf.NE_INFO_TABLE) as f_ne_info: |
| 60 | ne_info_table = json.load(f_ne_info) |
| 61 | |
| 62 | ne_info["updateTime"] = time.asctime() |
| 63 | |
| 64 | if ne_info_table: |
| 65 | for i in range(0, len(ne_info_table)): |
| 66 | if ne_info_table[i]["nEIdentification"] == ne_info["nEIdentification"]: |
| 67 | ne_info_table[i] = ne_info |
| 68 | break |
| 69 | else: |
| 70 | ne_info_table.append(ne_info) |
| 71 | else: |
| 72 | ne_info_table = [ne_info] |
| 73 | |
| 74 | with open(conf.NE_INFO_TABLE, 'w') as f_ne_info: |
| 75 | json.dump(ne_info_table, f_ne_info, indent=2) |
| 76 | |
| 77 | |
| 78 | def send_notification(notification, process_id): |
Enbo Wang | 818f76a | 2020-03-04 00:42:31 +0800 | [diff] [blame] | 79 | notification_file = os.path.join(conf.NOTIFICATION_DIR, '%s-%d' % (notification['notificationType'], process_id)) |
Enbo Wang | 6ab8b62 | 2019-04-23 13:42:21 +0000 | [diff] [blame] | 80 | |
| 81 | with open(notification_file, 'w') as f_notification: |
| 82 | f_notification.write(json.dumps(notification)) |