demx8as6 | a93cb37 | 2021-06-06 16:05:58 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ################################################################################ |
Martin Skorupski | 990de72 | 2023-02-03 12:30:55 +0100 | [diff] [blame] | 3 | # Copyright 2023 highstreet technologies GmbH |
demx8as6 | a93cb37 | 2021-06-06 16:05:58 +0200 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | # importing the sys, json, requests library |
| 19 | import os |
| 20 | import sys |
| 21 | import json |
| 22 | import requests |
| 23 | import subprocess |
Martin Skorupski | 990de72 | 2023-02-03 12:30:55 +0100 | [diff] [blame] | 24 | import pathlib |
| 25 | from jproperties import Properties |
| 26 | |
| 27 | def get_environment_variable(name): |
| 28 | configs = Properties() |
| 29 | path = pathlib.Path( os.path.dirname(os.path.abspath(__file__)) ) |
| 30 | env_file = str(path.absolute()) + '/.env' |
| 31 | with open(env_file, "rb") as read_prop: |
| 32 | configs.load(read_prop) |
| 33 | return configs.get(name).data |
demx8as6 | a93cb37 | 2021-06-06 16:05:58 +0200 | [diff] [blame] | 34 | |
| 35 | dockerFilter = subprocess.check_output("docker ps --format '{{.Names}}'", shell=True) |
| 36 | containers = dockerFilter.splitlines() |
| 37 | |
Alex Stancu | 80bfd37 | 2022-02-04 17:30:41 +0200 | [diff] [blame] | 38 | mapping = dict({"ntsim-ng-o-ru": "O-RU", "ntsim-ng-o-du": "O-DU"}) |
demx8as6 | a93cb37 | 2021-06-06 16:05:58 +0200 | [diff] [blame] | 39 | # base = 'https://sdnc-web:8453' |
Martin Skorupski | 990de72 | 2023-02-03 12:30:55 +0100 | [diff] [blame] | 40 | base = get_environment_variable('SDN_CONTROLLER_PROTOCOL') + '://' + get_environment_variable('SDNC_OAM_HOST') |
demx8as6 | a93cb37 | 2021-06-06 16:05:58 +0200 | [diff] [blame] | 41 | username = 'admin' |
| 42 | password = 'Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U' |
| 43 | |
| 44 | # REST to set event settings |
| 45 | def configEventSettings(nfName, nfType): |
| 46 | file = os.path.dirname(os.path.abspath(__file__)) + '/' + nfType + '/event-settings.json' |
| 47 | with open(file) as json_file: |
| 48 | body = json.load(json_file) |
| 49 | url = base + '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nfName + '/yang-ext:mount/nts-network-function:simulation/network-function' |
| 50 | headers = { |
| 51 | 'content-type': 'application/yang-data+json', |
| 52 | 'accept': 'application/yang-data+json' |
| 53 | } |
| 54 | try: |
| 55 | response = requests.put(url, verify=False, auth=(username, password), json=body, headers=headers) |
| 56 | except requests.exceptions.Timeout: |
| 57 | sys.exit('HTTP request failed, please check you internet connection.') |
| 58 | except requests.exceptions.TooManyRedirects: |
| 59 | sys.exit('HTTP request failed, please check your proxy settings.') |
| 60 | except requests.exceptions.RequestException as e: |
| 61 | # catastrophic error. bail. |
| 62 | raise SystemExit(e) |
| 63 | |
| 64 | return response.status_code >= 200 and response.status_code < 300 |
| 65 | |
| 66 | # main |
| 67 | for container in containers: |
| 68 | name = container.decode("utf-8") |
| 69 | if "ntsim-ng" in name: |
| 70 | if "ntsim-ng-o-ru" in name: |
| 71 | nfName = mapping["ntsim-ng-o-ru"] + name[name.rindex("-"):] |
| 72 | print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-ru")) |
| 73 | if "ntsim-ng-o-du" in name: |
| 74 | nfName = mapping["ntsim-ng-o-du"] + name[name.rindex("-"):] |
| 75 | print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-du")) |