demx8as6 | 53d47d4 | 2021-05-31 12:44:37 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ################################################################################ |
| 3 | # Copyright 2021 highstreet technologies GmbH |
| 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 |
| 24 | |
| 25 | dockerFilter = subprocess.check_output("docker ps --format '{{.Names}}'", shell=True) |
| 26 | containers = dockerFilter.splitlines() |
| 27 | |
| 28 | mapping = dict({"ntsim-ng-o-ru": "highstreet-O-RU", "ntsim-ng-o-du": "highstreet-O-DU"}) |
| 29 | base = 'http://localhost:8181' |
| 30 | username = 'admin' |
| 31 | password = 'Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U' |
| 32 | |
| 33 | # REST to set event settings |
| 34 | def configEventSettings(nfName, nfType): |
| 35 | file = os.path.dirname(os.path.abspath(__file__)) + '/' + nfType + '/event-settings.json' |
| 36 | with open(file) as json_file: |
| 37 | body = json.load(json_file) |
| 38 | url = base + '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nfName + '/yang-ext:mount/nts-network-function:simulation/network-function' |
| 39 | headers = { |
| 40 | 'content-type': 'application/yang-data+json', |
| 41 | 'accept': 'application/yang-data+json' |
| 42 | } |
| 43 | try: |
| 44 | response = requests.put(url, verify=False, auth=(username, password), json=body, headers=headers) |
| 45 | except requests.exceptions.Timeout: |
| 46 | sys.exit('HTTP request failed, please check you internet connection.') |
| 47 | except requests.exceptions.TooManyRedirects: |
| 48 | sys.exit('HTTP request failed, please check your proxy settings.') |
| 49 | except requests.exceptions.RequestException as e: |
| 50 | # catastrophic error. bail. |
| 51 | raise SystemExit(e) |
| 52 | |
| 53 | return response.status_code >= 200 and response.status_code < 300 |
| 54 | |
| 55 | # main |
| 56 | for container in containers: |
| 57 | name = container.decode("utf-8") |
| 58 | if "ntsim-ng" in name: |
| 59 | if "ntsim-ng-o-ru" in name: |
| 60 | nfName = mapping["ntsim-ng-o-ru"] + name[name.rindex("-"):] |
| 61 | print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-ru")) |
| 62 | if "ntsim-ng-o-du" in name: |
| 63 | nfName = mapping["ntsim-ng-o-du"] + name[name.rindex("-"):] |
| 64 | print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-du")) |