Add to_directory method to relevant object classes
[oam.git] / solution / integration / network / config.py
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 = 'https://sdnc-web:8453'
30 base = 'https://localhost:8453'
31 username = 'admin'
32 password = 'Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U'
33
34 # REST to set event settings
35 def configEventSettings(nfName, nfType):
36   file = os.path.dirname(os.path.abspath(__file__)) + '/' + nfType + '/event-settings.json'
37   with open(file) as json_file:
38     body = json.load(json_file)
39     url = base + '/rests/data/network-topology:network-topology/topology=topology-netconf/node=' + nfName + '/yang-ext:mount/nts-network-function:simulation/network-function'
40     headers = {
41         'content-type': 'application/yang-data+json',
42         'accept': 'application/yang-data+json'
43     }
44     try:
45       response = requests.put(url, verify=False, auth=(username, password), json=body, headers=headers)
46     except requests.exceptions.Timeout:
47       sys.exit('HTTP request failed, please check you internet connection.')
48     except requests.exceptions.TooManyRedirects:
49       sys.exit('HTTP request failed, please check your proxy settings.')
50     except requests.exceptions.RequestException as e:
51       # catastrophic error. bail.
52       raise SystemExit(e)
53
54     return response.status_code >= 200 and response.status_code < 300
55
56 # main
57 for container in containers:
58   name = container.decode("utf-8")
59   if "ntsim-ng" in name:
60     if "ntsim-ng-o-ru" in name:
61       nfName = mapping["ntsim-ng-o-ru"] + name[name.rindex("-"):]
62       print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-ru"))
63     if "ntsim-ng-o-du" in name:
64       nfName = mapping["ntsim-ng-o-du"] + name[name.rindex("-"):]
65       print("Set", nfName, configEventSettings(nfName, "ntsim-ng-o-du"))