Add to_directory method to relevant object classes
[oam.git] / solution / network / config.py
1 #!/usr/bin/env python
2 ################################################################################
3 # Copyright 2023 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 re
23 import requests
24 import subprocess
25 import pathlib
26 from jproperties import Properties
27
28 def get_environment_variable(name):
29     configs = Properties()
30     path = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))
31     env_file = str(path.absolute()) + '/.env'
32     with open(env_file, "rb") as read_prop:
33         configs.load(read_prop)
34     value = configs.get(name).data
35
36     regex = r"\$\{([^\}]+)\}"
37     matches = re.finditer(regex, value)
38     while True:
39         match = next(matches, None)
40         if match is None:
41             break
42         inner = get_environment_variable(match.group(1))
43         value = value.replace("${" + match.group(1) + "}", inner )
44     return value
45
46 dockerFilter = subprocess.check_output("docker ps --format '{{.Names}}'", shell=True)
47 containers = dockerFilter.splitlines()
48
49 mapping = dict({"ntsim-ng-o-ru": "O-RU", "ntsim-ng-o-du": "O-DU"})
50 base = get_environment_variable('SDN_CONTROLLER_PROTOCOL') + '://' + get_environment_variable('SDNC_OAM_HOST')
51 username = get_environment_variable('ADMIN_USERNAME')
52 password = get_environment_variable('ADMIN_PASSWORD')
53
54 # REST to set event settings
55 def configEventSettings(nfName, nfType):
56   file = os.path.dirname(os.path.abspath(__file__)) + '/' + nfType + '/event-settings.json'
57   with open(file) as json_file:
58     body = json.load(json_file)
59     url = base + '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nfName + '/yang-ext:mount/nts-network-function:simulation/network-function'
60     headers = {
61         'content-type': 'application/yang-data+json',
62         'accept': 'application/yang-data+json'
63     }
64     try:
65       response = requests.put(url, verify=False, auth=(username, password), json=body, headers=headers)
66     except requests.exceptions.Timeout:
67       sys.exit('HTTP request failed, please check you internet connection.')
68     except requests.exceptions.TooManyRedirects:
69       sys.exit('HTTP request failed, please check your proxy settings.')
70     except requests.exceptions.RequestException as e:
71       # catastrophic error. bail.
72       raise SystemExit(e)
73
74     return response.status_code >= 200 and response.status_code < 300
75
76 # main
77 for container in containers:
78   name = container.decode("utf-8")
79   if "ntsim-ng" in name:
80     if "ntsim-ng-o-ru" in name:
81       nfName = mapping["ntsim-ng-o-ru"] + name[name.rindex("-"):]
82       print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-ru"))
83     if "ntsim-ng-o-du" in name:
84       nfName = mapping["ntsim-ng-o-du"] + name[name.rindex("-"):]
85       print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-du"))