Distinguish between DCN and OAM network
[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 requests
23 import subprocess
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
34
35 dockerFilter = subprocess.check_output("docker ps --format '{{.Names}}'", shell=True)
36 containers = dockerFilter.splitlines()
37
38 mapping = dict({"ntsim-ng-o-ru": "O-RU", "ntsim-ng-o-du": "O-DU"})
39 base = get_environment_variable('SDN_CONTROLLER_PROTOCOL') + '://' + get_environment_variable('SDNC_OAM_HOST')
40 username = get_environment_variable('ADMIN_USERNAME')
41 password = get_environment_variable('ADMIN_PASSWORD')
42
43 # REST to set event settings
44 def configEventSettings(nfName, nfType):
45   file = os.path.dirname(os.path.abspath(__file__)) + '/' + nfType + '/event-settings.json'
46   with open(file) as json_file:
47     body = json.load(json_file)
48     url = base + '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nfName + '/yang-ext:mount/nts-network-function:simulation/network-function'
49     headers = {
50         'content-type': 'application/yang-data+json',
51         'accept': 'application/yang-data+json'
52     }
53     try:
54       response = requests.put(url, verify=False, auth=(username, password), json=body, headers=headers)
55     except requests.exceptions.Timeout:
56       sys.exit('HTTP request failed, please check you internet connection.')
57     except requests.exceptions.TooManyRedirects:
58       sys.exit('HTTP request failed, please check your proxy settings.')
59     except requests.exceptions.RequestException as e:
60       # catastrophic error. bail.
61       raise SystemExit(e)
62
63     return response.status_code >= 200 and response.status_code < 300
64
65 # main
66 for container in containers:
67   name = container.decode("utf-8")
68   if "ntsim-ng" in name:
69     if "ntsim-ng-o-ru" in name:
70       nfName = mapping["ntsim-ng-o-ru"] + name[name.rindex("-"):]
71       print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-ru"))
72     if "ntsim-ng-o-du" in name:
73       nfName = mapping["ntsim-ng-o-du"] + name[name.rindex("-"):]
74       print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-du"))